MM Unit V

You might also like

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

20ECE107 MICROPROCESSORS AND

MICROCONTROLLERS

Unit - 5

1
8051 Programming for Stepper Motor

2
■ Stepper motors are used to translate electrical pulses into
mechanical movements. In some disk drives, dot matrix printers,
and some other different places the stepper motors are used.
The main advantage of using the stepper motor is the position
control.

■ A stepper motor usually have a number of field coils (phases) and


a toothed rotor. The step size of the motor is determined by the
number of phases and the number of teeth on the rotor. Step size
is the angular displacement of the rotor in one step. If a stepper
motor has 4 phases and 50 teeth, it takes 50×4=200 steps to
make one complete rotation. So step angle will be 360/200=1.8°

■ Stepper motor is operated in one of the three modes

3
■ Wave Drive Mode: In this mode, one coil is energized at a time as
shown in Table x. So all four coils are energized one after another.
This mode produces less torque than full step drive mode.
Steps Coil 1 Coil 2 Coil 3 Coil 4
1 1 0 0 0
Table x
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

■ Full Drive Mode: In this mode, two coils are energized at the
same time as shown in Table y. This mode produces more torque.
Here the power consumption is also high.

■ Half Drive Mode: This is basically a combination of wave and full


drive mode whereby coils are energized as shown in Table z. It
increases the angular rotation of the motor
4
Steps Coil 1 Coil 2 Coil 3 Coil 4
1 1 1 0 0
2 0 1 1 0 Table y
3 0 0 1 1
4 1 0 0 1

Steps Coil 1 Coil 2 Coil 3 Coil 4


1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
Table z
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1
5
Program 1: Design an Interface for interfacing stepper motor with
8051, and develop a program to run the motor in forward and
reverse direction. (assume the content of internal RAM 20H control
the direction, for example, for 01H the motor run forward and for
02H it run reverse)
U1
19 P0.0/AD0 39
XTAL1
P0.1/AD1 38
37 (AC)
P0.2/AD2
18 P0.3/AD3 36
XTAL2
P0.4/AD4 35
P0.5/AD5 34
P0.6/AD6 33
9 RST P0.7/AD7 32

P2.0/A8 21
P2.1/A9 22 U2 +88.8
P2.2/A10 23 COM 9
29 PSEN P2.3/A11 24 1 1C 16
1B
30 25 2 15
ALE P2.4/A12 2B 2C
31 26 3 14
EA P2.5/A13 3B 3C
27 4 13
P2.6/A14 4B 4C
28 5 12
P2.7/A15 5B 5C
6 11
6B 6C
1 10 7 10
P1.0 P3.0/RXD 7B 7C
2 11
P1.1 P3.1/TXD
3 P1.2 P3.2/INT0 12 ULN2003A
4 13
P1.3 P3.3/INT1
5 P1.4 P3.4/T0 14
6 15
P1.5 P3.5/T1
7 P1.6 P3.6/WR 16
8 17
P1.7 P3.7/RD
AT89C51

6
org 8000h REVERSE:
MAIN: MOV A,#09H
MOV A, 20H MOV P2,A
MOV C, ACC.0 ACALL DELAY
JC FORWARD MOV A,#03H
MOV C, ACC.1 MOV P2,A
JC REVERSE ACALL DELAY
SJMP MAIN MOV A,#06H
MOV P2,A
FORWARD: ACALL DELAY
MOV A,#0CH MOV A,#0CH
MOV P2,A MOV P2,A
ACALL DELAY ACALL DELAY
MOV A,#06H SJMP MAIN
MOV P2,A DELAY :
ACALL DELAY MOV R2,#02H
MOV A,#03H LOOP3: MOV R1,#02H
MOV P2,A LOOP2: MOV R0,#0ffH
ACALL DELAY LOOP1: DJNZ R0, LOOP1
MOV A,#09H DJNZ R1,LOOP2
MOV P2,A DJNZ R2,LOOP3
ACALL DELAY RET
SJMP MAIN end
7
Proteus simulation (clockwise direction)
(* Simulation visible in PPT file in presentation mode)
*just for understanding (Not for exam)

8
Proteus simulation (anticlockwise direction)
(* Simulation visible in PPT file in presentation mode)
*just for understanding (Not for exam)

