Вы находитесь на странице: 1из 23

PIC18f452

INTRO
Four Timers in PIC18f452
Timer 0
Timer 1
Timer 2
Timer 3
TIMER0
TIMER 0 Timer0 can operate as a timer or as a
counter.
The Timer0 module has the following features: 1. When the clock source is the instruction
1. Software selectable as an 8-bit or 16-bit timer/counter cycle clock(external oscillator), it
2. Readable and writable operates as a timer.
2. When the clock source is the T0CKI pin,
3. Dedicated 8-bit software programmable prescaler
it operates a counter
4. Clock source selectable to be external or internal
5. Interrupt-on-overflow from FFh to 00h in 8-bitmode and FFFFh to 0000h in 16-bit mode
6. Edge select for external clock

Registers of Timer0
1. T0CON (Timer0 control register)
2. TMR0L (Timer0 low byte)
3. TMR0H (Timer0 high byte)
INSIDE T0CON REGISTER

bit 7: TMR0ON: Timer0 On/Off Control bit


1 = Enables Timer0
0 = Stops Timer0

bit 6: T08BIT: Timer0 8-bit/16-bit Control bit


1 = Timer0 is configured as an 8-bit timer/counter
0 = Timer0 is configured as a 16-bit timer/counter

bit 5: T0CS: Timer0 Clock Source Select bit


1 = External clk from RA4/T0CKI pin
0 = Internal clock (FOSC/4 from XTAL)
INSIDE T0CON REGISTER

bit 4 T0SE: Timer0 Source Edge Select bit


1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin

bit 3 : PSA: Timer0 Prescaler Assignment bit


1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler.
0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.

bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits


111 = 1:256 prescale value
110 = 1:128 prescale value
101 = 1:64 prescale value
100 = 1:32 prescale value
011 = 1:16 prescale value
010 = 1:8 prescale value
001 = 1:4 prescale value
000 = 1:2 prescale value
TIMER WORKING 16-BIT WITHOUT PRESCALER

What would be amendment in above figure if


we have to use prescaler?
TIMER0 FORMULAS
In 8- bit mode In 16-bit mode
Step 1: Step 1:
n = (time delay needed) / [(4*prescaler)Xtal)] n = (time delay needed) / [(4*prescaler)Xtal)]
NOTE: Take prescaler=1 if not using it. NOTE: Take prescaler=1 if not using it.

Step 2: Step 2:
Value= 256 n Value= 65536 n
xx= binary of Value yyxx= binary of Value

TMR0L=xx TMR0L=xx
TMROH=yy

Example: Given: Delay needed=50ms , Prescaler=4 , Xtal=10Mhz for


16-bit mode calculations , find values for timer registers?
Solution::
n = (50*10-3) / [(4*4)10*106)] = 31250
Value= 65536 31250 = 34286 = (1000010111101110)b = (85EE)hex
TMR0H=0x85, TMROL=0xEE
TIMER0 INTERNAL BLOCK DIAGRAM IN 8-BIT

For 16-bit mode diagram see Fig 10-2 in datasheet!!!


Load
Load T0CON reg Wait till TMR0IF
TMR0H/TMR0L
according to Start timer using flag becomes
according to
required settings TMR0ON bit high
required delay or
# of counts

Goto next step Stop timer using


Clear flag TMR0ON bit
according your
requirement
Write a function delayTimer0( ) to create a time delay that is equal to 50 ms in 16-bit mode with
prescaler=4, Take Xtal=10Mhz .. Then in main function toggle RE0 constantly after 50ms?
SOLUTION:

void main( )
void delayTimer0( )
{
{
ADCON1 = 0x07; //make all digital
T0CON = 0x01; // TMR0 in 16-bit mode with
TRISE.TRISE0=0; //RE0 as o/p
internal clk, prescaler set to 4
while(1)
TMR0H =0x85; // values for 50ms
{
TMR0L =0xEE;
PORTE.RE0=~PORTE.RE0;
T0CON.TMR0ON=1; //turn on timer0
delayTImer0();
while(INTCON.TMR0IF==0); // wait until TMR0 overflows
}
T0CON.TMR0ON=0; //turn off timer0
}
INTCON.TMR0IF=0
}
TIMER0 as COUNTER
TRICK : For ease of calculation try to avoid using prescaler when using timer as a counter
Problem: TOCKI/RA4 is connected with a weight sensor which gives us 10 pulses whenever weight
>=5kg is applied to it . We want to toggle a LED connected to RD0 whenever 5kg above weight is
applied on it?
SOLUTION:
void main( )
{
TRISD.TRISD0=0;
TRISA.TRISA4=1; // make RA4 as input
T0CON = 0x68; // TMR0 as counter in 8-bit mode with external clk@RA4, no prescaler
while(1)
{
TMR0L =246;
T0CON.TMR0ON=1; //turn on timer0
while(INTCON.TMR0IF==0); // wait until TMR0 overflows
T0CON.TMR0ON=0; //turn off timer0
INTCON.TMR0IF=0
PORTD.RD0=~PORTD.RD0;
}
}
Another way of writing code for previous scenario
TOCKI/RA4 is connected with a weight sensor which gives us 10 pulses whenever weight >=5kg is
applied to it . We want to toggle a LED connected to RD0 whenever 5kg above weight is applied on it?
SOLUTION:

