You are on page 1of 86

08.

601 Microcontroller Based System Design


Module 2 Programming Timer /Counter Two 16 bit timers SFRs for programming timer/counter are: 1. TCON 2. TMOD 3. T0 (as TH0 and TL0) 4. T1 (as TH1 and TL1) TCON To control timer/counter operation Byte and Bit addressable, where 88H is byte address and 88H (TCON.0), 89H (TCON.1), 8AH (TCON.2), .are bit address Bit orientation:

Department of ECE, VKCET

Page 1

08.601 Microcontroller Based System Design


TMOD Timer Mode control SFR 8-bit register, byte address is 89H Bit orientation:

Upper nibble for timer 1 and lower nibble for timer 0

8051 Timer modes: 1. Mode 1 programming 16 bit timer/counter TLx hold lower byte of timer/count value (00H to FFH) THx hold upper byte of timer/count value (00H to FFH) Allows 0000H to FFFFH values TLx acts as 8-bit prescaler

Department of ECE, VKCET

Page 2

08.601 Microcontroller Based System Design

Steps to program: 1. Select timer by TMOD register 2. Load THx and TLx values 3. Run timer by setting TRx bit in TCON 4. Overflows occur TFx becomes high and monitoring it get the delay by timer or count value 5. Stop the timer by TRx = 0 6. Clear the flag TFx = 0 7. For continuous operation repeat the process from loading values to THx and TLx

Department of ECE, VKCET

Page 3

08.601 Microcontroller Based System Design


To design count value for delay in mode 1 operation If f is Xtal frequency, then ft = f / 12 is timer frequency Timer operation is based on time period Tt = 1/ft seconds Let f = 12MHz , ft = 1MHz and Tt = 1us If THx and TLx hold XXYYH (16 bit ) count value, then overflow occurs when it incremented from FFFFH to 0000H Then delay Td = (FFFFH XXYYH + 1) Tt In decimal Td = (65535 N + 1) Tt, where N is the count value in decimal Example: Count value for delay 1ms with 12MHz Xtal frequency. From the above equation, N = 65536 (Td /Tt) = 65536 (1m / 1) N = 64536 = FC18H Then THx = FCH and TLx = 18H are the initial count values to provide 1ms delay. Program 1: Program to generate square wave of 10kHz at pin P1.7 using timer 0 (Xtal frequency to 8051 is 11.0592MHz) Solution: Design for count value : For 10kHz square wave, time period T = 1/10k = 100s And Ton = Toff = T/2 = 50s Then desired delay Td = 50s For timer in mode 1, count value N = 65536 (50s / 1.0851s) [Xtal frequency f = 11.0592MHz, timer frequency ft = 0.922MHz and Tt = 1.0851s] N = 65489.9 65489 = FFD1H Initial count value to THx = FFH and TLx = D1H

Department of ECE, VKCET

Page 4

08.601 Microcontroller Based System Design


Program: ORG 0 MOV TMOD, #00000001B ; Timer 0 , sw controlled , and mode 1 MOV TH0, #FFH ; Higher byte of 16-bit initial count MOV TL0, #D1H ; Lower byte of 16-bit initial count CPL P1.7 ACALL DELAY ; 50s delay CPL P1.7 ACALL DELAY SJMP REPEAT SETB TR0 JNB TF0, WAIT CLR TR0 CLR TR0 RET END

REPEAT:

DELAY: WAIT:

Department of ECE, VKCET

Page 5

08.601 Microcontroller Based System Design


2. Mode 0 programming Similar to mode 1, but 13-bit timer operation Least five bit of TLx as 5-bit prescaler timer and THx as 8-bit register

0000 to 1FFFH count values Count value N = 8192 (Td / Tt) Let N = 8182 = 1FF6H = 0001 1111 111 1 0110 b Then load TLx = 0001 0110b = 16H and THx = 1111 1111b = FFH All other operations are similar to mode 1

3. Mode 2 programming 8-bit auto reload mode, values ranges from 00H to FFH and THx hold it

Count value N = 256 (Td/Tt)

Steps to program: Select timer by TMOD Load count value to THx and TLx Start timer by TRx = 1 When overflows in TLx results TFx set to 1 and THx value reloaded to TLx Stop timer by setting TRx = 0 and reset flag TFx = 0 For continuous operation, start timer again

Department of ECE, VKCET

Page 6

08.601 Microcontroller Based System Design


Program 2: Program to generate square wave 100 kHz at P1.0 (Xtal frequency to 8051 is 11.0592MHz) Solution: Design for count value: Required delay Td = 5s, timer time period Tt = 1.0851 s Count value N = 256 (5/1.0851) = 251.39 251 = FBH Program: ORG 0 MOV TMOD,#00100000B ; Timer 1, sw controlled and mode 2 MOV TH1,#0FBH ; Count value for 5s MOV TL1,TH1 RESTART: CPL P1.0 ACALL DELAY SJMP RESTART DELAY: WAIT: SETB TR1 JNB TF1, WAIT CLR TR1 CLR TF1 RET END

4. Mode 3 programming Split timer mode Timer 0 is split into two 8-bit timers TL0 to hold count value for timer 0 with over flow setting in TF0 TH0 to hold count value for timer 1 (Timer 1 stopped from other mode, but can be switched into mode 0, 1 or 2) with overflow setting in TF1

Department of ECE, VKCET

Page 7

08.601 Microcontroller Based System Design


Counter programming Timer as event counter Pulse outside from the microcontroller is source of frequency Modes, THx and TLx registers are same as timer operation C/T bit in TMOD is set as 1 activate timer as counter

Counter applications: To count objects, measure unknown frequency etc. P3.4 (T0) and P3.5 (T1) pins are used Program 3: System to measure unknown frequency and display it to LCD: Solution: Circuit diagram:

(Xtal and RST connections are not shown)


Department of ECE, VKCET Page 8

08.601 Microcontroller Based System Design


Program: INPUT RS RW E LCD_DATA BIT BIT BIT BIT EQU P3.4 P2.0 P2.1 P2.2 P1

ORG 0 MAIN: MOV TMOD,#00010101B ; Timer 1 as mode 1 timer & timer 0 as ;counter in mode 1 SETB INPUT ACALL LCD_INIT ACALL DELAY CONTINUE: MOV A,#80H ; DDRAM address of LCD LCALL CMD_WRITE LCALL DELAY MOV TL0,#00 ;Lower byte initial count value MOV TH0,#00 ;Higher byte initial count value SETB TR0 ;Start counter ACALL DELAY_1S ;Wait 1 second MOV A,TL0 ;Load lower byte count value to A ACALL HEX_2_BCD ;Convert count value to BCD MOV A,R7 ;3rd digit of BCD to A ACALL BCD_2_ASCII ;Convert BCD to ASCII ACALL LCD_DISPLAY ;Display 3rd digit to LCD MOV A,R6 ; 2nd digit of BCD to A ACALL BCD_2_ASCII ;Convert BCD to ASCII ACALL LCD_DISPLAY ;Display 2nd digit to LCD MOV A,R5 ; 1st digit of BCD to A ACALL BCD_2_ASCII ;Convert BCD to ASCII ACALL LCD_DISPLAY ;Display 1st digit to LCD CLR TR0 ;Stop counter CLR TF0 ;Clear counter flag ACALL DELAY ;Wait a short time SJMP CONTINUE ;Repeat the process DELAY: MOV TH1,#0F8H ; 2ms delay program
Page 9

Department of ECE, VKCET

08.601 Microcontroller Based System Design


MOV TL1,#0CDH SETB TR1 JNB TF1,WAIT_2m CLR TR1 CLR TF1 RET ;1 second delay program

WAIT_2m:

DELAY_1S: MOV R0,#0FH NEXT: MOV TH1,#00 MOV TL1,#00 SETB TR1 WAIT_1S: JNB TF1,WAIT_1S CLR TR1 CLR TF1 DJNZ R0,NEXT RET ;LCD initializing LCD_INIT: CLR E MOV A,#38H LCALL CMD_WRITE LCALL DELAY MOV A,#01H LCALL CMD_WRITE LCALL DELAY MOV A,#0CH LCALL CMD_WRITE LCALL DELAY MOV A,#06H LCALL CMD_WRITE LCALL DELAY RET CMD_WRITE: MOV LCD_DATA,A CLR RW CLR RS SETB E