9
8051 Programming for LCD

10
LCD PIN Details

Pin Symbol I/O Descriptions


1 VSS - Ground
2 VCC - +5V Power supply
3 VEE - Power supply for control contrast
4 Rs I Rs=0 to select command register
Rs=1 to select data register
5 R/W I R/W=0 for write
R/W=1 for Read
6 E I Enable
7-14 D0-D7 I/O 8-bit data bus
15 LEDA I Anode for backlight LED
“ e 1S6erve wLEleDdKge, wIith wCaletdhgoed”e for backlight LED 11
LCD Command Details
Code (Hex) Command to LCD Instruction Register
1 Clear display screen
2 Return home
4 Decrement cursor (shift cursor to left)
6 Increment cursor (shift cursor to right)
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
F 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 to 1st line
C0 Force cursor to beginning to 2nd line
38 2 lines and 5x7 matrix

12
LCD Timing for Write

tDSW = Data set up time


= 195 ns (minimum)

Data
tH = Data holdtime
tH = 10 ns (minimum)
E tDSW

R/W tAS tPWH tAH

RS
tAH = Hold time after E has
come down for both RS and
R/W = 10 ns (minimum)

tPWH = Enable pulse width


= 450 ns (minimum)

tAS = Setup time prior to E


(going high) for both RS and
R/W = 140 ns (minimum)
Note : Write requires an H-to-L pulse for the E pin
13
Program 2: Consider an LCD display is interfaced with the 8051
microcontroller as shown in Figure Below. Develop a complete
program to display the strings ‘MITS’ at the LCD.

LCD2
LM032L

U1

VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
19 P0.0/AD0 39
XTAL1
38
P0.1/AD1

7
8
9
1
2
3

4
5
6

11
10

12
13
14
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3
35
P0.4/AD4
34
P0.5/AD5
33
P0.6/AD6
9 32
RST P0.7/AD7

P2.0/A8 21
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 P1.0 P3.0/RXD 10
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/W R
8 17
P1.7 P3.7/RD

AT89C51

14
org 8000h MOV A,#'S'
LcdComWrite: LcdDataWrite: MOV P2,A
CLR P3.0 SETB P3.0 ACALL Delay
SETB P3.2
MOV A,#38H MOV A,#'M' CLR P3.2
MOV P2,A MOV P2,A
ACALL Delay ACALL Delay Last: SJMP Last
SETB P3.2 SETB P3.2
CLR P3.2 CLR P3.2 Delay: MOV R2,#2H
Loop3: MOV R1,#80H
MOV A,#0CH MOV A,#'I' Loop2: MOV R0,#0FFH
MOV P2,A MOV P2,A Loop1: DJNZ R0, Loop1
ACALL Delay ACALL Delay DJNZ R1,Loop2
SETB P3.2 SETB P3.2 DJNZ R2,Loop3
CLR P3.2 CLR P3.2 RET

MOV A,#80H MOV A,#'T' END


MOV P2,A MOV P2,A
ACALL Delay ACALL Delay
SETB P3.2 SETB P3.2
CLR P3.2 CLR P3.2
15
LCD2
LM032L

U1

VDD
VEE
VSS

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
19 39
XTAL1 P0.0/AD0
38
P0.1/AD1

7
8
9
10
11
12
13
14
2
1

4
5
6
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3
35
P0.4/AD4
34
P0.5/AD5
33
P0.6/AD6
9 32
RST P0.7/AD7
21
P2.0/A8
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 10
P1.0 P3.0/RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/WR
8 17
P1.7 P3.7/RD
AT89C51
rve Knowledge, with Knowledge” 16
Program 3: Consider an LCD display is interfaced with the 8051
microcontroller as shown in Figure Below. Develop a complete
program to display the strings ‘MITS COLLEGE’ at the 1st Row, and
the string “MADANAPALLE” at the 2nd row. (This time, assume the
LCD commands and display strings are at the memory)
LCD2
LM032L

U1

VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
19 39
XTAL1 P0.0/AD0
38
P0.1/AD1

1
2
3

4
5
6

7
8
9
10
11
12
13
14
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3 35
P0.4/AD4 34
P0.5/AD5 33
P0.6/AD6 32
9
RST P0.7/AD7

