|
|
View previous topic :: View next topic |
Author |
Message |
Demetrius Guest
|
Timer1 count too fast |
Posted: Wed Apr 22, 2009 4:29 pm |
|
|
I used an LC circuit with a comparator to generate a square ware, and measure the frequency.
I want to use the 16 bit Timer1 as counter, but it seems to count too fast.
Code: | #include <16F876A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#include "lcd.c"
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
setup_timer_1(T1_EXTERNAL_SYNC|T1_DIV_BY_1);
//setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(A0_A3_NC_NC_OUT_ON_A4);
setup_vref(FALSE);
lcd_init();
printf(lcd_putc,"\f init");
while(true) {
input(PIN_C6); // open gate
set_timer0(0);
set_timer1(0);
delay_ms(100);
output_high(PIN_C6); // close gate
printf(lcd_putc,"\fT1: %Lu",get_timer1());
printf(lcd_putc,"\nT0: %u",get_timer0());
}
}
|
C6 is connected to C0/T1CKI and A4/T0CKI
On Timer0 I get correct count (226) but I want to increase the frequency and use a 16bit counter. On Timer1 instead I get a value that varies (apparently) randomly between 320-360.
I tried also to enable/disable timer before/after reading and writing value to timer register, and I tried also setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
but with no luck.
Thanks for any help! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 22, 2009 4:50 pm |
|
|
Quote: | while(true) {
input(PIN_C6); // open gate
set_timer0(0);
set_timer1(0);
delay_ms(100);
output_high(PIN_C6); // close gate
printf(lcd_putc,"\fT1: %Lu",get_timer1());
printf(lcd_putc,"\nT0: %u",get_timer0());
} |
The input() function sets the TRIS for pin C6 to be an input.
Do you have a pull-up or pull-down resistor on that pin ?
If you don't have one of those on it, the pin will be a "floating" input.
Why did you set it to be an input ?
Also, you have a while() loop to create your pulses, but then you have
printf statements inside the loop. The printf statements will take a
variable amount of time, depending upon how many digits they have
to display. |
|
|
Demetrius Guest
|
|
Posted: Wed Apr 22, 2009 7:13 pm |
|
|
No, i don't generate any pulses with the code.
I want to measure the frequency of a square ware counting the rising front of the signal.
C6 is connected with the pin C0/T1CKI and the signal source (it don't remain floating).
I set C6 as input and the signal go into it and the C0/T1CKI (timer1 counter pin). After 100ms, I set the C6 as output, so at the C0/T1CKI there is a constant value and the timer dont' increment.
I read the timer value and display it on lcd.
With Timer0 this work correctly, with timer1 no. |
|
|
|
|
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
|