PDA

View Full Version : Controlling Timers in PIC



NO2K
- 13th January 2007, 19:12
How does one use PIC Basic Pro Commands to control the timer1 module, or can it even be done?

Archangel
- 13th January 2007, 21:01
Yes it can, get the data sheet for the chip you intend to use and let us know which chip it is and then we will teach you how to implement this as Sayzer taught me.
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1434

Archangel
- 14th January 2007, 08:32
1. From the Data sheet you see pin ?, the clock input
pin for timer 1 (varies by pic number). A 16F628A it is RB6
a 16F877 it is RC0 et. al . . . . Check your data sheet for
your particular PIC.

2. Look at the T1CON register in the datasheet. It is a byte
variable, 8 (bits). Bit 7 and Bit 6 are not used, just ignore
them.

3. Now look at bits 5 and 4. These are the bits you select based
upon your application. This is where you set the prescaler.


4. Now on to bit 3. This is the T1OSCEN bit. usually this is
turned off so make this a zero too.
1 = T1OSCEN on, 0= T1OSCEN off

5. We are at bit 2 now. To use timer 1 in counter mode, we choose
not to synchronize external clockinput, because we want to
count the incomming ticks, so set this bit as 1.

6. Now we are at bit 1. "This is our guy", this selects timer or
counter mode. Set this to 1 to count external pulses.

7. Bit zero at last. set bit zero to 1 and timer is enabled, set
to 0 and it is turned off. Use it this way in your code:


T1CON = %00110111 ' Sets up timer and prescaler
(zero bit 3-4 to alter prescaler)

the following code is put into your code where you want to start
and stop the counter/timer:
T1CON.1 'turn on timer
T1CON.0 'turn off timer

8. Now we come to the point where we see the number of ticks The
ticks are stored in a word variable, set up like this:
CounterTotal var word 'initialize variables needed for program.

9. The counts are stored in TMR1L and TMR1H and you set up the
variable like this: CounterTotal.LowByte=TMR1L
CounterTotal.HighByte=TMR1H
Prior to beginning to count at the start of program, zero
these as follows:
TMR1L=0
TMR1H=0
you may zero these for your own purposes anywhere in your
program or you may preload these with a value greater than zero.

10. Now look at the datasheet re: T1CON register and set
up the T1CON register for your program. The example below
is set up as follows:

Prescaler = 1:8 ' 8 counts in = 1 count out
T1OSCEN = off
T1SYNC = 1 ' do not synchronize external clock input
TMR1CS = 1 ' External clock from clock input pin
TMR1ON = 1 ' Timer is enabled and running


T1CON = %00110111 ' Sets up timer and prescaler(zero bit 5-4 to
alter prescaler)

That was easy, and yes I do keep these instructions available
to reference when I need them.

NO2K
- 14th January 2007, 17:13
Thanks Joe!! That is terrific info.
The end task I had in mind is to clk tmr1 from a noise clocking source (random clock pulses), then sample the timer count every 5 seconds or so and display the number on an LCD....i.e. a random number generator between 0 and 65535. Why? Someone challenged me to do it! Your info could easily be used with other applications....thanks again!

NO2K
- 14th January 2007, 17:14
I forgot to mention I am using the 16F870 PIC

Archangel
- 14th January 2007, 19:36
Your Welcome, the credit goes to Sayzer, this is what he taught me !
JS