;E = 0 ;8-bit, 2 line and 5 x 7 dot function

;Clear display

;Display on, Cursor off

;Increment (Left entry)

;Command to LCD data bus ;RW = 0 ;RS = 0 ;E = 1


Page 10

Department of ECE, VKCET

08.601 Microcontroller Based System Design


LCALL DELAY CLR E SETB RW SETB RS RET DATA_WRITE: MOV LCD_DATA,A SETB RS CLR RW SETB E LCALL DELAY CLR E CLR RS SETB RW RET ACALL DATA_WRITE RET MOV R1,A MOV B,#64H DIV AB MOV R7,A MOV A,B MOV B,#0AH DIV AB MOV R6,A MOV R5,B RET ADD A,#30H RET END

;E = 0 ;RW = 1 ;RS = 1

;Data to LCD data bus ;RS = 1 ;RW = 0 ;E = 1 ;E = 0 ;RS = 0 ;RW = 1

LCD_DISPLAY:

HEX_2_BCD:

;Hex number divided by 100 ;3rd digit of BCD to R7 ;Reminder to A ;Reminder divided by 10 ; 2nd digit of BCD to R6 ;Reminder is 1st digit of BCD and move to R5

BCD_2_ASCII:

;Add BCD to 30H

Department of ECE, VKCET

Page 11

08.601 Microcontroller Based System Design


Program 4 Program to display count value of 1Hz pulse up to 60 pulses: Circuit diagram is similar to previous example RS BIT P2.0 RW BIT P2.1 E BIT P2.2 INPUT BIT P3.4 LCD_DATA EQU P1 ORG 0 SETB INPUT MOV TMOD,#00010110B

MAIN:

MOV TH0,#0C4H MOV TL0,TH0 LCALL LCD_INIT CONTINUE: SETB TR0 NEXT: MOV R3,TL0 MOV A,#88H ACALL CMD_WRITE ACALL DELAY MOV A,R3 ADD A,#3CH ACALL HEX_2_BCD MOV A,R6 ACALL BCD_2_ASCII ACALL LCD_DISPLAY ACALL DELAY MOV A,R5 ACALL BCD_2_ASCII ACALL LCD_DISPLAY ACALL DELAY JNB TF0,NEXT CLR TR0 CLR TF0 SJMP CONTINUE All subroutines are similar to previous example.
Department of ECE, VKCET

; Timer 0 as counter in mode 2 & timer 1 as ;timer in mode 1 ; Initial count to timer 0, 256 60 = 196 = C4H

;Start counter ;Current count value to R3 ; DDRAM address

;Current count value to A ;Current count + 60 ;2nd digit of BCD

; 1st digit of BCD

;If count 60 go to label NEXT ; Stop counter ; Clear counter flag ; Continue the process
Page 12

08.601 Microcontroller Based System Design


Hardware control of timers By setting GATE = 1 bit in TCON

Set TRx = 1 and timer starts or stop according to the pulse by the switch connected to INTx pins

Department of ECE, VKCET

Page 13

08.601 Microcontroller Based System Design


8051 interrupts To serve several tasks or devices at a time by single microcontroller Conventional to polling: microcontroller continuously monitor the status of device/task, when the status condition is met, the microcontroller performs the service. An example is: WAIT: JNB TF0, WAIT Interrupt: microcontroller interrupted by device/task causes execution of program associated with the interrupt called Interrupt Service Routine (ISR) Advantages of interrupts: Microcontroller can serve many devices/tasks Priority to the devices/tasks can assign Masking (ignoring) interrupt request of devices/tasks is possible Doesnt waste the time to check the status of devices/tasks Interrupt Service Routine: All interrupts must have an ISR or interrupt handler There is a fixed location in code memory that holds the ISR Group of memory location addresses of ISRs is called Interrupt Vector Table (IVT) Interrupt Vector Table of 8051 ROM location Interrupt Pin Flag clearing (ISR address) Reset 0000H RESET (9) Auto INT0 (External h/w interrupt 0) TF0(Timer 0 interrupt) INT1 (External h/w interrupt 1) TF1 (Timer 1 interrupt) RI and TI (Serial port interrupt) 0003H 000BH 0013H 001BH 0023H P3.3 (13) P3.2 (12) IE0 - Auto TF0 - Auto IE1 - Auto TF1 - Auto RI/TI Programmer clears it

Department of ECE, VKCET

Page 14

08.601 Microcontroller Based System Design


Steps in executing an interrupt 1. Finishes the instruction executing and save the address of next instruction on the stack 2. Saves the current status of all interrupts internally (not on stack) 3. Jumps to fixed location in memory in IVT, that holds the address of ISR 4. Starts execution of ISR until it reaches last instruction of subroutine, which must be RETI 5. Returns to place where microcontroller is interrupted, continue the process from saved address and status

Department of ECE, VKCET

Page 15

08.601 Microcontroller Based System Design


8051 interrupt handling registers 1. IE (Interrupt Enable) Register: 8-bit, bit addressable SFR byte address : A8H or A8H, A9H, .. to AFH Bit orientation:

EA Global interrupt enable bit, set to 1 to enable all interrupts ET2 Enable Timer 2 interrupt (only in 8052) ES Enable Serial port interrupt, set to 1 to enable serial port interrupt ET1 Timer 1 overflow interrupt EX1 Enable External interrupt 1 (INT1 ) ET0 Enable Timer 0 overflow interrupt EX0 Enable External interrupt 0 (INT0) 2. IP (Interrupt Priority) Register 8-bit, bit addressable SFR byte address: B8H

or B8H, B9H,.. to BFH Bit orientation:

PT2 Priority Timer 2 (only in 8052) PS Priority of Serial port interrupt , set to 1 for highest priority , else default priority PT1 Priority of timer 1 interrupt, set to 1 for highest priority , else default priority PX1 Priority of external interrupt 1 (INT1), set to 1 for highest priority , else default priority PT0 Priority of timer 0 interrupt, set to 1 for highest priority , else default priority PX0 Priority of external interrupt 0 (INT0), set to 1 for highest priority , else default priority
Department of ECE, VKCET Page 16

08.601 Microcontroller Based System Design


Interrupt priority Highest to lowest priority External interrupt 0 INT0 Timer 0 interrupt TF0 External interrupt 1 INT1 Timer 1 interrupt TF1 Serial port RI and TI If any two or more interrupt priority bit is set to 1, then the priority of all interrupts are default priority Programming Timer Interrupts Steps: 1. Select timer by TMOD, load THx and TLx 2. Enable timer interrupt by IE 3. Run timer by TRx bit in TCON 4. Wait ISR (000BH or 001BH) of timer must contain the program for task Note: 1. Must avoid the memory space allocated to IVT for main program 2. If ISR is more size than allocated space (maximum 8 bytes), use jump instructions to point somewhere else in program memory

Department of ECE, VKCET

Page 17

08.601 Microcontroller Based System Design


Program 1: Program to generate square wave of 3kHz using timer 1 interrupt (11.0592MHz Xtal frequency) ORG 0 LJMP MAIN ORG 001BH CPL P0.0 RETI ORG 0030H MOV TMOD,#00100000B MOV TH1,#66H MOV TL1,TH1 MOV IE,#10001000B SETB TR1 SJMP WAIT END

ISR_T1:

MAIN:

; TIMER 1, MODE 2, AUTO RELOAD ; COUNT VALUE FOR 0.1666mS ; EA =1 and ET1 = 1

WAIT:

Program 2: Program to generate a square pulse of 9 kHz at pin P0.0 and toggle pin P2.0 for every 1 second. ORG 0 LJMP MAIN ORG 000BH CPL P0.0 RETI ORG 001BH LJMP WAIT_1S ORG 0030H MOV TMOD,#00010010B MOV TH0,#0CCH MOV TL0,TH0 MOV TH1,#4CH MOV TL1,#00H MOV R0,#00H

ISR_T0:

