Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] 16F877 TIMER0 Interrupt not triggering

Status
Not open for further replies.

WStevens_sa

Member level 2
Joined
Jan 5, 2011
Messages
47
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
South Africa
Activity points
1,695
Hi Guys

Some where I am doing something wrong. I am trying to get TMR0 interrupt to trigger. RA0 Flashes constantly. When TMR0 triggers it makes RA5 turn on and off then returns back to continue flashing on RA0. At the moment only RA0 is flashing.

Please tell me what I am missing and also what code I dont need.

Code:
void interrupt(void) {

     if(INTCON.T0IF == 1)
     {
     RA5_bit=1;   //To see if interrupt is triggered
     Delay_ms(2000);
     INTCON.INTF = 0; //You HAVE to clear interrupt flag
     }
  }

void main()
{
TMR0 = 0;   //Clear timer 0
INTCON.T0IF = 0; //clear the interrupt flag
INTCON.T0IE = 0; //enable tmr0 interrupt
INTCON.GIE = 1; //Enable Global Interrupt
OPTION_REG.INTEDG = 1; //Interrupt on rising edge
INTCON.PEIE = 0; //Disable all unmasked peripheral interrupt

PORTA = 0; /*set RA0-RA5 low */
TRISA = 0; /*set PORTA to output*/
PORTD = 0; /*set RA0-RA5 low */
TRISD = 0; /*set PORTA to output*/

while(1)
{
RA0_bit=1;
Delay_ms(200);
RA0_bit=0;
Delay_ms(200);
};

}
 

Hi,
This is the corrected code:
Code:
void interrupt(void) {

     if(T0IF_bit == 1)
     {
     RA5_bit=1;   //To see if interrupt is triggered
     Delay_ms(2000);
     RA5_bit = 0;
     T0IF_bit = 0; //You HAVE to clear interrupt flag
     }
  }

void main()
{
TMR0 = 0;   //Clear timer 0

OPTION_REG = 8;
GIE_bit = 1; //Enable Global Interrupt
T0IF_bit = 0;
T0IE_bit = 1; //Enable TMR0 interrupt
PEIE_bit = 1;

PORTA = 0; /*set RA0-RA5 low */
TRISA = 0; /*set PORTA to output*/
PORTD = 0; /*set RA0-RA5 low */
TRISD = 0; /*set PORTA to output*/

ADCON1 = 7; //Disable ADC
//CMCON = 7; - FOR 16F877A

while(1)
{
RA0_bit=1;
Delay_ms(200);
RA0_bit=0;
Delay_ms(200);
}

}

It is a very bad idea to have that 2000ms delay in the ISR, as, then you won't be able to see the LEDs flashing. So, remove that, like here:
Code:
void interrupt(void) {

     if(T0IF_bit == 1)
     {
     RA5_bit=1;   //To see if interrupt is triggered
     T0IF_bit = 0; //You HAVE to clear interrupt flag
     }
  }

void main()
{
TMR0 = 0;   //Clear timer 0

OPTION_REG = 8;
GIE_bit = 1; //Enable Global Interrupt
T0IF_bit = 0;
T0IE_bit = 1; //Enable TMR0 interrupt
PEIE_bit = 1;

PORTA = 0; /*set RA0-RA5 low */
TRISA = 0; /*set PORTA to output*/
PORTD = 0; /*set RA0-RA5 low */
TRISD = 0; /*set PORTA to output*/

ADCON1 = 7; //Disable ADC
//CMCON = 7; - FOR 16F877A

while(1)
{
RA0_bit=1;
Delay_ms(200);
RA0_bit=0;
Delay_ms(200);
}

}

I hope you can understand what I've done in the coding. If not, feel free to ask.

Hope this helps.
Tahmid.
 
Thanks Tahmid

The only reason I put it there is for debugging to see that it is triggering. How do you verify that a trigger is working. I will try the new code. Thanks
 

Hi,

You have not selected clock source for TMR0. (T0CS bit)
Default is External-clock; then you should give pulse on T0CKI pin. if so please check it should work

For internal clock source clear T0CS bit.

Hope this also helps you
 
Hi all

DineshSL
You have not selected clock source for TMR0. (T0CS bit)
Default is External-clock; then you should give pulse on T0CKI pin. (In original code you have selected INTEDG = 1, it seems you intended to use external-clock, if so please check it should work)

For internal clock source clear T0CS bit.

This is the next thing I will be trying


What is the purpose of using either an internal or external interrupt?
 

Hi,

It's not an "interrupt". It's the clock source for your timer. How you signal the timer to increase.
You can select external-clock-signal (outside the PIC) or you may use instruction-cycle-clock of the PIC.

Got it ?

---------- Post added at 13:58 ---------- Previous post was at 13:54 ----------

"External interrupt" requires a signal from outside the PIC to raise interrupt condition. (like pulse to a pin of PIC from another circuit)
 

It's not an "interrupt". It's the clock source for your timer. How you signal the timer to increase.
You can select external-clock-signal (outside the PIC) or you may use instruction-cycle-clock of the PIC.

Got it ?

I understand how interrupts work. What I do not understand is what would you use it for. For example if I create a Timer that counts to 60 or an alarm clock. Would I use an interrupt and if so why would I?
 

Well, think you design a Digital-clock; It display 88:88:88 (hours,min,sec).

When you know that display should be updated. At every second your Clock display must be updated.
So you use a timer-interrupt (set to count for 1 sec) to kick the processor to say "Update the display".
I hope you got it.

uC do what ever it has to process (Blinking LED, Calculations, ADC, communication,.......); timers count regardless of them and at triggering point, they ask uC to stop it's work and listen to me (ie. Interrupt !!!)
 
Well, think you design a Digital-clock; It display 88:88:88 (hours,min,sec).

When you know that display should be updated. At every second your Clock display must be updated.
So you use a timer-interrupt (set to count for 1 sec) to kick the processor to say "Update the display".
I hope you got it.

uC do what ever it has to process (Blinking LED, Calculations, ADC, communication,.......); timers count regardless of them and at triggering point, they ask uC to stop it's work and listen to me (ie. Interrupt !!!)


Okay. I thought that this was the purpose that is why I used a alarm clock for an example. The next thing is I read up about the timing of the interrupt triggers. But cannot figure it out. I created a counter with 4 seg leds. When I simulate it, it is so fast that I cannot see the count. So common sense dictates that the interrupt is triggering to fast. I am running it a 4MHZ clock speed. From what I read is that you divide it by 4 and it gives you a value and this is value is the speed in mil sec of trigger rate. But I cannot get it to update at a1 second interval.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top