21
P2.0/A8
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11 25
30
ALE P2.4/A12 26
31
EA P2.5/A13 27
P2.6/A14 28
P2.7/A15

1 10
P1.0 P3.0/RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0
4 13
5 P1.3 P3.3/INT1 14
6 P1.4 P3.4/T0 15
7 P1.5 P3.5/T1 16
8 P1.6 P3.6/W R 17
P1.7 P3.7/RD

AT89C51

17
Org 8000H
Last: SJMP Last
CLR P3.0 CLR P3.0
MOV DPTR,#LcdCom1 MOV DPTR,#LcdCom2 Delay: MOV R2,#2H
Repeat1:CLR A Repeat11:CLR A Loop3: MOV R1,#80H
MOVC A,@A+DPTR MOVC A,@A+DPTR Loop2: MOV R0,#0FFH
JZ Next1 JZ Next11 Loop1: DJNZ R0, Loop1
MOV P2,A MOV P2,A DJNZ R1,Loop2
ACALL Delay ACALL Delay DJNZ R2,Loop3
SETB P3.2 SETB P3.2 RET
CLR P3.2 CLR P3.2
INC DPTR INC DPTR LcdCom1: DB 38H,0EH,80H,0
SJMP Repeat1 SJMP Repeat11 LcdData1: DB "MITS COLLEGE",0
Next1: Next11: LcdCom2: DB 38H,0EH,0C0H,0
SETB P3.0 SETB P3.0 LcdData2: DB "MADANAPALLE",0
MOV DPTR,#LcdData1 MOV DPTR,#LcdData2
Repeat2:CLR A Repeat22:CLR A END
MOVC A,@A+DPTR MOVC A,@A+DPTR
JZ Next2 JZ Next22
MOV P2,A MOV P2,A
ACALL Delay ACALL Delay
SETB P3.2 SETB P3.2
CLR P3.2 CLR P3.2
INC DPTR INC DPTR
SJMP Repeat2 SJMP Repeat22
Next2: Next22:
18
U1

VDD
VSS

VEE

RW
RS

D4
D0
D1
D2
D3

D5
D6
D7
E
19 39
XTAL1 P0.0/AD0
38
P0.1/AD1

7
8
9
10
11
12
13
14
4
1
2
3

5
6
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3
35
P0.4/AD4
34
P0.5/AD5
33
P0.6/AD6
9 32
RST P0.7/AD7
21
P2.0/A8
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 10
P1.0 P3.0/RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/WR
8 17
P1.7 P3.7/RD
“We Serve Knowledge, with Knowledge”
AT89C51 19
8051 Programming for Keyboard

20
■ Keyboards are organized in a matrix of rows and columns
➢ The CPU accesses both rows and columns through
ports
▪ Therefore, with two 8-bit ports, an 8 x 8 matrix of
keys can be connected to a microprocessor
➢ When a key is pressed, a row and a column
make a contact
▪ Otherwise, there is no connection between rows
and columns
■ In IBM PC keyboards, a single microcontroller takes care
of hardware and software interfacing

21
■ Keyboards are organized in a matrix of rows and columns
➢ The CPU accesses both rows and columns through
ports
▪ Therefore, with two 8-bit ports, an 8 x 8 matrix of
keys can be connected to a microprocessor
➢ When a key is pressed, a row and a column
make a contact
▪ Otherwise, there is no connection between rows
and columns
■ In IBM PC keyboards, a single microcontroller takes care
of hardware and software interfacing

22
■ Consider a 4x4 matrix connected to two ports
➢ The rows are connected to an output port and the
columns are connected to an input port
If all the rows
are grounded
and a key is Vcc
pressed, one of
the columns will
3 2 1 0
have 0 since the
key pressed D0
provides the 7 6 5 4
path to ground D1
B A 9 8
If no key has D2
been pressed, F E D C
reading the D3
input port will
yield 1s for all Port 1
columns since (Out) D3 D2 D1 D0 Port 2
they are all (In)
connected to
high (Vcc)

23
■ The Algorithm for detection and identification of key
activation goes through the following stages:
1. 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
2. To see if any key is pressed, the columns are scanned over
and over in an infinite loop until one of them has a 0 on it
▪ After the key press detection, it waits 20 ms for the bounce
and then scans the columns again
(a) to ensures that the first key press detection was not an
erroneous one due a spike noise
(b) If after the 20-ms delay the key is still pressed, it goes
back into the loop to detect a real key press