ISR_T1:

MAIN:

; TIMER 1 & 2, MODE 2 ; COUNT VALUE FOR 55us ; COUNT VALUE FOR 50ms

Department of ECE, VKCET

Page 18

08.601 Microcontroller Based System Design


MOV IE,#10001010B SETB TR0 SETB TR1 SJMP WAIT CJNE R0,#20,RETURN CPL P2.0 MOV R0,#00H MOV TH1,#4CH MOV TL1,#00H RETI INC R0 MOV TH1,#4CH MOV TL1,#00H RETI ; EI = 1, ET1 & ET0 = 1

WAIT: WAIT_1S:

RETURN:

Programming External Hardware Interrupts INT0 (P3.2) and INT1 (P3.3) Activating these interrupts, 8051 execution jumps to the ISRs of IVT location 0003H and 0013H respectively Enabled by IE register Two type triggering: Level triggered

Edge triggered

Department of ECE, VKCET

Page 19

08.601 Microcontroller Based System Design


SFR to select triggering: TCON

Lower nibble IEx : set to 1 by microcontroller when external interrupt edge (H to L) is detected, cleared by microcontroller when the interrupt is processed ITx : Interrupt type control bit, set to 1 specify falling edge triggering external interrupt, cleared to 0 specify low level triggered external interrupt

Minimum duration for low level triggering signal is 4 Machine cycles Minimum pulse duration to detect edge triggered interrupt is 1 Machine cycle HIGH and 1 Machine cycle LOW

Difference between RET and RETI instructions Both return to main program RETI clears ISR flags, indicates the service is over If RET is used all other lower priority interrupts are blocked

Department of ECE, VKCET

Page 20

08.601 Microcontroller Based System Design


Program 1: Program for up/down counter and display the count. Switch SW1 on P3.2 for up count and SW2 on P3.3 for down count. Display is a seven segment display on port 1. Circuit diagram:

Program: ORG 0 LJMP MAIN ORG 0003H LJMP UP ORG 0013H LJMP DOWN MAIN: MOV R1,#00 MOV DPTR,#SEG_CODE CONTINUE: CLR A ADD A,R1
Department of ECE, VKCET Page 21

08.601 Microcontroller Based System Design


MOVC A,@A+DPTR MOV P1,A MOV IE,#10000101B SETB IT0 SETB IT1 SJMP CONTINUE UP: INCR: CJNE R1,#09,INCR RETI INC R1 RETI CJNE R1,#0,DECR RETI DEC R1 RETI ;0 , ASCII for 0 ;1, ASCII for 1 ;2, ASCII for 2 ;3, ASCII for 3 ;4, ASCII for 4 ;5, ASCII for 5 ;6, ASCII for 6 ;7, ASCII for 7 ;8, ASCII for 8 ;9, ASCII for 9

; EA = 1, EX1 = 1 & EX0 =1 ; Edge triggering INT0 ; Edge triggering INT1

DOWN: DECR:

SEG_CODE: DB 11000000B DB 11111001B DB 10100100B DB 10110000B DB 10011001B DB 10010010B DB 10000010B DB 11111000B DB 10000000B DB 10011000B END

Department of ECE, VKCET

Page 22

08.601 Microcontroller Based System Design


Serial data communication Parallel Communication (Printer) Fast, but distance cannot be great. Expensive

Serial Communication (Telephone line) Long distance Cheaper

Serial communication uses single or pair of data line instead of 8-bit data line of parallel communication For long distance transmission serial data is converted/de-converted into tones by modem

Department of ECE, VKCET

Page 23

08.601 Microcontroller Based System Design


Asynchronous or synchronous methods of serial data transmission: Asynchronous transmission transfers one byte at a time Synchronous transmission transfers a block of data at a time. They are commonly referred as UART (Universal Asynchronous ReceiverTransmitter) and USART (Universal Synchronous Asynchronous ReceiverTransmitter) Types of communication: 1. Simplex (only transmit)

2. Duplex

Department of ECE, VKCET

Page 24

08.601 Microcontroller Based System Design

8051 has full-duplex, asynchronous serial port, ie two lines and one byte transmission at a time Asynchronous serial communication data framing: Character oriented data transmission Each character is placed between a start and stop bits and is called framing

Mark is 1 signal to indicate no transfer Space is 0 signal indicates transfer is over Framing for ASCII of character A ASCII for A is 41H

Transmissions begins from start bit 0 followed by D0 (LSB) and ends at stop bit 1 after D7 (MSB) for 8-bit UART For 7-bit mode (older system), transmission end at D6 and 2 stop bits are used Other system have Parity bit with 7 or 8 bit character bits and start/stop bits are also included for data integrity

Department of ECE, VKCET

Page 25

08.601 Microcontroller Based System Design


Data Transfer Rate Bits per second (bps) is also called Baud rate (the number of signals changes per second, MODEM terminology) IBM PC/XT 100 to 9600 bps Pentium PC 56k bps (recent MODEM) Asynchronous serial data communication, the baud rate is limited to 100,000 bps. RS232 Standard To allow compatibility between different data communication equipments from different manufacturers Set by EIA (Electronics Industries Association) in 1960 Modified versions are: 1963 - RS232A, 1965- RS232B, 1969 - RS232C RS232 is serial I/O interfacing standard, but the input and output lines are not compatible with TTL/CMOS family RS232 logic: 0 bit is +3V to +25V 1 bit is -3V to -25V MAX232: Line driver Voltage converter Convert TTL logic to RS232 logic and vice versa Pin out diagram and circuit diagram:

Department of ECE, VKCET

Page 26

08.601 Microcontroller Based System Design


RS232 Connectors: DB25 Connector (25 pin) DB25P Plug connector (Male) DB25S Socket connector (Female)

Pin 1 GND Pin 2 TxD (transmitted data) Pin 3 RxD (Received data) Other pins for USART connections DB9 Connector (9 pin) DB9P (Male) and DB9S (Female)

Pin 2 RxD (Received data) Pin 3 TxD (Transmitted data) Pin 5 GND Other pins for USART

Department of ECE, VKCET

Page 27

08.601 Microcontroller Based System Design


8051 connection to RS232

8051 serial port programming SFRs for serial port programming: SBUF: To hold the byte of data for Txr or Rxr SCON: For select serial port mode, serial interrupt flags, receiver enable etc TH1, TMOD and TCON: To set baud rate using timer 1. TH1: Hold baud rate information, TMOD: To select timer 1 in auto-reload mode(Mode 2), TCON: To run the timer 1 IE and IP: If program is in ISR of serial port PCON: To double the selected baud rate SBUF: Serial buffer 8-bit register to hold serial data for txn or rxn Physically two register with one address 99H, one register only have write operation (for Txn) and other for read operation (for Rxn)

Department of ECE, VKCET

Page 28

08.601 Microcontroller Based System Design


SCON: Serial Control Register 8-bit, bit addressable Byte address: 98H, Bit addresses 98H, 99H,.or SCON.0, SCON.1, Bit orientation:

SM0 & SM1: Serial port Mode bits SM2: Multiprocessing communication enable bit, 1 to enable & 0 to disable REN: Receiver Enable bit, 1 to enable & 0 to disable TB8: Transmitted bit 8 (9th bit), 1 or 0 by program RB8: Received bit 8 (9th bit) TI: Transmit Interrupt Flag bit, 1 if transmission is over & cleared by sw RI: Receive Interrupt Flag bit, 1 if receiving is over & cleared by sw

Serial port modes:

Mode 0: Shift register mode SBUF configures to receive/transmit 8 data bits using RxD pin (P3.0) for both functions. TxD serves as clock, where it serve a clock frequency of f/12 to external circuits Timing diagram:

Department of ECE, VKCET

Page 29

08.601 Microcontroller Based System Design


Not used for data communication between PC Only used for special high speed data communication devices

Mode 1: 8-bit UART, variable baud rate mode SBUF configures to receive 8-bit data, one start bit and a stop bit for fullduplex UART The format (framing) of data is:

