Use 8051 To Switch On and Off An LED Using A Toggle Switch
Use 8051 To Switch On and Off An LED Using A Toggle Switch
Use 8051 To Switch On and Off An LED Using A Toggle Switch
ORG 0000H
SJMP MAIN
ORG 0030H ; starting address of main label
MAIN:SETB P1.0 ; pin is used to switch on led
SETB P1.1 ; pin is used to switch off led
KEY1:JB P1.0,KEY2 ; continuously checking key press
ACALL DELAY ; debouncing technique
JB P1.0,KEY2
CLR P3.7 ; switch on the led
KEY2:JB P1.1,KEY1; continuously checking key press
ACALL DELAY ; debouncing technique
JB P1.1,KEY1
SETB P3.7; switch off the led
SJMP KEY1
;-------20ms delay for debouncing checking---------
DELAY:MOV R1,#24H
L2:MOV R3,#0FFH
L1:DJNZ R3,L1
DJNZ R1,L2
RET
END
1
2. Design a system to generate a Square wave using a counter.
ORG 0000H
SJMP MAIN
ORG 0030H
MAIN:MOV TMOD,#05H ;using counter
BACK:MOV TL0,#0FDH ; to generate 10Hz
MOV TH0,#4BH
SETB TR0
LOOP:JNB TF0,LOOP ; checking for flag bit
CLR TF0
CLR TR0
CPL P2.0
SJMP BACK
END
ORG 0000H
SJMP MAIN
ORG 000BH
SJMP TMS ;Short jump to timer 0 ISR
ORG 0030H
MAIN:MOV TMOD,#01H ;using timer 0 mode 1
MOV IE,#82H ; Enabling the timer 0 interrupt after 200 usecs
MOV TH0,#0FFH ; Loading timer 0 register with FF38H to generate 200
2
usec interrupt interval
MOV TL0,#38H
SETB TR0
LOOP:SJMP LOOP
;----ISR---
TMS:CLR TR0
MOV TH0,#0FFH
MOV TL0,#38H
SETB TR0
RETI
4. Use 2 push buttons to generate square waves of 100Hz and 50 Hz ( i.e. if push
button 1) is on then wave of 100Hz is generated (Using timer interrupt).
ORG 0000H
SJMP MAIN
ORG 000BH ; timer0 interrupt address
SJMP TMS
ORG 001BH ; timer1 interrupt address
SJMP TMS1
ORG 0030H ; starting address of main program
MAIN:MOV TMOD,#22H ; timer 1 and timer 0 set to auto reload mode
MOV IE,#8AH ; enabling the timer 0 and timer 1 interrupts
;----- checking for the key1 press to generate 100Hz square wave-------
KEY1:JB P1.0,KEY2
ACALL DELAY
JB P1.0,KEY2
3
ACALL FREQ100
;------ checking for the key2 press to generate 50Hz square wave------
KEY2:JB P1.1,KEY1
ACALL DELAY
JB P1.1,KEY1
ACALL FREQ50
SJMP KEY1
4
5. Write an assembly code to generate a square wave of frequency equal to 10 times
of last two digits of roll number. a) Use timer auto reload mode b) Use both
timer and interrupt c) Compare both results
ORG 0000H
SJMP MAIN ;jump above interrupt vector adress
ORG 000BH ;ISR for TM0
SJMP TMS ;jump to subroutine TMS
ORG 0030H
MAIN:MOV TMOD,#02H ;sets timer0 in mode 2(auto reload mode)
MOV IE,#82H ;enabling the timer0 interrupt
MOV R1,#5 ;to generate 390hz square wave
MOV TH0,#0H ;value loaded into timer 0
SETB TR0 ;start timer
SETB P1.0
LOOP:SJMP LOOP ;do nothing
TMS: DJNZ R1,HERE
MOV R1,#5
CPL P1.0
HERE:RETI ;return interrupt
END
5
6. Write an assembly program to generate square wave of frequency f = 50 hz on
pressing of one switch out of 4*4 matrix keypad. Also connect 8 bit ADC and use
another switch to start A-D conversion and indicate it with 8 LEDs.
ORG 0000H
SJMP MAIN
ORG 000BH ; timer0 interrupt address
SJMP TMS
ORG 0003H ; external interrupt address
SJMP TMS1
ORG 0030H ; starting address of main program
MAIN:MOV P1,#0FFH
MOV P2,#0FFH
MOV IE,#83H
SETB IP.1 ; giving priority to the timer0 interrupt
CLR IP.0 ; second priority to the external interrupt
;---- checking the key1 and key2 of the first row in 4*4 keypad------
ROW1:CLR P1.4
MOV A,P1
ANL A,#00001111B
MOV R2,A
XRL A,#00001111B
JZ ROW1
ACALL DELAY
MOV A,P1
ANL A,#00001111B
XRL A,R2
JNZ ROW1
MOV A,R2
RRC A
JNC SQUARE ; jump to generate the square wave
RRC A
6
JNC AD ; jump to analog to digital conversion
SJMP ROW1
;------ subroutine to generate squarewave of f = 50Hz------
SQUARE:MOV TMOD,#02H
MOV TH0,#00H
MOV R4,#39
SETB TR0
SJMP ROW1
;------- subroutine to analog to digital conversion of a dc value------
AD:
SETB P3.0; ALE
NOP
CLR P3.0
SETB P3.1 ;SC
NOP
CLR P3.1
SJMP ROW1
TMS:DJNZ R4,BACK
MOV R4,#39
CPL P3.7
BACK:RETI
TMS1: SETB P3.2
SETB P3.3
NOP
CLR P3.3
MOV A,P2
NOP
MOV P0,A
NOP
SETB P3.0; ALE
CLR P3.0
SETB P3.1 ;SC
CLR P3.1
RETI
7
7. Display 2 digit number in 7 segment LED display from 8051 through shift registers
to use pin efficiently.
ORG 0000H
SJMP MAIN
ORG 0030H ; starting address of main program
MAIN:
MOV A,#0B0H ; 7-segment code for representing digit 3
ACALL OUT ; calling out function
MOV A,#0A4H; 7-segment code for representing digit 2
ACALL OUT
SETB P2.3 ; enabling the output of shift register
CLR P2.3
SJMP MAIN
;---- serially sending the accumulator data to shift registers-------
OUT:MOV R1,#08H
LOOP:RRC A ; rotate right the accumulator data
MOV P2.0,C
SETB P2.1 ; giving the clock for each bit
CLR P2.1
DJNZ R1,LOOP ; it will be in loop till the end of the 8-bits
CLR P2.2 ; giving the strobe signal
SETB P2.2
NOP
RET
END
8
8. Write assembly code of 8051 to convert HEX to BCD number.
ORG 0000H
SJMP MAIN
ORG 0030H
MAIN:
MOV R0,#40H ; LOADING R0 WITH RAM LOCATION 40H
MOV A,#0FFH ; HEX DECIAMAL VAUE THAT IS CONVERTED INTO BCD
MOV B,#10
DIV AB
MOV @R0,B ;STORING LOWER DIGIT INTO RAM LOCATION 40H
INC R0
MOV B,#10
DIV AB
MOV @R0,B ;STORING NEXT DIGIT INTO RAM LOCATION 41H
INC R0
MOV @R0,A ;STORING HIGHER DIGIT INTO RAM LOCATION 41H
ORL A,#30H
MOV R2,A
ACALL LCD
DEC R0
MOV A,@R0
ORL A,#30H
MOV R2,A
9
ACALL LCD
DEC R0
MOV A,@R0
ORL A,#30H
MOV R2,A
ACALL LCD
LOOP:SJMP LOOP
LCD:
MOV A, #38H ; INITIATE LCD
ACALL COMMWRT
ACALL DELAY1 ;
DELAY1:
MOV R3, #50H ;
BACK:
MOV R4, #255H ;
HERE:
DJNZ R4, HERE ;
DJNZ R3, BACK ;
RET
COMMWRT:
MOV P3, A ;
CLR P2.5 ; RS = 0 FOR COMMAND REGISTER
CLR P2.6 ; R/W = 0 FOR WRITE
SETB P2.7 ; E = 1 FOR HIGH PULSE
ACALL DELAY1 ;
CLR P2.7 ; E = 0 FOR LOW PULSE
RET
DATAWRT:
10
MOV P3, A ;
SETB P2.5 ; RS = 1 FOR DATA REGISTER
CLR P2.6 ; R/W = 0 FOR WRITE
SETB P2.7 ; E = 1 FOR HIGH PULSE
ACALL DELAY1 ;
CLR P2.7 ; E = 0 FOR LOW PULSE
RET
END
9. Design a calculator to realize addition and subtraction of two numbers using hex
keypad, 8051 and an LCD display. You may use the microcontroller pins efficiently.
Assume clock frequency as 11.059Hz.
ORG 0000H
SJMP MAIN ;jumping over vector address table
ORG 0030H ;starting address of main programm
MAIN:MOV R0,#40H ; Loading R0 with RAM address 40H
MOV R1,#50H ;Loading R1 with RAM address 41H
11
SJMP COLUMN ;jump to column subroutine to know which column key pressed
12
ONE: RRC A
JNC TABLE
INC DPTR
SJMP ONE
;--------------substaction subroutine-------------------
SUB1: CJNE @R0,#2DH,ERROR; ;checking to do substraction if not go to error
MOV R0,#41H
MOV A,@R0
ANL A,#00001111B
13
MOV R7,A
INC R0
INC R0
MOV A,@R0
ANL A,#00001111B
MOV B,A
MOV A,R7
SUBB A,B
ACALL CONVER
MOV A,@R1
ORL A,#30H
ACALL DATAWRT
DEC R1
MOV A,@R1
ORL A,#30H
MOV R5,A
ACALL DATAWRT
LJMP MAIN
; INC R0
; DJNZ R7,HI
; MOV R0,#40H
;MOV A,@R0
; INC R0
; INC R0
; MOV B,@R0
CONVER:MOV B,#10
DIV AB ;dividing Accumulator with 10
MOV @R1,B ;Storing the lower digit into RAM location 40H
INC R1 ;Incrementing RAM address
MOV @R1,A ;loading B register with 10(dec)
RET
LCD:
MOV A, #38H ; INITIATE LCD
ACALL COMMWRT
14
10. Design 8051 based digital clock. You may use component specification as per your
own choice and mention them clearly.
RS BIT P2.5
RW BIT P2.6
EN BIT P2.7
ORG 0000H
SJMP MAIN
ORG 000BH
LJMP TMS
ORG 0030H
MAIN: MOV R0,#40H ;RAM address to store secs
MOV @R0,#00H ; loading RAM address 40H with 00 value
MOV 41,#00H ;RAM address to store minutes
MOV 42,#00H ;RAM address to store hours
MOV 43,#2
MOV R7,#60
MOV R1,#44H
MOV R6,#60
MOV R2,#24
MOV TMOD,#01H ; setting timer 0 in mode 1
MOV IE,#82H ;Enabling the interrupt of timer 0
MOV TH0,#0FFH
MOV TL0,#0FEH ; to generate 1 sec delay
ACALL LCDINI ; Initializing the LCD
MOV R5,#1 ; required to generate 1sec delay
15
SETB TR0
LOOP:SJMP LOOP ; loops here until interrupt comes
COMMWRT:CLR RS
CLR RW
MOV P3,A
SETB EN
NOP
CLR EN
RET
DATAWRT:SETB RS
CLR RW
16
MOV P3,A
SETB EN
NOP
CLR EN
RET
;---- delay subroutine to give LCD some time to perform operation------
DJNZ R5,BACK
MOV TH0,#0FFH; to generate 1sec delay
MOV TL0,#0FEH
MOV R5,#1
;-------------------------------Displaying Secs----------------
MOV A,#88H
ACALL COMMWRT
ACALL DELAY
INC @R0
MOV A,@R0
ACALL CONVER
MOV A,@R1
ORL A,#30H
ACALL DATAWRT
ACALL DELAY
DEC R1
MOV A,@R1
ORL A,#30H
ACALL DATAWRT
ACALL DELAY
DJNZ R6,BACK
MOV R6,#60
MOV @R0,#00H
;-----------------------------Displaying Minutes ------------
INC R0
MOV A,#85H
ACALL COMMWRT
ACALL DELAY
INC @R0
MOV A,@R0
ACALL CONVER
MOV A,@R1
ORL A,#30H
ACALL DATAWRT
ACALL DELAY
17
MOV A,#06H
ACALL COMMWRT
ACALL DELAY
DEC R1
MOV A,@R1
ORL A,#30H
ACALL DATAWRT
ACALL DELAY
DEC R0
DJNZ R7,BACK
INC R0
MOV R7,#60
MOV @R0,#00H
;-----------------------------Displaying Hours------------------------
INC R0
MOV A,#82H
ACALL COMMWRT
ACALL DELAY
INC @R0
MOV A,@R0
ACALL CONVER
MOV A,@R1
ORL A,#30H
ACALL DATAWRT
ACALL DELAY
MOV A,#06H
ACALL COMMWRT
ACALL DELAY
DEC R1
MOV A,@R1
ORL A,#30H
ACALL DATAWRT
ACALL DELAY
DEC R0
DEC R0
DJNZ R2,BACK
MOV R2,24
INC R0
INC R0
MOV @R0,#00H
DEC R0
DEC R0
BACK:RETI
;-----------------------------------------HEX TO BCD
CONVER:MOV B,#10
DIV AB ;dividing Accumulator with 10
MOV @R1,B ;Storing the lower digit into RAM location 40H
INC R1 ;Incrementing RAM address
MOV @R1,A ;loading B register with 10(dec)
18
RET
END
11. Design an embedded system to digitize a) DC and b) sinusoidal signal using 8051
microntroller.Also convert the digital signal so obtained to analog using DAC
connected to your microcontroller.
ORG 0000H
SJMP MAIN
ORG 0003H ;EXTERNAL INTERRUPT 0 VECTOR ADDRESS
SJMP TMS ;JUMP TO INTERRUPT SERVICE ROUTINE
ORG 0030H
MAIN:
MOV IE,#81H
SETB TCON.0
SETB P3.2
SETB P3.6 ;ALE
NOP
CLR P3.6
NOP
SETB P3.4 ;SC
NOP
CLR P3.4
LOOP:SJMP LOOP
19
CLR P3.4
RETI
END
20