24
3. To detect which row key press belongs to, it grounds one row
at a time, reading the columns each 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 until it
finds the row the key press belongs to.
▪ Upon finding the row that the key press belongs to, it sets
up the starting address for the look-up table holding the
scan codes (or ASCII) for that row.
4. To identify the key press, it rotates the column bits, one bit
at a time, into the carry flag and checks to see if it is low
▪ Upon finding the zero, it pulls out the ASCII code for
that key from the look-up table.
▪ otherwise, it increments the pointer to point to the
next element of the look-up table

25
■ The Flowchart for detection and identification of key
activation goes through the following stages:
1 2

Read all columns Ground next row


Start

no key
Ground all rows no key
press? press?
yes
yes
Read all columns
Wait for debounce Find which key
is pressed
no All keys
open? Read all columns
Get scan code
yes
from table
no key
1 press?
yes Return

“We Serve Knowledge, with Knowledge”


2 26
Program 4: A hexadecimal keypad as shown in Figure is interfaced
with the 8051 microcontroller. P3.0-P3.3 connected to rows, P1.0-
P1.3 to columns of the keypad. Develop a complete program that
sends the ASCII code of the key pressed through Port 2

27
Org 8000H SCAN: FIND: RRC A
MOV P3,#00001110B JNC MATCH
MOV P1,#0FFH MOV A,P1 INC DPTR
ANL A,#00001111B SJMP FIND
KEY_CHECK: CJNE A,#00001111B,ROW_0 MATCH: CLR A
MOV P3,#00000000B MOVC A,@A+DPTR
MOV P3,#00001101B
MOV A,P1 MOV P2,A
MOV A,P1
ANL A,#00001111B ANL A,#00001111B
SETB P0.0
CJNE A,#00001111B,BOUNCE1 CJNE A,#00001111B,ROW_1 ACALL Delay
SJMP KEY_CHECK SETB P0.2
MOV P3,#00001011B CLR P0.2
BOUNCE1: MOV A,P1 SJMP KEY_CHECK
ACALL DELAY ANL A,#00001111B Last: SJMP Last
MOV A,P1 CJNE A,#00001111B,ROW_2
ANL A,#00001111B Delay: MOV R2,#3H
CJNE A,#00001111B,BOUNCE2 MOV P3,#00000111B Loop3: MOV R1,#80H
SJMP BOUNCE1 MOV A,P1 Loop2: MOV R0,#0FFH
ANL A,#00001111B Loop1: DJNZ R0, Loop1
BOUNCE2: CJNE A,#00001111B,ROW_3 DJNZ R1,Loop2
ACALL DELAY SJMP BOUNCE1 DJNZ R2,Loop3
MOV A,P1 RET
ROW_0: MOV DPTR,#KCODE0 KCODE0: DB '÷','9','8','7'
ANL A,#00001111B SJMP FIND
CJNE A,#00001111B,SCAN KCODE1: DB '*','6','5','4'
ROW_1: MOV DPTR,#KCODE1
SJMP BOUNCE2 SJMP FIND KCODE2: DB '-','3','2','1'
ROW_2: MOV DPTR,#KCODE2 KCODE3: DB ‘+','=','0','C’
SJMP FIND
ROW_3: MOV DPTR,#KCODE3 END
SJMP FIND
28
LCD2
LM032L

VDD
VSS

VEE
R2 R3

RW
RS

D3
D0
D1
D2

D4
D5
D6
D7
E
PULL
UPPULLUP

7
8
9
1
2
3

4
5
6

14
10
11
12
13
U1
A 7 8 9
19 XTAL1 39
P0.0/AD0
P0.1/AD1 38
P0.2/AD2
37
B 4 5 6
18 XTAL2 P0.3/AD3 36
P0.4/AD4 35
P0.5/AD5
P0.6/AD6
34
33
C 1 2 3
9 RST P0.7/AD7 32

P2.0/A8
P2.1/A9
P2.2/A10
21
22
23
D
ON
C 0 = +

4
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 10
P1.0 P3.0/RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/WR
8 17
P1.7 P3.7/RD
AT89C51

29 29
ARM Programming for Stepper Motor