After transmission (stop bit) TI flag (SCON.1) set to 1 Receiving bits at the center of bit time (inverse of baud rate), to avoid false triggering (cause noise) For receiving, RxD pin bits are shifted into SBUF at programmed baud rate only if: RI = 0 and SM2 = 0 After discarding start bit, 8 data bit go to SBUF, stop bit saved to RB8 (SCON.2) and RI = 1, indicates receiving data is over Mode 1 Baud rate: Timer 1 in its auto-reload mode (mode 2) is used to generate baud rate Timer 1 overflow flag TF1 determine baud frequency Baud frequency generated by timer 1:

Where SMOD is PCON.7 bit, double baud rate bit; if it is 0,

else

f is Xtal frequency and TH1 is 8-bit value to program different baud rate
Department of ECE, VKCET Page 30

08.601 Microcontroller Based System Design


Standard baud rate for data communication between PCs are: 1. 110 bps 2. 150 bps 3. 300 bps 4. 600 bps 5. 1200 bps 6. 2400 bps 7. 4800 bps 8. 9600 bps 9. 19200 bps

Q: With Xtal frequency 11.0592MHz, find the TH1 value for getting baud frequencies a) 1200 bps b) 9600 bps Solution: For 2400 bps:

Similarly for 9600 bps:

Mode 2: 9-bit UART, fixed baud rate mode Similar to mode 1, but 11 bits are transmitted: (1 start bit, 9 data bit and 1 stop bit). 9 th data bit is from TB8 bit of SCON register Baud rate can be programmed as:

Data format:

Department of ECE, VKCET

Page 31

08.601 Microcontroller Based System Design


Mode 3: 9-bit UART, variable baud rate mode Similar to mode 2, but baud frequency is similar to mode 1 and is:

Note: Commonly used mode is mode 1: 8-bit UART variable baud rate, because of its simplicity and versatility with other data communication devices Program 1: Send a string of message to 8051 TxD pin (P3.1) ORG 0 LJMP MAIN ORG 30H MOV TMOD,#00100000B MOV TH1,#0FDH MOV TL1,TH1 SETB TR1 MOV SCON,#01000000B MOV DPTR,#MESSAGE CLR A MOVC A,@A+DPTR CJNE A,#00,SEND SJMP STOP MOV SBUF,A JNB TI,WAIT CLR TI INC DPTR SJMP NEXT

MAIN:

; Timer 1, mode2 ; Baud rate 9600bps ; Start timer ;Serial mode 1 ;Pointer for message string ; Access code memory for message ;If null character, stop txn.

NEXT:

STOP: SEND: WAIT:

; SBUF = A, data to Txr ; Wait for Txn Flag = 1 ; Clear Txn Flag for next txn ;For next character

MESSAGE: DB Hello world,0 END

Department of ECE, VKCET

Page 32

08.601 Microcontroller Based System Design


Program 2: Receive a string of message (end of message is Carriage Return - ASCII is 0DH) from RxD (P3.0) and send it to TxD (P3.1) ORG 0 LJMP MAIN ORG 30H MOV TMOD,#00100000B MOV TH1,#0FDH MOV TL1,TH1 SETB TR1 MOV SCON,#01010000B MOV R0,#40H ACALL RECEIVE MOV A,R7 MOV @R0,A INC R0 CJNE A,#0DH,NEXT_RXR MOV R0,#40H MOV A,@R0 ACALL SEND CJNE A,#0DH,SEND_CHAR SJMP CONTINUE INC R0 SJMP NEXT_TXR JNB RI,RECEIVE CLR RI MOV R7,SBUF RET MOV SBUF,A JNB TI,WAIT CLR TI RET END
Page 33

MAIN:

CONTINUE: NEXT_RXR:

; SM 1 & REN = 1 ; RAM address to store message

;0DH is ASCII for CR

NEXT_TXR:

SEND_CHAR:

RECEIVE:

SEND: WAIT:

Department of ECE, VKCET

08.601 Microcontroller Based System Design


ADC interfacing Analog to digital converters are widely used for data acquisition Physical signals like temperature, velocity, pressure, light etc. are converted into electrical signals using transducers and these signals are processed by digital system by converting to digital data using ADC ADC with n-bit resolution has 8, 10, 12, 16 or 24 bit digital data output Resolution determines step-size of the signal to convert Conversion time of ADC is defined as the time taken to convert one instant analog input to its corresponding digital data Different type ADCs; parallel and serial where parallel, data output is in parallel form and in serial type data in serial form ADC0800 series is common type ADC from National semiconductors, 8-bit parallel type ADC with 100s conversion time ADC0804 Chip Pin out diagram:

Department of ECE, VKCET

Page 34

08.601 Microcontroller Based System Design


Functions of pins: Pins Functions CS Active low input, if low chip is active else chip is off RD Active low input also called output enable (OE), if it is low converted digital output is available in data bus D7 to D0 WR Active low input, if it is high to low ADC initialize for conversion and if it is low to high ADC starts conversion. So this pin is also called start of conversion (SOC) INTR Active low output, this will be high after ADC initialize and becomes low after conversion time of ADC. This pin can be use to interrupt microcontroller to read digital data from ADC. This pin is also called end of conversion (EOC) CLKin Input pin to apply external clock source. To use internal clock source, this and CLK R pins are used to connect frequency determining elements R an C CLK R To use internal clock source R is connected across this and CLK in pin, the clock frequency is f = 1/ (1.1 RC). If external clock is using this pin is at floating Vin(+) and Differential analog inputs. To connect single ended input, Vin(-) = 0V Vin(-) AGND Analog ground Vcc and Power supply and digital ground DGND Vref/2 Reference voltage, it determines the range of input analog voltage and also stepsize D7 to D0 Digital data output and output digit value is Dout = Vin / step size

Department of ECE, VKCET

Page 35

08.601 Microcontroller Based System Design


Relation between Vref and step size: Vref/2 (V) Vin (V) No connection 0 to 5V 2 0 to 4V 1.5 0 to 3V 1.28 0 to 2.56V ADC0804 timing diagram:

Step size (mV) 5/256 = 19.53 4/255 = 15.62 3/256 = 11.71 2.56/256 = 10

Department of ECE, VKCET

Page 36

08.601 Microcontroller Based System Design


Timing and electrical characteristics:

ADC0804 interfacing Interfacing circuit

Department of ECE, VKCET

Page 37

08.601 Microcontroller Based System Design


Programs: Prrogram to convert analog signal to digital data and display the data on LEDs connected to port 0 Solution: Circuit diagram:

(Xtal and RST connections are not shown) Algorithm: 1. Start 2. Set Port 1 as input 3. Set EOC (INTR) pin as 1 4. Set SOC (WR) pin as 1 5. Set OE (RD) pin as 1 6. Clear SOC (WR) pin to 0 7. Wait 150ns 8. Set SOC (WR) pin as 1 9. If EOC (INTR) pin is high, wait in this step, else go to next step 10. Clear OE (RD) signal 11. Read digital data from Port 1 12. Write digital data to Port 0 13. Go to step 6 for repeat the process (Assume crystal frequency to 8051 is 12 MHz)
Department of ECE, VKCET Page 38

08.601 Microcontroller Based System Design


OE SOC EOC IN_DATA OUT_DATA BIT BIT BIT EQU EQU P2.7 P2.6 P2.5 P1 P0

ORG 0 LJMP MAIN ORG 30H MAIN: MOV P1, #FFH SETB EOC SETB SOC SETB OE CONTINUE: CLR SOC NOP NOP SETB SOC WAIT: JB EOC, WAIT CLR OE MOV A, IN_DATA MOV OUT_DATA, A SJMP CONTINUE

Department of ECE, VKCET

Page 39

08.601 Microcontroller Based System Design


DAC interfacing Counter part of ADC Digital to analog converter converts digital data to analog voltage/current signal Basically two types: binary weighted and R/2R ladder Most familiar is R/2R ladder type DAC0808 DAC0808 chip Some features: 8-bit monolithic Full scale output current settling time of 150 ns No reference current (IREF) trimming is required for most applications since the full scale output current is typically 1 LSB of 255 IREF/256. Interface directly with popular TTL, DTL or CMOS logic levels Low power consumption: 33 mW @ 5V Pin out diagram:

