micro controller programming & applications unit v mr. s. vinod lecturer eee department
DESCRIPTION
MICRO CONTROLLER PROGRAMMING & APPLICATIONS UNIT V Mr. S. VINOD LECTURER EEE DEPARTMENT. Data Transfer, Manipulation, Control & I/O instructions Simple programming exercises key board and display interface Closed loop control of servo motor stepper motor control. - PowerPoint PPT PresentationTRANSCRIPT
MICRO CONTROLLER PROGRAMMING & APPLICATIONS
UNIT V
Mr. S. VINOD
LECTURER
EEE DEPARTMENT
• Data Transfer,
• Manipulation,
• Control & I/O instructions
• Simple programming exercises key board and display interface
• Closed loop control of servo motor
• stepper motor control.
1.Program for addition of two 8-bit numbers
Algorithm:
1. Clear c – register for carry
2. Get the data immediately
3. Add the two data
4. Store the result in memory pointed by DPTR
Program:
ORG 4100
CLR C
MOV A,# data 1
ADD A,# data 2
MOV DPTR, #4500
MOVX @ DPTR,A
HERE: SJMP HERE
2.Subtraction Of Two 8-bit Number
ALGORITHM:1. Clear c – register for carry2. Get the data immediately3. Subtract the two data4. Store the result in memory pointed by DPTR
Program:
ORG 4100CLR CMOV A,# data 1SUBB A,# data 2MOV DPTR, #4500MOVX @ DPTR,A
HERE: SJMP HERE
3.Multiplication of two 8-bit numberALGORITHM:1. Get the data in A-reg2. Get the value to be multiplied in B-reg3. Multiply the two data4. The higher order of the result is in B-reg5. The lower order of the result is in A- reg6. Store the result
PROGRAM:ORG 4100CLR CMOV A,# DATA1MOV B,# DATA1MUL ABMOV DPTR,#4500MOVX @DPTR,#AINC DPTRMOV A,BMOVX @DPTR,A
HERE: SJMP HERE
4.Division of two 8-bit numberALGORITHM:1. Get the data in A-reg2. Get the value to be divided in B-reg3. divided the two data4. The quotient is in A-reg5. The remainder is in B- reg6. Store the result
PROGRAM:ORG 4100CLR CMOV A,# DATA1MOV B,# DATA1DIV ABMOV DPTR,#4500MOVX @DPTR,#AINC DPTRMOV A,BMOVX @DPTR,A
HERE: SJMP HERE
5.RAM ADDRESSING
Program:
Bit Addressing
SETB PSW.3
MOV R0,#data1
MOV A, #data 2
ADD A,R0
MOV DPTR,#4500
MOVX @DPTR, A
HERE: SJMP HERE
To exhibit the ram direct addressing and bit addressing schemes of 8051
Program:Direct Addressing
MOV 30,#data1MOV A, #data 2ADD A,30MOV DPTR,#4500MOVX @DPTR, A
HERE: SJMP HERE
6.INTERFACING STEPPER MOTOR
STEPPER MOTOR
INTERFACING STEPPER MOTOR
ORG 4100
START: MOV DPTR,#4500
MOV R0,#04
AGAIN; MOVX A,@DPTR
PUSH DPH
PUSH DPL
MOV DPTR,#FFCOH
MOV R2, #04H
MOV R1,# FFH
DLY1: MOV R3,#FFH
DLY: DJNZ R3, DLY
DJNZ R1, DLY1
MOVX @DPTR, A
POP DPL
POP DP
INC DPTR
DJNZ R0, AGAIN
SJMP START
DATA:4500 094501 054502 064503 0A
7.Find the maximum number from a given 8 bit number
MOV DPTR, #2000
MOV R0,#0A
MOV R3,#00
AGAIN: MOVX A, @DPTR
CJNE A,R3, NE
AJMP SKIP
NE: JC SKIP
MOV R3 ,A
SKIP: INC DPTR
DJNZ R0, AGAIN
8.Write a program to clear ACC, thenadd 3 to the accumulator ten time
MOV A,#0
MOV R2,#10
AGAIN: ADD A,#03
DJNZ R2,AGAIN ;repeat until R2=0 (10 times)
MOV R5,A
9.Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM
location starting at 59hMOV R0,#37h ; source pointerMOV R1,#59h ; dest pointer MOV R2,#10 ; counter
L1: MOV A,@R0MOV @R1,AINC R0INC R1DJNZ R2,L1
10.Write a program using Timer0 to create a 10khz square wave on P1.0
MOV TMOD,#02H ;8-bit auto-reload modeMOV TH0,#-50 ;-50 reload value in TH0SETB TR0 ;start timer0
LOOP:JNB TF0, LOOP ;wait for overflowCLR TF0 ;clear timer0 overflow flagCPL P1.0 ;toggle port bitSJMP LOOP ;repeatEND
Liquid Crystal Displays (LCD)• These components are “specialized” for being used with the
microcontrollers, which means that they cannot be activated by standard IC circuits. They are used for writing different messages on a miniature LCD.
• HD44780
• can display messages in two lines with 16 characters each . It displays all letters of alphabet, greek letters, punctuation marks, mathematical symbols etc. In addition, it is possible to display symbols that user makes up on its own. Automatic shifting message on display (shift left and right), appearance of the pointer, backlight etc.are considered as useful characteristics
Pin description for LCD
LCD command code HEX REGISTER
1 clear display screen
2 Return home
4 decrement cursor
6 increment cursor
5 shift display right
7 shift display left
8 display off, cursor off
A display off, cursor on
C display on, cursor off
E display on , cursor blinking
10 shift cursor position to left
14 shift cursor position to right
18 shift the entire display to the left
1C shift the entire display to the right
80 force cursor to beginning of 1st line
C0 force cursor to beginning of 2nd line
38 2 line and 5*7 matrix
Sending command and data to Lcds with a time delay
• To send command to lcd RS= 0, for data RS=1
• P1.0 to P1.7 – are connected to LCD data/command
• P2.0 is connected to RS pin of LCD
• P2.1 is connected to R/W pin of LCD
• P2.2 is connected to E pin of LCD
ORG 0HMOV A, # 38H ; init. Lcd 2 line, 5*7 matrixACALL COMNWRTACALL DELAYMOV A,# 0EH ; display on cursor onACALL COMNWRTACALL DELAYMOV A,#01H ;clear LCDACALL COMNWRTACALL DELAYMOV A,#06H ;shift cursor rightACALL COMNWRTACALL DELAYMOV A,#84H ;cursor at line 1, position 4ACALL COMNWRTACALL DELAYMOV A, # ‘N’ ; display letter NACALL DATAWRTACALL DELAYMOV A,# ‘O’ ; display letter OACALL DATAWRT
AGAIN: SJMP AGAIN
COMNWRT: MOV P1,ACLR P2.0 ; RS =0 for commandCLR P2.1 ;R/W =0 for writeSETB P2.2 ; E=1 for high pluseACALL DELAYCLR P2.2 ; E=0 RET
DATAWRT: MOV P1,ASETB P2.0 ; RS =1 for data CLR P2.1 ;R/W =0 for writeSETB P2.2 ; E=1 for high pluseACALL DELAYCLR P2.2 ; E=0 RET
DELAY: MOV R3,#FFMOV R4,#FFDJNZ R4,HEREDJNZ R3,HERE2RETEND
KEY BOARD INTERFACING
• The key board here we are interfacing is a matrix keyboard. This key board is designed with a particular rows and columns. These rows and columns are connected to the microcontroller through its ports of the micro controller 8051. We normally use 8*8 matrix key board. So only two ports of 8051 can be easily connected to the rows and columns of the key board
Scanning and identifying the key• To make sure that the preceding key has been released, 0s are output to
all rows at once, and the columns are read and checked repeatedly until all the columns are high. When all columns are found to be high, the program waits for a short amount of time before it goes to the next stage of waiting for a key to be pressed.
• Columns re scanned over and over in an infinite loop until one of them has a 0 on it. Rows are 0
• After a key is pressed mp wait for 20ms and then scans the column again
• To detect which rows the key press belongs to , the mp grounds one row at a time, if it finds that all columns are high, this means that the key press cannot belong to that row, therefore, it grounds the next row and continues
• After finding the key, it sets up the starting address for the lookup table holding the scan codes
• To identify the key pressed the mp controller rotates the column bits one bit at time, in to carry flag and checks to see if its low. If zero, it pull the ASCII code for that key from the look-up table. Otherwise it increment the pointer to point the next element of the look-up table.
Ground all row
start
Read all columns
Read all columns
Wait for Debounce
Read all columns
Ground next row
Read all columns
Find which is pressed
Get scan code from table
return
yes
keys down?
All keys open?
no
no
keys down?no
yes
Key press in this row?
yes
mov p2,#0ffh
k1: mov p1, #0
mov a,p2
ani a, #00001111
cjne a,#00001111, k1
k2: acall delay
mov a,p2
ani a, #00001111
cjne a,#00001111, over
sjmp k2
Over1: mov p1, #11111110
mov a,p2
ani a, #00001111
cjne a,#00001111, row0
mov p1, #11111101
mov a,p2
ani a, #00001111
cjne a,#00001111, row1
mov p1, #11111011
mov a,p2
ani a, #00001111
cjne a,#00001111, row2
mov p1, #11110111mov a,p2ani a, #00001111cjne a,#00001111, row3ljmp k2
Row0: mov dptr,#kcode0sjmp find
Row1: mov dptr,#kcode1sjmp find
Row2: mov dptr,#kcode2sjmp find
Row3: mov dptr,#kcode3find: rrc a
jnc matchinc dptrsjmp find
match: clr amovc a,@a+dptrmov p0,aljmp k1
Look-up table
Org 300h
Kcode0: DB ‘0’ , ‘1’, ‘2’, ‘3’
Kcode1: DB ‘4’ , ‘5’, ‘6’, ‘7’
Kcode2: DB ‘8’ , ‘9’, ‘A’, ‘B’
Kcode3: DB ‘C’ , ‘D’, ‘E’, ‘F’
end
Servo Motor Control • Servo motor are so called “closed feedback” systems. This means that motor comes
with control circuit, which senses if motor mechanism is in desired location and if not it continuously corrects an error until motor reaches proper point.
• Servo motors are widely used in robotics, remote controlled planes, vehicles. So they come in many shapes and sizes, but they operate in almost the same way. Usually Servo motors are controlled by computer, microcontroller or even simple timer circuit. Of course you may find more advanced servos – R/C so called radio controlled. But again, they are same servos just it takes signals from receiver.
• servo motors are put in plastic box, but inside there is motor itself, gears and motor driving and control circuit
Servo control signals • Servo motor shaft is positioned with pulse width modulated signals. So all servos
comes with three wires (Power, Ground and Control). So pulses are sent via control wire. Usually in servos with rotation angle 90° signal width vary between 1 and 2ms. If pulse is more wide rotation continues until reaches mechanical limits.
Main program$mod51
org 0000h
main: mov a,p0
mov r1,a
Anl a,#01h
xrl a,#01h
jz m1
mov a,r1
anl a,#02h
xrl a,#02h
jz m1.5
mov a,r1
anl a,#04h
xrl a,#04h
jz m2
ajmp main
Program for 1msM1: Mov r6, #01h ; load 10d in r6
Acall delay ; call 1 ms delay
Clr p2.0 ; send 0 to port pin
Mov r6, #01h ; load 1d in r6
Acall delay ; call 1 ms delay
Delay:
Lp2: Mov r7, #0FAh
Lp1: Nop ; 1 cycle
Nop ; 1+1=2 cycles
Djnz r7, lp1 ; 1+1+2 = 4 cycles
Djnz r6, lp2 ; 4×250 = 1000 cycles = 1000 µs = 1 ms
• ret
Program 2ms
M2: Mov r6, #02h ; load 10d in r6
Acall delay ; call 1 ms delay
Clr p2.0 ; send 0 to port pin
Mov r6, #02h ; load 1d in r6
Acall delay ; call 1 ms delay
Delay:
Lp2: Mov r7, #0FAh
Lp1: Nop ; 1 cycle
Nop ; 1+1=2 cycles
Djnz r7, lp1 ; 1+1+2 = 4 cycles
Djnz r6, lp2 ; 4×250 = 1000 cycles = 1000 µs = 1 ms ret
Program 1.5msM1.5: Mov r6, #02h ; load 10d in r6
Acall delay ; call 1 ms delay Clr p2.0 ; send 0 to port pinMov r6, #02h ; load 1d in r6Acall delay ; call 1 ms delay
Delay: Lp2: Mov r7, #0FAh Lp1: Nop ; 1 cycle
Nop ; 1+1=2 cyclesDjnz r7, lp1 ; 1+1+2 = 4 cyclesDjnz r6, lp2 ; 4×375 = 1000 cycles = = 1.5 ms
Lp3: Mov r7, #07Dh Lp4: Nop ; 1 cycle
Nop ; 1+1=2 cyclesDjnz r7, lp4 ; 1+1+2 = 4 cyclesDjnz r6, lp3 ; 4×125 = 500 cycles = 500 µs = .5 ms ret
Minimum connection for AT89C51
40 pin base 110K 811.05MHz 1105 board 130pf 28.2KΩ 110μf 1AT89C51 1230/12v transformer 1B.Rectifier 11000μf 17805 1