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

Timers

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



Joined: 15 Apr 2009
Posts: 4

View user's profile Send private message

Timers
PostPosted: Wed Apr 15, 2009 6:54 am     Reply with quote

Hi.

I have a problem. I'm trying to turn on a led for one second and after that to turn off the led, I want to use interrupts in order to do that. But using timers , I don't know how to set them up to overflow in 1000 ms. I saw that there are standards settings for that, but none of them are overflowing in 1000 ms.
Are some formulas for this type of settings? For example, the code that I wrote for turning on the led is this, but it doesn't respect what I should do.

#include <16F917.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)

#int_TIMER1
void clock_isr()
{
sec++;
if (sec==10) //trying to repete 10 times in order to get to that one second
{
output_high(PIN_D0);
sec=0;
}
if (sec==3) output_low(PIN_D0);
}

void main()
{
set_tris_d(0x00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
set_timer1(T1_INTERNAL | T1_DIV_BY_8); //overflows every 104.896 ms
while (TRUE) ;
}


I'll appreciate if you could help me.
Thanks in regards.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Apr 15, 2009 8:06 am     Reply with quote

Use the 'Code' buttons when posting code in order to preserve formatting. Easier reading will give you more and quicker responses.

What is your compiler version number?

Bug: Variable sec is not defined nor initialized.

Tip: setting the TRIS register is not required, the CCS compiler will do this for you unless you specify #use fast_io.

The D0 output is multiplexed with COM3 for the LCD. Try disabling the LCD at the start of your program:
Code:
setup_lcd( LCD_DISABLED, 0);
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Apr 16, 2009 12:01 pm     Reply with quote

I would suggest you try using timer2. It has more pre/post scalers that will help you arrive at a correct delay.

Code:
setup_timer_2(T2_DIV_BY_16,249,10);


This will have your ISR entering 125 times each second. You can put a counter inside the ISR to count 125 times and then set/reset your output.

The calculation is:

20MHZ/4 = 5MHZ. This is the main clock speed used by the PIC.
5MHZ/16 = 312500HZ. The clock speed coming out of the prescaler.
312500/250 = 1250HZ. The clock speed coming out of the comparator register.
1250/10 = 125. The clock speed coming out of the postscaler.

Your timer setup needs to be set to 249 because the ISR will be flagged on the rollover of the timer which will be the 250th count.

Look at section 7.0 of the spec. sheet to get a better detail of the inner workings of this timer.

Clear as mud?

Ronald
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