Ecs Assignments

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Assignments on Embedded Control Systems

Submitted by, Debashish Mohapatra Roll No: 213EE3308 M.Tech in Control & Automation, Electrical Engineering NIT, Rourkela

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 SJMP MAIN: MOV MOV SETB LOOP: JB SJMP CMPL: CPL CLR SJMP END TMOD, #02H TH0, #1AH TR0 TF0, CMPL LOOP P1.0 TF0 LOOP ;timer 0 in mode 2 auto reload mode ;pre-load value for 2 khz square wave ;start timer 0 ;jump to complement p1.0 if tf==1 ;otherwise check tf again ;toggle/complement bit p1.0 ;clear the TF0 flag ;keep looping 0000H MAIN

DSO Output

0 Channel A V/Div Offset Invert Coupling 2.00 V 24.00 V Normal AC Horizontal Source Position S/Div Trace 2.00 mS 200.00 uS Source Level Coupling Edge Mode Channel B 5.00 V 20.00 V Normal AC Channel C 5.00 V -20.00 V Normal AC Trigger Channel A 0.00 V DC Rising Auto Channel D 5.00 V -60.00 V Normal AC

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 its 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 LED2 BIT SWITCH1 ORG SETB SETB SETB WAIT: JB SWITCH1, WAIT ; wait till switch1 has been pressed acall debounce_delay JB SWITCH1, C1_WAIT ;after debouncing period, switch is low hence switch succesfully debounced CLR JNB SETB AJMP C1_WAIT: CPL AJMP LED1 SWITCH1, $ LED1 WAIT LED2 WAIT ;turn on led1 ;wait till switch has been released ;turn off led1 ;switch pin high after debounce period so error in debouncing P2.0 P2.1 BIT 0000H SWITCH1 LED1 LED2 ;initialize switch 1 as input ;turn off led1 ;turn off led2

P1.0

;subroutine for generating 20ms delay DEBOUNCE_DELAY: MOV R7, #245 L1_DEBOUNCE_DELAY: MOV R6, #40 DJNZ R6, $ DJNZ R7, L1_DEBOUNCE_DELAY RET END

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 rw en bf bit bit bit bit mov acall mov acall mov acall mov acall mov acall mov acall mov acall mov acall mov acall mov acall here: sjmp P2.0 P2.1 P2.2 p1.7 a, #38h send_cmd a, #0Eh send_cmd a, #01h send_cmd a, #06h send_cmd a, #'E' send_data a, #'S' send_data a, #'R' send_data a, #'T' send_data a, #'.' send_data a, #' ' send_data here ;register select bit connected to p2.0 ;read/write bit connected to p2.1 ;enable bit connected to p2.2 ;busy flag check bit on p1.7 ;8 bit 2 line display 5x7 font ;LCD on cursor on ;clear lcd ;cursor position auto increment move cursor to right ;the data to be displayed

;wait indefinitely ;the subroutine to write command to the command register ;wait till ready ;put the command byte on databus of LCD ;select command register ;rw=0 for write operation ;a high-to-low transition on enable to latch the command to the

send_cmd: acall ready mov p1, A clr rs clr rw setb en command register clr en ret send_data: acall mov setb clr setb nop clr ret ready: clr setb back: clr nop setb jb ret end ready p1, a rs rw en en setb rs rw en bf

;send the data in a similar fashion ; ;rs=1 for selecting the data register ;write operation ;high-to-low pulse on enable

;check busy flag ;rs=0 for command register select ; read/rwite=1 for read operation ;low-to-high transition on enable to latch the busy flag ;check busy flag again till it is not 0

en bf, back

B. Using 4 bit mode directly to Microcontroller

;+++++++++++++++++++++++++++variables U L PORT RS RW EN EQU EQU EQU EQU EQU EQU 31 32 P1 P2.0 P2.1 P2.2 ;memory location to hold upper nibble ;memory location to hold lower nibble ;data port to connect lcd ;rs pin connection ;rw pin connection ;en pin connection

;+++++++++++++++++++++++++++++++++++++ ORG 0000H CLR RW ACALL 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 ACALL LCD_CMD MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV A, #' ' LCD_DATA A, #'I' LCD_DATA A, #' ' LCD_DATA A, #'A' LCD_DATA A, #'M' LCD_DATA A, #' ' LCD_DATA A, #'D' LCD_DATA A, #'E' ;switch to 2nd line of lcd

