Sunteți pe pagina 1din 15

8051 Timer Operation & Serial Port Interface

By

Dr. Shaiq A. Haq


Professor

Air University, Sector E-9, Islamabad

Layout of Presentation
Introduction Timer Operation
Timer Mode Setting
13-bit timer, 16-bit timer, 8-bit auto reload, split timer Event counting or continuous timer using internal clock

Timer Control Register Setting

Serial Port Interface


Serial Port Modes
Shift register, 8-bit UART and 9-bit UART modes

Serial Port Control Register

Introduction
There are two timers in 8051 and three in 8052 Six SFRs control the timer operation of 8051 SFR Purpose
TCON TMOD TL0 TL1 TH0 TH1 Control Mode Timer0 low byte Timer1 low byte Timer0 high byte Timer1 high byte

Five additional SFRs are used for 8052 operation Let us first understand the function of 8051 timers

Timer Mode Setting


The TMOD register contains two groups of four bits to set the operating mode for Timer0 and Timer1.
Bit 7 6 5 4 3 2 1 0 Name Gate C/T M1 M0 Gate C/T M1 M0 Timer 1 1 1 M1 1 0 0 0 0 1 0 1 0

M0 0 1 0 1

Mode (Description) 0 13-bit timer 1 16-bit timer 2 8-bit auto reload 3 Split timer mode

Example: T1 T0 MOV TMOD, #0001 0000b ;set timer1 to mode 1

Timer Control Register


The TCON register contains status and control bits for Timer0 and Timer1.
Bit TCON.7 TCON.6 TCON.5 TCON.4 TCON.3 TCON.2 TCON.1 TCON.0 Symbol TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Description Timer1 overflow flag Timer1 run control bit Timer0 overflow flag Timer0 run control bit External interrupt 1 flag, set by s/w cleared by h/w External interrupt 1 type flag External interrupt 0 flag, set by s/w cleared by h/w External interrupt 0 type flag

Example: SETB

TR0

; start timer0

Example: 10kHz Square Wave Generation


Write a program using Timer0 to create a 10 kHz square wave on P1.0
ORG 8100h MOV TMOD, #02h MOV TH0, #-50 SETB TR0 LOOP: JNB TF0, LOOP CLR TF0 CPL P1.0 SJMP LOOP END ; set 8-bit auto reload mode ; -50 would give 50sec for each half ; of the clock cycle (for 12 MHz crystal) ; start Timer0 ; wait for Timer0 overflow ; clear timer overflow flag ; toggle port bit (to get square wave) ; repeat

8052 Timer 2
8052 has one more timer (Timer2) in addition to the two just described. Five extra SFRs are added to accommodate Timer2; T2CON, TL2, TH2, RCAP2L, RCAP2H. The mode as well as the run control of Timer2 is set by T2CON register;
Bit T2CON.7 T2CON.6 T2CON.5 T2CON.4 T2CON.3 T2CON.2 T2CON.1 T2CON.0 Symbol TF2 EXF2 RCLK TCLK Description Timer2 overflow flag. Not set when TCLK or RCLK=1) Timer2 external flag. Set by external count signal Timer2 receiver clock. When set provides receiver clock Timer2 transmit clock. When set provides trasmit clock EXEN2 External enable. When set timer2 works on T2EX signal TR2 Timer2 run control bit; C/T2 Counter/timer select;1=event counter, 0=interval timer CP/RL2 Timer2 capture/reload flag. 1=capture, 0=auto reload.

Introduction to Serial Port


The 8051 includes an on-chip serial port that can operate in several modes over a wide range of frequencies. Two SFRs (SBUF and SCON) provide software access to the serial port. Serial port features Full Duplex operation. Although SBUF is one name but actually there are two separate registers. Writing to SBUF loads data to be transmitted and reading SBUF accesses the received data.

Serial Port Control Register


