|
|
View previous topic :: View next topic |
Author |
Message |
flip
Joined: 15 Feb 2005 Posts: 11
|
Timer0 overflow interrupt does not work |
Posted: Tue Feb 15, 2005 3:07 pm |
|
|
I think all of the calculations for the time delay is correct and the program compiles however when I program it on to my PIC it does not do anything. I think I might be initializing my Timer0 interrupt incorrectly.
Code: |
//Output on a LED every two seconds using Timer0
#include <18F458.h>
#use delay(clock=40000000) //40MHz
#fuses HS, OSCSEN, BROWNOUT, BORV20, STVREN, NODEBUG, NOLVP //High Speed, Oscillator switching enabled, Brownout reset at 2.0V, Stack full causes rest, Low Voltage programming
int16 t = 0;
int out;
#int_TIMER0 // interrupt occurs every 3.2micro sec * 256 = 819.12 micro sec
TIMER0_isr()
{
set_timer0(0); // set timer back to zero
if (t++ == 2441) // After t gets up to 2441 , 2 seconds has lapsed 819.12 micro seconds * 2442 = 2 seconds
{
t = 0;
output_c(0x01); // output a light every 2 seconds
}
}
void main()
{
set_tris_c(0xFF);
setup_counters(RTCC_INTERNAL, RTCC_DIV_32); //pulse occurs every 3.2 micro seconds (1/(40MHz/4*32))
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
while(1);
}
|
If anyone could figure out the problem that would be great. Thanks |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Feb 15, 2005 3:35 pm |
|
|
You set pin 0 on port C to go high, but you don't ever set it to go low. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Feb 15, 2005 3:36 pm |
|
|
Quote: | #include <18F458.h>
#use delay(clock=40000000) //40MHz
#fuses HS, OSCSEN, BROWNOUT, BORV20, STVREN, NODEBUG, NOLVP | In order to run at 40MHz you have two options:
1) A 10MHz crystal and the fuse setting H4 i.s.o. HS
2) A 40MHz external clock driver (not a crystal!!!) and fuse setting EC or EC_IO i.s.o. HS. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Feb 15, 2005 3:47 pm |
|
|
Code: | set_timer0(0); // set timer back to zero
| You don't need this line. The timer interrupt is generated on a timer overflow and the timer continues counting from zero.
In fact, this line will introduce a timing error: Interrupts are not not magic, it takes some time to enter the interrupt rountine (saving registers, discovering which interrupt is active, etc). On the PIC18 with CCS dispatcher code it takes about 40 instruction cycles (or so) to enter the interrupt routine. In this time the timer has advanced some 40+ ticks and you are setting it back.....
Code: | if (t++ == 2441) // After t gets up to 2441 , 2 seconds has lapsed 819.12 micro seconds * 2442 = 2 seconds |
Because of the above mentioned interrupt overhead this is a waste of processing power!!! Please setup your counter with a higher prescaler setting so as to get just the minimum number of interrupts required for your purpose. |
|
|
flip
Joined: 15 Feb 2005 Posts: 11
|
Timer0 doesnt work |
Posted: Tue Feb 15, 2005 4:04 pm |
|
|
I added a line of code into my main() and something for sure is not working because it should output high on Port C but it doesnt?
Code: |
//Output on a LED every two seconds using Timer0
#include <18F458.h>
#use delay(clock=40000000) //40MHz
#fuses HS, OSCSEN, BROWNOUT, BORV20, STVREN, NODEBUG, NOLVP //High Speed, Oscillator switching enabled, Brownout reset at 2.0V, Stack full causes rest, Low Voltage programming
unsigned int16 t = 0;
#int_TIMER0 // interrupt occurs every 3.2micro sec * 256 = 819.12 micro seconds
TIMER0_isr()
{
if (t++ == 2441) // After t gets up to 2441 , 2 seconds has lapsed 819.12 micro seconds * 2442 = 2 seconds
{
t = 0;
output_c(0x03); // output a light every 2 seconds
}
}
void main()
{
set_tris_c(0xFF);
setup_counters(RTCC_INTERNAL, RTCC_DIV_32); //pulse occurs every 3.2 micro seconds (1/(40MHz/4*32))
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
while(1);
{
output_c(0x01);
}
}
|
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Feb 15, 2005 4:29 pm |
|
|
As ckielstra asked you earlier, what are you using for a clock source? Are you using an external oscillator or a crystal? As he told you earlier you cannot run at 40MHZ using a crystal., the max is 25MHZ in HS mode.
I hope you have your LED tied to PIN 16 (RC1) otherwise, as newguy told you, it wont work at all.
Why are you using 0x3 and 0x1 to change the C1 pin? You are setting C0 (pin 15) high repeatedly. You can use 0x2 and 0x0.
What version of compiler are you using? If it is a fairly recent version why not use output_high(PIN_C1) and output_low(PIN_C1)? |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|