Vous êtes sur la page 1sur 4

University of Portsmouth, Department of Electronic and Computer Engineering

B222L Microcontrollers and Programmable Logic - Lectures 9


Branislav Vuksanovic

Timer 0 - Examples
T0CON:

(TIMER0 CONTROL REGISTER)

Exercise 1:

Configure Timer 0 with the following settings:


16-bit timer mode
no-prescaler
internal clock (from oscillator) source
increment on positive edge

Solution:
7

T0CON

TMR0
ON

T08BIT

T0CS

T0SE

PSA

T0PS2

T0PS1

T0PS0

0xFD5

so:
T0CON = 0x08;

// configures the Timer 0 with required settings

University of Portsmouth, Department of Electronic and Computer Engineering


B222L Microcontrollers and Programmable Logic - Lectures 9
Branislav Vuksanovic

Exercise 2:

Calculate the amount of time delay generated by Timer 0 with the following specifications:
fosc = 10 MHz,
TMR0H:TMR0L=0XFFF22

Solution:

Tic =

4
4
=
= 0.4 106 s = 0.4 s
6
f osc 10 10

How many clock ticks are counted before timer reaches its full count?
(0xFFFF 0xFFF2) + 1 = 0x000D + 1 = 0x000E = 14
Time delay: Td = 14 Tic = 14 0.4 = 5.6 s
Exercise 3:

Write a C program to toggle all the bits of PORTB continuously with 1 ms delay.
Use Timer 0 in 16-bit mode and no prescaler options. Assume fosc = 20 MHz.

Solution:

Tic =

4
4
=
= 0.2 106 s = 0.2 s
f osc 20 106

How many clock ticks are needed for 1 ms delay?

N=

Td
1 ms
1 103
=
=
= 5 103 = 5000 = 0x1388
6
Tic 0.2 s 0.2 10

So the Timer 0 register value can be calculated from:


(0xFFFF register value) + 1 = 0x1388
i.e. register value = 0xFFFF + 1 0x1388 = 0xEC78
C program is given at the end of this handout.
Exercise 4:

Write a C program to toggle RB0 pin every 50 ms.


Use Timer 0 in 16-bit mode and 1:4 prescaler setting. Assume fosc = 20 MHz.

Solution:

Tic =

4
4
=
= 0.2 106 s = 0.2 s
6
f osc 20 10
T
50 ms
N= d =
= 250 103 = 250000 = 0x3D090
Tic 0.2 s

This is outside of the 16-bit range of TMR0H:TMR0L register, so prescaler is needed.


With 1:4 prescaler: 250000:4 = 62500 = 0xF424 (in the range 9).
So Timer 0 needs to be loaded with: register value = 0xFFFF + 1 0xF424 = 0x0BDC
C program is given at the end of this handout.

University of Portsmouth, Department of Electronic and Computer Engineering


B222L Microcontrollers and Programmable Logic - Lectures 9
Branislav Vuksanovic

// Exercise 3 solution. 1 ms delay generated using Timer 0.


// 16-bit Timer 0 mode, no prescaler and 20 MHz internal clock assumed.
#include <p18f252.h>
void Timer0_delay(void)
{
T0CON = 0x08;
TMR0H = 0xEC;
TMR0L = 0x78;
T0CONbits.TMR0ON = 1; // Turn ON Timer 0
while(INTCONbits.TMR0IF == 0);
T0CONbits.TMR0ON = 0; // Turn OFF Timer 0
INTCONbits.TMR0IF = 0; // clear Timer 0 interrupt flag
}
void main(void)
{
TRISB = 0x00;
while(1)
{
PORTB = 0x55;
Timer0_delay();
PORTB = 0xAA;
Timer0_delay();
}
}

University of Portsmouth, Department of Electronic and Computer Engineering


B222L Microcontrollers and Programmable Logic - Lectures 9
Branislav Vuksanovic

// Exercise 4 solution. 50 ms delay generated using Timer 0.


// 16-bit Timer 0 mode, 1:4 prescaler and 20 MHz internal clock assumed.
#include <p18f252.h>
void Timer0_delay(void)
{
T0CON = 0x01;
TMR0H = 0x0B;
TMR0L = 0xDC;
T0CONbits.TMR0ON = 1; // Turn ON Timer 0
while(INTCONbits.TMR0IF == 0);
T0CONbits.TMR0ON = 0; // Turn OFF Timer 0
INTCONbits.TMR0IF = 0; // clear Timer 0 interrupt flag
}
void main(void)
{
TRISB = 0x00;
while(1)
{
PORTBbits.RB0 = 1;
Timer0_delay();
PORTBbits.RB0 = 0;
Timer0_delay();
}
}

Vous aimerez peut-être aussi