|
|
View previous topic :: View next topic |
Author |
Message |
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
Timer0 questions |
Posted: Tue Oct 02, 2007 9:50 am |
|
|
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
|
Re: Timer0 questions |
Posted: Tue Oct 02, 2007 11:09 am |
|
|
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
|
|
Posted: Tue Oct 02, 2007 11:36 am |
|
|
@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
|
|
Posted: Tue Oct 02, 2007 3:48 pm |
|
|
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
// ....
}
}
|
|
|
|
|
|
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
|