B222L Lect2-3

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 7 6 5 4 3 2 1 0 T2CON - TOUTP S3 TOUTP S2 TOUTP S1 TOUTP S0 TMR2 ON T2CKP S1 T2CKP S0 0x4F 7 6 5 4 3 2 1 0 PR2 C7 C6 C5 C4 C3 C2 C1 C0 0xF9 A B start/stop Timer 2 C

description

timer

Transcript of B222L Lect2-3

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

7 6 5 4 3 2 1 0

T2CON - TOUTPS3

TOUTPS2

TOUTPS1

TOUTPS0

TMR2ON

T2CKPS1

T2CKPS0

0x4F

7 6 5 4 3 2 1 0

PR2 C7 C6 C5 C4 C3 C2 C1 C0

0xF9

A B start/stop Timer 2

C

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 A 0 0 0 0 1 0 0 0 1 2 0 0 1 0 3 0 0 1 1 4 0 1 0 0 5 0 1 0 1 6 0 1 1 0 7 0 1 1 1 8 1 0 0 0 9 1 0 0 1 10 1 0 1 0 11 1 0 1 1 12 1 1 0 0 13 1 1 0 1 14 1 1 1 0 15 1 1 1 1 16

Configuration bits B for Timer 2 interrupt rate adjustment

T2CKPS1 T2CKPS0 B

0 0 1 0 1 4 1 0 16 1 1 16

Timer 2 interrupt rate icA B C T= × × ×4

icosc

Tf

=

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 icA B C T= × × × where: 4ic oscT f= (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.

7 6 5 4 3 2 1 0

T2CON - TOUTPS3

TOUTPS2

TOUTPS1

TOUTPS0

TMR2ON

T2CKPS1

T2CKPS0

7 6 5 4 3 2 1 0

PR2 C7 C6 C5 C4 C3 C2 C1 C0

7 6 5 4 3 2 1 0

PIR1 PSPIF ADIF RCIF TXIF SSPIF CCP1

IF

TMR2

IF

TMR1

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; // stop the Timer 2 PIR1bits.TMR2IF = 0; // clear timer flag } void main (void) { TRISB = 0x00; // configure Port B as output while(1) { PORTBbits.RB0 = 1; // set RB0 to 1 // PORTB = 0b00000001; // alternative ways to do the // PORTB = 0x01; // same thing but all pins // PORTB = 1; // of Port B will be affected delay(); // wait PORTBbits.RB0 = 0; // set RB0 to 0 delay(); // wait again and loop back } }