View previous topic :: View next topic |
Author |
Message |
Andrew83
Joined: 16 Sep 2008 Posts: 51
|
Problem with simple program ! |
Posted: Tue Jan 06, 2009 3:32 am |
|
|
Hello !
I have a problem with the following program that normaly should work flawlessly. I am trying to make a led blink for a period of 0,842 ms. Where is my mistake ?
I'm using CCS C compiler version 4.057, pic18f452 and a 10 MHz clock.
Here is the code :
Code: |
#include <18F452.H>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=10MHZ)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#INT_TIMER0
void timer0_isr( )
{
output_toggle(pin_B1);
set_timer0(0);
}
//==================================
void main()
{
setup_timer_0(RTCC_INTERNAL |RTCC_8_BIT | RTCC_DIV_16);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_timer0(129);
while(1);
}
|
Thank you for your input !!! |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Jan 06, 2009 7:48 am |
|
|
Andrew,
What is the problem? Does the LED blink at the wrong rate or not blink at all? |
|
|
Andrew83
Joined: 16 Sep 2008 Posts: 51
|
|
Posted: Tue Jan 06, 2009 8:02 am |
|
|
Hello dyeatman!
The problem is that the led lights up as it should at 0,8146 ms (the closest interval to 0,842 ms that i could come up with), but turns off at about 2,4 ms. The total time that timer zero should run is 1,6 ms and i wanted that for 0,842 ms the led should be on and for the rest time the led should be off.
I am noticing that the led turn off at 0,842 + 1,6 ms.
I wonder why is this happening ?
Thank you four your time and patience !! |
|
|
Andrew83
Joined: 16 Sep 2008 Posts: 51
|
|
Posted: Tue Jan 06, 2009 8:05 am |
|
|
And another thing ...i've verified that the interrupt triggers every 1,6 ms....That part is ok ..I think that it's something to do with the way that i initialize my timer, aka set_timer0(129).
Is this the right way to initialize a timer to get a custom interrupt value ? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Jan 06, 2009 8:19 am |
|
|
OK, let's look at what you have:
Code: | #INT_TIMER0
void timer0_isr( )
{
output_toggle(pin_B1);
set_timer0(0);
}
//==================================
void main()
{
setup_timer_0(RTCC_INTERNAL |RTCC_8_BIT | RTCC_DIV_16);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_timer0(129);
while(1);
} |
If you think about the problem you will realize it's a little more complicated than what you have so far.
In Main you preset the clock to 129 then wait for the interrupt. After the interrupt fires you preset the Timer to 0. The timer runs the full timer length after that and never changes.
First you have to establish which part of the timer cycle you are in by defining a flag (i.e. LED_ON). Then, in the interrupt routine, you will have to alternate setting LED_ON true then false.
Example:
if LED_ON
{
set_timer0(xxx)
LED_ON = false
}
else
{
set_timer0(129)
LED_ON = true
}
Hopefully this will help get you on track. I will leave you to figure out the exact timer values needed. |
|
|
Andrew83
Joined: 16 Sep 2008 Posts: 51
|
|
Posted: Tue Jan 06, 2009 8:52 am |
|
|
Hmmm ...i must have read your mind !
I am working on a similar solution right now ...hopefully it will solve my problem !
Thank you very much |
|
|
|