Top Banner
MICRO CONTROLLER PROGRAMMING & APPLICATIONS UNIT V Mr. S. VINOD LECTURER EEE DEPARTMENT
35

8051-unit-5.ppt.pptx

Jan 11, 2016

Download

Documents

8051 Microcontroller
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 8051-unit-5.ppt.pptx

MICRO CONTROLLER PROGRAMMING & APPLICATIONS

UNIT V

Mr. S. VINOD

LECTURER

EEE DEPARTMENT

Page 2: 8051-unit-5.ppt.pptx

Data Transfer, Manipulation, Control & I/O instructions Simple programming exercises key board

and display interface Closed loop control of servo motor stepper motor control.

Page 3: 8051-unit-5.ppt.pptx

1.Program for addition of two 8-bit numbers

Algorithm:Clear c – register for carryGet the data immediatelyAdd the two dataStore 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

Page 4: 8051-unit-5.ppt.pptx

2.Subtraction Of Two 8-bit Number

ALGORITHM:Clear c – register for carryGet the data immediatelySubtract the two dataStore the result in memory pointed by DPTR

Program:

ORG 4100CLR CMOV A,# data 1SUBB A,# data 2MOV DPTR, #4500MOVX @ DPTR,A

HERE: SJMP HERE

Page 5: 8051-unit-5.ppt.pptx

3.Multiplication of two 8-bit number

ALGORITHM:Get the data in A-regGet the value to be multiplied in B-regMultiply the two dataThe higher order of the result is in B-regThe lower order of the result is in A- regStore the result

PROGRAM:ORG 4100CLR CMOV A,# DATA1MOV B,# DATA1MUL ABMOV DPTR,#4500MOVX @DPTR,#AINC DPTRMOV A,BMOVX @DPTR,A

HERE: SJMP HERE

Page 6: 8051-unit-5.ppt.pptx

4.Division of two 8-bit numberALGORITHM:Get the data in A-regGet the value to be divided in B-regdivided the two dataThe quotient is in A-regThe remainder is in B- regStore the result

PROGRAM:ORG 4100CLR CMOV A,# DATA1MOV B,# DATA1DIV ABMOV DPTR,#4500MOVX @DPTR,#AINC DPTRMOV A,BMOVX @DPTR,A

HERE: SJMP HERE

Page 7: 8051-unit-5.ppt.pptx

5.RAM ADDRESSING

Program:Bit Addressing

SETB PSW.3MOV R0,#data1MOV A, #data 2ADD A,R0MOV

DPTR,#4500MOVX @DPTR,

AHERE: 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

Page 8: 8051-unit-5.ppt.pptx

6.INTERFACING STEPPER MOTOR

Page 9: 8051-unit-5.ppt.pptx

STEPPER MOTOR

Page 10: 8051-unit-5.ppt.pptx

INTERFACING STEPPER MOTOR

ORG 4100START: MOV DPTR,#4500

MOV R0,#04AGAIN; MOVX A,@DPTR

PUSH DPHPUSH DPLMOV

DPTR,#FFCOHMOV R2,

#04HMOV R1,#

FFHDLY1: MOV R3,#FFHDLY: DJNZ R3,

DLYDJNZ R1,

DLY1MOVX

@DPTR, APOP DPLPOP DPINC DPTRDJNZ R0,

AGAINSJMP START

DATA:4500. 094501. 054502. 064503. 0A

Page 11: 8051-unit-5.ppt.pptx

7.Find the maximum number from a given 8 bit number

MOV DPTR, #2000MOV R0,#0AMOV R3,#00

AGAIN: MOVX A, @DPTRCJNE A,R3, NEAJMP SKIP

NE: JC SKIPMOV R3 ,A

SKIP: INC DPTRDJNZ R0, AGAIN

Page 12: 8051-unit-5.ppt.pptx

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

Page 13: 8051-unit-5.ppt.pptx

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

Page 14: 8051-unit-5.ppt.pptx

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

Page 15: 8051-unit-5.ppt.pptx

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

Page 16: 8051-unit-5.ppt.pptx

Pin description for LCD

Page 17: 8051-unit-5.ppt.pptx
Page 18: 8051-unit-5.ppt.pptx

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

Page 19: 8051-unit-5.ppt.pptx

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

Page 20: 8051-unit-5.ppt.pptx

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

Page 21: 8051-unit-5.ppt.pptx

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

Page 22: 8051-unit-5.ppt.pptx

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

Page 23: 8051-unit-5.ppt.pptx
Page 24: 8051-unit-5.ppt.pptx

Scanning and identifying the keyTo 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 0After a key is pressed mp wait for 20ms and then scans the column againTo 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 continuesAfter finding the key, it sets up the starting address for the lookup table holding the scan codesTo 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.

Page 25: 8051-unit-5.ppt.pptx

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

Page 26: 8051-unit-5.ppt.pptx

mov p2,#0ffhk1: mov p1, #0

mov a,p2ani a, #00001111cjne a,#00001111, k1

k2: acall delaymov a,p2ani a, #00001111cjne a,#00001111, oversjmp k2

Over1: mov p1, #11111110mov a,p2ani a, #00001111cjne a,#00001111, row0mov p1, #11111101mov a,p2ani a, #00001111cjne a,#00001111, row1mov p1, #11111011mov a,p2ani a, #00001111cjne 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

Page 27: 8051-unit-5.ppt.pptx

Look-up table

Org 300hKcode0: DB ‘0’ , ‘1’, ‘2’, ‘3’Kcode1: DB ‘4’ , ‘5’, ‘6’, ‘7’Kcode2: DB ‘8’ , ‘9’, ‘A’, ‘B’Kcode3: DB ‘C’ , ‘D’, ‘E’, ‘F’end

Page 28: 8051-unit-5.ppt.pptx

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

Page 29: 8051-unit-5.ppt.pptx

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.

Page 30: 8051-unit-5.ppt.pptx

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

Page 31: 8051-unit-5.ppt.pptx

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

Page 32: 8051-unit-5.ppt.pptx

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

Page 33: 8051-unit-5.ppt.pptx

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

Page 34: 8051-unit-5.ppt.pptx

Minimum connection for AT89C51

Page 35: 8051-unit-5.ppt.pptx

40 pin base 110K 811.05MHz 1105 board 130pf 28.2KΩ 110μf 1AT89C51 1230/12v transformer 1B.Rectifier 11000μf 17805 1