Final Cup Code
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
#if defined(__XC)
#include <xc.h> /* XC8 General Include File */
#elif defined(HI_TECH_C)
#include <htc.h> /* HiTech General Include File */
#elif defined(__18CXX)
#include <p18cxxx.h> /* C18 General Include File */
#endif
#if defined(__XC) || defined(HI_TECH_C)
#include <stdINT.h> /* For uINT8_t definition */
#include <stdbool.h> /* For true/false definition */
#endif
#include <delays.h>
#include <string.h>
#include <usart.h>
/*
* File: FoP_Cup.c
* Author: Nolander
*
* Created on October 4, 2013, 3:44 PM
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* C O N F I G S
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#pragma config RETEN = OFF // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = ON // Extended Instruction Set (Disabled)
// CONFIG1H
#pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = SWDTDIS // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576 // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7 // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF // Code Protect 00800-03FFF (Disabled)
#pragma config CP1 = OFF // Code Protect 04000-07FFF (Disabled)
#pragma config CP2 = OFF // Code Protect 08000-0BFFF (Disabled)
#pragma config CP3 = OFF // Code Protect 0C000-0FFFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF // (Disabled)
#pragma config CPD = OFF // Data EE Read Protect (Disabled)
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* D E F I N E S
*
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define CUP_ID 2
//#pragma config CANMX = PORTB
//#pragma config XINST = ON
//#pragma config WDTEN = OFF //WDT disabled (enabled by SWDTEN bit)
//#pragma config SOSCSEL = LOW
// ...
//MAIN
//IR TRANSMISSION
//VISUAL
#if CUP_ID == 1
#define MY_COLOR 'g'
#define HOME_RFID_STRING "6A003E4FA7BC"
#define AWAY_RFID_STRING "6A003E539F98"
#elif CUP_ID == 2
#define MY_COLOR 'm'
#define HOME_RFID_STRING "6A003E714366"
#define AWAY_RFID_STRING "6A003E2AF789"
#else
#define MY_COLOR 'w'
#endif
#define SCLK PORTCbits.RC2
#define SOUT PORTCbits.RC3
#define BLANK PORTCbits.RC4
#define XLAT PORTCbits.RC5
#define MAX_STEP 255
#define POUR_TIME 5
//AUDIO
#define TRACK1 LATAbits.LATA1
#define TRACK2 LATAbits.LATA2
#define TRACK3 LATAbits.LATA3
#define DEBUG 1
//set INTernal clock to 8 MHz (default)
#define TIMER_3_ON T3CONbits.TMR3ON
#define TIMER_0_INTERRUPT_FLAG INTCONbits.TMR0IF
#define TIMER_1_INTERRUPT_FLAG PIR1bits.TMR1IF
#define TIMER_2_INTERRUPT_FLAG PIR1bits.TMR2IF
#define TIMER_3_INTERRUPT_FLAG PIR2bits.TMR3IF
#define TIMER_4_INTERRUPT_FLAG PIR4bits.TMR4IF
#define TIMER_0_INTERRUPT_ENABLED INTCONbits.TMR0IE
#define TIMER_1_INTERRUPT_ENABLED PIE1bits.TMR1IE
#define TIMER_2_INTERRUPT_ENABLED PIE1bits.TMR2IE
#define TIMER_3_INTERRUPT_ENABLED PIE2bits.TMR3IE
#define TIMER_4_INTERRUPT_ENABLED PIE4bits.TMR4IE
#define HLVD_INTERRUPT_ENABLED PIE2bits.HLVDIE
#define HLVD_ENABLED HLVDCONbits.HLVDEN
#define HLVD_INTERRUPT_FLAG PIR2bits.HLVDIF
#define CAN_BUS_NOT_CONNECTED INTCON2bits.INTEDG0
#define FALLING_EDGE_INTERRUPT_FLAG PIR3bits.CCP2IF
#define FALLING_EDGE_INTERRUPT_ENABLED PIE3bits.CCP2IE
#define RETURN_HOME_INTERRUPT 1
#define BEGIN_POUR_INTERRUPT 1
#define TMR2_PRESCALE 1;
#define MIRP_PWM_PERIOD 0x23
/*//PR2 = 1/(56,000 * 4 * TOSC *(TMR2 Prescale Value)) - 1
//PR2 = 1/(56,000 * 4 * (1/8,000,000) * 4) - 1
* PR2 = 35 */
#define POUR_PWM_PERIOD 0x34
/*//PR2 = 1/(38,000 * 4 * TOSC *(TMR2 Prescale Value)) - 1
//PR2 = 1/(38,000 * 4 * (1/8,000,000) * 4) - 1
* PR2 = 52 */
#define MIRP_DUTY_CYCLE_H 0x11
#define MIRP_DUTY_CYCLE_L 0xCF
#define SET_POUR_DUTY_CYCLE CCPR5L = POUR_DUTY_CYCLE_H; CCP5CONbits &= POUR_DUTY_CYCLE_L;
#define MIRP_READY_COUNT 50//62500 // an 8-bit timer register will overflow 62500 times in 2 seconds (at 8 MHz)
#define MIRP_FRAME_COUNT 1//0xAC4B // =0b1010 1100 0100 1011//65536(0xFFFF) - 21429(8MHz cycles for 150 56kHz cycles) = 44107
#define OUT_OF_JUICE_COUNT 912//30 seconds
#define DEADLY_COUNT 152 //5 seconds
#define SET_MIRP_PWM PR2 = MIRP_PWM_PERIOD;
#define SET_MIRP_DUTY_CYCLE CCPR5L = MIRP_DUTY_CYCLE_H; CCP5CONbits &= (MIRP_DUTY_CYCLE_L << 4);
#define SET_POUR_PWM PR2 = POUR_PWM_PERIOD;
//#define SET_POUR_DUTY_CYCLE CCPR5L = POUR_DUTY_CYCLE_H; CCP5CONbits &= (POUR_DUTY_CYCLE_L << 4);
/*//CCPR5L:CCP5CON<5:4> = (1/112,000) / (*TOSC* (TMR2 Prescale Value)) or 76000
//CCPR5L:CCP5CON<5:4> = (1/112,000) / (1/8,000,000)
//CCPR5L:CCP5CON<5:4> = 71 = 0b0001 0001:11 //Duty = 50%
*/
#define LED1 LATAbits.LATA1
#define LED2 LATAbits.LATA2
#define AWAY_RFID PORTAbits.RA0
#define HOME_RFID PORTAbits.RA3
#define ON 1
#define OFF 0
#define FALSE 0
#define TRUE 1
#define INPUT 1
#define OUTPUT 0
#define MIRP_START_NUM_CYCLES 10
#define MIRP_PAYLOAD_damage_NUM_CYCLES 20
#define MIRP_STOP_NUM_CYCLES 150
#define bool unsigned int
#define DEBOUNCE_TIME 3
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* V A R I A B L E S
*
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
///////////////
/////main
///////////////
unsigned int LED_counter = 0;
unsigned int dcounter = 0;
unsigned int jcounter = 0;
bool outOfJuice = FALSE;
bool pouring = FALSE;
bool debug = FALSE;
bool athome = TRUE;
bool deadly = FALSE;
bool lastReadWasHome = TRUE;
// RFID_READ[12];
//char* RFID_READ[5] = NULL;
///////////////
/////xbee
///////////////
unsigned char _HOME_RFID[13] = HOME_RFID_STRING;
unsigned char _AWAY_RFID[13] = AWAY_RFID_STRING;
// char card1[13] = "6A003E4FA7BC"; // Card number 1
//unsigned char card2[13] = "6A003E714366"; // Card number 2
// No idea why, but 17 is the magic number
unsigned char scanned[17]; // Scanned in char array
unsigned char formated[12]; // Formated char array
// Again, no idea why it works with these random values, but it does
int RFID_SIZE = 16;
int xcounter = 0;
int audioCheck = 5;
int rfidStatus( void );
///////////////
/////audio
///////////////
///////////////
/////visual
///////////////
unsigned int pwmbuffer[24]; // Brightness buffer
int b, c, i, j, k; // Loop variables
unsigned int d; // for delay function only
int fillLevel; // Counter variable for fill LEDs
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* F U N C T I O N S
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
////////////////////////////
//PWM Period = [(PR2) + 1] * 4 * TOSC *(TMR2 Prescale Value) = 1/56,000
//PWM Duty Cycle = (CCPR4L:CCP4CON<5:4>) *TOSC* (TMR2 Prescale Value); Duty = 50% (1/112,000), 100% (1/56,000)
//CCPR4L:CCP4CON<5:4> is the value
//1. Set the PWM period by writing to the PR2
//register.
//PR2 = 1/(56,000 * 4 * TOSC * (TMR2 Prescale Value)) - 1
//PR2 = 1/(56,000 * 4 * (1/10,000,000) * 4) - 1
//PR2 = //for deadly IR
//PR2 = 1/(38,000 * 4 * (1/10,000,000) * 4) - 1
//PR2 = //for pouring
//2. Set the PWM duty cycle by writing to the
//CCPR4L register and CCP4CON<5:4> bits.
//CCPR4L:CCP4CON<5:4> = 1/(112,000*TOSC* (TMR2 Prescale Value))
//CCPR4L:CCP4CON<5:4> = 1/(112,000 * (1/10,000,000) * 1))
//CCPR4L:CCP4CON<5:4> = 89 = 0b00010110:01 //Duty = 50%
///////////////////////////////////////////////////////
void sendDeadlyPacket( void);
void sendPourPulse(void);
void playAudio( unsigned int track );
//signed char strcmppgm2ram(const char * str1, const rom char * str2 );
//visual functions
void pourLEDs( char color,int step );
void followLEDs( char color, int step,int loop);
void fadeLEDs(char color, int step);
void flashLEDs(char color, int time,int brightness);
void fillLEDs( char color, int step );
void randomLED( int step );
void delay( int cycles );
void setLED( int lednum, int r, int g, int b);
void setPWM( int chan, int pwm);
void writeLED( void );
void clearAll( void );
void fadeInLEDs(char color, int step);
void fadeOutLEDs(char color, int step);
//canbus functions
void canTransmit(void);
void canInitialize(void);
void sendDeadlyPacket( void){
CCPR5L = 0x11;
Delay10TCYx(38);//on for 10 56k pulses
CCPR5L = 0x0;
Delay1KTCYx(5);//off for 140 56k pulses
CCPR5L = 0x11;
Delay10TCYx(71);//on for 20
CCPR5L = 0x0;
Delay1KTCYx(4);//off for 130
Delay10TCYx(64);
CCPR5L = 0x11;
Delay10TCYx(71);//on for 20
CCPR5L = 0x0;
Delay1KTCYx(4);//off for 130
Delay10TCYx(64);
CCPR5L = 0x11;
Delay1KTCYx(5);//on for 150
Delay10TCYx(36);
CCPR5L = 0x0;//off
Delay1KTCYx(100);
}
void sendPourPulse(void){
CCPR5L = 0x11;//turn on IR signal
#if CUP_ID == 1
Delay1KTCYx(3);
Delay10TCYx(32);//on for 63 38k pulses
#elif CUP_ID == 2
Delay1KTCYx(1);
Delay10TCYx(66);//on for 63 38k pulses
#endif
CCPR5L = 0x0;//turn off IR signal
#if CUP_ID == 1
Delay1KTCYx(3);
Delay10TCYx(32);//off for 63 38k pulses
#elif CUP_ID == 2
Delay1KTCYx(1);
Delay10TCYx(66);//off for 63 38k pulses
#endif
}
void main( void ){
// OSCCON = 0b01100011;
// OSCCON = 0b01110111;
// OSCCONbits.IRCF = 0b110; set osc to 8MHz (default)
INTCONbits.PEIE = 1;//enables all peripheral INTerrupts
INTCONbits.GIE = 1;
//RCONbits.IPEN = 0;//disables priority INTerrupts
//INIT LEDS
TRISAbits.TRISA0 = 0;//input
TRISAbits.TRISA1 = 0;//output
TRISAbits.TRISA2 = 0;
TRISAbits.TRISA3 = 0;//input
LED1 = ON;
LED2 = ON;
//INIT UART
Open1USART(USART_TX_INT_OFF & USART_RX_INT_OFF
& USART_ASYNCH_MODE & USART_EIGHT_BIT
& USART_SINGLE_RX & USART_BRGH_LOW, 12); // try 12, 51, 207
RCSTA1bits.SPEN = 1; // Enable USART
RCSTA1bits.RX9 = 0; // Nine Bit mode off
RCSTA1bits.CREN = 1; // Something
RCSTA1bits.ADDEN = 0; // Something
TRISCbits.TRISC6 = 0; //TX1
TRISCbits.TRISC7 = 1; //RX1
Delay1KTCYx(4);
//PIE1bits.RC1IE = 1;
//INIT CAN BUS
canInitialize();
//INIT PWM
PR2 = 0x23;//56KHz, //set PWM freqency to 56kHz
CCPR5L = 0x0; CCP5CON &= 0xCF;//11001111//CCP5CON |= 0x30;//0b00110000//set duty cycle to 50%
TRISBbits.TRISB5 = 0; //set CCP5 (PWM) to output
T2CONbits.T2CKPS = 0b00; //prescalar = 1
CCPTMRSbits.C5TSEL = 0;//CCP5 is based off of timer 2
T2CONbits.TMR2ON = 1; //turn on timer 2
CCP5CON |= 0x0F; // enable PWM mode
TIMER_3_INTERRUPT_FLAG = FALSE;
TIMER_3_INTERRUPT_ENABLED = TRUE;
// T1CONbits.TMR1CS = 0b01;//using INTernal osc
T3CONbits.TMR3ON = 1;
// T1CONbits.SOSCEN = 1;
//INIT CAN INTERRUPT
TRISBbits.TRISB0 = 1;
ANCON1bits.ANSEL8 = 0;//digital
INTCONbits.INT0F = 0;
INTCONbits.INT0E = 1;
INTCON2bits.INTEDG0 = 1; // from low to high //CAN_BUS_NOT_CONNECTED = TRUE
//INIT RFID INTERRUPT
TRISBbits.TRISB1 = 1;//input
ANCON1bits.ANSEL10 = 0;//digital
INTCON3bits.INT1F = 0;
INTCON3bits.INT1E = 1;
INTCON2bits.INTEDG1 = 1;//from low to high
//Write1USART('c');
//stuff
TRISBbits.TRISB4 = 1;
ANCON1bits.ANSEL9 = 0;//digital
LATBbits.LATB4 = 1;
//INIT VISUAL
TRISCbits.TRISC2 = TRISCbits.TRISC3 = TRISCbits.TRISC4 = TRISCbits.TRISC5 = 0;
SCLK = 0;
SOUT = 0;
BLANK = 0;
XLAT = 0;
//INIT AUDIO
TRISAbits.TRISA1 = 0;
TRISAbits.TRISA2 = 0;
TRISAbits.TRISA3 = 0;
TRACK1 = 1;
TRACK2 = 1;
TRACK3 = 1;
clearAll();
#if DEBUG
//deadly = TRUE;
while(1){
athome = lastReadWasHome & PORTBbits.RB4;
// Left home RFID
if(lastReadWasHome && !PORTBbits.RB4){
lastReadWasHome = FALSE;
playAudio(1);
}
// Shooting Deadly IR
if(deadly){
fadeInLEDs('r',3);
flashLEDs('r', 10, 200);
sendDeadlyPacket();
if (audioCheck % 5 == 0)
{
playAudio(2);
audioCheck = 0;
}
fadeOutLEDs('r',20);
//2 second visual animation
audioCheck++;
}
// Pouring
else if(pouring){
sendPourPulse();
//begin pour pwms
}
// Back Home
else if (athome){
fadeLEDs(MY_COLOR, 10);
}
//
else if (CAN_BUS_NOT_CONNECTED){
//5 second visual animation
fadeLEDs('r', 50);
}
else{
#if MY_COLOR == 'b'
setLED(0, 0 , 0, 255 );//blue
setLED(1, 0 , 0, 255 );//blue
setLED(2, 0 , 0, 255 );//blue
setLED(3, 0 , 0, 255 );//blue
setLED(4, 0 , 0, 255 );//blue
setLED(5, 0 , 0, 255 );//blue
setLED(6, 0 , 0, 255 );//blue
setLED(7, 0 , 0, 255 );//blue
writeLED();
#elif MY_COLOR == 'g'
setLED(0, 0 , 255, 0 );//green
setLED(1, 0 , 255, 0 );//green
setLED(2, 0 , 255, 0 );//green
setLED(3, 0 , 255, 0 );//green
setLED(4, 0 , 255, 0 );//green
setLED(5, 0 , 255, 0 );//green
setLED(6, 0 , 255, 0 );//green
setLED(7, 0 , 255, 0 );//green
writeLED();
#endif
}
}
#else
while(1){
if(!CAN_BUS_NOT_CONNECTED){
LED1 = pouring;
}
else{
LED1 = athome;
LED2 = deadly;
}
}
#endif
}
void canTransmit(void){
//while loop to wait for canbus to be connected
while(i<50){
if((!(TXB0CONbits.TXREQ)))
{
TXB0EIDH = 0x00;
TXB0EIDL = 0x00;
//set the standard identifier to 0x02
TXB0SIDH = 0x02;
TXB0SIDL = 0x02;
//send the byte that was passed to the transmit function
TXB0D1 = 0x00; // values to transmit
TXB0D2 = 0x01;
TXB0D3 = 0x00;
TXB0D4 = 0x00;
TXB0CONbits.TXREQ = 1; //Set the buffer to transmit
Delay10KTCYx(100); // add delay for transmit time
Delay10KTCYx(100);
Delay10KTCYx(100); // add delay for transmit time
Delay10KTCYx(100);
}
else if((!(TXB1CONbits.TXREQ)))
{
TXB1EIDH = 0x00;
TXB1EIDL = 0x00;
//set the standard identifier to 0x02
TXB1SIDH = 0x02;
TXB1SIDL = 0x02;
//send the byte that was passed to the transmit function
TXB1D1 = 0x00;
TXB1D2 = 0x01;
TXB1D3 = 0x00;
TXB1D4 = 0x00;
TXB1CONbits.TXREQ = 1; //Set the buffer to transmit
Delay10KTCYx(100); // add delay for transmit time
Delay10KTCYx(100);
Delay10KTCYx(100); // add delay for transmit time
Delay10KTCYx(100);
}
else if((!(TXB2CONbits.TXREQ)))
{
TXB2EIDH = 0x00;
TXB2EIDL = 0x00;
//set the standard identifier to 0x02
TXB2SIDH = 0x02;
TXB2SIDL = 0x02;
//send the byte that was passed to the transmit function
TXB2D1 = 0x00;
TXB2D2 = 0x01;
TXB2D3 = 0x00;
TXB2D4 = 0x00;
TXB2CONbits.TXREQ = 1; //Set the buffer to transmit
Delay10KTCYx(100); // add delay for transmit time
Delay10KTCYx(100);
Delay10KTCYx(100); // add delay for transmit time
Delay10KTCYx(100);
}
i++;
}
}
void canInitialize(void){
//Configure the oscillator for the device
//OSCCON = 0b01100011; // set clock to 8 MHz
CANCON = 128; // set canbus to config mode
while(!((CANSTAT&0x80)==0x80)); // wait to enter config mode
ECANCONbits.MDSEL0=0; // select mode 0
ECANCONbits.MDSEL1=0;
TRISBbits.TRISB2 = 0; // sets Tx to output
TRISBbits.TRISB3 = 1; // sets Rx to input
BRGCON1 = 0x03; //Configure the baud rate of the CANBUS
BRGCON2 = 0x83; //62.5 kb/s
BRGCON3 = 0x01; // another baud rate configruation
TXB0DLC = 0x01;// how many bytes in data field 0-8 bytes
CANCON = 0; // set Canbus to normal mode
while((CANSTAT!=0)); // wait for normal mode
}
int rfidStatus( void )
{
// Read in card number
while (xcounter < RFID_SIZE)
{
PIR1bits.RC1IF = 0;
while (!PIR1bits.RC1IF);//{fadeLEDs('w', 10);} // Wait for recieve flag to be set
scanned[xcounter] = RCREG1; // Store character in array
xcounter++; // Increment xcounter
Write1USART(RCREG1);
RCREG1 = 0x00; // Clear register
}
// clear xcounter for next scan
xcounter = 0;
// Remove nonsense characters
for (i = 0; i <= RFID_SIZE; i++)
{
// Accept 0-9 and A-F
if ( (scanned[i] >= 0x30 && scanned[i] <= 0x39)
|| (scanned[i] >= 0x41 && scanned[i] <= 0x46) )
{
formated[xcounter] = scanned[i];
xcounter++;
}
}
// Clear xcounter again
xcounter = 0;
//
// Handle scanned input
//
if ((formated[10] == _AWAY_RFID[10]) && (formated[11] == _AWAY_RFID[11]))
// Scanned matches Card1
return 1;
else if((formated[10] == _HOME_RFID[10]) && (formated[11] == _HOME_RFID[11]))
// Scanned doesn't match Card1
return 0;
return -1;
}
#pragma code highPriorityINTerrupts = 0x08
#pragma interrupt highPriorityINTerrupts
void highPriorityINTerrupts( void ){
if(INTCONbits.INT0F){//CAN
Delay1KTCYx(DEBOUNCE_TIME);
INTCONbits.INT0E = 0;
CAN_BUS_NOT_CONNECTED ^= 1;//CAN_BUS_NOT_CONNECTED = !CAN_BUS_NOT_CONNECTED
if(CAN_BUS_NOT_CONNECTED){
PR2 = 0x23;//prepare to send deadly
dcounter = 0;
T3CONbits.TMR3ON = TRUE;
}
else{
deadly = FALSE;
canTransmit();
}
INTCONbits.INT0F = 0;
INTCONbits.INT0E = 1;
}
if(INTCON3bits.INT1F){//RFID
Delay1KTCYx(DEBOUNCE_TIME);
INTCON3bits.INT1E = 0;
INTCON3bits.INT1F = 0;
if(INTCON2bits.INTEDG1){
unsigned int stat = rfidStatus();
if(stat == 0){//home
athome = TRUE;
fadeLEDs('w', 10);
outOfJuice = FALSE;
jcounter = 0;
lastReadWasHome = TRUE;
deadly = FALSE;
dcounter = 0;
pouring = FALSE;
LED_counter = 0;
}
else if(stat == 1){//away
// fadeInLEDs('b', 10);
lastReadWasHome = FALSE;
if(!CAN_BUS_NOT_CONNECTED && !outOfJuice){
if(!pouring){
PR2 = 0x34; //38KHz,//set PWM to 38 k
jcounter = 0;//first time to read, reset pour counter
playAudio(3);
fadeInLEDs('b', 10);
}
pouring = TRUE;
}
}
}
//INTCON2bits.INTEDG1 ^= 1;
INTCON3bits.INT1F = 0;
INTCON3bits.INT1E = 1;
}
if(TIMER_3_INTERRUPT_FLAG){
TIMER_3_INTERRUPT_ENABLED = FALSE;
if(CAN_BUS_NOT_CONNECTED & ~athome){
dcounter++;
}
if(pouring){
jcounter++;
}
if(dcounter > DEADLY_COUNT){
deadly = TRUE;
dcounter = 0;
//T3CONbits.TMR3ON = FALSE; //turn off timer
playAudio(1); //turn alarm off
//playAudio(2);
//playAudio(2);
}
else if(jcounter > OUT_OF_JUICE_COUNT){
outOfJuice = TRUE;
pouring = FALSE;
LED_counter = 0;
jcounter = 0;
//T3CONbits.TMR3ON = FALSE; //turn off timer
}
else if(pouring && (jcounter % 110 == 0)){
setLED(LED_counter, 0,0,0);//turn LED off
LED_counter++;
writeLED();
}
TIMER_3_INTERRUPT_FLAG = FALSE;
TIMER_3_INTERRUPT_ENABLED = TRUE;
}
}
void playAudio( unsigned int track )
{
switch(track)
{
case 1:
//Set pin to Low to play the track
TRACK1 = 0;
//Delay to make sure the pin is grounded
Delay1KTCYx(100);
//Set pin back to high so it can be played again later
TRACK1 = 1;
break;
case 2:
//Set pin to low to play the track
TRACK2 = 0;
//Delay to make sure the pin is grounded
Delay1KTCYx(100);
//Set pin back to high so it can be played again later
TRACK2 = 1;
break;
case 3:
//Set pin to low to play the track
TRACK3 = 0;
//Delay to make sure the pin is grounded
Delay1KTCYx(100);
//Set pin back to high so it can be played again later
TRACK3 = 1;
break;
}
}
void pourLEDs( char color, int timer )
{
int scaledTime;
scaledTime = (MAX_STEP / POUR_TIME) * timer;
for (i = 0; i < 8; i++)
{
if (color == 'r')
setLED(i, scaledTime, 0, 0);
if (color == 'g')
setLED(i, 0, scaledTime, 0);
if (color == 'b')
setLED(i, 0, 0, scaledTime);
}
writeLED();
}
void followLEDs( char color, int step, int loop)
{
int firstLoop = 1;
while (loop >= 0 )
{
for (i = 0; i < 8; i++)
{
if (i == 0 && firstLoop)
for (j = 0; j < MAX_STEP; j++)
{
if (color == 'r')
setLED(i, j, 0, 0);
if (color == 'g')
setLED(i, 0, j, 0);
if (color == 'b')
setLED(i, 0, 0, j);
if (color == 'y')
setLED(i, j, j, 0);
if (color == 'c')
setLED(i, 0, j, j);
if (color == 'm')
setLED(i, j, 0, j);
if (color == 'w')
setLED(i, j, j, j);
writeLED();
j = j + step;
firstLoop = 0;
}
if (i > 0)
{
for (j = 0, k = MAX_STEP; j < MAX_STEP || k > 0; j++, k--)
{
if (color == 'r')
{
setLED(i, j, 0, 0);
setLED(i-1, k, 0, 0);
}
if (color == 'g')
{
setLED(i, 0, j, 0);
setLED(i-1, 0, k, 0);
}
if (color == 'b')
{
setLED(i, 0, 0, j);
setLED(i-1, 0, 0, k);
}
if (color == 'y')
{
setLED(i, j, j, 0);
setLED(i-1, k, k, 0);
}
if (color == 'c')
{
setLED(i, 0, j, j);
setLED(i-1, 0, k, k);
}
if (color == 'm')
{
setLED(i, j, 0, j);
setLED(i-1, k, 0, k);
}
if (color == 'w')
{
setLED(i, j, j, j);
setLED(i-1, k, k, k);
}
writeLED();
j = j + step;
k = k - step;
}
setLED(i-1, 0, 0, 0);
writeLED();
}
if (i == 7)
{
for (j = 0, k = MAX_STEP; j < MAX_STEP, k > 0; k++, j--)
{
if (color == 'r'){
if (loop > 0)
setLED(0, j, 0, 0);
setLED(i, k, 0, 0);
}
if (color == 'g'){
if (loop > 0)
setLED(0, 0, j, 0);
setLED(i, 0, k, 0);
}
if (color == 'b'){
if (loop > 0)
setLED(0, 0, 0, j);
setLED(i, 0, 0, k);
}
if (color == 'y'){
if (loop > 0)
setLED(0, j, j, 0);
setLED(i, k, k, 0);
}
if (color == 'c'){
if (loop > 0)
setLED(0, 0, j, j);
setLED(i, 0, k, k);
}
if (color == 'm'){
if (loop > 0)
setLED(0, j, 0, j);
setLED(i, k, 0, k);
}
if (color == 'w'){
if (loop > 0)
setLED(0, j, j, j);
setLED(i, k, k, k);
}
writeLED();
k = k - step;
}
setLED(i, 0, 0, 0);
writeLED();
}
}
loop--;
}
}
void fillLEDs( char color, int step )
{
for (i = 0; i < 8; i++)
for (j = 0; j < MAX_STEP; j++)
{
if (color == 'r')
setLED(i, j, 0, 0);
if (color == 'g')
setLED(i, 0, j, 0);
if (color == 'b')
setLED(i, 0, 0, j);
if (color == 'y')
setLED(i, j, j, 0);
if (color == 'c')
setLED(i, 0, j, j);
if (color == 'm')
setLED(i, j, 0, j);
if (color == 'w')
setLED(i, j, j, j);
writeLED();
j = j + step;
}
for (i = 0; i < 8; i++)
{
for (j = MAX_STEP; j > 0; j--)
{
if (color == 'r')
setLED(i, j, 0, 0);
if (color == 'g')
setLED(i, 0, j, 0);
if (color == 'b')
setLED(i, 0, 0, j);
if (color == 'y')
setLED(i, j, j, 0);
if (color == 'c')
setLED(i, 0, j, j);
if (color == 'm')
setLED(i, j, 0, j);
if (color == 'w')
setLED(i, j, j, j);
writeLED();
j = j - step;
}
setLED(i, 0, 0, 0);
writeLED();
}
}
void flashLEDs(char color, int time, int brt)
{
for (i = 0; i < 8; i++)
{
if (color == 'r')
setLED(i, brt, 0, 0);
if (color == 'g')
setLED(i, 0, brt, 0);
if (color == 'b')
setLED(i, 0, 0, brt);
if (color == 'c')
setLED(i, 0, brt, brt);
if (color == 'y')
setLED(i, brt, brt, 0);
if (color == 'm')
setLED(i, brt, 0, brt);
if (color == 'w')
setLED(i, brt, brt, brt);
}
writeLED();
// Delay function times 10
for(i = 0; i < 10; i++)
delay(time);
// Turn off all LEDs
clearAll();
for(i = 0; i < 10; i++)
delay(time);
}
void fadeInLEDs(char color, int step){
for (i = 0; i < MAX_STEP; i++)
{
for (j = 0; j < 8; j++)
{
if (color == 'r')
setLED(j, i, 0, 0);
if (color == 'g')
setLED(j, 0, i, 0);
if (color == 'b')
setLED(j, 0, 0, i);
if (color == 'y')
setLED(j, i, i, 0);
if (color == 'c')
setLED(j, 0, i, i);
if (color == 'm')
setLED(j, i, 0, i);
if (color == 'w')
setLED(j, i, i, i);
}
writeLED();
i = i + step;
}
}
void fadeOutLEDs(char color, int step){
for (i = MAX_STEP; i > 0; i--)
{
for (j = 0; j < 8; j++)
{
if (color == 'r')
setLED(j, i, 0, 0);
if (color == 'g')
setLED(j, 0, i, 0);
if (color == 'b')
setLED(j, 0, 0, i);
if (color == 'y')
setLED(j, i, i, 0);
if (color == 'c')
setLED(j, 0, i, i);
if (color == 'm')
setLED(j, i, 0, i);
if (color == 'w')
setLED(j, i, i, i);
}
writeLED();
i = i - step;
}
}
void fadeLEDs(char color, int step)
{
for (i = 0; i < MAX_STEP; i++)
{
for (j = 0; j < 8; j++)
{
if (color == 'r')
setLED(j, i, 0, 0);
if (color == 'g')
setLED(j, 0, i, 0);
if (color == 'b')
setLED(j, 0, 0, i);
if (color == 'y')
setLED(j, i, i, 0);
if (color == 'c')
setLED(j, 0, i, i);
if (color == 'm')
setLED(j, i, 0, i);
if (color == 'w')
setLED(j, i, i, i);
}
writeLED();
i = i + step;
}
for (i = MAX_STEP; i > 0; i--)
{
for (j = 0; j < 8; j++)
{
if (color == 'r')
setLED(j, i, 0, 0);
if (color == 'g')
setLED(j, 0, i, 0);
if (color == 'b')
setLED(j, 0, 0, i);
if (color == 'y')
setLED(j, i, i, 0);
if (color == 'c')
setLED(j, 0, i, i);
if (color == 'm')
setLED(j, i, 0, i);
if (color == 'w')
setLED(j, i, i, i);
}
writeLED();
i = i - step;
}
}
void setLED(int lednum, int r, int g, int b)
{
setPWM(lednum*3, b);
setPWM(lednum*3+1, g);
setPWM(lednum*3+2, r);
}
void setPWM(int chan, int pwm)
{
if (pwm > MAX_STEP) pwm = 255;
if (chan > 24) return;
pwmbuffer[chan] = pwm;
}
void clearAll( void )
{
for (i = 0; i < 24; i++)
pwmbuffer[i] = 0;
writeLED();
}
void writeLED( void )
{
XLAT = 0;
for (c = 23; c >= 0; c--)
{
for (b = 11; b >= 0; b--)
{
SCLK = 0;
delay(2);
if ( pwmbuffer[c] & (1 << b) )
SOUT = 1;
else
SOUT = 0;
SCLK = 1;
}
}
SCLK = 0;
BLANK = 1;
XLAT = 1;
BLANK = 0;
XLAT = 0;
}
void delay(int cycles )
{
for (d = 0; d < cycles; d++);
}
/*
#pragma code lowPriorityINTerrupts = 0x18
#pragma INTerrupt lowPriorityINTerrupts
void lowPriorityINTerrupts( void )
{
if(TIMER_2_INTERRUPT_FLAG){//Deadly count, juice expiration count, or MIRP ready count increment
TIMER_2_INTERRUPT_FLAG = FALSE;
dcounter++;
if(CAN_BUS_NOT_CONNECTED && (dcounter == MIRP_READY_COUNT || dcounter == DEADLY_COUNT)){//if should send deadly IR packet
dcounter = 0;
LED2 = ON;//deadly
MIRPcyclesPerFrame = MIRP_START_NUM_CYCLES;
TMR3L = TMR3H = 0x0; //reset the value of timer 3
TIMER_3_INTERRUPT_ENABLED = TRUE; //begin mirp frame timer
FALLING_EDGE_INTERRUPT_ENABLED = TRUE;
}
}
else if(TIMER_3_INTERRUPT_FLAG){//MIRP frame time
TIMER_3_INTERRUPT_FLAG = FALSE;
counter_3 ++;
if(counter_3 == MIRP_FRAME_COUNT){
counter_3 == 0;
TIMER_3_INTERRUPT_FLAG = FALSE;//disable this counter until the cycles in this frame are finished
FALLING_EDGE_INTERRUPT_ENABLED = TRUE;//enable the cycle counter INTerrupt
MIRPcycleCounter = 0;
switch(MIRPframeCounter){
case 1://data envelope
MIRPcyclesPerFrame = MIRP_PAYLOAD_damage_NUM_CYCLES;
CCPR5L = 0x11; CCP5CON |= 0x30;//0b00110000//set duty cycle to 50%
break;
case 2://data envelope
MIRPcyclesPerFrame = MIRP_PAYLOAD_damage_NUM_CYCLES;
CCPR5L = 0x11; CCP5CON |= 0x30;//0b00110000//set duty cycle to 50%
break;
case 3://stop envelope
MIRPcyclesPerFrame = MIRP_STOP_NUM_CYCLES;
break;
case 4://stop sending data
TIMER_3_INTERRUPT_ENABLED = FALSE;
FALLING_EDGE_INTERRUPT_ENABLED = FALSE;
MIRPframeCounter = 1;
break;
}
}
}
}*/