0% found this document useful (0 votes)
56 views

Lab Manual Part 2 Updated

The document contains 6 code examples for 8051 microcontroller programs: 1. A code to blink LEDs with a delay. 2. A code for 8-bit binary up/down counting displayed on LEDs. 3. A code for 8-bit binary up/down counting using switch inputs. 4. A code for BCD up counting on a 7-segment display. 5. A code to display "1234" on a 4-digit 7-segment display using time multiplexing. 6. A code to interface an LCD in 8-bit communication mode.

Uploaded by

Pradyumna GR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Lab Manual Part 2 Updated

The document contains 6 code examples for 8051 microcontroller programs: 1. A code to blink LEDs with a delay. 2. A code for 8-bit binary up/down counting displayed on LEDs. 3. A code for 8-bit binary up/down counting using switch inputs. 4. A code for BCD up counting on a 7-segment display. 5. A code to display "1234" on a 4-digit 7-segment display using time multiplexing. 6. A code to interface an LCD in 8-bit communication mode.

Uploaded by

Pradyumna GR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Department of Electronics and Communication Engineering

8051 MICROCONTROLLER INTERFACE PROGRAMS

1. Write a 8051 code to blink LEDs with a specific delay

;*********************************************************************
************;;******************;

; Program to perform blink LED

;
;
;*********************************************************************
************;;***************
;***;******************
; Author : Dr. Sukesh Rao M.
; Email : [email protected] , [email protected]

; 26 FRC cable is connected to IO extension module and 10 pin FRC is


;used to connect LED module using P0.
; keil Monitor-51 driver is selected under debug settings*/

ORG 00H
MOV P0,#00H ; initialize the port 0 as 00h(output port)
UP: MOV A,#00H
MOV P0,A ; turn off the leds for a specific period using
LCALL DELAY ; delay function
MOV A,#0FFH
MOV P0,A ; turn off the leds for a specific period using
LCALL DELAY ; delay function
SJMP UP

DELAY: MOV R0,#0FFH ; MOV instruction takes 1 machine cycle


RPT2: MOV R1,#0FFH
RPT1: DJNZ R1,RPT1 ; DJNZ instruction takes 2 machine cycles
DJNZ R0,RPT2
RET
END

DELAY CALCULATION
Total machine cycles required to execute the delay procedure ={(2x255 + 2 + 1)x255}+ 1
= 130816
1 Machine cycle = 12 clock cycles ,
Clock frequency of 8051 = 11.0592MHz
Time Period = (1/11.0592M) = 0.09042us
1 machine cycle takes 0.09042us x 12 = 1.086us

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
Therefore, total delay produced by the delay procedure = total machine cycles x 1.086us
= 130816 x 1.086us = 0.142s
Schematic circuit
+5 V

2. Write a 8051 assembly code to perform 8 bit binary up-


count/down and display the count values on LED
;*********************************************************************
************;;******************;

; Program to perform binary up/down counting

;
;
;*********************************************************************
************;;***************
;***;******************
; Author : Dr. Sukesh Rao M.
; Email : [email protected] , [email protected]

; 26 FRC cable is connected to IO extension module and 10 pin FRC is


;used to connect LED module using P0.
; keil Monitor-51 driver is selected under debug settings*/

ORG 00H
MOV P0,#00H ; initialize the port 0 as 00h(output port)
MOV A,#00H
UP:MOV P0,A ; Turn on LED with corresponding value of A for
LCALL DELAY ; specific period using DELAY function
INC A ; increment the counter value
SJMP UP

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
DELAY: ; procedure to give delay
MOV R0,#0FFH ; MOV instruction takes 1 machine cycle
RPT2:MOV R1,#0FFH
RPT1:DJNZ R1,RPT1 ; DJNZ instruction takes 2 machine cycles
DJNZ R0,RPT2
RET
END

3. Write a 8051 assembly code to perform 8 bit binary up/down-


count using switches

;*********************************************************************
************;;******************;

; Program to perform binary up/down counting using switch control

;
;
;*********************************************************************
************;;***************
;***;******************
; Author : Dr. Sukesh Rao M.
; Email : [email protected] , [email protected]

; 26 FRC cable is connected to IO extension module and 10 pin FRC is


;used to connect LED module using P0.
; Assume that two switches (S1 and S2) are used to control Up and down
;count
; P3.2 and P3.3 on the ESA 51 board is used as up and down control
;switch
; keil Monitor-51 driver is selected under debug settings*/