Functions of pins: Pins Functions Vcc & GND Power supply pins VEE -ve power supply (-12V) IoAnalog current output A1(MSB) to Digital input data lines A8(LSB) Vref(+) and Reference voltage to provide reference current Iref Vref(-) Compensation Compensation capacitor for ve power supply

Department of ECE, VKCET

Page 40

08.601 Microcontroller Based System Design


Output current

Where R14 is the resistance connected to pin 14 DAC0808 interfacing circuit: The output of DAC0808 is current and have ripples Output require I-V converter followed by LPF Circuit diagram:

Vout = Io Rf For LPF RC value can be determined by either the maximum frequency of the required analog output or by the data transfer rate of 8051

Department of ECE, VKCET

Page 41

08.601 Microcontroller Based System Design


Square wave and rectangular wave generation: By sending a data corresponding to peak amplitude and zero continuously Frequency of wave depends on delay program Program: ORG 0 MAIN: MOV P1, #00 ACALL DELAY MOV P1, #FFH ACALL DELAY SJMP MAIN DELAY: WAIT: MOV R0, #2CH DJNZ R0, WAIT RET END The peak amplitude of the signal is Io Rf * 255/256 = 9.96V Ton = Toff = 100s, then frequency of the square wave is 5 kHz

Using different delay programs rectangular wave can be generated. Saw tooth and triangular wave generation: By sending data from 00H to FFH continuously with one step size. The frequency and peak amplitude of the signal are maximum value if there is no delay introduced between each step Frequency can be changed by including required delay between each step. Peak voltage of the wave is reduced by changing the maximum data. To generate triangular wave send data from 00H to FFH for rising and FFH to 00H for falling with one step size. Similar to other waves, the frequency and amplitude can change.

Department of ECE, VKCET

Page 42

08.601 Microcontroller Based System Design


Programs: a) Saw tooth wave MAIN : REPEAT: ORG 0 CLR A MOV P1, A INC A SJMP REPEAT END ORG 0 CLR A MOV P1, A INC A CJNE A, #FF, INCREMENT MOV P1, A DEC A CJNE A, #00, DECREMENT SJMP INCREMENT END

b) Triangular wave: MAIN: INCREMENT :

DECREMENT:

Applications: Temperature measurement system: The basic block diagram of temperature measurement system is:

Sensor are thermostats, thermisters or any other temperature sensors Usually LM34/35 IC temperature sensors are used; whose output voltage is directly proportional to the temperature as given below: For LM35 temperature range -55 to 150oC, 10mV/oC sensitivity Signal conditioner is either amplifier, I-V or V-I converter etc. to provide suitable input signal to ADC ADC converts analog signal corresponding to temperature into digital data Microcontroller controls ADC and acquire digital data corresponding to temperature and process it to get a suitable signal to display
Page 43

Department of ECE, VKCET

08.601 Microcontroller Based System Design


LED (either LEDs or 7 segment display) or LCD are used to display the measure quantity of temperature Circuit diagram:

Signal conditioning is not used Program to read data corresponding to temp and display its BCD data. (Assume that calibrated output is available at ADC output: ie for 0 oC data is 0000 0000, for 10 oC data is 0000 1010, for 50oC data is 0011 0010, and so on..) OE SOC EOC BIT BIT BIT P2.0 P2.1 P2.2

MAIN: CONTINUE:

ORG 0 MOV P1, #FFH SETB EOC ACALL ADC_START ACALL ADC_READ MOV R0, A ACALL HEX_BCD MOV P3, A CONTINUE

; Hex to BCD conversion ; Display


Page 44

Department of ECE, VKCET

08.601 Microcontroller Based System Design


ADC_START: SETB SOC SETB OE CLR SOC SETB SOC RET JB EOC, ADC_READ CLR OE MOV A, P1 RET MOV B, #0AH DIV AB MOV R7, B ; Lower digit MOV B, #0AH DIV AB MOV A, B ; Upper byte SWAP A ORL A, R7 ; Packing BCD RET

ADC_READ:

HEX_BCD:

Department of ECE, VKCET

Page 45

08.601 Microcontroller Based System Design


Stepper motor interfacing Stepper motor translates electrical pulses into mechanical motion Possible to precise control speed and position without feedback sensors Applications: Computer peripherals (Hard disk, CD, FD, Printer, Plotter etc.) Business machines (Card reader, Type writer, Copy machine, etc.) Control system Machine tools (Milling machines, drilling machines, etc) Constructional details: Consists rotor (Permanent magnet) surrounded by stator

Different views:

Department of ECE, VKCET

Page 46

08.601 Microcontroller Based System Design

Four stator motor also called four-phase or uni-polar stepper motor Total 6 leads: 4 leads for stator windings and 2 for center tapped common windings

As the sequence of pulse is applied to each stator winding, the rotor will switch to next step due to the flux linkage across the stator and rotor Rotor of conventional motors runs freely, but stepper motors rotor moves in a fixed repeatable increment, allows a precise position The most common stepper motors have four stator windings that are paired with a center-tapped common. This type of stepper motor is commonly referred to as a four-phase stepper motor The center tap allows a change of current direction in each of two coils when a winding is grounded, thereby resulting in a polarity change of the stator.

Department of ECE, VKCET

Page 47

08.601 Microcontroller Based System Design


Direction of motion and position of rotor: Full step 4 step motion (Wave drive)

Full step 4-step motion (Normal or 2-phase)

Half step 8-step motion (mixed)

Step sequences are different with different degree of precision

Department of ECE, VKCET

Page 48

08.601 Microcontroller Based System Design


Step angle Depends on internal construction; number of teeth on the stator and the rotor.

Consider a motor; Stator with 8 teeth and rotor with 6 teeth, have 15o step angle Step angle is minimum degree of rotation associated with a single step Different motor have different step angle Standard angles and steps per revolution (or 360 degree) are: 0.72 500 1.8 200 2.0 180 5.0 72 7.5 48 15 24 Steps per second and rpm relationship is:

After completing every four steps, the rotor moves only one tooth pitch The smaller the step-angle, the more teeth the motor passes Eg. In a stepper motor with 200 steps per revolution, its rotor has 50 teeth since 4 x 50 = 200 steps are needed To allow for finer resolutions, all stepper motors allow what is called an 8-step switching sequence. Its also called half-stepping, since each step is half of the normal step angle
Department of ECE, VKCET Page 49

08.601 Microcontroller Based System Design


Full step 4-step sequence (clock wise) Step 1 2 3 4 Winding Winding Winding Winding A B C D 1 0 0 1 0 1 1 0 0 0 1 1 1 1 0 0

Full step 4-step wave drive sequence (clock wise) Step 1 2 3 4 Winding Winding Winding Winding A B C D 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0

Department of ECE, VKCET

Page 50

08.601 Microcontroller Based System Design


Half step 8-step sequence (clock wise) Combination of normal and wave step sequence Have finer resolution

Step 1 2 3 4 5 6 7 8

Winding Winding Winding Winding A B C D 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 1 1 1 0 0 0 0

Motor speed Measure in steps per second; function of switching rate Determined by delay introduced between each step Eg. Let step angle of motor is 7.5 degree. If required RPM is 20, then steps per second = 20 x 48 /60 = 16 Therefore for one step 1/16 = 62.5ms is required Then pulse duration of a step sequence signal is 62.5ms
Department of ECE, VKCET Page 51

08.601 Microcontroller Based System Design


Holding torque Motor shaft at zero rpm, amount of torque from an external source to break away the shaft from the position and it is function of applied power Types of stepper motor Universal (eight leads) Unipolar (6 leads) Bipolar (4 leads) Motor drivers To avoid back emf from motor winding To provide higher current to drive motor Either by Darlington pair transistors or driver ICs ULN2003, ULN2803, L298 etc. 8051 and stepper motor interfacing circuit and programming

Xtal and RST connections are not shown

Department of ECE, VKCET

Page 52

08.601 Microcontroller Based System Design


