37
EGRE 631 8051 Timer and UART Timer/counters Used in embedded designs to provide a series of time or count related events. o Minimum of processor and software overhead. Timing for control sequences System ticks for operating system Generation of waveforms 1

jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

Embed Size (px)

Citation preview

Page 1: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

EGRE 6318051 Timer and UART

Timer/counters Used in embedded designs to provide a series of time or

count related events.o Minimum of processor and software overhead.

Timing for control sequences System ticks for operating system Generation of waveforms

They are available in several different types but are essentially based around a simple structure as shown below.

1

Page 2: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

o Timing derived form a clock inputo Clock may be internal processor clock or an external pin.o Clock may be divided down (typically by a power f two).o The divided clock is passed to a count down counter

o Loaded with a value and counts down to 0.o The count reaching 0 triggers an event.

o Timer/Counters can be of different sizes.

2

Page 3: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

o 8, 16, and 24 bits are common.

3

Page 4: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

Serial portso Pin efficient method of communicating between deviceso With those microcontrollers which do not have an external extension

bus, they may be the only method of adding additional functionality.o In the simplest form serial ports are a pair of shift registers clocked by

a common clock.

4

Page 5: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

5

Page 6: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

Serial ports can be either synchronous or asynchronous In synchronous ports both clock and data are sent and

received.o The clock line can be bi-directional or supplied by one

device. In asynchronous ports only data is sent and received.

FIFO’s FIFO’s are often used to buffer data. Prevents data loss when new data is transmitted before

previous data is read.o Data is move to FIFO as soon as it is received.o Data is then read from FIFO.

6

Page 7: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

7

Page 8: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

Serial peripheral interface (SPI) A simple synchronous interface Provides full-duplex communication between processor and slave

devices.

Data is written to the SPDR register in the master device and clocked out into the slave device SPDR using the common clock signal SCK.

When 8 bits have been transferred, an interrupt is locally generated so that the data can be read before the next byte is clocked through.

The SS or slave select signal is used to select which slave is to receive the data.

Example with single slave

8

Page 9: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

With multiple slaves, spare parallel I/O pins are used to select the slave prior to data transmission. Unselected slaves tri-state the SPI connections.

It should not be assumed that an implementation buffers data. As soon as the master writes the data into the SPDR it is transmitted as there is no buffering. As soon as the byte has been clocked out, an interrupt is generated indicating that the byte has been transferred. In addition, the SPIF flag in the status register (SPSR) is set. This flag must be cleared by the ISR before transmitting the next byte.The slave device does have some buffering and the data is transferred to the SPDR when a complete byte is transferred. Again, an interrupt is generated when a byte is received. It is essential that the interrupt that is generated by the full shift register is serviced quickly to transfer the data before the next byte is transmitted and transferred to the SPDR. This means that there is an eight clock time period for the slave to receive the interrupt and transfer the data. This effectively determines the maximum data rate.

9

Page 10: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

RS232 Asynchronous interfaceCan be used for longer distances than synchronous communicationOnly one line is used for transmitting data and one line is used for receiving data.Typically control lines are also included and used for hardware handshaking.A logic one is signaled by +12 volt (+3 to +15 volts) levelA logic zero is signaled by –12 volts (–3 to –15 volts) level.Special IC’s are used convert logic levels to the RS232 levels.A universal asynchronous receiver transmitter (UART) handles the serial to parallel conversions.

The 8051

10

Page 11: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

The basic 8051 has two timer counters – Timer 0 and Timer 1The 8052 added Timer 2.Each time

INITCOM: MOV TMOD,#00100000B ;8-bit auto-reload timer ;Software control MOV TH1,#0FDH ;Set Baud rate to 9600 SETB TR1 ;Turn timer 1 on MOV SCON,# 01010010B ;8 bit UART set trans interrupt flag

11

Page 12: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

INITCOM: MOV TMOD,#00100000B ;8-bit auto-reload timer ;Software control MOV TH1,#0FDH ;Set Baud rate to 9600 SETB TR1 ;Turn timer 1 on MOV SCON,#01010010B ;8 bit UART set trans interrupt flag

12

Page 13: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

MOV SCON,#01010010B ;8 bit UART set trans interrupt flag

13

Page 14: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