void main( )
void count10pulses( )
{
{
TRISD.TRISD0=0;
T0CON = 0x68; // TMR0 as counter in 8-bit mode with
while(1)
external clk@RA4, with no prescaler
{
TMR0L =246;
PORTD.RD0=~PORTD.RD0;
T0CON.TMR0ON=1; //turn on timer0
count10pulses( )
while(INTCON.TMR0IF==0); // wait until TMR0 overflows
}
T0CON.TMR0ON=0; //turn off timer0
}
INTCON.TMR0IF=0
}
TIMER1
TIMER 1 Timer1 can operate as a timer or as a
counter.
The Timer1 module has the following features: 1. When the clock source is the instruction
1. 16-bit timer/counter cycle clock(external oscillator), it
operates as a timer.
2. Readable and writable 2. When the clock source is the
RC0/T1OSO/T1CKI pin, it operates a
3. Internal or external clock select counter
4. Interrupt-on-overflow from FFFFh to 0000h
5. RESET from CCP module special event trigger

Registers of Timer1
1. T1CON (Timer1 control register)
2. TMR1L (Timer1 low byte)
3. TMR1H (Timer1 high byte)
INSIDE T1CON REGISTER

bit 7 RD16: 16-bit Read/Write Mode Enable bit


1 = Enables register Read/Write of Timer1 in one 16-bit operation
0 = Enables register Read/Write of Timer1 in two 8-bit operations

bit 6 Unimplemented: Read as '0

bit 5-4 T1CKPS1:T1CKPS0: Timer1 Input Clock Prescale Select bits


11 = 1:8 Prescale value
10 = 1:4 Prescale value
01 = 1:2 Prescale value
00 = 1:1 Prescale value

bit 3 T1OSCEN: Timer1 Oscillator Enable bit


1 = Timer1 Oscillator is enabled
0 = Timer1 Oscillator is shut-off
INSIDE T0CON REGISTER

bit 2 T1SYNC: Timer1 External Clock Input Synchronization Select bit


When TMR1CS = 1:
1 = Do not synchronize external clock input
0 = Synchronize external clock input
When TMR1CS = 0: This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.

bit 1 TMR1CS: Timer1 Clock Source Select bit


1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
0 = Internal clock (FOSC/4 from XTAL)

bit 0 TMR1ON: Timer1 On bit


1 = Enables Timer1
0 = Stops Timer1
TIMER0 FORMULAS
In 16-bit mode (only 16-bit mode as TIMER1 does not support 8-bit mode)
Step 1:
n = (time delay needed) / [(4*prescaler)Xtal)]

Step 2:
Value= 65536 n
yyxx= binary of Value

TMR1L=xx
TMR1H=yy
Load
Load T1CON reg Wait till TMR1IF
TMR1H/TMR1L
according to Start timer using flag becomes
according to
required settings TMR1ON bit high
required delay or
# of counts

Goto next step Stop timer using


Clear flag TMR1ON bit
according your
requirement
Write a C18 program to create a frequency of 2500Hz with 50% dutycycle on RB0. Use timer1 to create
delay. Take Xtal=10Mhz ?
SOLUTION:

void main( )
void delayTimer1( )
{
{
ADCON = 0x07;
T1CON = 0x00; // TMR1 in 16-bit mode with
TRISB.TRISB0=0;
internal clk, no prescaler
while(1)
TMR1H =0xFE; // values for 200us (50% duty cycle, f=2500hz)
{
TMR1L =0x0C;
PORTB.RB0=~PORTB.RB0;
T1CON.TMR1ON=1; //turn on timer1
delayTImer1();
while(PIR1.TMR1IF==0); // wait until TMR1 overflows
}
T1CON.TMR1ON=0; //turn off timer1
}
PIR1.TMR1IF=0
}
TIMER1 as COUNTER
TRICK : For ease of calculation try to avoid using prescaler when using timer as a counter
Problem: RC0/T1OSO/T1CKI is connected with a weight sensor which gives us 10 pulses whenever
weight >=5kg is applied to it . We want to toggle a LED connected to RE0 whenever 5kg above weight
is applied on it?
SOLUTION:
void main( )
{
ADCON1=0x07;
TRISE.TRISE0=0;
TRISC.TRISC0=1; // make RC0 as input
T1CON = 0x02; // TMR1 as counter in 16-bit mode with external clk@RC0, no prescaler
while(1)
{
TMR1H =0xFF; // 65526 = 0xFFF6 moved so that they overflow after 10 counts
TMR1L =0xF6;
T1CON.TMR1ON=1; //turn on timer0
while(PIR1.TMR1IF==0); // wait until TMR1 overflows
T1CON.TMR1ON=0; //turn off timer1
PIR1.TMR1IF=0
PORTE.RE0=~PORTE.RE0;
}
For practice do examples from Mazidis book 9.28 9.37
TIMER2

TIMER3 (WILL BE DISCUSSEED WITH CCP MODULE)