Program to rotate the stepper motor continuously in clock-wise direction (full step 4-step wave drive sequence) Solution: Step sequence as per the connection on circuit diagram Winding A Winding B Winding C Winding D Step (P2.0) (P2.3) (P2.2) (P2.1) 1 0 0 0 1 0 2 0 0 1 0 3 0 1 0 0 4 1 0 0 Program: ORG 0 MAIN: MOV TMOD,#01H MOV DPTR,#FS_WAVE MOV R0,#04H REPEAT: CLR A MOVC A,@A+DPTR MOV P2,A ACALL DELAY_1S INC DPTR DJNZ R0,REPEAT SJMP MAIN DELAY_1S: WAIT_1S: MOV R7,#14H ;50ms x 20 = 1sec delay LCALL DELAY DJNZ R7,WAIT_1S RET MOV TH0,#4CH ;Higher byte count value for 50ms MOV TL0,#01H ;Lower byte count value for 50ms SETB TR0 JNB TF0,WAIT CLR TR0 CLR TF0 RET DB 01H,02H,04H,08H ;Step sequence END
Page 53

DELAY:

WAIT:

FS_WAVE:

Department of ECE, VKCET

08.601 Microcontroller Based System Design


Program to rotate stepper motor in anti-clock wise direction when P1.0 is 0, else rotate in clock wise direction (use half step 8-step sequence) Solution: Sequence as per the connection in circuit diagram: Step 1 2 3 4 5 6 7 8 Winding Winding Winding Winding A B C D 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0

Program: MAIN: CONTINUE: ORG 0 SETB P1.0 MOV TMOD,#01 MOV C,P1.0 JC C_WISE MOV DPTR,#ACW SJMP STRT MOV DPTR,#CW MOV R0,#08H CLR A MOVC A,@A+DPTR MOV P2,A ACALL DELAY_1S JNC DECR INC DPTR DJNZ R0,REPEAT SJMP CONTINUE DEC DPL SJMP NEXT ;P1.0 as input ;Timer 0 for delay ;Status of P1.0 to CY ;If P1.0 is 1, go to label C_WISE for CW ;rotation ;Pointer for ACW step sequence ;Pointer for CW step sequence ;Counter for half-step sequence

C_WISE: STRT: REPEAT:

;If CY =0 go to label DECR

NEXT: DECR:

Department of ECE, VKCET

Page 54

08.601 Microcontroller Based System Design


DELAY_1S: WAIT_1S: MOV R7,#14H LCALL DELAY DJNZ R7,WAIT_1S RET MOV TH0,#4CH MOV TL0,#01H SETB TR0 JNB TF0,WAIT CLR TR0 CLR TF0 RET DB 09H, 01H, 03H, 02H, 06H, 04H, 0CH DB 08H END

DELAY:

WAIT:

;Step sequence CW: ACW:

Program to rotate stepper motor with 1.8o step angle, at a speed of 6 RPM in 360o clockwise for 2 times and 11 RPM in 360 o anti-clock wise direction for 4 times continuously (Xtal frequency of 8051 is 11.0592MHz ) Solution: Step sequence for normal drive as per the connection shown in the circuit is: For clockwise rotation: Step Winding A Winding B Winding C Winding D (P2.0) (P2.3) (P2.2) (P2.1) 1 1 0 0 1 1 2 0 0 1 0 3 0 1 1 0 4 1 1 0 o For RPM = 6, Step angle = 1.8 Steps/sec = 6 x 200/60 = 20 For one step 1/20 = 50ms Design 50ms delay program After full step pulses 1.8 x 4 = 7.2 degree rotation takes place, then for 360 degree, repeat the process for 360/7.2 = 50 times Continue the above process for two times
Page 55

Department of ECE, VKCET

08.601 Microcontroller Based System Design


Anti-clock wise rotation For anti-clockwise rotation (Half step): Step 1 2 3 4 5 6 7 8 Winding Winding Winding Winding A B C D 1 0 0 0 0 0 1 1

Program: MAIN: CONTINUE: REPEAT: NEXT:

1 0 0 1 0 0 1 1 0 0 1 0 0 1 1 0 0 1 0 0 1 0 0 0 For RPM = 11, Step angle = 1.8o Steps/sec = 11 x 200/60 = 36.66 For one step 1/36.66 = 27.27ms (use 25ms) Design 25ms delay program After half step pulses 1.8 x 8 = 14.4 degree rotation takes place and for 360 degree repeat the process for 360/14.4 = 25 times Continue the above process for 4 times ORG 0 MOV TMOD,#01 MOV R2,#02H ;counter for 2 times cw rotation MOV R1,#32H ;counter for 50 times to rotate 360o MOV R0,#04H MOV DPTR,#FS_CW CLR A MOVC A,@A+DPTR MOV P2,A ACALL DELAY ;25ms delay ACALL DELAY ;25ms delay INC DPTR DJNZ R0,NEXT DJNZ R1,REPEAT DJNZ R2,CONTINUE MOV R2,#04 ;Count for 4 times acw rotation MOV R1,#19H ;count for 25 times to rotate 360o
Page 56

CONTINUE1:

Department of ECE, VKCET

08.601 Microcontroller Based System Design


REPEAT1: NEXT1: MOV R0,#08H MOV DPTR,#FS_ACW CLR A MOVC A,@A+DPTR MOV P2,A ACALL DELAY INC DPTR DJNZ R0,NEXT1 DJNZ R1,REPEAT1 DJNZ R2,CONTINUE1 SJMP MAIN MOV TH0,#0A6H MOV TL0,#00H SETB TR0 JNB TF0,WAIT CLR TR0 CLR TF0 RET ;half step sequence count

;25ms

DELAY:

;Count value for 25ms

WAIT:

;Sequences FS_CW: FS_ACW:

DB 09H,03H,06H,0CH DB 0CH,04H,06H,02H,03H,01H,09H,08H END

Department of ECE, VKCET

Page 57

08.601 Microcontroller Based System Design


LCD Interfacing LCD is wide spreading display conventional to LEDs, due to: a) Low cost b) Ability to display numbers, characters and graphics c) Display refreshing task is not required d) Easy to program for characters and graphics The pins and its functions of 16 x 2 character LCD module:

Timing diagram:

Department of ECE, VKCET

Page 58

08.601 Microcontroller Based System Design


Timing characteristics:

Note: DDRAM Display Data RAM Where I/D = 1, increment DDRAM address, cursor moves to right I/D = 0, decrement DDRAM address, cursor moves to left SH = 1, shift entire display according to I/D
Department of ECE, VKCET Page 59

08.601 Microcontroller Based System Design


SH = 1, no shift of entire display CGRAM Character Generator RAM D = 1, entire display turn on D = 0, display turn off, but display data remains in DDRAM C = 1, cursor turn on C = 0, cursor disappear, but I/D preserves its data B = 1, cursor blink on B = 0, cursor blink off

AC Address Counter

DL = 1, 8-bit bus mode DL = 0, 4 bit bus mode N = 1, 2 line display mode N = 0, 1 line display mode F = 1, 5 x 10 dots font F = 0, 5 x 7 dots font Address for DDRAM: AC6 to AC0 are:

The for position 1 in line 1 the command byte is 1000 0000 = 80H, for position 2 : 1000 0001 = 81H .
Department of ECE, VKCET Page 60

08.601 Microcontroller Based System Design


Program 1 Interface 16 x 2 LCD and display a message (maximum length of the message should be less than 32) Solution: Circuit diagram

Xtal, RST and Power supply connection are not shown Program: Algorithm: Main program Steps: 1. Start 2. Write command byte to LCD for function setting. Command byte: 0011 1000 = 38H 3. Wait minimum 40s 4. Write command byte to LCD for function setting. Command byte: 0000 0001 = 01H 5. Wait minimum 1.64ms 6. Write command byte to LCD for display on and cursor blinking. Command byte: 0000 1111 = 0FH 7. Wait minimum 40s
Department of ECE, VKCET Page 61

08.601 Microcontroller Based System Design