ORG 00H
SETB P3.2
SETB P3.3
CLR A;
MOV P0,A;

CHECK_AGAIN: JNB P3.2, UP ; check S1


JNB P3.3, DOWN ; check S2
SJMP CHECK_AGAIN
UP: INCA
MOVP0,A
WAIT1: JNB P3.2, WAIT1 ; WAIT for key release
SJMP CHECK_AGAIN

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
DOWN: DEC A
MOV P0,A
WAIT2: JNB P3.3, WAIT2 ; WAIT for key release
SJMP CHECK_AGAIN
END

4. Write a code for 8051 to get BCD up count on single digit seven
segment display
;*********************************************************************
************;;******************;

; Program to display single digit value on common cathode seven


segment display

;
;
;*********************************************************************
************;;***************
;***;******************
; Author : Dr. Sukesh Rao M.
; Email : [email protected] , [email protected]

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
; 26 FRC cable is connected to IO extension module and 16 FRC is used
;to connect SEVEN SEGMENT module. P1.0 is activated to select one
segment and P0 as data line.
; keil Monitor-51 driver is selected under debug settings*/

UP:MOV DPTR,#UPVALUE
CLR P1.0 ; select first seven segment
MOV R0,#00 ;initial value to be displayed
MOV R2,#10 ;count=10
LCALL UPCOUNT ;calling procedure for upcount
SJMP UP

UPCOUNT:MOV A,R0 ;copying content of R0 to a


MOVC A,@A+DPTR ;copying content pointed by the location
A+DPTR to A
MOV P0,A ;displaying the value on seven segment
INC R0
LCALL DELAY ;calling procedure for delay
DJNZ R2,UPCOUNT
RET

UPVALUE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH ;values to be


displayed on seven segment

END

Schematic circuit diagram

5. Write an assembly language program to display “1234” on 4


digit seven segment display using time multiplexing technique
;*********************************************************************
************;;******************;

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
; Program to display 4 digit value on common cathode seven segment
display

;
;
;*********************************************************************
************;;***************
;***;******************
; Author : Dr. Sukesh Rao M.
; Email : [email protected] , [email protected]

; 4 segments are connected to common data line and activated for the
; fixed duration of time. The method is known as time multiplexing.
; 26 FRC cable is connected to IO extension module and 16 FRC is used
;to connect SEVEN SEGMENT module. P1.0 to P1.3 are used as select
lines and P0 as data line.
; keil Monitor-51 driver is selected under debug settings*/

MOV P0, # 00H


UP: MOV 20H,#01H ; digits to be displayed are stored in 20H onwards
MOV 21H,#02H
MOV 22H,#03H
MOV 23H,#04H
MOV R0,#20H; Memory poiner to read numbers
MOV R2,#04
MOV R1,#0FEH ; 11111110 – Sequence to select the segment (0 is active
signal)
MOV DPTR,#seven_seg_code
LCALL DISPLAY_SEG
SJMP UP
DISPLAY_SEG: MOV A,@R0 ; Read the number to be displayed
MOV P1,R1 ; pass the value of the select lines to the P1 ( 0 to 3)
MOVC A,@A+DPTR ; Read the seven segment code from lookup table
MOV P0,A ; Display value on the selected segment
LCALL DELAY ; delay for persistence of display
INC R0 ; Point the next number memory loc.
MOV A,R1 ; update the select line sequence
RL A ; Rotate bit left
MOV R1,A ; update rotated value in R1
DJNZ R2, DISPLAY_SEG ; Repeat for each segment
RET

seven_seg_code: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH ;values to be


displayed on seven segment

DELAY: MOV R4,#02H


WAIT2: MOV R3,#0FFH
WAIT1:DJNZ R3, WAIT1
DJNZ R4, WAIT2
RET
END
Schematic circuit

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering

6. Write an assembly language program to interface LCD in 8 bit


communication mode
;*********************************************************************************;;**********
********;******************
; Program to interface 16x2 LCD
;
;*********************************************************************************;;**********
********;******************
; Author : Dr. Sukesh Rao M.
; Email : [email protected] , [email protected]

; LCD is internally connected in ESA51 in 8 bit mode. The IO port configuration is same as given in the source code
; Make sure COM port is selected as per your system status under device manager
; keil Monitor-51 driver is selected under debug settings*/

