ecs assignments
TRANSCRIPT
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 1/17
Assignments on
Embedded ControlSystems
Submitted by,
Debashish Mohapatra
Roll No: 213EE3308
M.Tech in Control & Automation,
Electrical Engineering
NIT, Rourkela
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 2/17
Q1. Configure a Timer in 8 bit Auto-Reload mode.
;program to generate a 2 khz square wave on pin p1.0 of port 1. ;freq=2Khz, time_period=.5 mS, on_time=off_time=.25mS=250uS ;at 250uS the bit is to be toggled,i.e. the time_delay=250uS ;oscillator frequency is 12 mhz; i.e. clock_period = 1.085 ;timer time_delay=(255-N+1)*Cloclk_period = 26d = #1Ah
.ORG 0000H
SJMP MAINMAIN: MOV TMOD, #02H ;timer 0 in mode 2 auto reload mode MOV TH0, #1AH ;pre-load value for 2 khz square wave SETB TR0 ;start timer 0
LOOP: JB TF0, CMPL ;jump to complement p1.0 if tf==1 SJMP LOOP ;otherwise check tf again
CMPL: CPL P1.0 ;toggle/complement bit p1.0 CLR TF0 ;clear the TF0 flag SJMP LOOP ;keep looping END
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 3/17
DSO Output
0
V/Div
Channel A
2.00 V
Channel B
5.00 V
Channel C
5.00 V
Channel D
5.00 V
Offset
Invert
Coupling
24.00 V
Normal
AC
20.00 V
Normal
AC
-20.00 V
Normal
AC
-60.00 V
Normal
AC
Source
Horizontal
Trace Source
Trigger
Channel A
Position
S/Div
2.00 mS
200.00 uS
Level
Coupling
Edge
Mode
0.00 V
DC
Rising
Auto
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 4/17
Q2.Write a code for key de-bouncing algorithm for 8051.(Code with schematics)
This is a program that will turn on a led1 if a switch is de-bounced properly & will toggle led2 if there is some error in
switch de-bouncing. Led1 will be on until the switch is pressed & will turn off as soon as it’s released. We have taken
the de-bounce period as 20ms. So once a low signal has been received from the switch the controller will again check
the switch status after 20ms & if at that time the switch is high led2 will be toggled and if the switch is low led1 will be
turned on.
LED1 BIT P2.0LED2 BIT P2.1SWITCH1 BIT P1.0
ORG 0000H
SETB SWITCH1 ;initialize switch 1 as input SETB LED1 ;turn off led1 SETB LED2 ;turn off led2
WAIT: JB SWITCH1, WAIT ; wait till switch1 has been pressed
acall debounce_delayJB SWITCH1, C1_WAIT
;after debouncing period, switch is low hence switch succesfully debounced
CLR LED1 ;turn on led1 JNB SWITCH1, $ ;wait till switch has been released SETB LED1 ;turn off led1 AJMP WAIT
C1_WAIT: ;switch pin high after debounce period so error in debouncing CPL LED2AJMP WAIT
;subroutine for generating 20ms delay DEBOUNCE_DELAY:
MOV R7, #245 L1_DEBOUNCE_DELAY:
MOV R6, #40 DJNZ R6, $ DJNZ R7, L1_DEBOUNCE_DELAYRET
END
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 5/17
Q3: Interface a 16x2 LCD
A. Using 8 bit mode directly connected to microcontroller
B. Using 4 bit mode directly to Microcontroller
C. Using 8 bit mode through Shift Register
A. Using 8 bit mode directly connected to microcontroller
rs bit P2.0 ;register select bit connected to p2.0
rw bit P2.1 ;read/write bit connected to p2.1 en bit P2.2 ;enable bit connected to p2.2 bf bit p1.7 ;busy flag check bit on p1.7
mov a, #38h ;8 bit 2 line display 5x7 font acall send_cmd mov a, #0Eh ;LCD on cursor on acall send_cmd mov a, #01h ;clear lcd acall send_cmd mov a, #06h ;cursor position auto increment move cursor to right acall send_cmd
mov a, #'E' ;the data to be displayed acall send_data mov a, #'S' acall send_data mov a, #'R' acall send_data mov a, #'T' acall send_data mov a, #'.' acall send_data mov a, #' ' acall send_data
here: sjmp here ;wait indefinitely
send_cmd: ;the subroutine to write command to the command register acall ready ;wait till ready mov p1, A ;put the command byte on databus of LCD clr rs ;select command register clr rw ;rw=0 for write operation setb en ;a high-to-low transition on enable to latch the command to the
command register clr enret
send_data:
acall ready ;send the data in a similar fashion mov p1, a ; setb rs ;rs=1 for selecting the data register clr rw ;write operation setb en ;high-to-low pulse on enable nop clr enret
ready: setb bf ;check busy flag clr rs ;rs=0 for command register select setb rw ; read/rwite=1 for read operation
back: clr en ;low-to-high transition on enable to latch the busy flag
nop setb enjb bf, back ;check busy flag again till it is not 0 ret
end
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 6/17
B. Using 4 bit mode directly to Microcontroller
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 7/17
;+++++++++++++++++++++++++++variables
U EQU 31 ;memory location to hold upper nibble L EQU 32 ;memory location to hold lower nibble
PORT EQU P1 ;data port to connect lcd RS EQU P2.0 ;rs pin connection RW EQU P2.1 ;rw pin connection EN EQU P2.2 ;en pin connection
;+++++++++++++++++++++++++++++++++++++ ORG 0000H CLR RWACALL INIT MOV A, #' ' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATA MOV A, #'H' ;send " hi everyone " ACALL LCD_DATA ;in first line of lcd MOV A, #'I' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATA MOV A, #'E' ACALL LCD_DATA MOV A, #'V' ACALL LCD_DATA MOV A, #'E' ACALL LCD_DATA MOV A, #'R' ACALL LCD_DATA MOV A, #'Y' ACALL LCD_DATA MOV A, #'O'
ACALL LCD_DATA MOV A, #'N' ACALL LCD_DATA MOV A, #'E' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATA
MOV A, #0C0H ;switch to 2nd line of lcd ACALL LCD_CMD
MOV A, #' ' ACALL LCD_DATA MOV A, #'I' ;send " i am debashish " ACALL LCD_DATA ;in second line of lcd MOV A, #' ' ACALL LCD_DATA MOV A, #'A' ACALL LCD_DATA MOV A, #'M' ACALL LCD_DATA
MOV A, #' ' ACALL LCD_DATA MOV A, #'D' ACALL LCD_DATA MOV A, #'E'
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 8/17
ACALL LCD_DATA MOV A, #'B' ACALL LCD_DATA MOV A, #'A' ACALL LCD_DATA MOV A, #'S' ACALL LCD_DATA MOV A, #'H' ACALL LCD_DATA MOV
A, #'I' ACALL LCD_DATA
MOV A, #'S' ACALL LCD_DATA MOV A, #'H' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATA
SJMP $ ;infinite long loop
;+++++++++++++++++++++++++++++separate Upper and Lower Nibble
SEPARATOR: MOV U, A ;save a at temp location u ANL U, #0F0H ;mask it with 0fh (28h & f0h = 20h) SWAP A ;swap nibble (28h => 82h) ANL A, #0F0H ;mask it with 0fh (82h & f0h = 80h) MOV L, A ;save it at temp location l RET ;return
;++++++++++++++++++++++++++move to port
MOVE_TO_PORT:
MOV PORT, A ;put content of a to port SETB EN ;make en high ACALL DELAY ;call a short delay routine CLR EN ;clear en ACALL DELAY ;short delay RET ;return
;++++++++++++++++++++++++++lcd command
LCD_CMD: CLR RS ;clear rs, going to send command ACALL SEPARATOR ;separate the command and save to u and l
MOV A, U ;copy u to a ACALL MOVE_TO_PORT ;move content of a to port MOV A, L ;copy l to a ACALL MOVE_TO_PORT ;move content of a to port RET ;return
;++++++++++++++++++++++++++++++lcd data
LCD_DATA: SETB RS ;rs=1, going to send data ACALL SEPARATOR ;separate the data and save to u & l MOV A, U ;copy u to a ACALL MOVE_TO_PORT ;send it to lcd
MOV A, L ;copy l to a ACALL MOVE_TO_PORT ;send it to lcd RET ;return
;+++++++++++++++++++++++++initilization
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 9/17
INIT:
ACALL DELAY ;some delay to lcd after power on ACALL DELAY
MOV PORT, #20H ;send 20h to lcd to set 4 bit mode CLR RS ;after that we can use lcd_cmd SETB EN ;make en switching ACALL DELAY
CLR EN
MOV A, #28H ACALL LCD_CMD MOV A, #0CH ACALL LCD_CMD MOV A, #06H ACALL LCD_CMD MOV A, #01H ACALL LCD_CMDRET
;+++++++++++++++++++++++++++++++++delay
DELAY: MOV R0, #10H
L2: MOV R1, #0FH L1: DJNZ R1, L1
DJNZ R0, L2RET
;+++++++++++++++++++++++++++++++++++++++
END
C. Using 8 bit mode through Shift Register
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 10/17
RS EQU P2.0RW EQU P2.1EN EQU P2.2
LCD_DATA BIT P2.4LCD_CLK BIT P2.5LCD_STB BIT P2.6LCD_OE BIT P2.7
ORG 0000H
CLR LCD_STBCLR LCD_OE
CLR RW ;because we are writing to lcd ACALL LCD_CLR ;clear display ACALL LCD_INIT ;initialize the lcd ACALL LCD_CLR ;clear display
MOV A, #'D' ;here we sending a single character at a time and thencall lcd_data to tell
ACALL LCD_DATA ;lcd that we sending some thing to display on lcd MOV A, #'E' ACALL LCD_DATA MOV A, #'B' ACALL LCD_DATA MOV A, #'A' ACALL LCD_DATA MOV A, #'S' ACALL LCD_DATA MOV A, #'H' ACALL LCD_DATA MOV A, #'I' ACALL LCD_DATA MOV A, #'S' ACALL LCD_DATA
MOV A, #'H' ACALL LCD_DATA MOV A, #'.' ACALL LCD_DATA MOV A, #'.' ACALL LCD_DATA MOV A, #' ' ACALL LCD_DATASJMP $
;...................... ;all the subroutine goes here ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ LCD_CLR:
MOV A, #01H ;clear lcd ACALL LCD_CMD ;sending command to lcd RET
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ LCD_INIT:
ACALL DELAY ;some delay to lcd after power on ACALL DELAY
MOV A, #38H ;telling the lcd that we use 8 bit mode, ;2 line display and 5x7 font ;#38h because here dl=1,n=1 and f=0 ;in "set interface length"
ACALL LCD_CMD ;sending command to lcd MOV A, #0EH ;we use display on,cursor on and cursor blinking off ;#0ch to display on,cursor off and cursor blinking off ACALL LCD_CMD ;sending command to lcd MOV A, #06H ; cursor position auto increment and move cursor to
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 11/17
;right #04 cursor moved to left ACALL LCD_CMD ;sending command to lcd RET
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;...................... ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LCD_CMD: CLR RS ;telling the lcd that the data is a command
;mov p1,a ; command is send to the port ACALL SEND_DATASETB EN ; make en high ACALL DELAY ;a short delay CLR EN ;en is low to make a higt-low pulse RET ;return
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ LCD_DATA:
SETB RS ;telling the lcd that the data which is being send is to bedisplayed
;mov p1, a ;data to displayed is stored in a and now send to port p1 ACALL SEND_DATA
SETB EN ;en is high ACALL DELAY ;a short delay CLR EN ;en is low to make a high-low pulse RET ; return to main
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;++++++++++++++++++++++++++++++++++++++
;serial bit send SEND_DATA: MOV R2, #08H ;8 bits to send SEND_BIT: RLC A ;rotate accumulator left with carry
MOV LCD_DATA, C ;move the 1st bit to the shift register CLR LCD_CLK ;give a low-to-high transition on clock NOP
SETB LCD_CLKDJNZ R2, SEND_BIT ;jump to resend next bit till all bits are sent
SETB LCD_OE ;enable the shift registers data SETB LCD_STB ;latch the data on the parallel output NOP CLR LCD_STB ; RET
DELAY: MOV R0, #5H ;a delay subroutine
L2: MOV R1, #0FFH L1: DJNZ R1, L1
DJNZ R0, L2RET
;+++++++++++++++++++++++++++++++++++++++ END ; end of code
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 12/17
Q4.
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 13/17
D_DATA BIT P2.0 ;initialize port pinsD_CLK BIT P2.1D_STB BIT P2.2D_OE BIT P2.3
.ORG 0000H ;set origin to 0000h MAIN: ;main program
MOV DPTR, #300H ;initialize data pointer ACALL RAM ;call ram subroutine CLR D_STB ;clear strobe pin
CLR D_OE ;clear output enable AGAIN: MOV R2, #03H ;set counter for hour1 MOV 55H, #30H ;start with hex code of 0
S6: MOV R3, #0AH ;set counter for hour0 MOV 54H, #30H ;start with hex code of 0
S5: MOV R4, #06H ;set counter for minute1 MOV 53H, #30H ;start with hex code of 0
S4: MOV R5, #0AH ;set counter for minute0 MOV 52H, #30H ;start with hex code of 0
S3: MOV R6, #06 ;set counter for second1 MOV 51H, #30H ;start with hex code of 0
S2: MOV R7, #0AH ;set counter for second0 MOV 50H, #30H ;start with hex code of 0
S1: ACALL S_D1 ;call data subroutine ACALL DELAY ;give delay of one second INC 50H ;increment each locationDJNZ R7, S1 ;check for second counter INC 51H ;increment each location DJNZ R6, S2 ;check for second counter INC 52H ;increment each location DJNZ R5, S3 ;check for minute counter INC 53H ;increment each location DJNZ R4, S4 ;check for minute counter CJNE R2, #1, NEXT ;check for not more than 23hour INC 54H ;increment each location DJNZ R3, S5 ;check for hour counter
H1: INC 55H ;increment each location
DJNZ R2, S6 ;check for hour counter NEXT: LJMP AGAIN ;repeat loop S_D1: MOV R0, 50H ;;//serial data send
MOV A, @R0ACALL S_D MOV R0, 51H MOV A, @R0ACALL S_D MOV R0, 52H MOV A, @R0ACALL S_D MOV R0, 53H MOV A, @R0
ACALL S_D MOV R0, 54H MOV A, @R0ACALL S_D MOV R0, 55H MOV A, @R0ACALL S_D NOP NOP SETB D_OE ;show output NOP SETB D_STB NOP
NOP NOP CLR D_STB ;clear strobe to get next RET ;return to main program
;serial bit send
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 14/17
S_D: MOV R1, #08H ;8 bits to send S_1: RLC A ;rotate accumulator left through carry
MOV D_DATA, C ;send the msb which is in cary CLR D_CLK ;initiate a low to high transition on clock NOP SETB D_CLKDJNZ R1, S_1 ;send next bit, repeat till all bits are sent RET ;return
;seven seg code saved in ram address
RAM: MOV
R0, #30H MOV R1, #0AH
R_1: CLR AMOVC A, @A+DPTR ;load accumulator with 7 seg code MOV @R0, AINC DPTR ;increment memory which points to the db for led INC R0DJNZ R1, R_1 ; decrement r1, jump to r_1 if not zero RET ;return
;1 sec dealy DELAY:
MOV TMOD, #01H ;set timer to mode 1 MOV B, #14 ;14 loops for 1 sec delay
D_2: MOV TL0, #00H ;count from 00 to ff MOV TH0, #00H SETB TR0 ;start timer
D_1: JNB TF0, D_1 ;wait till timer overflows CLR TR0 ;stop timer CLR TF0 ;clear timer overflow flag DJNZ B, D_2 ;loop 14 times RET ;return
;hex code for seven seg .ORG 300H DB 40H, 0F9H, 24H, 30H, 19H, 12H, 02H, 0F8H, 00H, 10H END
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 15/17
5. Write a code to interface 16 keys with 8051 uC using shift resisters. Display the content of key pressed in 7
segment leds.
plsl bit p1.1clock bit p1.2data_in bit p1.0
DATA1 BIT P2.0CLK BIT P2.1ST BIT P2.2OE BIT P2.3
.org 0000h sjmp main
main: MOV DPTR, #LUT ; moves starting address of LUT to DPTR MOV P2,#00000000B ; initializes P2 as output port
acall get_data_1acall get_data_2
acall display_dataajmp main
get_data_1: mov p1, #0ffh nop ;
mov r6, #08h ; no of time the reading has to be done, i.e. no of bits toread
mov a, #00h ;give a default value in accumulator i.e. 00000000
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 16/17
get_new_bit_1: clr plsl ; ;clear parallel/serial pin
clr clock ;;generate a +ve transition on clock nop ; setb clock ;generate a +ve transition on clock
mov c, p1.0 ;read the bit to Carry bit rlc a ;rotate accumulator with carry
djnz r6, get_new_bit_1;data has been received in Accumulator mov r4,a ;save the data received in Shift register 2 to r4 ret
get_data_2: mov p1, #0ffh nop ;
mov r6, #08h ; no of time the reading has to be done, i.e. no of bits toread
mov a, #00h ;give a default value in accumulator i.e. 00000000
get_new_bit_2: clr plsl ; ;clear parallel/serial pin
clr clock ;;generate a +ve transition on clock nop ; setb clock ;generate a +ve transition on clock
mov c, p1.3 ;read the bit to Carry bit rlc a ;rotate accumulator with carry
djnz r6, get_new_bit_2;data has been received in Accumulator ret
mov r5,a ;save the data received in Shift register 1 to r4
display_data: ;display the received data in LCD
NEXT0: cjne r4,#0feh,next1 MOV A, #0D ; ACALL DISPLAY ; calls DISPLAY subroutine
NEXT1: cjne r4,#0fdh,next2 MOV A, #1D ACALL DISPLAY
NEXT2: cjne r4,#0fbh,next3
MOV A, #2D ACALL DISPLAY
NEXT3: cjne r4,#0f7h,next4 MOV A, #3D ACALL DISPLAY
NEXT4: cjne r4,#0efh,next5 MOV A, #4D ACALL DISPLAY
NEXT5: cjne r4,#0dfh,next6 MOV A, #5D ACALL DISPLAY
NEXT6: cjne r4,#0bfh,next7 MOV A, #6D
ACALL DISPLAYNEXT7: cjne r4,#7fh,next8
MOV A, #7D ACALL DISPLAY
NEXT8: cjne r5,#0feh,next9
8/12/2019 Ecs Assignments
http://slidepdf.com/reader/full/ecs-assignments 17/17
MOV A, #8D ACALL DISPLAY
NEXT9: cjne r5,#0fdh,next10 MOV A, #9D ACALL DISPLAY
NEXT10: cjne r5,#0fbh,next11 MOV A, #10D ACALL DISPLAY
NEXT11: cjne r5,#0f7h,next12 MOV
A, #11D ACALL DISPLAY
NEXT12: cjne r5,#0efh,next13 MOV A, #12D ACALL DISPLAY
NEXT13: cjne r5,#0dfh,next14 MOV A, #13D ACALL DISPLAY
NEXT14: cjne r5,#0bfh,next15 MOV A, #14D ACALL DISPLAY
NEXT15: cjne r5,#7fh,next16 MOV A, #15D ACALL DISPLAY
next16: mov a,#16d acall displayret
DISPLAY:MOVC A, @A+DPTR ; gets digit drive pattern for the current key from LUT MOV P2, A ; puts corresponding digit drive pattern into P0 RET
;//serial data send S_D1: ACALL S_D
MOV A, BACALL S_DSETB OE
SETB ST CLR ST RET
;//serial bit send S_D: MOV R2, #08H S_1: RLC A
MOV DATA1, C CLR CLK NOP SETB CLKDJNZ R2, S_1RET
;//seven seg code saved in ram address RAM: MOV R0, #30H
MOV R1, #0AH R_1: CLR A
MOVC A, @A+DPTR MOV @R0, AINC DPTRINC R0DJNZ R1, R_1RET
;//hex code for seven seg .ORG 300H
DB 40H, 0F9H, 24H, 30H, 19H, 12H, 02H, 0F8H, 00H, 10H END
END