X_COMIO.A51$PAGELENGTH(50) ;SET FOR PRINTER$PAGEWIDTH(110)$MOD51$TITLE(X_COMIO - TEST COM ROUTINES) $SYMBOLS ;INCLUDE SYMBOLS IN LISTING;****************************************************************; PROGRAM: X_COMIO.A51 - Echos characters type in serial window;****************************************************************CR equ 0DH ; carriage returnLF equ 0AH ; line feed EXTRN CODE (PUTCHAR, GETCHAR, CRLF, INITCOM) ;IN COMIO.A51EXTRN CODE (UPPER, HEX2ASC, ASC2HEX, HEXCHK) ;IN ASCII.A51EXTRN CODE (PUTSTR, SPACE) ;IN COMIO.A51PROG SEGMENT CODE ; SEGMENT FOR PROGRAM IN ROMCONST SEGMENT CODE ; SEGMENT FOR CONSTANTS IN ROMDVAR SEGMENT DATA ; SEGMENT FOR VARIABLES IN RAM;BVAR SEGMENT BIT ; SEGMENT FOR BIT VARIABLESSTACK SEGMENT IDATA ; SEGMENT FOR STACK IN INTERNAL DATA RAM RSEG STACK DS 20H ; Reserve 32 Bytes for Stack RSEG DVAR ; SWITCH TO DATA SEGMENTBYTES: DS 4 ; Example of reserving 4 BYTES IN DATA RAM RSEG CONST

14

Page 15: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

; CONSTANTS.MSG: DB 'Keyboard input is ecoed to terminal screen.' DB CR,LF DB 'If the key is a hex character, its uper case is output.' DB 0; START OF CSEG AT 0 ; absolute Segment at Address 0RESET: LJMP MAIN ; reset location (jump to start OF PROGRAM) ORG 3HINTR0: ;LCALL ISR RETI ORG 0BHTIME0: RETI ORG 13H ;ENTRY POINT FOR INTR1INTR1: RETI ORG 1BHTIME1: RETI ORG 23HCOM: RETI ORG 2BHTIME2: RETI$EJECT;START OF PROGRAM

15

Page 16: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

RSEG PROG ; SWITCH TO CODE SEGMENT USING 0 ; TELL ASEMBLER TO USE REGISTER BANK 0MAIN: MOV SP,#STACK-1 ; Initialize stack pointer CALL INITCOM ;INITILIZE COM PORT MOV DPTR, #MSG ;POINT TO SIGNON MESSAGE CALL PUTSTR ;OUTPUT TO CONSOLEAGAIN: CALL CRLF ;GOTO NEW LINE CALL GETCHAR ;READ COM PORT CALL PUTCHAR ;ECHO CHARCHKIT: ;CHECK FOR VALID HEX CALL UPPER ;CONVERT TO UPPER CASE CALL HEXCHK ;BE SURE IT IS A HEX CHAR JC AGAIN ;IGNORE Non-HEX CHAR CALL SPACE ;OUTPUT A SPACE CALL PUTCHAR ;ECHO CAHR JMP AGAIN ;GET NEXT CHAR END

16

Page 17: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

COMIO.A51$PAGELENGTH(50) ;SET FOR PRINTER IN LANDSCAPE MODE$PAGEWIDTH(110)$MOD51$TITLE (COMIO - COM IO ROUTINES)NAME COMIOCOMIO_CODE SEGMENT CODERSEG COMIO_CODEPUBLIC PUTCHAR, GETCHAR, CRLF, INITCOM, SPACE, PUTSTREXTRN CODE(HEX2ASC) ; in ascii.A51CR equ 0DH ; carriage returnLF equ 0AH ; line feed

17

Page 18: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

;***************************************************************; FUNCTION: INITCOM - INITILIZE 8051's COM PORT to 9600 BAUD; USING TIMER 1 TO GENERATE BAUD RATES ASSUMES; 11.059 MHz XTAL. BAUD rate determined by; TH1 = 256 - (? X Osc Freq)/(384 X Baud rate) ; See IUG p 3-13 TO 3-16; INPUTS: NONE ; OUTPUTS: NONE ; CALLS: NONE ; DESTROYS: THMOD, TH1, TR1, SCON ;***************************************************************;INITCOM: MOV TMOD,#00100000B ;8-bit auto-reload timer ;Software control MOV TH1,#0FDH ;Set Baud rate to 9600 SETB TR1 ;Turn timer 1 on MOV SCON,#01010010B ;8 bit UART set trans interrupt flag RET ;RETURN

18

Page 19: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

