View previous topic :: View next topic |
Author |
Message |
Guest
|
TIMER operation |
Posted: Sat Sep 10, 2005 6:03 am |
|
|
Hi all,
Im a newbie and wanted to start, stop timer1 when certain events occur, i saw the example ex_patg.c. I wanted to know how can we start,stop the timer1. Plz guide |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Sep 12, 2005 8:30 am |
|
|
It depends on which PIC you are using. Since you didn't specify which one you have I will show you how I control an 18F452.
First you need to define some registers..
Code: |
/////////////////////////
// register definitions
/////////////////////////
#byte T0CON = 0xFD5 // register that has Timer0's ON/OFF bit in it
#byte T1CON = 0xFCD // register that has Timer1's ON/OFF bit in it
// TOCON bits
#bit TMR0ON = T0CON.7 // Timer0's ON/OFF bit
// T1CON bits
#bit TMR1ON = T1CON.0 // Timer1's ON/OFF bit
|
Now, somewhere in your code you will turn the timer on or off...
Code: |
TMR0ON = 1; // turn on Timer0(start the counter counting)
TMR0ON = 0; // turn off Timer0
TMR1ON = 1; // turn on Timer1
TMR1ON = 0; // turn off Timer1
|
Remember, this is for the 18F452. Not all timers, on all PIC's, have this bit available. You'll need to examine the spec. sheet in the timer section.
Ronald |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Mon Sep 12, 2005 10:03 am |
|
|
For the 18F45X (e.g. 458 or 452), I have used the following library functions in the beginning of my main() to set up the timers and set them to zero. They are automatically running from that point.
Code: |
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_64);
set_timer0(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
set_timer1(0);
setup_timer_3(T3_INTERNAL | T3_DIV_BY_1);
set_timer3(0);
enable_interrupts(INT_TIMER3);
enable_interrupts(INT_TIMER1);
|
|
|
|
|