"

;send " i am debashish " ;in second line of lcd

ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL MOV ACALL SJMP

LCD_DATA A, #'B' LCD_DATA A, #'A' LCD_DATA A, #'S' LCD_DATA A, #'H' LCD_DATA A, #'I' LCD_DATA A, #'S' LCD_DATA A, #'H' LCD_DATA A, #' ' LCD_DATA A, #' ' LCD_DATA $ ;infinite long loop

;+++++++++++++++++++++++++++++separate Upper and Lower Nibble SEPARATOR: MOV ANL SWAP ANL MOV RET

U, U, A A, L,

A #0F0H #0F0H A

;save a at temp location u ;mask it with 0fh (28h & f0h = 20h) ;swap nibble (28h => 82h) ;mask it with 0fh (82h & f0h = 80h) ;save it at temp location l ;return

;++++++++++++++++++++++++++move to port MOVE_TO_PORT: MOV PORT, A SETB EN ACALL DELAY CLR EN ACALL DELAY RET ;put content of a to port ;make en high ;call a short delay routine ;clear en ;short delay ;return

;++++++++++++++++++++++++++lcd command LCD_CMD: CLR ACALL MOV ACALL MOV ACALL RET RS ;clear rs, going to send command SEPARATOR ;separate the command and save to u and l A, U ;copy u to a MOVE_TO_PORT ;move content of a to port A, L ;copy l to a MOVE_TO_PORT ;move content of a to port ;return

;++++++++++++++++++++++++++++++lcd data LCD_DATA: SETB ACALL MOV ACALL MOV ACALL RET RS ;rs=1, going to send data SEPARATOR ;separate the data and save to u & l A, U ;copy u to a MOVE_TO_PORT ;send it to lcd A, L ;copy l to a MOVE_TO_PORT ;send it to lcd ;return

;+++++++++++++++++++++++++initilization

INIT: ACALL DELAY ACALL DELAY MOV CLR SETB ACALL CLR MOV ACALL MOV ACALL MOV ACALL MOV ACALL RET PORT, #20H RS EN DELAY EN A, #28H LCD_CMD A, #0CH LCD_CMD A, #06H LCD_CMD A, #01H LCD_CMD ;some delay to lcd after power on ;send 20h to lcd to set 4 bit mode ;after that we can use lcd_cmd ;make en switching

;+++++++++++++++++++++++++++++++++delay DELAY: L2: L1: MOV MOV DJNZ DJNZ RET R0, R1, R1, R0, #10H #0FH L1 L2

;+++++++++++++++++++++++++++++++++++++++ END

C. Using 8 bit mode through Shift Register

RS RW EN

EQU EQU EQU

P2.0 P2.1 P2.2 BIT BIT BIT BIT 0000H LCD_STB LCD_OE RW LCD_CLR LCD_INIT LCD_CLR ;because we are writing to lcd ;clear display ;initialize the lcd ;clear display P2.4 P2.5 P2.6 P2.7

LCD_DATA LCD_CLK LCD_STB LCD_OE ORG CLR CLR CLR ACALL ACALL ACALL

MOV A, #'D' ;here we sending a single character at a time and then call 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_DATA SJMP $ ;...................... ;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

;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_DATA SETB 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 be displayed ;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_DATA: SEND_BIT: MOV CLR NOP SETB DJNZ SETB SETB NOP CLR RET DELAY: MOV R0, #5H ;a delay subroutine MOV R1, #0FFH DJNZ R1, L1 DJNZ R0, L2 RET ;+++++++++++++++++++++++++++++++++++++++ END ; end of code ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ L2: L1: send MOV R2, #08H RLC A LCD_DATA, C LCD_CLK LCD_CLK R2, SEND_BIT LCD_OE LCD_STB LCD_STB ;8 bits to send ;rotate accumulator left with carry ;move the 1st bit to the shift register ;give a low-to-high transition on clock ;jump to resend next bit till all bits are sent ;enable the shift registers data ;latch the data on the parallel output ;

Q4.

