Hi guys, I have problems with the folowing piece of code. I modified the original example from Microchip C18 which implements TIMER0 interrupt and i added PORTB on-change interrupt. The code does only one thing : it increments pulse_count.b1234 which is an unsigned long (this being made inside ISR) when it detects a change from 0 to 1 on RB4 and in the main loop sends the value to PORTC so i can see it. I tested this code using Proteus VSM and it works well. I applied an impulse train on RB4 and the programs acts as it shoul with only one problem : after ~30 seconds it behaves erratically for a few moments and starts to count again from 0 ! I disabled watchdog timer (even from VSM), i disabled LVP, i disabled BOR, i disabled Stack overflow reset, no luck whatsoever. Can anyone see if i am doing something wrong with the folowing piece of code ? I have the .lst as well as .hex files if that might help. I have been doing this for the last 10 hours and i feel like i am loosing my mind. Thank you for your understanding, //---------------------------------------------------------------------------- #include #pragma config OSC = XT, OSCS = OFF, PWRT = ON, BOR = OFF, WDT = OFF, STVR = OFF, LVP = OFF, DEBUG = OFF //---------------------------------------------------------------------------- void main (void); void InterruptHandlerHigh (void); union { struct { unsigned Timeout:1; //flag to indicate a TMR0 timeout unsigned None:7; } Bit; unsigned char Byte; } Flags; //========================= union { struct { unsigned char B0; unsigned char B1; unsigned char B2; unsigned char B3; } byte; unsigned long b1234; } pulse_count; //========================= union { struct { unsigned RB0:1; unsigned RB1:1; unsigned RB2:1; unsigned RB3:1; unsigned RB4:1; unsigned RB5:1; unsigned RB6:1; unsigned RB7:1; }; unsigned char old_portb; } old_portb_bits; //---------------------------------------------------------------------------- // Main routine void main () { Flags.Byte = 0; INTCON = 0x28; //disable global and enable TMR0 interrupt and RB INTCON2 = 0x85; //TMR0 high priority RCONbits.IPEN = 1; //enable priority levels TMR0H = 0; //clear timer and set TMR0H TMR0L = 0; //clear timer low byte and also updates high byte from TMR0H T0CON = 0x88; //set up timer0 - no prescaler INTCONbits.GIEH = 1; //enable interrupts TRISB = 0x7E; //PORTB bits 0 and 7 are output, the rest are input TRISC = 0; //PORTC bits are output PORTC = 0; old_portb_bits.old_portb = 0; pulse_count.b1234 = 0; while (1) { PORTC = pulse_count.byte.B0; } } //---------------------------------------------------------------------------- // High priority interrupt vector #pragma code InterruptVectorHigh = 0x08 void InterruptVectorHigh (void) { _asm goto InterruptHandlerHigh //jump to interrupt routine _endasm } //---------------------------------------------------------------------------- // High priority interrupt routine #pragma code #pragma interrupt InterruptHandlerHigh void InterruptHandlerHigh () { if (INTCONbits.TMR0IF) { //check for TMR0 overflow INTCONbits.TMR0IF = 0; //clear interrupt flag } if (INTCONbits.RBIF) { PORTB = PORTB; INTCONbits.RBIF = 0; if (PORTBbits.RB4 == 1 && old_portb_bits.RB4 == 0) (unsigned long)pulse_count.b1234++; old_portb_bits.old_portb = PORTB; } } -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist