View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
how to use timer0 |
Posted: Thu Oct 18, 2012 7:29 am |
|
|
i did programming using delays. looking forward to do with timer0.
compiler - CCS c compiler
pic - 18F2520.
internal oscillator - 4MHz
Please help how to start with? how to calculate time?
thanks in advance |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Oct 18, 2012 9:27 am |
|
|
It's all in the data sheet.
Timer0 advances on each instruction cycle that's 4 oscillator cycles or 1us, with your 4MHz clock.
Timer0 can be either an 8 or 16 bit counter timer.
You use Timer0 overflow to trigger an interrupt.
So in 8 bit mode it will trigger every 256us with no prescaler.
In 8 bit mode and 1:2 prescaler interrupts at 512us etc.
Mike |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
|
Posted: Thu Oct 18, 2012 9:33 am |
|
|
Thanks for the information.
I'm blinking the led with 5 sec delay. please tell that I'm doing it right?
Code: |
#include "18F2520.h"
#fuses HS, NOPROTECT, INTRC_IO
#use delay(clock=4000000) // 4MHZ internal clock
// fout = fclk/(4*prescaler* (256-TMR0)*count)
// for prescaler = 1 : 256 and TMR0 = 0
// if need 5sec delay
// Tout = 1/fout = 1/0.2Hz = 5sec
// count = 4Mhz/(4*256*(256-0)*0.2Hz) = 76.29 = 76 counts(approxi)
unsigned int count;
#int_RTCC
void clock_isr()
{
set_timer0(0);
count++;
if(count==76) // 76 for 5sec delay // 15 for 1 sec delay
{
output_high(PIN_B7);
count=0;
}
}
void main()
{
set_tris_b(0x00);
setup_oscillator(OSC_4MHZ);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(1)
{
output_high(PIN_B6);
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Oct 18, 2012 11:09 am |
|
|
I'm assuming you've got an LED connected to pin B7.
As it stands you're turning the LED on after 5s then no more changes.
If you want it to flash you can use the toggle function.
If you have a real board in front of you, it should be obvious by now.
You don't need the set_timer(0) line. When timer0 overflows its value will be zero. By the time you get into the interrupt routine timer0 will have advanced, so resetting it to zero again screws up your timings.
Mike |
|
|
juan
Joined: 21 Apr 2015 Posts: 3 Location: argentina
|
|
Posted: Tue Apr 21, 2015 3:08 pm |
|
|
Mike Walne wrote: | I'm assuming you've got an LED connected to pin B7.
As it stands you're turning the LED on after 5s then no more changes.
If you want it to flash you can use the toggle function.
If you have a real board in front of you, it should be obvious by now.
You don't need the set_timer(0) line. When timer0 overflows its value will be zero. By the time you get into the interrupt routine timer0 will have advanced, so resetting it to zero again screws up your timings.
Mike |
me sirvio el ejemplo, gracias |
|
|
juan
Joined: 21 Apr 2015 Posts: 3 Location: argentina
|
Re: how to use timer0 |
Posted: Tue Apr 21, 2015 3:11 pm |
|
|
hemnath wrote: | i did programming using delays. looking forward to do with timer0.
compiler - CCS c compiler
pic - 18F2520.
internal oscillator - 4MHz
Please help how to start with? how to calculate time?
thanks in advance |
Can you give me an example ? Thanks. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Sun Apr 26, 2015 2:28 am |
|
|
Let me add a couple of comments.
In the code library, there is a routine 'Timer based Real Time Clock (RTC)', which shows a way to generate long term accurate times by actually effectively counting clock cycles between the interrupts. This approach can be used without the complexity of actually updating a set of clock variables, to give nice accurate times from any timer. Worth studying.
The start of this post also mentions the other approach. Basically if you have a timer like timer2, which can be programmed for a particular count, then you can generate an interrupt that occurs at a much nicer interval. So (for instance), if you set timer2 to use 249 (so counts 0 to 249), (Fosc/4)/4, and interrupt every ten cycles, then this interrupt will give:
4000000/(4*4*250*10) = 100Hz.
Advantage is it makes counts really easy to work out!. Also generally it is useful to have a system 'tick' at a nice regular interval like this. Downside is the limited number of timers supporting this, hence the other approach if needed.
Generally it'd be much easier to see what was going on, if the interrupt did something like toggling the LED.... |
|
|
|