View previous topic :: View next topic |
Author |
Message |
johnl
Joined: 30 Sep 2003 Posts: 120
|
timer0 external input |
Posted: Wed Dec 29, 2004 8:50 pm |
|
|
In simulation, timer 1 counts external pulses from a switch properly, but timer 0 does not. Is there something I am missing with the code? (if not then it's probably my simulator.)
The compiler is version 3.214.
Code: | ///////// timer0bug.c ///////////////////////////
#include <18F452.h>
#FUSES HS,NOBROWNOUT,NOWDT,NODEBUG,NOLVP,NOPROTECT
#use delay(clock=20000000)
#byte PORTB=0xF81
#byte PORTD=0xF83
void portinit(void);
main()
{
portinit();
setup_timer_0(RTCC_EXT_L_TO_H);
setup_timer_1(T1_EXTERNAL );
set_timer0(0);
set_timer1(0);
while(1)
{
delay_ms(100);
PORTB=(byte)get_timer0();
PORTD=(byte)get_timer1();
}
}
portinit()
{
set_tris_a(0b00010000); // all port A outputs except A4 for timer0 input
set_tris_b(0b00000000); // all port B outputs
set_tris_c(0b00000001); // all port C outputs except C0 for timer1 input
set_tris_d(0b00000000); // all port D outputs
set_tris_e(0b00000000); // all port E outputs
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Thu Dec 30, 2004 5:45 am |
|
|
Keep in mind that RA4/T0CKI pin is a Schmitt Trigger input and an open drain output. All other RA port pins have TTL input levels and
full CMOS output drivers. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
Re: timer0 external input |
Posted: Thu Dec 30, 2004 6:47 am |
|
|
Check the prescalers.
Timer0 can only count every other pulse.
Timer1 can count all pulses.
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_DIV_2);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1); |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Dec 30, 2004 7:17 am |
|
|
I guess that Guest meant that in your example program the Timer0 is configured to divide by 2. If you don't specify the prescaler value, then it defaults to a divide by 2, a stupid default value.
If you want timer0 not to divide the input signal, then configure it as
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_DIV_1); |
|
![](templates/subSilver/images/spacer.gif) |
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Thu Dec 30, 2004 8:35 am |
|
|
The prescaler doesn't seem to be an issue since timer0 is not counting at all. Port B always shows a count of zero (the pre-loaded value) regardless of the number of input pulses.
Haplo, A4 is an input so the open drain doesn't matter. I tested the A4 pin as an input and it works. I also tested timer0 with the internal clock and it works. I just can't get the A4 pin to work as an input to timer0.
It seems like thing are pointing to my simulator....
Thanks to all. |
|
![](templates/subSilver/images/spacer.gif) |
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Thu Dec 30, 2004 9:04 am |
|
|
I am using the 18F4620 and RA4 is incrementing timer 0 just fine. If you look at the chip data book, reading timer 0 (get_timer0()) will return a 16 bit value and is prescaled 2:1. |
|
![](templates/subSilver/images/spacer.gif) |
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Thu Dec 30, 2004 9:09 am |
|
|
ljbeng - Are you using similar code (or my code)? |
|
![](templates/subSilver/images/spacer.gif) |
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Thu Dec 30, 2004 7:14 pm |
|
|
I just confirmed that it does work in hardware -so the simulator has a problem.
Thanks to all who responded.
Happy New Year. |
|
![](templates/subSilver/images/spacer.gif) |
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Fri Dec 31, 2004 2:21 am |
|
|
What simulator are you using? |
|
![](templates/subSilver/images/spacer.gif) |
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Fri Dec 31, 2004 8:45 am |
|
|
I am using Proteus VSM. Overall it works well and saves developing time. It's really handy to work out many code and hardware issues without having to prototype hardware.
Some of the models for PIC12 and PIC18 have bugs but they are working on that.
John |
|
![](templates/subSilver/images/spacer.gif) |
|