30
Program 5:
A stepper motor is connected to STM32 ARM microcontroller. Pins
PD15, PD13,PD11, PD9 are used to drive the stepper motor while
the pin PC1 is use to control the direction. Develop a C program to
run the motor in forward/reverse direction

31
i n t main(void)
{
HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

while ( 1 )
{

( * Weshould enter the code here; and please


see the code on next s l i d e )

32
uint8_t pinvalue;
pinvalue=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_1); // Direction control input
if(pinvalue==1)
{ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 0); HAL_Delay(1);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 0);


HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 0); HAL_Delay(1);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 0);


HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 0); HAL_Delay(1);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 0);


HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 1); HAL_Delay(1);
}
33
else
{ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 1); HAL_Delay(1);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 0);


HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 0); HAL_Delay(1);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 0);


HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 0); HAL_Delay(1);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, 1);


HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 0); HAL_Delay(1);
}

34
ARM Programming for Digital Clock

35
Program 6:
An LCD is connected to STM32 ARM microcontroller. Pins PD0 and
PD1 are connected to RS and E pins, while pins PE0-PE7 are
connected to data pins of the LCD. Develop a C program to display a
digital clock in HH:MM:SS format on the LCD.

36
i n t main(void)
{
HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

while ( 1 )
{

( * Weshould enter the main program code here;


and please see the code on next s l i d e )

}
( * Weshould enter the subroutine program code
here; and please see the code on next s l i d e )
37
for (uint8_t hh=00; hh<=23; hh++)
{ for (uint8_t mm=00; mm<=59; mm++)
{ for (uint8_t ss=00; ss<=59; ss++)
{ LCD_Write(0x80,0); // cursor to the 1st line beginning

LCD_Write((hh&0xF0)>>4 +48,1);
LCD_Write((hh&0x0F)+48,1);

LCD_Write(0X3A,1);

LCD_Write((mm&0xF0)>>4 +48,1);
LCD_Write((mm&0x0F)+48,1);

LCD_Writ(0X3A,1);

LCD_Write((ss&0xF0)>>4 +48,1);
LCD_Write((ss&0x0F)+48,1);

HAL_Delay(1000); // 1 second delay


}
}
}

38
void LCD_Write(uint8_t value, uint8_t rs)
{ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0, rs); // RS=0 or 1 based on the calling
//function
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_0, value >>0 & 0x01);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, value>>1 & 0x01);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, value>>2 & 0x01);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, value>>3 & 0x01);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, value>>4 & 0x01);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, value>>5 & 0x01);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6, value>>6 & 0x01);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7, value>>7 & 0x01);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, 1); // Enable=1


HAL_Delay(1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, 0); // Enable=0
}

39
8051 Programming for Serial Data Transmit

40
❑ The steps that 8051 goes through in transmitting a character via
TxD
1. TMOD register is loaded with the value 20H, indicating the
use of timer 1 in mode 2 (8-bit auto-reload) to set baud rate
2. The TH1 is loaded with one of the values to set baud rate
for serial data transfer
3. The SCON register is loaded with the value 50H, indicating
serial mode 1, where an 8- bit data is framed with start and
stop bits
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is loaded
into SBUF register
7. The TI flag bit is monitored with the use of instruction JNB TI,
xx to see if the character has been transferred completely
8. To transfer the next byte, go to step 5

RECALL the above steps you learnt in unit THREE


(*Not for Exam*)
41
Program 7: Write a program for the 8051 to transfer “MITS” serially at 9600
baud, 8-bit data, 1 stop bit.

Solution:

42
43
8051 Programming for Traffic Light

44
Program 8: Develop an 8051 assembly language program
to control the traffic sequence on a 4-way road crossing.
Each side should have signals for green, amber, red and
pedestrian crossing.

✓ Assume that the Traffic Signals are connected to Port


A, B and C as shown in the look-up table below.

✓ And the HEX equivalents are stored starting from


memory 9000H.

