Hello, still getting the hang of using interrupts properly and I'm having a hard time modifying a global variable in my ISR. It seems like no matter what I do state is never modified. Here are the relevant parts of the code. I know it is a bit much but any help it all is appreciated!
defines and variables
ISRs
state machine
Moderators note: Please use code tags for the right language
defines and variables
C:
// Defines
#define _XTAL_FREQ 4000000 // running the chip at 4Mhz
#define ID_MAX 63 // highest ID number possible. 6 bits base^n
#define ID 1 // #DEFINE for hardcoded ID's. Safe route for presentation
#define BOOT_STALL 750 // Number of mS to delay upon receiving ACK from raspi
#define TMR_TWO_PERIOD 62500 // Remaing counts, 245 rolls rougly one second. 50% duty cycle
//#define TMR_TWO_HALF_PERIOD // see above...
#define DISTRESS 0x11 // distress state
#define ALERT 0x02 // alert state
#define BOOT_MASK 0x40 // boot packet mask
#define ALARM_MASK 0xC0 // alarm packet mask
char rx_buffer; // dumping UART data in here for processing
int timer_1_overflow_count = 0; // Timer 1 overflow counter, keeps track of how often timer 1 overflows
int timer_2_overflow_count = 0; // Timer 2 overflow counter, keeps track of how often timer 2(16bit) overflows
int id_ok = 0; // Set high when id is echoed(accepted) back by raspi.
int tmr0_rc = 0; // counts rolls of timer 0
int tmr1_rc = 0; // counts rolls of timer 1
int tmr2_rc = 0; // counts rolls of timer 2
char strobe_ok = 0x0; // determines weather or not its ok for the strobe light to come on
char vibe_ok = 0x0; // determines weather or not its ok for the vibration to come on
char packet = 0x0; // transmission packet
volatile char state = 0x00;
ISRs
C:
void interrupt ISR (void)
{
/* Receiving ISR */
if(RCIF) // If i receive a message from the network
{
state = ALERT;
rx_buffer = RCREG;
}
/* End Receiving ISR */
/* Timer 0 ISR drives // used for strobe light
* strobe light and Vibration */
/*if(T0IF)
{
if(strobe_ok || vibe_ok) // we only want to keep track of rolls if its ok to strobe
{
tmr0_rc++;
T0IF = 0; // reset flag
}
}*/
/* Timer 1(16 bit) ISR transmits distress signal */
if(TMR1IF)
{
TMR1IF = 0;
tmr1_rc++;
TMR1 = 0x7AE0; // dumping 31456 inside the register. Leaving roughly 1 second worth of counts remaining
}
/* end timer 1 ISR*/
/* Timer 2 ISR drives piezo buzzer */
if(TMR2IF) // timer 2 overflowed, used to control peizo
{
TMR2IF = 0; // reset flag
tmr2_rc++;
}
/* End Timer 2 ISR */
/* External RB0 rising edge interrupt */
if(INTF) // RBO/INT interrupt, must clear flag in software. Disable Rx interrupt.
{
INTF = 0;
state = DISTRESS;
RA6 = 1;
}
}
C:
while(1) // Loop forever...state machine
{
// state machine
switch(state)
{
case DISTRESS :
RCIE = 0; // at this point we're ignoring all incoming messages
RA1 = 0; // kill GIL
/* while(RCREG) // unlatch RCIF by...
if(RCREG); // clearing the fifo buffer*/
// tmr0 is constantly r
T1CONbits.TMR1ON = 1; // turn timer1 on Tx clock
T2CONbits.TMR2ON = 1; // turn timer2 on Strobe clock
strobe_ok = 0x01; // let tmr0 know its ok to turn on the strobe
if(tmr1_rc >= 5) // 5 rolls..5 seconds.
{
packet = packet | ALARM_MASK;// turn it into a fall packet
packet = packet | ID; // attaching ID to last 6 bits
TXREG = packet; // transmit the distress signal
tmr1_rc = 0; // reset roll counter
}
if(tmr2_rc >= 122) // 122 rolls = half second
{
RB4 = !RB4; // drive/silence peizo
RB7 = !RB7; // drive/silence strobe
tmr2_rc = 0;
}
break;
case ALERT :
if((rx_buffer & ALARM_MASK) == ALARM_MASK) // someone transmitted, &'ing packet against fall mask 0xC0. Latching state, unlatched by INTF ISR
{
RCIE = 0; // disable Rx Interrupt, someone fell we're in alarm until the device is power cycled. Ignore all other incoming packets
vibe_ok = 1; // putting us in "help" or "alert" mode
} else if ((rx_buffer & BOOT_MASK) == BOOT_MASK) { // we've received a boot packet
if(rx_buffer == packet) // boot packet matches what was sent out
{
id_ok = 1; // id accepted by network
while(RCREG); // clear the fifo buffer
}
}
if(vibe_ok & tmr2_rc >= 122) // we're ok...but someone else fell wake up and help! Vibe on
{
RB6 = !RB6; // Vibe toggle
tmr2_rc = 0;
}
break;
default:
// do nothing
break;
}
}
Last edited by a moderator: