PIC 18f2520
microcontroller timer0
In my
previous chapter, we made a LED blinking program with an undesired time delay
during on-off. Now we are going to control LED with accurate time delay. For
this we would know that how timer works?
The Timer0 module incorporates the following features:
•
Software selectable operation as a timer or counter in both 8-bit or 16-bit
modes
•
Readable and writable registers
•
Dedicated 8-bit, software programmable prescaler
•
Selectable clock source (internal or external)
• Edge
select for external clock
• Interrupt-on-overflow
Timer0 has a
specific 8bits register T0CON to control the timer functionality. See
controller datasheet for detailed description.
Bit7 – switch the timer on or off.
· If
bit is set to 0 timer will be off condition, if bit set as 1 timer will
in on condition. Either we can set all of bits in a time and/or set separately
one.
T0CONbits.TMR0ON = 1; // Start
the timer
Or
T0CON = 0x80; // Starts the timer but changes to all other bits
to 0
So the above codes will be suitable in all of need.
Bit6 – Selection of timer/counter
bits mode.
· Timer0 can count in 8 bit or 16 bit
mode. It depends on your need that how much time delay you need. With 16 bits
of mood, we can set a long time delay and other facility is also available that
is prescaler assignment.
T0CONbits.T08BIT = 1; // 16 bits mode.
T0CONbits.T08BIT = 0; // 8 bits mode.
Bit5 – Timer clock
selection bit
· Timer0 can be operate with internal
or external clock source. Controller pin number 6 (T0CKI) is external clock
source pin for timer increment. Every rising or falling edge increase time as
per Bit4 (T0SE) selection.
T0CONbits.T0CS = 1; //External clock select.
T0CONbits.T0CS = 0; //Internal clock select.
Bit4 – Timer clock source
edge selection bit
T0CONbits.T0SE = 1; // Increment on high-to-low transition on
T0CKI pin.
T0CONbits.T0SE = 0; // Increment on low-to-high transition on
T0CKI pin.
Bit3 – Prescaling selection
· We can assign prescaling for our suitable
need. We need set this to 1 then Bit0-2 prescaler assignment will work
otherwise prescaler will not work.
T0CONbits.PSA = 1; //Prescaler selection on.
T0CONbits.PSA = 0; //Prescaler selection off.
Bit0-2 are prescaler value selection bits.
We can define timer starting value by TMR0L and TMR0H
registers. If we are using 8 bit mode, we would use TMR0L register. If we are
using 16 bit mode, both registers will use. The TMR0 interrupt is generated
when the TMR0 register overflows from FFh to 00h in 8-bit mode, or from FFFFh
to 0000h in 16-bit mode.
The circuit diagram will be same as my previous LED bilking
circuit. Even after that i am enclosing circuit.
We are going to use 20Mhz external oscillator for system
clock and we would define TMR0 register’s bits base on 20Mhz frequency. You can
set register value bit by bit or can define in a stoke.
Now comes most important part of timer is Calculating delay
time. There are the two method to calculate delay time. Fist is calculate yourself
by given blow formulas or by any software like PIC TIMER CALCULATOR.
Exe :- Time delay 1 second with 16bit mode
Clock Period
(T) = 1/Frq = 1/20000000 = 0.00000005 s = 0.05μs
Instruction
cycle clock (Ic) period = 0.05x4 =
0.2μs
For
one second time delay required instruction cycles
= 1/0.0000002 = 5000000
Ic
With
Prescaler 1:128 = 5000000/128 = 39063
Counting
start value = 65536 – 39063 = 26473
26473 in
Hexadecimal = 6769
Initial
loading value of TMR0H = 67
TMR0L = 69
I trusty say that your timer fundamental will be clear and
now you will be able to make your timer codes.
Codes
#include
<p18f2520.h>
#define LED
PORTCbits.RC0
void delay_1_sec(void); //One second delay time function.
void main()
{
TRISC = 0; //
PORTC as output.
LED = 0; //
Turn Off led at staring.
//Configure timer bits.
T0CONbits.T08BIT = 1; //
16 bits mode.
T0CONbits.T0CS = 0; //Internal
clock select.
T0CONbits.PSA = 1; //Prescaler
selection on.
//Prescaler 1:128 selection bits.
T0CONbits.T0PS2 = 1;
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS0 = 0;
while(1)
{
delay_1_sec(); //Call delay time function.
LED = 1;
delay_1_sec(); //Call delay time function.
LED = 0;
}
}
void delay_1_sec()
{
TMR0H = 0x67; //
Timer higher register value.
TMR0L = 0x69; //
Timer higher register value.
T0CONbits.TMR0ON
= 1; // Turn on timer.
while(INTCONbits.TMR0IF == 0); // Wait until interrupt generate.
T0CONbits.TMR0ON = 0; //
Turn off timer.
INTCONbits.TMR0IF = 0; //
Clear interrupt flag.
Download Project file :- https://drive.google.com/file/d/0B886Kbl42IVuYTF0T2tzWl9MNDA/view?usp=sharing
No comments:
Post a Comment