SCON register summary;
Bit Symbol Description SCON.7 SM0 Serial port mode bit 0 SCON.6 SM1 Serial port mode bit 1 SCON.5 SM2 Serial port mode bit 2. Enables multiprocessor communication. RI will not be activated if received 9th bit is 0. SCON.4 REN Receiver enable. Must be set to receive characters SCON.3 TB8 Transmit bit 8. 9th bit transmitted in modes 2 and 3 SCON.2 RB8 Receive bit 8. 9th bit received SCON.1 TI Transmit interrupt flag, set at end of character transmission, to be cleared by software SCON.0 RI Receive interrupt flag, set at end of character reception, to be cleared by software

Serial Port Modes of Operation


The 8051 serial port has four modes of operation;
SM0 SM1 Mode 0 0 0 0 1 1 1 0 2 1 1 3 Description Shift Register. Fixed (crystal freq 12) 8-bit UART. Variable (set by timer) 9-bit UART. Fixed (crystal freq32 or 64) 9-bit UART. Variable (set by timer)

SMOD bit in PCON register when set doubles the baud rate. Default values (i.e, PCON register SMOD bit=0) for Mode 1&3 is timer1 overflow rate32, whereas for Mode 2 it is crystal freq64.

Initializing & Accessing Serial Port


Receiver Enable; REN bit must be set by software to enable the reception of characters. e.g; SETB REN or MOV SCON, #xxx1xxxb The 9th bit; the ninth data bit transmitted in modes 2 & 3 must be loaded into TB8 by software. The 9th bit also plays an important role in multiprocessor communication. Adding a Parity bit; MOV C, P MOV TB8, C MOV SBUF, A Interrupt Flags; RI and TI in SCON are set by hardware but must be cleared by software. e.g; WAIT: JNB RI, WAIT CLR RI MOV A, SBUF

Using Timer1 as the Baud Rate Clock


Configure TMOD for 8-bit auto reload mode and put the correct reload value in TH1. e.g; MOV TMOD, #0010xxxxb MOV TH1, #-26 Baud Rate Summary; Target Actual Baud Xtal SMOD TH1 Baud Error
9600 12.000 2400 12.000 1200 12.000 19200 11.059 9600 11.059 2400 11.059 1200 11.059 1 0 0 1 0 0 0 -7 (F9h) -13 (F3h) -26 (E6h) -3 (FDh) -3 (FDh) -12 (F4h) -24 (E8h) 8923 2404 1202 19200 9600 2400 1200 7% 0.16% 0.16% 0 0 0 0

Example: Initialize serial port as 8-bit UART @ 2400bps


For this example four registers must be initialized: SMOD, TMOD, TCON, and TH1.
SM0 SM1 SM2 REN TB8 RB8 TI RI SCON: 0 1 0 1 0 0 1 0 GTE C/T M1 M0 GTE C/T M1 M0 TMOD: 0 0 1 0 0 0 0 0 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TCON: 0 1 0 0 0 0 0 0 TH1: 1 1 1 1 0 0 1 1 ; -13 (or F3h) Program: ORG 8100h INIT: MOV SCON, #52h ; serial port mode 1 MOV TMOD, #20h ; timer1, mode 2 MOV TH1, #-13 ; reload counter for 2400 baud SETB TR1 ; start timer1 END

Example: Output Character Subroutine


Write a subroutine called OUTCHR to transmit the 7-bit ASCII code through 8051 serial port with odd parity added as the 8th bit.
ORG 8100h OUTCHR: MOV C, P ; put parity bit in C flag CPL C ; change to odd parity MOV ACC.7, C ; add to character code AGAIN: JNB TI, AGAIN ; Tx empty? No: check again CLR TI ; yes: clear flag and MOV SBUF, A ; send character through serial port CLR ACC.7 ; strip off parity bit and RET ; return END

Summary
This presentation describes the operation of timers in 8051 and 8052 microcontrollers and also describes the registers used for the operation of the serial port.

S-ar putea să vă placă și