LCD_data EQU P2
RS EQU P3.7
RW EQU P3.6

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
EN EQU P3.5
ORG 0000H
LJMP START
ORG 0020H
MSG1: DB 'NMAMIT',0FH ; Messages to be displayed stored in the program memory of the code
MSG2: DB 'ECE Dept.',0Fh ; Stored in ASCII format
ORG 0050H
START: LCALL INTI_LCD ; basic initialization of lcd, refer manual
MOV A,#80H ; command for choosing first line of LCD
LCALL CMD ; write command
MOV DPTR,#MSG1 ; memory pointer for MSG1
LCALL DISP_STRING ;display string on LCD
MOV A,#0C0H ; command for choosing second line
LCALL CMD ; write command
MOV DPTR,#MSG2 ; memory pointer for MSG2
LCALL DISP_STRING ; display string on LCD
HERE: SJMP HERE ; stop the IP/PC

INTI_LCD: MOV A,#3CH ;refer manual for the bit meaning


LCALL CMD ; manual can be downloaded from website for the perticular part number
of LCD
MOV A,#3CH
LCALL CMD
MOV A,#3CH
LCALL CMD
MOV A,#0EH
LCALL CMD
MOV A,#06H
LCALL CMD
MOV A,#01 ; clear display
LCALL CMD
RET

CMD: LCALL READY ; check the busy status of LCD


MOV LCD_data,A ; send the command to data line of LCD
CLR RS ; make RS=0, selectig command mode

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
CLR RW ; make RW=0 , write mode
SETB EN ; high to low on Enable line
CLR EN
RET

DISP: LCALL READY ; check the busy status of LCD


MOV LCD_data, A ; send the command to data line of LCD
SETB RS ; make RS=1, selecting display mode
CLR RW ; make RW=0, write mode
SETB EN ; high to low on Enable line
CLR EN
RET

READY: MOV LCD_data,#0FFH ; data lines are made input


CLR RS ; make RS=0 , command mode
SETB RW ; RW=1, read mode
WAIT: CLR EN ; low to high on enable line
SETB EN
JB 0A7H,WAIT ; check for the busy status on P1.7
RET

DISP_STRING:NOP
UP11: CLR A
MOVC A,@A+DPTR ;use lookup table to get ascii character
CJNE A,#0FH,SKIP5 ; check for the end of the character 0fh
RET
SKIP5: INC DPTR
LCALL DISP ; display character on LCD
SJMP UP11

END

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering

7. Write an assembly language program to interface ADC0804


with 8051 to perform analog to digital conversion
;*********************************************************************
************;;******************;******************

; Program to perform analog to digital conversion using ADC0804


;
;
;*********************************************************************
************;;***************

;***;******************
; Author : Dr. Sukesh Rao M.
; Email : [email protected] , [email protected]

; 8 bit result of ADC is displayed on the LED. The analog input is


provided by varying the potentiometer
; provided on the ADC0804 module.

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering
; 26 FRC cable is connected to IO extention module and 16 pin FRC
connector is used to connect ADC module and 10 bit FRC from P2 to LED
; keil Monitor-51 driver is selected under debug settings*/

CS EQU P1.7
RD_A EQU P1.6
WR_A EQU P1.5
INTR EQU P1.4
ADC_DATA EQU P0
LED_DISPLAY EQU P2
ORG 0000H
LJMP START
ORG 0050H
START: MOV ADC_DATA,#0FFH ; make P1 as input to read ADC output
MOV LED_DISPLAY,#0FH ; display value on LED
SETB INTR ; input port line for end of conversion (EOC)

REPEAT: LCALL ADC_READ


MOV LED_DISPLAY,A ; display on LED connected at P2
SJMP REPEAT

ADC_READ: CLR CS ;make CS=0


CLR WR_A ;make WR=0
LCALL DELAY
SETB WR_A ; WR=1
SETB CS ;CS=1 ; giving a pulse to start the ADC
conversion
HERE1: JB INTR, HERE1 ; EOC PIN POLLING / check for
end of conversion
CLR CS ;CS=0
CLR RD_A ; RD=0
nop
nop
MOV A,ADC_DATA ; read the ADC value to
A reg
SETB RD_A
SETB CS
RET
DELAY: MOV R1,#0FH
WAIT2: MOV R2,#0FFH
WAIT1:DJNZ R2, WAIT1
DJNZ R1, WAIT2
RET
END

System Design using Microprocessor/ Microcontroller Lab 16EC606


Department of Electronics and Communication Engineering

ADC timming Diagram

System Design using Microprocessor/ Microcontroller Lab 16EC606

You might also like