8. Write command byte to LCD for entry mode: left entry (increment cursor to right). Command byte: 0000 0110 = 06H 9. Write command byte to LCD for DDRAM address. Command byte: 1000 0000 = 80H 10. Wait minimum 40 s 11. Point starting address of LUT by DPTR, Counter R0 = 0 for getting the length of message 12. A [DPTR], ASCII of character for message to A 13. If A = 0 (ASCII of NULL character) go to step 20, else next step 14. If R0 =16 go to next step, else go to step 16 15. Write command byte to LCD for DDRAM address. Command byte: 1100 0000 = C0H 16. Write data byte (ASCII) to LCD 17. Wait minimum 40s 18. DPTR = DPTR +1, R0=R0+1 19. Go to step 12 20. Stop LCD command write Steps: 1. Clear E 2. Move command byte to DB0-DB7 3. Clear RS and RW 4. Set E to 1 5. Wait 40 s 6. Clear E 7. Set RS and RW 8. Return to main program

Department of ECE, VKCET

Page 62

08.601 Microcontroller Based System Design


LCD data write Steps: 1. Move data byte to DB0-DB7 2. Set RS to 1 and clear RW 3. Set E to 1 4. Wait 40 s 5. Clear E 6. Clear RS and set RW to 1 7. Return to main program Program: RS BIT P2.0 RW BIT P2.1 E BIT P2.2 LCD_BUS EQU P1 ORG 0 MOV TMOD,#01H MOV A,#38H LCALL CMD_WRITE LCALL DELAY_40u MOV A,#01H LCALL CMD_WRITE LCALL DELAY_2m MOV A,#0FH LCALL CMD_WRITE LCALL DELAY_40u MOV A,#06H LCALL CMD_WRITE LCALL DELAY_40u MOV A,#80H LCALL CMD_WRITE LCALL DELAY_40u MOV R0,#00H MOV DPTR,#MESSAGE CLR A MOVC A,@A+DPTR CJNE A,#0, DISPLAY
Page 63

MAIN:

NEXT:

Department of ECE, VKCET

08.601 Microcontroller Based System Design


STOP: DISPLAY: LINE_2: SJMP STOP CJNE R0,#10H,LINE_1 MOV R1,A MOV A,#0C0H LCALL CMD_WRITE LCALL DELAY_40u MOV A,R1 LCALL DATA_WRITE LCALL DELAY_40u INC R0 INC DPTR SJMP NEXT CLR E MOV LCD_BUS,A CLR RW CLR RS SETB E LCALL DELAY_40u CLR E SETB RW SETB RS RET MOV LCD_BUS,A SETB RS CLR RW SETB E LCALL DELAY_40u CLR E CLR RS SETB RW RET MOV TH0,#0FFH MOV TL0,#0DBH SETB TR0
Page 64

LINE_1:

CMD_WRITE:

DATA_WRITE:

DELAY_40u:

Department of ECE, VKCET

08.601 Microcontroller Based System Design


WAIT_40u: JNB TF0,WAIT_40u CLR TR0 CLR TF0 RET

Department of ECE, VKCET

Page 65

08.601 Microcontroller Based System Design


DELAY_2m: MOV TH0,#0F8H MOV TL0,#0CCH SETB TR0 JNB TF0,WAIT_2m CLR TR0 CLR TF0 RET DB"ECE Department, VKCET",0 END

WAIT_2m:

MESSAGE:

Keyboard Interfacing Keyboard is widely used input device along with output devices like LCD, seven segment displays etc To interface keyboard, understanding keyboard mechanism, key pressing and detecting is essential Interfacing keyboard to 8051 Interfacing program must guard against two factors: a. Human factors: 1. More than one key pressed 2. Key pressed and held 3. Rapid key press and release b. Key factors Bouncing of key contacts Key configurations Commercially available key configurations are: 1. Lead per keyboard

16 input lines are required to read the keys


Department of ECE, VKCET Page 66

08.601 Microcontroller Based System Design


2. Matrix keyboard

4 input and 4 output lines are required to read the keys 3. Coded key board

Expensive, but better to read multiple key press

Department of ECE, VKCET

Page 67

08.601 Microcontroller Based System Design


Scanning and identify the key Consider the connection of 4x4 matrix keyboard with 8051

Scanning and identifying steps Ground all rows , P1.3 P1.0 =0000 Read all columns, if data read P2.3 P2.0 = 1111, no key is pressed Continue above process until a key is pressed If P2.3 P2.0 = 1101, one of the key in column 1 is pressed To identify the key in the column, ground row 0 only, P1.3 P1.0 = 1110 Read columns, if P2.3 P2.0 = 1111 , no key in the row 0 is pressed Continue the above process until pressed is detected If key 5 is pressed, then read data during scanning is P2.3 P2.0 = 1101 Identifying step; grounding P1.3 P1.0 = 1101, read data becomes P2.3 P2.0 = 1101

Department of ECE, VKCET

Page 68

08.601 Microcontroller Based System Design


Programs Program to read a key from 4 x 4 matrix keyboard and display its value to seven segment display ORG 0 LJMP MAIN ORG 30H MAIN: MOV TMOD,#00000001B MOV P2,#0FFH K1: MOV P1,#0 MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,K1 ACALL DELAY MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,OVER SJMP K2 ACALL DELAY MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,OVER1 SJMP K2 MOV P1,#00001110B MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,ROW_0 MOV P1,#00001101B MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,ROW_1 MOV P1,#00001011B MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,ROW_2 MOV P1,#00000111B
Page 69

K2:

OVER:

OVER1:

Department of ECE, VKCET

08.601 Microcontroller Based System Design


MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,ROW_3 SJMP K2 MOV P1,#00001101B MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,ROW_1 MOV P1,#00001011B MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,ROW_2 MOV P1,#00000111B MOV A,P2 ANL A,#00001111B CJNE A,#00001111B,ROW_3 SJMP K2 DB 11000000B,11111001B,10100100B,10110000B DB 10011001B,10010010B,10000010B,11111000B DB 10000000B,10011000B,10001000B,10000011B DB 11000110B,10100001B,10000110B,10001110B MOV TH0,#4CH MOV TL0,#01H SETB TR0 JNB TF0,WAIT CLR TR0 CLR TF0 RET END

KEYCODE_0: KEYCODE_1: KEYCODE_2: KEYCODE_3: DELAY:

WAIT:

Department of ECE, VKCET

Page 70

08.601 Microcontroller Based System Design


PIC Microcontrollers Different 8 bit microcontrollers: 1970s: Motorolas 6800 1980s: Intels 8051 1990s: Microchips PIC Cheap, small and practical microcontroller which was both easy to use and program PIC Peripheral Interface Controller Why PIC popular? Low cost Wide availability Large user base Extensive collection of application notes Availability of low cost or free development tools Serial programming capability. .Different PIC core architecture 1. Baseline Core Devices (ex:PIC12C50x, PIC16C5x) 12 bit wide code memory, Tiny two level deep call stack. 33 instructions PIC10, PIC12 & PIC16 devices 6 pin to 40 pin packages. 2. Mid-Range Core Devices (ex:PIC12C50x, PIC16C5x) 14 bit wide code memory Improved 8 level deep call stack 35 instructions Increased opcode width allows Addressing of more memory PIC12 and PIC16 devices. 3. PIC17 High End Core Devices (Ex:PIC17C4x,PIC17C7xx) Never became popular and superseded by the PIC18 architecture. 16 bit wide opcodes (allowing many new instructions) : 58 instructions 16 level deep call stack. Packages of 40 to 68 pins. A memory mapped accumulator Read access to code memory (table reads) Direct register to register moves An external program memory interface to expand the code space An 8bit x 8bit hardware multiplier
Department of ECE, VKCET Page 71

08.601 Microcontroller Based System Design


Auto-increment/decrement addressing