;***************************************************************; FUNCTION: PUTCHAR - OUTPUT CHAR IN A REGISTER TO COM PORT ; INPUTS: A - CHARACTER TO OUTPUT TO COM PORT ; OUTPUTS: NONE ; CALLS: NONE ; DESTROYS: NONE - ACTUALLY T1, SBUF ;***************************************************************PUTCHAR: JNB TI,$ ;WAIT TILL TRANSMIT BUFF EMPTY CLR TI ;CLEAR TRANSMITER INT FLAG MOV SBUF,A ;PUT CHAR IN UAET TRANSMIT REGISTER RET;***************************************************************; FUNCTION: GETCHAR - RECEIVE CHAR FORM COM PORT ; INPUTS: NONE ; OUTPUTS: A - CHARACTER FROM COM PORT ; CALLS: NONE ; DESTROYS: RI ;***************************************************************GETCHAR: JNB RI,GETCHAR ;WAIT TILL CHAR RECEIVED CLR RI ;CLEAR RECEIVER INT FLAG MOV A,SBUF ;READ CHAR FROM UART RECEIVE REGISTER RET

19

Page 20: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

;***************************************************************; FUNCTION: CRLF - OUTPUT CARRAGE RETURN LINE FEED TO COM PRT ; INPUTS: NONE ; OUTPUTS: NONE ; CALLS: PUTCHAR ; DESTROYS: NONE - ACTUALLY TI, SBUF ;***************************************************************;CRLF: PUSH PSW ;SAVE FLAGS PUSH ACC ;SAVE A REG MOV A,#CR ;OUTPUT CARRAGE RETURN TO CALL PUTCHAR ;COM PORT MOV A,#LF ;OUTPUT LINE FEED TO CALL PUTCHAR ;COM PORT POP ACC ;RESTORE ACC POP PSW ;RESTORE FLAGS RET ;RETURN

20

Page 21: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

;***************************************************************; FUNCTION: SPACE - OUTPUT SPACE TO COM PRT ; INPUTS: NONE ; OUTPUTS: NONE ; CALLS: PUTCHAR ; DESTROYS: TI, SBUF ;***************************************************************;SPACE: PUSH PSW ;SAVE FLAGS PUSH ACC ;SAVE A REG MOV A,#' ' ;OUTPUT CARRAGE RETURN TO CALL PUTCHAR ;COM PORT POP ACC ;RESTORE ACC POP PSW ;RESTORE FLAGS RET ;RETURN

21

Page 22: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

;***************************************************************; FUNCTION: PUTSTG - OUTPUT ASCIIZ STRING POINTED TO BY DPTR TO; COM PORT. ; INPUTS: CM(DPTR)... ; OUTPUTS: NONE ; CALLS: PUTCHAR ; DESTROYS: TI, SBUF ;***************************************************************PUTSTR: PUSH PSW ;SAVE FLAGS PUSH ACC ;SAVE ACC MOV A,#-1 ;INITILIZE STRING OFFSETPSNEXT: INC A ;POINT TO NEXT CHAR PUSH ACC ;SAVE OFFSET MOVC A,@A+DPTR ;GET CHAR JZ PSDONE ;EXIT IF NULL CHAR CALL PUTCHAR ;OUTPUT CHAR POP ACC ;RESTORE OFFSET JMP PSNEXT ;LOOP TILL END OF STRINGPSDONE: POP ACC ;RESTORE ACC POP ACC ;RESTORE ACC POP PSW ;RESTORE FLAGS RET ;RETURN END

22

Page 23: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

ASCII.A51$TITLE (ASCII - ASCII ROUTINES)NAME ASCIIASCII_CODE SEGMENT CODERSEG ASCII_CODEPUBLIC HEX2ASC, ASC2HEX, UPPER, HEXCHK ;****************************************************************;FUNCTION: HEX2ASC_TBL - CONVERT LEAST SIG NIBBLE OF A TO ASCII; USING LOOK UP TABLE; INPUTS: A(3_0) - HEX DIGIT; OUTPUTS: A - ASCII CHAR CORESPONDING TO HEX DIGIT IN A(3_0); CALLS: NONE;DESTROYS: A, DPTR;****************************************************************;ASCII LOOK UP TABLE ASC_TBL: DB '0' DB '1' DB '2'... DB 'E' DB 'F'HEX2ASC_TBL: MOV DPTR,#ASC_TBL ;POINT TO LOOK UP TABLE ANL A,#0FH ;USE ONLY LEAST SIG NIBBLE TO LOOK UP MOVC A,@A+DPTR ;ASCII VALUE RET

23

Page 24: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

