Im looking for a little help and advice on my code.
Im using C18 in the MPLab environment, PIC18f4520 with Fosc @ 4MHz, and usnig timer0 in 16 bit mode to count to overflow, set the overflow bit and interrupt flag, then jump to ISR and increment a variable 'count'. This is outputted to a port with LEDs attached so that I can get visualy confirmation of the program working.
However, the outputted count is always '1' (i.e. 0x01) and I believe that the ISR is only happening once, if at all.
Any help that you can offer would be most appreciated.
Here is my code:
void main (void) /* */
{
TRISA = 0; /* */
TRISC = 0; /* */
TRISB = 0;
TRISD = 0x00;
RTOS();
}
void low_interrupt (void)
{
_asm GOTO timer_isr _endasm
}
#pragma code
#pragma interruptlow timer_isr
void timer_isr (void)
{
INTCONbits.TMR0IF = 0;
count = count++;
LATD = count;
RTOS();
}
void RTOS (void)
{
T0CONbits.T08BIT = 0; // 16-bit timer
T0CONbits.T0CS = 0; // increment on instruction cycle input
T0CONbits.T0SE = 0; // increment on low--> high transition of clock
T0CONbits.PSA = 1; // T0 prescaler not assigned i.e. 1:1 prescaler.
RCONbits.IPEN = 1; //Enable Interrupt Priorities
INTCONbits.GIEL = 1; //Enable Low Priority Interrupt
INTCONbits.GIE = 1; //Enable Global Interrupts
INTCONbits.TMR0IE = 1; //Enable Timer0 Interrupt
INTCON2bits.TMR0IP = 0; //TMR0 set to Low Priority Interrupt
INTCONbits.TMR0IF = 0; // T0 int flag bit cleared before starting
T0CONbits.TMR0ON = 1; // timer0 START
while (1);
}
Thanks in advance for any guidance you can offer.
Best Answer
You are calling RTOS() in your interrupt and the RTOS() function has "while (1);" in it (infinite loop?)
I'm not sure why you would reset the complete interrupt registers within your interrupt.
Having an infinite loop in your interrupt will most likely cause your program to malfunction.
Also, check the comment of Roger Rowland: "Where is
count
defined? Have you marked itvolatile
? – Roger Rowland 15 mins ago". This is a pretty common mistake and might also be the point here.