. 4. PIC18 High End Core Devices (ex:PIC18Cxxx) New high end PIC architecture It inherits most of the features and instructions of the 17 series, 77 instructions, much deeper call stack (31 levels deep) The call stack may be read and written Offset addressing mode A new indexed addressing mode in some devices 5. PIC24 and dsPIC 16 bit Microcontrollers Architectures differ significantly from prior models. dsPICs are Microchip's newest family (started in 2004) Digital signal processing capabilities. Microchip's first inherent 16-bit (data) microcontrollers. Hardware MAC (multiply-accumulate) Barrel shifting Bit reversal (16x16)-bit multiplication Other digital signal processing operations. Can be efficiently programmed in C . .PIC16F87X Microcontrollers Mid range core 28/40-Pin 8-Bit CMOS FLASH Microcontrollers PIC16F873, PIC16F874 (28 pin ICs) PIC16F876 and PIC16F877 devices (40 pin ICs) Microcontroller Core Features: High performance RISC CPU Only 35 single word instructions All single cycle instructions except for program branches which are two cycle Operating speed: DC - 20 MHz clock input DC - 200 ns instruction cycle Up to 8K x 14 words of FLASH Program Memory, Up to 368 x 8 bytes of Data Memory (RAM) Up to 256 x 8 bytes of EEPROM Data Memory Pin out compatible to the PIC16C73B/74B/76/77
Department of ECE, VKCET Page 72

08.601 Microcontroller Based System Design


Interrupt capability (up to 14 sources) Eight level deep hardware stack Direct, indirect and relative addressing modes Power-on Reset (POR) Power-up Timer (PWRT) and Oscillator Start-up Timer (OST) Watchdog Timer (WDT) with its own on-chip RC oscillator for reliable operation Programmable code protection Power saving SLEEP mode Selectable oscillator options Low power, high speed CMOS FLASH/EEPROM technology Fully static design In-Circuit Serial Programming (ICSP) via two pins Single 5V In-Circuit Serial Programming capability In-Circuit Debugging via two pins Processor read/write access to program memory Wide operating voltage range: 2.0V to 5.5V High Sink/Source Current: 25 mA Commercial, Industrial and Extended temperature ranges Low-power consumption: < 0.6 mA typical @ 3V, 4 MHz 20 A typical @ 3V, 32 kHz < 1 A typical standby current Peripheral Features: Timer0: 8-bit timer/counter with 8-bit prescaler Timer1: 16-bit timer/counter with prescaler, can be incremented during SLEEP via external crystal/clock Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler 5 IO ports Two Capture, Compare, PWM modules Capture is 16-bit, max. resolution is 12.5 ns Compare is 16-bit, max. resolution is 200 ns PWM max. resolution is 10-bit 10-bit multi-channel Analog-to-Digital converter Synchronous Serial Port (SSP) with SPI (Mastermode) and I2C (Master/Slave) Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) with 9-bit address detection

Department of ECE, VKCET

Page 73

08.601 Microcontroller Based System Design


. Parallel Slave Port (PSP) 8-bits wide, with external RD, WR and CS controls (40/44-pin only) Brown-out detection circuitry for Brown-out Reset (BOR)

Department of ECE, VKCET

Page 74

08.601 Microcontroller Based System Design


Pin out diagram of PIC16F877

. Pin details:

Department of ECE, VKCET

Page 75

08.601 Microcontroller Based System Design

Department of ECE, VKCET

Page 76

08.601 Microcontroller Based System Design

Department of ECE, VKCET

Page 77

08.601 Microcontroller Based System Design


.Architecture of PIC16F877 .

Department of ECE, VKCET

Page 78

08.601 Microcontroller Based System Design


CPU ALU 8 bit Perform operation with temporary working register (W) and any other register file W register 8 bit register which hold one operand and result after an ALU operation Status register

8 bit Contains the arithmetic status of ALU To select register bank File Selection Register (FSR) Data pointer, used for indirect addressing in the whole register file IO ports Memory organization Three memory blocks: 1. Program memory (Flash) 2. Data memory (RAM) (Separate bus for both memory) 3. Data EEPROM Program memory mapping 8k x 14 word ROM (Flash or EEPROM)

Department of ECE, VKCET

Page 79

08.601 Microcontroller Based System Design

Program Counter (PC) .Keeps track of the program execution by holding the address of the current instruction .PCL and PCLATH SFRs for accessing 13 bit PC

PCL (8 bit) is readable and writable PCH (5 bit) not readable, but indirectly writable by PCLATH least 5 bits Stack memory The stack space is not part of either program or data space and the stack pointer (SP) is not readable or writable. Stack is a special block of RAM memory The PC is PUSHed onto the stack when a CALL instruction is executed, or an interrupt causes a branch.
Department of ECE, VKCET Page 80

08.601 Microcontroller Based System Design


The stack is POPed in the event of a RETURN, RETLW or a RETFIE instruction execution. The stack operates as a circular buffer. This means that after the stack has been PUSHed eight times, the ninth push overwrites the value that was stored from the first push. The tenth push overwrites the second push (and so on). Reset vector Each time the main program execution starts at address 0000 - Reset Vector. Interrupt vector The address 0004 is reserved for the interrupt service routine (ISR).

Department of ECE, VKCET

Page 81

08.601 Microcontroller Based System Design


Data memory mapping 368 byte RAM is partitioned to 4 register banks and contains GPRs and SFRs

Bank 2 GPRs and SFRs

Department of ECE, VKCET

Page 82

08.601 Microcontroller Based System Design


Bank 3 GPRs and SFRs

Summary of Data memory

By RP0 and RP1 bits in STATUS register any one bank can be selected Using IRP bit in STATUS register direct or indirect

Department of ECE, VKCET

Page 83

08.601 Microcontroller Based System Design

Data EEPROM (256 bytes) Similar to Program flash, but r/w operation is byte , where in program memory it is word r/w operation Write operation can be performed during normal power source Vdd SFRs for accessing Data EEPROM EEADR :Hold data EEPROM address EEDATA : Hold the data from/to the data EEPROM EEDATA : EEDATH Hold 14 bit data from flash memory EEADRH:EEADR hold 13 bit flash memory address EECON1 & EECON2 : Control registers for configure IO Ports 1. Port A and TRISA Registers PORTA is a 6-bit wide, bi-directional port 5 analog inputs and timer 0 clock input are multiplexed with this pins The corresponding data direction register is TRISA. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output 2. Port B and TRISB registers PORTB is an 8-bit wide, bi-directional port. The corresponding data direction register is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output
Department of ECE, VKCET Page 84

08.601 Microcontroller Based System Design


Three pins of PORTB are multiplexed with the Low Voltage Programming function: PGM, PGC and PGD Each of the PORTB pins has a weak internal pull-up and can be turn on or off 3. PORTC and the TRISC Register PORTC is an 8-bit wide, bi-directional port. The corresponding data direction register is TRISC. Setting a TRISC bit (= 1) will make the corresponding PORTC pin an Clearing a TRISC bit (= 0) will make the corresponding PORTC pin an output The multiplexed pins to this port are:

4. Port D and TRISD PORTD is an 8-bit port with Schmitt Trigger input buffers. Each pin is individually configurable as an input o output. PORTD can be configured as an 8-bit wide microprocessor port (parallel slave port) by setting to PSPMODE. In this mode, the input buffers are TTL. 5. PORTE and TRISE Register PORTE has three pins which are individually configurable as inputs or outputs. These pins have Schmitt Trigger input buffers. The PORTE pins become the I/O control inputs for the microprocessor port Three analog inputs are multiplexed with this pins TIMER0 MODULE The Timer0 module timer/counter has the following features: 8-bit timer/counter Readable and writable
Department of ECE, VKCET Page 85

08.601 Microcontroller Based System Design


8-bit software programmable prescaler Internal or external clock select Interrupt on overflow from FFh to 00h Edge select for external clock TIMER1 MODULE The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L), which are readable and writable. The TMR1 Register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h. The TMR1 Interrupt, if enabled, is generated on overflow, which is latched in interrupt flag bit TMR1IF (PIR1<0>). Timer1 can operate in one of two modes: o As a timer o As a counter TIMER2 MODULE Timer2 is an 8-bit timer with a prescaler and a postscaler. It can be used as the PWM time-base for the PWM mode of the CCP module(s). The TMR2 register is readable and writable, and is cleared on any device RESET The Timer2 module has an 8-bit period register, PR2 Timer2 increments from 00h until it matches PR2 and then resets to 00h on the next increment cycle. PR2 is a readable and writable register. The PR2 register is initialized to FFH upon RESET.

Department of ECE, VKCET

Page 86

You might also like