Sei sulla pagina 1di 4

University of Portsmouth, Department of Electronic and Computer Engineering

B222L Microcontrollers and Programmable Logic - Lectures 2 & 3


Branislav Vuksanovic

Timer 2 Operation on PIC18F 252

Timer 2 Block Diagram

Timer 2 Associated Registers

T2CON

TOUTP
S3

TOUTP
S2

TOUTP
S1

TOUTP
S0

TMR2
ON

T2CKP
S1

T2CKP
S0

0x4F

start/stop
Timer 2

PR2

C7

C6

C5

C4

C3

C2

C1

C0

0xF9

University of Portsmouth, Department of Electronic and Computer Engineering


B222L Microcontrollers and Programmable Logic - Lectures 2 & 3
Branislav Vuksanovic

Configuration bits A for Timer 2 interrupt rate adjustment


TOUTPS3 TOUTPS3 TOUTPS3 TOUTPS3
0
0
0
0
0
0
0
1
0
0
1
0
0
0
1
1
0
1
0
0
0
1
0
1
0
1
1
0
0
1
1
1
1
0
0
0
1
0
0
1
1
0
1
0
1
0
1
1
1
1
0
0
1
1
0
1
1
1
1
0
1
1
1
1

A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Configuration bits B for Timer 2 interrupt rate adjustment


T2CKPS1 T2CKPS0
0
0
0
1
1
0
1
1

B
1
4
16
16

Timer 2 interrupt rate = A B C Tic

Tic =

4
f osc

University of Portsmouth, Department of Electronic and Computer Engineering


B222L Microcontrollers and Programmable Logic - Lectures 2 & 3
Branislav Vuksanovic

Timer 2 Operation on PIC18F 252


Timer 2 operation on PIC18F252 is controlled by the value of bits from three PIC registers shown
below.
Timer is switched on and turned off by setting and resetting the bit 2 of T2CON register (i.e.
TMR2ON).
The setting of the Timer 2 interrupt flag is determined by the combination of other bits from register
T2CON as well as all 8 bits from register PR2.
Formula to calculate Timer 2 delay is:

Timer 2 interrupt rate = A B C Tic


where: Tic = 4 f osc (fosc is the oscillator frequency)
A is determined by the state of bits 3-6 from T2CON register (add 1 to a 4-bit value to get A)
B is determined by the state of bits 0 and 1 from T2CON register (B can take values of 1,4 and
16 for various combinations of bits T2CKPS1 and T2CKPS0 from T2CON)
C is the content of register PR2+1
Timer 2 flag is the bit 1 from the register PIR1. This bit is set to 1 by the signal from Tomer 2
postscaler controlled via bits TOUTPS3: TOUTPS0 from register T2CON.
Checking this bit regularly or configuring the timer 2 interrupt can achieve accurate timing for various
real-time applications of PIC18F252 microcontroller.

T2CON

PR2

PIR1

TOUT
PS3

TOUT
PS2

TOUT
PS1

TOUT
PS0

TMR2
ON

T2CK
PS1

T2CK
PS0

C7

C6

C5

C4

C3

C2

C1

C0

PSPIF

ADIF

RCIF

TXIF

SSPIF

CCP1

TMR2

TMR1

IF

IF

IF

University of Portsmouth, Department of Electronic and Computer Engineering


B222L Microcontrollers and Programmable Logic - Lectures 2 & 3
Branislav Vuksanovic

First PIC program


//**************************************************
// File Name: Lab1.c
// Purpose:
// This program uses Timer 2 to generate a
// square wave signal on the pin 0 of port B.
// The frequency of this signal is determined
// by the loading of Timer 2, i.e. by duration
// of delay() function
// B. Vuksanovic 30/09/2010
//**************************************************

#include <p18F252.h>
void delay(void)
{
// set the Timer 2 to produce certain delay
// by loading T2CON and PR2 registers
T2CON = 0x49;
// loading to achieve
PR2 = 0x7C;
// 1 ms delay at 20 MHz
T2CONbits.TMR2ON = 1;

// start the Timer 2

while(!PIR1bits.TMR2IF); // wait for timer flag


T2CONbits.TMR2ON = 0;
PIR1bits.TMR2IF = 0;

// stop the Timer 2


// clear timer flag

void main (void)


{
TRISB = 0x00;

//
//
//

while(1)
{
PORTBbits.RB0 = 1;
PORTB = 0b00000001;
PORTB = 0x01;
PORTB = 1;
delay();
PORTBbits.RB0 = 0;
delay();
}

// configure Port B as output

//
//
//
//
//
//
//

set RB0 to 1
alternative ways to do the
same thing but all pins
of Port B will be affected
wait
set RB0 to 0
wait again and loop back

Potrebbero piacerti anche