D_DATA D_CLK BIT D_STB BIT D_OE BIT .ORG MAIN: MOV ACALL CLR CLR AGAIN: MOV S6: MOV MOV S5: MOV MOV S4: MOV MOV S3: MOV MOV S2: MOV MOV S1: ACALL ACALL INC DJNZ INC DJNZ INC DJNZ INC DJNZ CJNE INC DJNZ H1: INC DJNZ NEXT: LJMP S_D1: MOV MOV ACALL MOV MOV ACALL MOV MOV ACALL MOV MOV ACALL MOV MOV ACALL MOV MOV ACALL NOP NOP SETB NOP SETB NOP NOP NOP CLR RET ;serial bit

BIT P2.0 P2.1 P2.2 P2.3 0000H

;initialize port pins

;set origin to 0000h ;main program DPTR, #300H ;initialize data pointer RAM ;call ram subroutine D_STB ;clear strobe pin D_OE ;clear output enable MOV R2, #03H ;set counter for hour1 55H, #30H ;start with hex code of 0 R3, #0AH ;set counter for hour0 54H, #30H ;start with hex code of 0 R4, #06H ;set counter for minute1 53H, #30H ;start with hex code of 0 R5, #0AH ;set counter for minute0 52H, #30H ;start with hex code of 0 R6, #06 ;set counter for second1 51H, #30H ;start with hex code of 0 R7, #0AH ;set counter for second0 50H, #30H ;start with hex code of 0 S_D1 ;call data subroutine DELAY ;give delay of one second 50H ;increment each location R7, S1 ;check for second counter 51H ;increment each location R6, S2 ;check for second counter 52H ;increment each location R5, S3 ;check for minute counter 53H ;increment each location R4, S4 ;check for minute counter R2, #1, NEXT ;check for not more than 23hour 54H ;increment each location R3, S5 ;check for hour counter 55H ;increment each location R2, S6 ;check for hour counter AGAIN ;repeat loop R0, 50H ;;//serial data send A, @R0 S_D R0, 51H A, @R0 S_D R0, 52H A, @R0 S_D R0, 53H A, @R0 S_D R0, 54H A, @R0 S_D R0, 55H A, @R0 S_D D_OE D_STB ;show output

D_STB send

;clear strobe to get next ;return to main program

S_D: S_1:

MOV R1, #08H ;8 bits to send 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_CLK DJNZ 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 A MOVC A, @A+DPTR ;load accumulator with 7 seg code MOV @R0, A INC DPTR ;increment memory which points to the db for led INC R0 DJNZ 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

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 clock bit data_in DATA1 CLK ST OE .org sjmp main: BIT BIT BIT BIT 0000h main

p1.1 p1.2 bit P2.0 P2.1 P2.2 P2.3

p1.0

MOV DPTR, #LUT ; moves starting address of LUT to DPTR MOV P2,#00000000B ; initializes P2 as output port acall get_data_1 acall get_data_2 acall ajmp main display_data

get_data_1: mov p1, #0ffh nop mov read mov a, #00h r6, #08h

; ; no of time the reading has to be done, i.e. no of bits to ;give a default value in accumulator i.e. 00000000

get_new_bit_1: clr plsl clr nop setb mov rlc clock clock c, p1.0 a

;clear parallel/serial pin

;;generate a +ve transition on clock ; ;generate a +ve transition on clock ;read the bit to Carry bit ;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 read mov a, #00h ; ;give a default value in accumulator i.e. 00000000 ;clear parallel/serial pin get_new_bit_2: clr plsl clr nop setb mov rlc clock clock c, p1.3 a r6, #08h

; ; no of time the reading has to be done, i.e. no of bits to

;;generate a +ve transition on clock ; ;generate a +ve transition on clock ;read the bit to Carry bit ;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 DISPLAY NEXT7: cjne r4,#7fh,next8 MOV A, #7D ACALL DISPLAY NEXT8: cjne r5,#0feh,next9

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 display ret 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, B ACALL S_D SETB 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 CLK DJNZ R2, S_1 RET ;//seven seg code saved in ram address RAM: MOV R0, #30H MOV R1, #0AH R_1: CLR A MOVC A, @A+DPTR MOV @R0, A INC DPTR INC R0 DJNZ R1, R_1 RET ;//hex code for seven seg .ORG 300H DB 40H, 0F9H, 24H, 30H, 19H, 12H, 02H, 0F8H, 00H, 10H END END

You might also like