CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with simple program !

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Andrew83



Joined: 16 Sep 2008
Posts: 51

View user's profile Send private message Send e-mail

Problem with simple program !
PostPosted: Tue Jan 06, 2009 3:32 am     Reply with quote

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: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Jan 06, 2009 7:48 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 06, 2009 8:02 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 06, 2009 8:05 am     Reply with quote

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: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Jan 06, 2009 8:19 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 06, 2009 8:52 am     Reply with quote

Very Happy 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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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