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 CCS Technical Support

Timer0 questions

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



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

Timer0 questions
PostPosted: Tue Oct 02, 2007 9:50 am     Reply with quote

i have this little programs thats blinks two leds.
How can i calculate om which freq the leds blinks?

Code:
#include <18F4620.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOLVP,DEBUG,NOSTVREN,NOPROTECT

int8 GL1_counter,GL2_counter;

#define Green_Led_1      PIN_C1
#define Green_Led_2      PIN_C2

// LEDs blinking-time constants
#define Green_Led_1_blink  4
#define Green_Led_2_blink  8

#int_RTCC
void RTCC_isr() {
   GL1_counter--;
   if(!GL1_counter) {
      GL1_counter=Green_Led_1_blink;
      output_toggle(Green_Led_1);
   }
   GL2_counter--;
   if(!GL2_counter) {
      GL2_counter=Green_Led_2_blink;
      output_toggle(Green_Led_2);
   }
}

void main() {
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   GL1_counter=Green_Led_1_blink;
   GL2_counter=Green_Led_2_blink;
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   
   while(TRUE) {
      // ....
      // Your can do anything here while
      // your Leds are blinking in background
      // ....
   }
}
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Timer0 questions
PostPosted: Tue Oct 02, 2007 11:09 am     Reply with quote

The [spam] wrote:
i have this little programs thats blinks two leds.
How can i calculate om which freq the leds blinks?

Code:
#include <18F4620.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOLVP,DEBUG,NOSTVREN,NOPROTECT

int8 GL1_counter,GL2_counter;

#define Green_Led_1      PIN_C1
#define Green_Led_2      PIN_C2

// LEDs blinking-time constants
#define Green_Led_1_blink  4
#define Green_Led_2_blink  8

#int_RTCC
void RTCC_isr() {
   GL1_counter--;
   if(!GL1_counter) {
      GL1_counter=Green_Led_1_blink;
      output_toggle(Green_Led_1);
   }
   GL2_counter--;
   if(!GL2_counter) {
      GL2_counter=Green_Led_2_blink;
      output_toggle(Green_Led_2);
   }
}

void main() {
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   GL1_counter=Green_Led_1_blink;
   GL2_counter=Green_Led_2_blink;
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   
   while(TRUE) {
      // ....
      // Your can do anything here while
      // your Leds are blinking in background
      // ....
   }
}


f=fosc*(1/4)*(1/(2^16))*(1/2)*(1/Green_Led_1_blink)=9.53 HZ

fosc = 20MHZ
1 instruction cycle per oscilator cycle = 1/4
time0 increments (2^16) times per interupt
Output is both on and off =2
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Oct 02, 2007 11:36 am     Reply with quote

@Neutone,

Ok, but how can i calculate if i want a 5 hz freq
Which value must Green_Led_1_blink have?
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Oct 02, 2007 3:48 pm     Reply with quote

Strange result for this example
This give a freq of 19.5Khz (scope)
but this should be 39Khz

Do i something wrong???

Code:
#include <18F4620.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOLVP,DEBUG,NOSTVREN,NOPROTECT

//   The cycle time will be (1/clock)*4*t2div*(period+1)
//   In this program clock=20000000 and period=127
//   (1/20000000)*4*1*128=25.6us or 39.0khz

#int_TIMER2
void TIMER2_isr() {
   output_toggle(PIN_C1);
}

void main() {
   setup_timer_2(T2_DIV_BY_1,127,1);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(GLOBAL);
   
   while(TRUE) {
      // ....
      // Your can do anything here while
      // your Led are blinking in background
      // ....
   }
}
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