View previous topic :: View next topic |
Author |
Message |
SimplyRed37
Joined: 13 Mar 2011 Posts: 1 Location: Montreal, Canada
|
Need Help with Timer0 Code ... |
Posted: Sun Mar 13, 2011 11:31 am |
|
|
Good day all, and thanks to all the "regulars" for helping newbies like me and providing extremely useful posts and code. Newbies like me really do appreciate it.
I have the following code which flashes a LED connected to pin A0 on a PIC12F629 and the interval frequency is set by Timer0... (PCW version 4.038). I am testing the code using a PIC12F629 circuit in Proteus ISIS .
The frequency does vary when I change the prescaler or if if I change the value of my Timer0Counter, but I find that changing the value or Timer0 has no effect. I have reviewed the code and can't find out why.
Would definitely appreciate the help of a seasoned programmer to spot what I am doing wrong... Thanks in advance and Happy Sunday....
Code: |
#include <12F629.h>
#fuses NOMCLR
#use delay(clock=4000000)
#define RED_LED PIN_A0
#define Switch1 PIN_A1
#define Timer0Value 128
int Timer0Counter;
#int_TIMER0
Void TIMER0_isr()
{
Timer0Counter++;
If (Timer0Counter >= 12)
{
Output_toggle(RED_LED);
// Set_Timer0 (Timer0Value);
Set_Timer0 (128);
Timer0Counter = 0;
Clear_interrupt(int_timer0); // Clear TIMER0 interrupt flag
}
}
void main(){
Setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
Timer0Counter = 0;
Set_Timer0 ( Timer0Value );
Enable_interrupts(Int_timer0); // enable timer0
Enable_interrupts(GLOBAL);
While(1)
{
// Do nothing and wait for TIMER0 interrupts ISR ...
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 13, 2011 2:24 pm |
|
|
See this example program:
http://www.ccsinfo.com/forum/viewtopic.php?t=37807&start=3
Note: RTCC is the same thing as Timer0.
Other comments:
Quote: | Clear_interrupt(int_timer0);
|
You don't need to do this. CCS automatically puts in a line of hidden code
at the end of the interrupt routine to clear the interrupt.
Quote: | Clear_interrupt(int_timer0); // Clear TIMER0
Enable_interrupts(Int_timer0); // enable timer0
|
Here you have changed INT_TIMER0 into "mixed case" spelling with two
different spellings. There are several things wrong with this. Other
people reading this will be momemtarily confused and/or "put off" by the
case change. It makes the code harder to read, because the C standard
is that constants are in "all caps". Also, the CCS compiler is uniquely
case-insensitive, by default. In any other compiler, changing the case
like this, will burn you. It's a bad habit to get into. |
|
|
|