45
North East South West HEX
RA G G G P R A G G G P R A G G G P R A G G G P Equivalent
em r r r e e m r r r e e m r r r e e m r r r e
db e e e d d b e e e d d b e e e d d b e e e d
e e e e e e e e e e e e e e e e e e e e
r n n n s r n n n s r n n n s r n n n s
1 2 3 1 2 3 1 2 3 1 2 3
PP P P P P P P P P P P P P P P P P P P P P P P
AA A A A A A A B B B B B B B B C C C C C C C C
76 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
1 1 1 1 1 1 1 1 1 1 3E 18 61
1 1 1 1 1 1 1 1 46 18 61
1 1 1 1 1 1 1 1 1 1 84 F8 61
1 1 1 1 1 1 1 1 85 18 61
1 1 1 1 1 1 1 1 1 1 86 13 E1
1 1 1 1 1 1 1 1 86 14 61

1 1 1 1 1 1 1 1 1 1 86 18 4F
1 1 1 1 1 1 1 1 86 18 51
1 0 1 0 1 0 1 0 82 08 20 46
MOV DPTR, #E803 INC DPTR
MOV A, #80 MOVX A, @DPTR
MOVX @DPTR, A PUSH 82
PUSH 83
MOV DPTR, #E802
REPEAT_CYCLE MOV DPTR, #9000
MOVX @DPTR, A
NEXT_CYCLE MOVX A, @DPTR POP 83
PUSH 82 POP 82
PUSH 83
MOV DPTR, #E800 INC DPTR
MOVX @DPTR, A LCALL DELAY
POP 83 MOV A,82
POP 82 CJNE A, #1B, NEXT_CYCLE
SJMP REPEAT_CYCLE

INC DPTR DELAY MOV R1, #05


MOVX A, @DPTR LOOP3 MOV R2, #0FF
PUSH 82 LOOP2 MOV R3, #0FF
PUSH 83 LOOP1 DEC R3
MOV DPTR, #E801 CJNE R3, #00, LOOP1
MOVX @DPTR, A DEC R2
POP 83 CJNE R2, #00, LOOP2
DEC R1
POP 82
CJNE R1, #00, LOOP3
RET

47
8051 Programming for DAC

48
DIGITAL TO ANALOG CONVERTER
Features of DAC 0800
❑ Settling time output current= 100ns
❑ Full scale error =± 1 LSB
❑ Nonlinearity over temperature = ±1 %
❑ Con be directly interfaced with TTL, CMOS
❑ Wide power supply range ±4.5 V to ±18 V
❑ Low power consumption 33mW at ±5V
❑ Complementary current outputs
❑ Differential output voltage of 20Vpp

Figure A: Pin configuration of DAC 0800


49
Figure B: DAC 0800 with OP-AMP
based current to voltage converter

❑ As shown in the Figure B, the chip outputs two currents, i.e., positive IOUT and
negative IOUT, so convert these current signals we have to use current to voltage
converter
50
Program 9: Show the simple sketch how to interface an 8-bit DAC
with 8051 microcontroller. Develop an
❑ Assembly language program to generate a square wave.

MOV DPTR, #E803 Write the control


MOV A, #80 word #80 into
MOVX @DPTR, A control register
✓ Sketch the DAC interface
shown in previous slide AGAIN MOV A, #0FF Write #FF
here MOV DPTR, #E800 corresponds to TON
MOVX @DPTR, A of the square wave
MOV A, #90
LOOP1 DEC A
JNZ LOOP1

MOV A, #00 Write #00


MOV DPTR, #E800 corresponds to TOFF
MOVX @DPTR, A of the square wave
MOV A, #90
LOOP2 DEC A
JNZ LOOP2

SJMP AGAIN Repeat forever


“We Serve Knowledge, with Knowledge” 51
Program 10: Show the simple sketch how to interface an 8-bit DAC
with 8051 microcontroller. Develop an
❑ Assembly language program to generate a triangular wave

MOV DPTR, #E803 Write the control word #80


MOV A, #80 into control register
MOVX @DPTR, A
✓ Sketch the DAC interface
shown in previous slide AGAIN MOV A, #00 Write #00 to #FF
here MOV DPTR, #E800 corresponds to rising edge
LOOP1 MOVX @DPTR, A of the triangular wave
INC A
CJNE A,#0FF, LOOP1

MOV A, #0FF Write #FF to #00


MOV DPTR, #E800 corresponds to falling edge
LOOP2 MOVX @DPTR, A of the triangular wave
DEC A
CJNE A,#00, LOOP2

SJMP AGAIN Repeat forever

52

You might also like