Page 1 of 1

PIC18F4580 Interrupt programming in C. Problem

PostPosted: Thu Dec 01, 2011 5:51 pm
by James.Josip
Hi,
I am using TMR0 as a counter and TMR1 as a timer of 2 sec in the PIC18F4580. Therefore the program I am writing is to use Interrupt whereby Timer 0 as it is counting external clock pulse coming in from RA4 infinitely upon receiving roll over of Timer1 interrupt flag will run the ISR to clear the TMR0IF and the count that is in TMR0L. The application is for building a speedometer that measure pulses received from RA4 in a certain amount of time. This is my program. The program can count and show the value in 8 LED connected to PORTC. But the program dont seem to be able to receive the Interrupt and run the ISR.
Please help me. I have tried and tried and tried but the program interrupt is not working. I dont know where is the mistake. This is the program code.

#include <p18f4580.h>
#pragma config LVP=OFF
#pragma config WDT=OFF
#pragma config OSC=HS
#pragma config MCLRE=ON
#pragma config PBADEN=OFF
void chk_isr(void);
void T1_ISR();

#pragma code my_hiprio_int=0x0008
void my_hiprio_int (void)
{
_asm
GOTO chk_isr
_endasm
}

void chk_isr(void)

{
if (PIR1bits.TMR1IF==0)
T1_ISR();

}

void main(void)
{
INTCONbits.PEIE=1;
INTCONbits.GIE=1;
PIE1bits.TMR1IE=1;
TRISAbits.TRISA4=1;
TRISC=0X00;
T0CON=0X68;
TMR0L=0X01;
T1CON=0x00;
TMR1H=0x09;
TMR1L=0xDE;
T1CONbits.TMR1ON=1;

while(1)
{

do
{

T0CONbits.TMR0ON=1;
PORTC=TMR0L;
}

while(INTCONbits.TMR0IF==0);


}

}

void T1_ISR(void)

{

T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
PORTC=0X00;
T1CONbits.TMR1ON=0;
PIR1bits.TMR1IF=0;
}

Re: PIC18F4580 Interrupt programming in C. Problem

PostPosted: Fri Dec 02, 2011 10:59 am
by sich
A quick glance at your code, I don't really understand your program flow. Split your code into small portions and test them one-by-one. Only continue to the integration once each of them work separately.

1. Can you control each of your 8 LEDs?
2. Is the Timer 0 able to count the external pulse from RA4? Since you have 8 LEDs, you can use them to show the count in binary.
3. Can the Timer 1 interrupt blink your LEDs according to the set period of time?