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

Timer0

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







Timer0
PostPosted: Fri Mar 21, 2003 8:24 pm     Reply with quote

I am trying to read in encoder pulses from a DC motor encoder. The CCS documentation is hard to understand how to set Timer0 and Timer1 as counters. I also don't know hot to specify Timer0 as 8bit or 16bit. Can anyone give me some insight on how to read encoder counts to determine speed. I am using a 18F452. Thanks!!
___________________________
This message was ported from CCS's old forum
Original Post ID: 12943
R.J.Hamlett
Guest







Re: Timer0
PostPosted: Sat Mar 22, 2003 3:55 am     Reply with quote

:=I am trying to read in encoder pulses from a DC motor encoder. The CCS documentation is hard to understand how to set Timer0 and Timer1 as counters. I also don't know hot to specify Timer0 as 8bit or 16bit. Can anyone give me some insight on how to read encoder counts to determine speed. I am using a 18F452. Thanks!!

Remember that Timer0, is also the RTCC. Hence some of the controls are listed under (and named) using RTCC, rather than Timer0. The control to switch to 8bit mode, is setup_timer0(RTCC_8_BIT). It defaults to 16bit, unless this is set. In general you should list, or printout the include file for the chip (18F452.h), since this lists these values, and which functions they are used with, allmost becoming an essential 'appendix' to the paperwork.
The same applies with the 'counter' setup. So in your case (assuming you want to count on falling edges, on the external pin), and want a 16bit counter, you would need:
setup_timer0(RTCC_EXT_H_TO_L | RTCC_DIV_1);
If you look at the data sheet for this timer, the configuration parts available, are:
1) select clock source
2) set prescaler
There is no further configuration available for this timer as such, so everything is now set (except interrupt control if required).
For Timer1, a similar check of the data sheet, gives basically the same settings, with the addition of the 'synchronous' selection, and the oscillator modes. The oscillator is enabled by turning on 'clock out' - which you don't want, and presumably you will run this timer as synchronous (since otherwise the response of the two timers will be different) Given the likely speed of encoder pulses (usually only a few KHz), this makes little difference really.
Hence the setup here becomes:
setup_timer1(T1_EXTERNAL_SYNC | T1_DIV_BY_1);

If you are only interested in 'speed', then you are better off really, externally combining the two signals, to give a pulse on each change, and feeding these into just one timer. If you want to fully decode the encoder outputs (to give speed and direction), then you should either look at implementing an external gate to feed the inward counts, seperately from the outward counts to the two timers. The circuitry to do this is described in the various Microchip 'servo' application notes. Alternatively, assuming the processor is not doing too much else, and is running at a reasonable speed, you can decode the signals in software, by connecting them to portB (which supports interrupt on change), then on the interrupt, look at the two bits, and increment/decrement the counters as needed. It is worth saying, that processors are fast, and are often fully capable of doing this (for instance, I happily do a full decode on a 500 line optical encoder, to give 2000PPR, using a 4MHz 16C671, at rotational rates up to just under 1000RPM...).

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 12950
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

Re: Timer0
PostPosted: Sat Mar 22, 2003 5:52 pm     Reply with quote

:=:=I am trying to read in encoder pulses from a DC motor encoder. The CCS documentation is hard to understand how to set Timer0 and Timer1 as counters. I also don't know hot to specify Timer0 as 8bit or 16bit. Can anyone give me some insight on how to read encoder counts to determine speed. I am using a 18F452. Thanks!!
:=
:=Remember that Timer0, is also the RTCC. Hence some of the controls are listed under (and named) using RTCC, rather than Timer0. The control to switch to 8bit mode, is setup_timer0(RTCC_8_BIT). It defaults to 16bit, unless this is set. In general you should list, or printout the include file for the chip (18F452.h), since this lists these values, and which functions they are used with, allmost becoming an essential 'appendix' to the paperwork.
:=The same applies with the 'counter' setup. So in your case (assuming you want to count on falling edges, on the external pin), and want a 16bit counter, you would need:
:=setup_timer0(RTCC_EXT_H_TO_L | RTCC_DIV_1);
:=If you look at the data sheet for this timer, the configuration parts available, are:
:=1) select clock source
:=2) set prescaler
:=There is no further configuration available for this timer as such, so everything is now set (except interrupt control if required).
:=For Timer1, a similar check of the data sheet, gives basically the same settings, with the addition of the 'synchronous' selection, and the oscillator modes. The oscillator is enabled by turning on 'clock out' - which you don't want, and presumably you will run this timer as synchronous (since otherwise the response of the two timers will be different) Given the likely speed of encoder pulses (usually only a few KHz), this makes little difference really.
:=Hence the setup here becomes:
:=setup_timer1(T1_EXTERNAL_SYNC | T1_DIV_BY_1);
:=
:=If you are only interested in 'speed', then you are better off really, externally combining the two signals, to give a pulse on each change, and feeding these into just one timer. If you want to fully decode the encoder outputs (to give speed and direction), then you should either look at implementing an external gate to feed the inward counts, seperately from the outward counts to the two timers. The circuitry to do this is described in the various Microchip 'servo' application notes. Alternatively, assuming the processor is not doing too much else, and is running at a reasonable speed, you can decode the signals in software, by connecting them to portB (which supports interrupt on change), then on the interrupt, look at the two bits, and increment/decrement the counters as needed. It is worth saying, that processors are fast, and are often fully capable of doing this (for instance, I happily do a full decode on a 500 line optical encoder, to give 2000PPR, using a 4MHz 16C671, at rotational rates up to just under 1000RPM...).
:=
:=Best Wishes

For higher speeds and where the processor is busy with other tasks I use a dedicated IC, the HP (Agilent) HCTL-2016 for full decoding. Using signed arithmetic in the processor I get a signed 32 bit counter with resolution four times the slot rate, a very large number. Only proviso is that, to avoid ambiguity the HCTL-2016 count must be taken at least twice in the time that it's 16 bit count would take to go from zero to full. Speed limit is then only determined by the encoder parameters ie mechanical speed limit and maximum frequency that it can output from the two quadrature signals, typical good quality encoders have around 100kHz response.
BTW save those old HP Deskjet printers, the early ones had a 500 slot(line) rotary encoder with DC motor attached and the later ones a 150 slot/inch linear encoder that with the times four multiplication gives 600 counts/inch.
Regards
Kenny
___________________________
This message was ported from CCS's old forum
Original Post ID: 12965
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