$EJECT;****************************************************************;FUNCTION: HEX2ASC - CONVERT LEAST SIG NIBBLE OF AL TO ASCII; INPUTS: A(3_0) - HEX DIGIT; OUTPUTS: A - ASCII CHAR CORESPONDING TO HEX DIGIT IN AL(3_0); CALLS: NONE;DESTROYS: A, PSW;****************************************************************HEX2ASC: ANL A,#0FH ;GET LEAST SIG NIBBLE ADD A,#-0AH ;SET CARRY IF ACC IS LESS THAN 0AH JC LTA ;JUMP IF ACC <= 9 ADD A,#'A'-('0'+0AH);PREPARE FOR NEXT ADDLTA: ADD A,#'0'+0AH ;MAKE ASCII RET;****************************************************************;FUNCTION: HEX2ASC2 - CONVERT LEAST SIG NIBBLE OF A TO ASCII;****************************************************************HEX2ASC2: ANL A,#0FH ;SAVE ONLY LOW NIBBLE ADD A,#90H ;FIX HIGH NIBBLE TO PROPAGATE CARRY DA A ;ADD 6 TO LSN IF > 9 i.e. A --> 00 AND SET CY ADDC A,#40H ;IF >9 CARRY WAS SET ie. A --> 41H ELSE DA A ;LSN IS INCREASED BY 30H RET

24

Page 25: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

$EJECT;****************************************************************;FUNCTION: ASC2HEX - CONVERT ASCII CHAR IN A TO HEX DIGIT. A IS; ASSUMED TO CONTAIN THE UPPER CASE ASCII FOR A VALID HEX DIGIT; THE TRANSLATION IS AS SHOWN BELOW; INPUT AL OUTPUT AL; '0'=30H 00000000B; '1'=31H 00000001B; ... ...; 'A'=41H 00001010B; ... ...; 'F'=46H 00001111B; ; INPUTS: AL - THE ASCII CHAR '0'..'9', 'A'..'F'; OUTPUTS: AL - HEX NIBBLE 00 ..09 , 0A ..0F; CALLS: NONE;DESTROYS: AL, PSW ;****************************************************************ASC2HEX: add a,#-'A' JNC HEXFIX ;JUMP IF A WAS 'A' - 'F' ADD A,#'A'-'0'-0AH ;DO THIS IF A WAS '0' - '9'HEXFIX: ADD A,#0AH ;A NOW CONTAINS HEX VALUE RET

25

Page 26: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

$EJECT;****************************************************************;FUNCTION: UPPER - CONVERT ASCII CHAR IN A TO UPPER CASE; INPUTS: A - ASCII CHAR; OUTPUTS: A - UPPER CASE ASCII CHAR; CALLS: NONE;DESTROYS: A, PSW;****************************************************************UPPER: CJNE A,#'a',LOWIFC ;SET CY IF CHAR < 'a'CHKHI: CJNE A,#'z'+1,UCONV ;SET CY IF CHAR <= 'z'UCONV: JNC UDONE ;EXIT IF NOT 'a' TO 'z' ANL A,#5FH ;CLEAR BIT 5 TO MAKE UPPER CASE RET LOWIFC: JNC CHKHI ;CHECK FOR ABOVE 'z'UDONE: RET ;EXIT WITH NO CHANGE

26

Page 27: jhtucker/s09-egre631/LHO 11 Timer and... · Web viewClock may be internal processor clock or an external pin. Clock may be divided down (typically by a power f two). The divided clock

;****************************************************************** ;FUNCTION: HEXCHK - CHECK A FOR AN UPPER CASE HEX CHAR IN ASCII.; IF A IS NOT THE ASCII FOR A HEX CHARACTER (0..9,A..F).; THE CARY FLAG IS SET.; INPUTS: A; OUTPUTS: A, CY FLAG; CALLS: NONE; DESTROYS: PSW;******************************************************************HEXCHK: CJNE A,#'0',CIFLT0 ;SET CY IF CHAR < '0'CIFLT0: JC HEXIT ;EXIT IF CHAR < '0' CJNE A,#'9'+1,NCIFLTE9 ;CLEAR CARRY IF CHAR >= 9NCIFLTE9: JC FIXIT ;RETURN IF CHAR '0' TO '9' CJNE A,#'A',HEX3 ;SET CY IF CHAR < 'A'HEX3: JC HEXIT ;EXIT IF NOT HEX CJNE A,#'F'+1,FIXIT ;SET CY IF CHAR <= 'F'FIXIT: CPL C ;COMPLEMENT CARRYHEXIT: RET END

27