Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

121
Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas

Transcript of Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

Page 1: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

Lecture 3

Programming the 8051 Microcontroller

Dr. Konstantinos Tatas

Page 2: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

2

RegistersA

B

R0

R1

R3

R4

R2

R5

R7

R6

DPH DPL

PC

DPTR

PC

Some 8051 16-bit Register

Some 8-bit Registers of the 8051

A: AccumulatorB: Used specially in MUL/DIVR0-R7: GPRs

Page 3: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

8051 Programming using Assembly

Page 4: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

4

The MOV Instruction – Addressing Modes

MOV dest,source ; dest = source

MOV A,#72H ;A=72H

MOV A, #’r’ ;A=‘r’ OR 72H

MOV R4,#62H ;R4=62H

MOV B,0F9H ;B=the content of F9’th byte of RAM

MOV DPTR,#7634H

MOV DPL,#34H

MOV DPH,#76H

MOV P1,A ;mov A to port 1

Note 1:MOV A,#72H ≠ MOV A,72H

After instruction “MOV A,72H ” the content of 72’th byte of RAM will replace in Accumulator.

8086 8051MOV AL,72H MOV A,#72H

MOV AL,’r’ MOV A,#’r’

MOV BX,72H

MOV AL,[BX] MOV A,72H

Note 2:MOV A,R3 ≡ MOV A,3

Page 5: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

5

Arithmetic InstructionsADD A, Source ;A=A+SOURCE

ADD A,#6 ;A=A+6

ADD A,R6 ;A=A+R6

ADD A,6 ;A=A+[6] or A=A+R6

ADD A,0F3H ;A=A+[0F3H]

Page 6: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

6

Set and Clear Instructions

SETB bit ; bit=1CLR bit ; bit=0

SETB C ; CY=1SETB P0.0 ;bit 0 from port 0 =1SETB P3.7 ;bit 7 from port 3 =1SETB ACC.2 ;bit 2 from ACCUMULATOR =1SETB 05 ;set high D5 of RAM loc. 20h

Note:

CLR instruction is as same as SETBi.e:

CLR C ;CY=0

But following instruction is only for CLR:CLR A ;A=0

Page 7: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

7

SUBB A,source ;A=A-source-CY

SETB C ;CY=1

SUBB A,R5 ;A=A-R5-1

ADC A,source ;A=A+source+CY

SETB C ;CY=1

ADC A,R5 ;A=A+R5+1

Page 8: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

8

DEC byte ;byte=byte-1INC byte ;byte=byte+1

INC R7DEC ADEC 40H ; [40]=[40]-1

CPL A ;1’s complementExample:

MOV A,#55H ;A=01010101 BL01: CPL A

MOV P1,AACALL DELAYSJMP L01

NOP & RET & RETI

All are like 8086 instructions.

CALL

Page 9: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

9

Logic Instructions

ANL byte/bit

ORL byte/bit

XRL byte

EXAMPLE:

MOV R5,#89H

ANL R5,#08H

Page 10: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

10

Rotate Instructions

• RR A Accumulator rotate right  • RL A Accumulator Rotate left 

• RRC A Accumulator Rotate right through the carry. 

• RLC A Accumulator Rotate left through the carry. 

 

                               

 

                                

                                  

Page 11: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

11

Structure of Assembly language and Running an 8051 program

ORG 0H MOV R5,#25H MOV R7,#34H MOV A,#0 ADD A,R5 ADD A,#12H

HERE: SJMP HERE END

EDITORPROGRAM

ASSEMBLERPROGRAM

LINKERPROGRAM

OHPROGRAM

Myfile.asm

Myfile.obj

Other obj fileMyfile.lst

Myfile.abs

Myfile.hex

Page 12: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

12

Memory mapping in 8051

• ROM memory map in 8051 family

0000H

0FFFH

0000H

1FFFH

0000H

7FFFH

8751AT89C51 8752

AT89C52

4k

DS5000-32

8k 32k

from Atmel Corporationfrom Dallas Semiconductor

Page 13: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

13

• RAM memory space allocation in the 8051

7FH

30H

2FH

20H

1FH

17H

10H

0FH

07H

08H

18H

00HRegister Bank 0

(Stack )Register Bank 1

Register Bank 2

Register Bank 3

Bit-Addressable RAM

Scratch pad RAM

Page 14: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

14

8051 Flag bits and the PSW register • PSW Register

CY AC F0 RS1 OVRS0 P--

CYPSW.7Carry flagACPSW.6Auxiliary carry flag--PSW.5Available to the user for general purpose

RS1PSW.4Register Bank selector bit 1RS0PSW.3Register Bank selector bit 0OVPSW.2Overflow flag--PSW.1User define bitPPSW.0Parity flag Set/Reset odd/even parity

RS1 RS0 Register Bank Address

0 0 0 00H-07H

0 1 1 08H-0FH

1 0 2 10H-17H

1 1 3 18H-1FH

Page 15: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

15

Instructions that Affect Flag Bits:

Note: X can be 0 or 1

Page 16: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

16

Example:MOV A,#38HADD A,#2FH

38 00111000+2F +00101111 ---- -------------- 67 01100111

CY=0 AC=1 P=1

Example:MOV A,#88HADD A,#93H

88 10001000+93 +10010011 ---- -------------- 11B 00011011

CY=1 AC=0 P=0

Example:MOV A,#9CHADD A,#64H

9C 10011100+64 +01100100 ---- -------------- 100 00000000

CY=1 AC=1 P=0

Page 17: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

17

Addressing Modes

• Immediate

• Register

• Direct

• Register Indirect

• Indexed

Page 18: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

18

Immediate Addressing ModeMOV A,#65HMOV A,#’A’MOV R6,#65HMOV DPTR,#2343HMOV P1,#65H

Example :

Num EQU 30…MOV R0,NumMOV DPTR,#data1…ORG 100Hdata1: db “Example”

Page 19: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

19

Example

• Write the decimal value 4 on the SSD in the following figure. Switch the decimal point off.

Page 20: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

20

Register Addressing Mode

MOV Rn, A ;n=0,..,7

ADD A, Rn

MOV DPL, R6

MOV DPTR, A

MOV Rm, Rn

Page 21: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

21

Direct Addressing ModeAlthough the entire of 128 bytes of RAM can be accessed using direct addressing mode, it is most often used to access RAM loc. 30 – 7FH.

MOV R0, 40HMOV 56H, AMOV A, 4 ; ≡ MOV A, R4MOV 6, 2 ; copy R2 to R6

; MOV R6,R2 is invalid !

SFR register and their address

MOV 0E0H, #66H ; ≡ MOV A,#66HMOV 0F0H, R2 ; ≡ MOV B, R2MOV 80H,A ; ≡ MOV P1,A

Page 22: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

22

Register Indirect Addressing Mode• In this mode, register is used as a pointer to the data.

MOV A,@Ri ; move content of RAM loc.Where address is held by Ri into A

( i=0 or 1 )MOV @R1,B

In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB insructions.

Example:Write a program to copy a block of 10 bytes from RAM location sterting at 37h to RAM location starting at 59h.

Solution:MOV R0,37h ; source pointerMOV R1,59h ; dest pointer MOV R2,10 ; counter

L1: MOV A,@R0MOV @R1,AINC R0INC R1DJNZ R2,L1

jump

Page 23: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

23

Indexed Addressing Mode And On-Chip ROM Access

• This mode is widely used in accessing data elements of look-up table entries located in the program (code) space ROM at the 8051

MOVC A,@A+DPTRA= content of address A +DPTR from ROM

Note:Because the data elements are stored in the program (code ) space ROM of the 8051, it uses the instruction MOVC instead of MOV. The “C” means code.

Page 24: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

24

• Example:Assuming that ROM space starting at 250h contains “Hello.”, write a program to transfer the bytes into RAM locations starting at 40h.

Solution:ORG 0MOV DPTR,#MYDATAMOV R0,#40H

L1: CLR AMOVC A,@A+DPTRJZ L2MOV @R0,AINC DPTRINC R0SJMP L1

L2: SJMP L2;-------------------------------------

ORG 250HMYDATA: DB “Hello”,0

END

Notice the NULL character ,0, as end of string and how we use the JZ instruction to detect that.

Page 25: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

25

• Example:Write a program to get the x value from P1 and send x2 to P2, continuously .

Solution:ORG 0 ;code segmentMOV DPTR, #TAB1 ;moving data segment to data pointerMOV A,#0FFH ;configuring P1 as input portMOV P1,A

L01:MOV A,P1 ;reading value from P1MOVC A,@A+DPTRMOV P2,ASJMP L01

;----------------------------------------------------ORG 300H ;data segment

TAB1: DB 0,1,4,9,16,25,36,49,64,81

END

Page 26: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

26

External Memory Addressing

• MOVX A, @R1 ; A [R1] (in external memory)

• MOVX A, @DPTR

• MOVX @DPTR, A

Page 27: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

27

16-bit, BCD and Signed Arithmetic in 8051

Exercise:

Write a program to add n 16-bit number. Get n from port 1. And sent Sum to SSDa) in hexb) in decimal

Write a program to subtract P1 from P0 and send result to LCD(Assume that “ACAL DISP” display A to SSD )

Page 28: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

28

MUL & DIV

• MUL AB ;B|A = A*BMOV A,#25HMOV B,#65HMUL AB ;25H*65H=0E99

;B=0EH, A=99H• DIV AB ;A = A/B, B = A mod B

MOV A,#25MOV B,#10DIV AB ;A=2, B=5

Page 29: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

29

Stack in the 8051

• The register used to access the stack is called SP (stack pointer) register.

• The stack pointer in the 8051 is only 8 bits wide, which means that it can take value 00 to FFH. When 8051 powered up, the SP register contains value 07.

7FH

30H

2FH

20H

1FH

17H10H

0FH

07H

08H

18H

00HRegister Bank 0

(Stack )Register Bank 1

Register Bank 2

Register Bank 3

Bit-Addressable RAM

Scratch pad RAM

Page 30: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

30

Example:MOV R6,#25HMOV R1,#12HMOV R4,#0F3HPUSH 6PUSH 1PUSH 4

0BH

0AH

09H

08H

Start SP=07H

25

0BH

0AH

09H

08H

SP=08H

F3

12

25

0BH

0AH

09H

08H

SP=10H

12

25

0BH

0AH

09H

08H

SP=09H

Page 31: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

31

Example (cont.)

POP 4POP1POP6

0BH

0AH

09H

08H

Start SP=07H

25

0BH

0AH

09H

08H

SP=08H

F3

12

25

0BH

0AH

09H

08H

SP=10H

12

25

0BH

0AH

09H

08H

SP=09H

Page 32: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

32

How to use the stack

• You can use the stack as temporary storage for variables when calling functions

RLC A ;you can only rotate ACall functionDIV AB ; A has the wrong value!!!!!…function: MOV A, #5 ;values are for example sake

MOV B, #10MUL AB ;you can only multiply on ARET

Page 33: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

33

Example (correct)RLC A ;you can only rotate APUSH A ;saving A and B on the stack beforePUSH B ;calling functionCall functionPOP B ;restoring BPOP A ;and A (POP in reverse order)DIV AB ; A has the correct value!!!!!…function: MOV A, #5 ;values are for example sakeMOV B, #10MUL AB ;you can only multiply on ARET

Page 34: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

34

Saving PSW• The Program Status Word registers contains flags that are often

important for correct program flow• You can push PSW on the stack before calling a function

ADD A, R0PUSH PSWPUSH A ;saving A and R0 on the stack beforePUSH R0 ;calling functionCall functionPOP R0 ;restoring R0POP A ;and A (POP in reverse order)POP PSWJC loop ;If this means the carry from the

;function then don’t push PSW…function: MOV A, #5 ;values are for example sakeADD A, R2 ;the flags are set according to ADD resultRET

Page 35: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

35

LOOP and JUMP Instructions

DJNZ:

Write a program to clear ACC, then

add 3 to the accumulator ten times

Solution:

MOV A,#0;

MOV R2,#10

AGAIN: ADD A,#03

DJNZ R2,AGAING ;repeat until R2=0 (10 times)

MOV R5,A

Page 36: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

36

• Other conditional jumps :

JZ Jump if A=0

JNZ Jump if A/=0

DJNZ Decrement and jump if A/=0

CJNE A,byte Jump if A/=byte

CJNE reg,#data Jump if byte/=#data

JC Jump if CY=1

JNC Jump if CY=0

JB Jump if bit=1

JNB Jump if bit=0

JBC Jump if bit=1 and clear bit

Page 37: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

37

SJMP and LJMP:

LJMP(long jump)LJMP is an unconditional jump. It is a 3-byte instruction in which the first byte is the opcode, and the second and third bytes represent the 16-bit address of the target location. The 20byte target address allows a jump to any memory location from 0000 to FFFFH.

SJMP(short jump)In this 2-byte instruction. The first byte is the opcode and the second byte is the relative address of the target location. The relative address range of 00-FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.

Page 38: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

38

CJNE , JNC

Exercise:

Write a program that compare R0,R1.

If R0>R1 then send 1 to port 2,

else if R0<R1 then send 0FFh to port 2,

else send 0 to port 2.

Page 39: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

39

CALL Instructions

Another control transfer instruction is the CALL instruction, which is used to call a subroutine.

• LCALL(long call)

In this 3-byte instruction, the first byte is the opcode an the second and third bytes are used for the address of target subroutine. Therefore, LCALL can be used to call subroutines located anywhere within the 64K byte address space of the 8051.

Page 40: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

40

• ACALL (absolute call)

ACALL is 2-byte instruction in contrast to LCALL, which is 13 bytes. Since ACALL is a 2-byte instruction, the target address of the subroutine must be within 2K bytes address because only 11 bits of the 2 bytes are used for the address. There is no difference between ACALL and LCALL in terms of saving the program counter on the stack or the function of the RET instruction. The only difference is that the target address for LCALL can be anywhere within the 64K byte address space of the 8051 while the target address of ACALL must be within a 2K-byte range.

Page 41: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

41

Example

A B R5 R7 Address Data

ORG 0H

VAL1 EQU 05H

MOV R5,#25H

LOOP: MOV R7,#VAL1

MOV A,#0

ADD A,R5

ADD A,#12H

RRC A

DJNZ A, LOOP

SETB ACC.3

CLR A

CJNE A, #0, LOOP

HERE: SJMP HERE

END

Page 42: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

42

I/O Port Programming

Port 1 ( pins 1-8)

• Port 1 is denoted by P1.

– P1.0 ~ P1.7

• We use P1 as examples to show the operations on ports.

– P1 as an output port (i.e., write CPU data to the external pin)

– P1 as an input port (i.e., read pin data into CPU bus)

Page 43: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

43

A Pin of Port 1

8051 IC

D Q

Clk Q

Vcc

Load(L1)

Read latch

Read pin

Write to latch

Internal CPU bus

M1

P1.X pinP1.X

TB1

TB2

P0.x

Page 44: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

44

Hardware Structure of I/O Pin

• Each pin of I/O ports

– Internal CPU bus : communicate with CPU

– A D latch store the value of this pin

• D latch is controlled by “Write to latch”

– Write to latch = 1 : write data into the D latch

– 2 Tri-state buffer:• TB1: controlled by “Read pin”

– Read pin = 1 : really read the data present at the pin

• TB2: controlled by “Read latch”

– Read latch = 1 : read value from internal latch

– A transistor M1 gate

• Gate=0: open

• Gate=1: close

Page 45: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

45

Tri-state Buffer

Output Input

Tri-state control (active high)

L H Low

Highimpedance (open-circuit)

HH

L H

Page 46: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

46

Writing “1” to Output Pin P1.X

D Q

Clk Q

Vcc

Load(L1)

Read latch

Read pin

Write to latch

Internal CPU bus

M1

P1.X pinP1.X

8051 IC

2. output pin is Vcc1. write a 1 to the pin

1

0 output 1

TB1

TB2

Page 47: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

47

Writing “0” to Output Pin P1.X

D Q

Clk Q

Vcc

Load(L1)

Read latch

Read pin

Write to latch

Internal CPU bus

M1

P1.X pinP1.X

8051 IC

2. output pin is ground1. write a 0 to the pin

0

1 output 0

TB1

TB2

Page 48: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

48

Port 1 as Output ( Write to a Port)• Send data to Port 1:

MOV A,#55H BACK: MOV P1,A

ACALL DELAYCPL ASJMP BACK

– Let P1 toggle.– You can write to P1 directly.

Page 49: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

49

Reading Input v.s. Port Latch • When reading ports, there are two possibilities:

– Read the status of the input pin. ( from external pin value)• MOV A, PX

• JNB P2.1, TARGET ; jump if P2.1 is not set

• JB P2.1, TARGET ; jump if P2.1 is set

• Figures C-11, C-12

– Read the internal latch of the output port.

• ANL P1, A ; P1 ← P1 AND A

• ORL P1, A ; P1 ← P1 OR A

• INC P1 ; increase P1

• Figure C-17

• Table C-6 Read-Modify-Write Instruction (or Table 8-5)

• See Section 8.3

Page 50: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

50

Reading “High” at Input Pin

D Q

Clk Q

Vcc

Load(L1)

Read latch

Read pin

Write to latch

Internal CPU bus

M1

P1.X pin

P1.X

8051 IC

2. MOV A,P1

external pin=High1. write a 1 to the pin MOV

P1,#0FFH

1

0

3. Read pin=1 Read latch=0 Write to latch=1

1

TB1

TB2

Page 51: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

51

Reading “Low” at Input Pin

D Q

Clk Q

Vcc

Load(L1)

Read latch

Read pin

Write to latch

Internal CPU bus

M1

P1.X pin

P1.X

8051 IC

2. MOV A,P1

external pin=Low1. write a 1 to the pin

MOV P1,#0FFH

1

0

3. Read pin=1 Read latch=0 Write to latch=1

0

TB1

TB2

Page 52: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

52

Port 1 as Input ( Read from Port)• In order to make P1 an input, the port must be programmed by writing 1 to

all the bit.

MOV A,#0FFH ;A=11111111B

MOV P1,A ;make P1 an input port

BACK: MOV A,P1 ;get data from P0

MOV P2,A ;send data to P2

SJMP BACK

– To be an input port, P0, P1, P2 and P3 have similar methods.

Page 53: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

53

Instructions For Reading an Input Port

Mnemonics Examples Description

MOV A,PX MOV A,P2Bring into A the data at P2 pins

JNB PX.Y,.. JNB P2.1,TARGET Jump if pin P2.1 is low

JB PX.Y,.. JB P1.3,TARGET Jump if pin P1.3 is high

MOV C,PX.Y MOV C,P2.4Copy status of pin P2.4 to CY

• Following are instructions for reading external pins of ports:

Page 54: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

54

Reading Latch

• Exclusive-or the Port 1:MOV P1,#55H ;P1=01010101

ORL P1,#0F0H ;P1=11110101

1. The read latch activates TB2 and bring the data from the Q latch into CPU.

• Read P1.0=0

2. CPU performs an operation.

• This data is ORed with bit 1 of register A. Get 1.

3. The latch is modified.

• D latch of P1.0 has value 1.

4. The result is written to the external pin.

• External pin (pin 1: P1.0) has value 1.

Page 55: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

55

Reading the Latch

D Q

Clk Q

Vcc

Load(L1)

Read latch

Read pin

Write to latch

Internal CPU bus

M1

P1.X pin

P1.X

8051 IC

4. P1.X=12. CPU compute P1.X OR 1

0

0

1. Read pin=0 Read latch=1 Write to latch=0 (Assume P1.X=0 initially)

1

TB1

TB2

3. write result to latch Read pin=0 Read latch=0

Write to latch=1

1

0

Page 56: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

56

Read-modify-write Feature

• Read-modify-write Instructions– Table C-6

• This features combines 3 actions in a single instruction:1. CPU reads the latch of the port

2. CPU perform the operation

3. Modifying the latch

4. Writing to the pin– Note that 8 pins of P1 work independently.

Page 57: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

57

Port 1 as Input ( Read from latch)

• Exclusive-or the Port 1: MOV P1,#55H ;P1=01010101

AGAIN: XOR P1,#0FFH ;complement

ACALL DELAY

SJMP AGAIN

– Note that the XOR of 55H and FFH gives AAH.

– XOR of AAH and FFH gives 55H.

– The instruction read the data in the latch (not from the pin).

– The instruction result will put into the latch and the pin.

Page 58: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

58

Read-Modify-Write Instructions

ExampleMnemonics

SETB P1.4SETB PX.Y

CLR P1.3CLR PX.Y

MOV P1.2,CMOV PX.Y,C

DJNZ P1,TARGETDJNZ PX, TARGET

INC P1INC

CPL P1.2CPL

JBC P1.1, TARGETJBC PX.Y, TARGET

XRL P1,AXRL

ORL P1,AORL

ANL P1,AANL

DEC P1DEC

Page 59: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

59

You are able to answer this Questions:

• How to write the data to a pin?• How to read the data from the pin?

– Read the value present at the external pin.

• Why we need to set the pin first?– Read the value come from the latch ( not from the external

pin ) .

• Why the instruction is called read-modify write?

Page 60: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

60

Other Pins

• P1, P2, and P3 have internal pull-up resisters.

– P1, P2, and P3 are not open drain.

• P0 has no internal pull-up resistors and does not connects to Vcc inside the 8051.

– P0 is open drain.

– Compare the figures of P1.X and P0.X. • However, for a programmer, it is the same to program P0, P1,

P2 and P3.

• All the ports upon RESET are configured as output.

Page 61: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

61

A Pin of Port 0

8051 IC

D Q

Clk Q

Read latch

Read pin

Write to latch

Internal CPU bus

M1

P0.X pinP1.X

TB1

TB2

P1.x

Page 62: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

62

Port 0 ( pins 32-39)• P0 is an open drain.

– Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips.

• When P0 is used for simple data I/O we must connect it to external pull-up resistors.

– Each pin of P0 must be connected externally to a 10K ohm pull-up resistor.

– With external pull-up resistors connected upon reset, port 0 is configured as an output port.

Page 63: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

63

Port 0 with Pull-Up Resistors

P0.0P0.1P0.2P0.3P0.4P0.5P0.6P0.7

DS5000

8751

8951

Vcc10 K

Port 0

Page 64: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

64

Dual Role of Port 0

• When connecting an 8051/8031 to an external memory, the 8051 uses ports to send addresses and read instructions.

– 8031 is capable of accessing 64K bytes of external memory.

– 16-bit address : P0 provides both address A0-A7, P2 provides address A8-A15.

– Also, P0 provides data lines D0-D7.

• When P0 is used for address/data multiplexing, it is connected to the 74LS373 to latch the address.

– There is no need for external pull-up resistors as shown in Chapter 14.

Page 65: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

65

74LS373

D

74LS373ALE

P0.0

P0.7

PSEN

A0

A7

D0

D7

P2.0

P2.7

A8

A15

OE

OC

EA

G

8051 ROM

Page 66: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

66

Reading ROM (1/2)

D

74LS373ALE

P0.0

P0.7

PSEN

A0

A7

D0

D7

P2.0

P2.7

A8

A12

OE

OC

EA

G

8051 ROM

1. Send address to ROM

2. 74373 latches the address and send to

ROM

Address

Page 67: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

67

Reading ROM (2/2)

D

74LS373ALE

P0.0

P0.7

PSEN

A0

A7

D0

D7

P2.0

P2.7

A8

A12

OE

OC

EA

G

8051 ROM

2. 74373 latches the address and send to

ROM

Address

3. ROM send the instruction back

Page 68: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

68

ALE Pin

• The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch.– When ALE=0, P0 provides data D0-D7.– When ALE=1, P0 provides address A0-A7.– The reason is to allow P0 to multiplex address and

data.

Page 69: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

69

Port 2 ( pins 21-28)• Port 2 does not need any pull-up resistors since

it already has pull-up resistors internally.

• In an 8031-based system, P2 are used to provide address A8-A15.

Page 70: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

70

Port 3 ( pins 10-17)• Port 3 does not need any pull-up resistors since it already has

pull-up resistors internally.• Although port 3 is configured as an output port upon reset,

this is not the way it is most commonly used.• Port 3 has the additional function of providing signals.

– Serial communications signal : RxD, TxD ( Chapter 10)

– External interrupt : /INT0, /INT1 ( Chapter 11)– Timer/counter : T0, T1 ( Chapter 9)– External memory accesses in 8031-based

system : /WR, /RD ( Chapter 14)

Page 71: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

71

Port 3 Alternate Functions

17RDP3.7

16WRP3.6

15T1P3.5

14T0P3.4

13INT1P3.3

12INT0P3.2

11TxDP3.1

10RxDP3.0

PinFunctionP3 Bit

Page 72: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

72

Generating Delays

• You can generate short delays using a register and incrementing or decrementing its value

• Example:mov r1, #0ah

loop: djnz r1, loop• How much delay is that?

– Djnz is a 2-byte instruction it takes two machine cycles– One machine cycle is 1/12 of the system clock period– For a 12 MHz system clock that is:– Machine cycle = 12/12 = 1 MHz– Machine period = 1/(1 MHz) = 10^(-6) s = 1 μs– Loop time = 10*2*1 μs = 20 μs

Page 73: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

73

Generating longer delays

• Each register is 8 bits long, so it can increment 256 times before overflowing

• For larger delays, or when interrupts are required 8051 uses two timers

Page 74: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

74

Page 75: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

75

TMOD Register:

• Gate : When set, timer only runs while INT(0,1) is high.

• C/T : Counter/Timer select bit.

• M1 : Mode bit 1.• M0 : Mode bit 0.

Page 76: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

76

TCON Register:

• TF1: Timer 1 overflow flag.• TR1: Timer 1 run control bit.• TF0: Timer 0 overflag.• TR0: Timer 0 run control bit.• IE1: External interrupt 1 edge flag.• IT1: External interrupt 1 type flag.• IE0: External interrupt 0 edge flag.• IT0: External interrupt 0 type flag.

Page 77: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

77

Timer Mode Register• Bit 7: Gate bit; when set, timer only runs while \INT high.

(T0)• Bit 6: Counter/timer select bit; when set timer is an event

counter when cleared timer is an interval timer (T0)• Bit 5: Mode bit 1 (T0)• Bit 4: Mode bit 0 (T0)• Bit 3: Gate bit; when set, timer only runs while \INT high.

(T1)• Bit 2: Counter/timer select bit; when set timer is an event

counter when cleared timer is an interval timer (T1)• Bit 1: Mode bit 1 (T1)• Bit 0: Mode bit 0 (T1)

Page 78: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

78

Timer Modes

• M1-M0: 00 (Mode 0) – 13-bit mode (not commonly used)

• M1-M0: 01 (Mode 1) - 16-bit timer mode

• M1-M0: 10 (Mode 2) - 8-bit auto-reload mode

• M1-M0: 11 (Mode 3) – Split timer mode

Page 79: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

79

Timer Control Register (TCON)• Bit 7 (TF1) 8FH : Timer 1 overflow flag; set by hardware

upon overflow, cleared by software• Bit 6 (TR1) 8EH: Timer 1 run-control bit; manipulated by

software - setting starts timer 1, resetting stops timer 1• Bit 5 (TF0) 8DH: Timer 0 overflow flag; set by hardware

upon overflow, cleared by software.• Bit 4 (TR0) 8CH: Timer 0 run-control bit; manipulated by

software - setting starts timer 0, resetting stops timer 0• Bit 3 (IE1) 8BH: External 1 Interrupt flag bit• Bit 2 (IT1) 8AH: • Bit 1 (IE0) 89H: External 0 Interrupt flag bit• Bit 0 (IT0) 88H:

Page 80: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

80

Initializing and stopping timers

• MOV TMOD, #16H ;initialization

• SETB TR0 ;starting timersSETB TR1

• CLR TR0 ; stop timer 0CLR TR1 ; stop timer 1

• MOV R7, TH0 ; reading timers

• MOV R6, TL0

Page 81: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

81

Reading timers on the fly• Reading the Timers on the Fly To read the contents of a timer

while it is running (ie; on the fly) poses a problem. • MOV A, TH0 ;Let TH0 = 07 and TL0 = FF• MOV R6, TL0 ;Now TH0=08 and TL0=00 but A=07 and R6=00!• The solution is to read the high byte, then read the low byte, then

read the high byte again. If the two readings of the low-byte are not the same repeat the procedure. The code for this method is detailed below.     

• tryAgain:MOV A, TH0MOV R6, TL0CJNE A, TH0, tryAgain

• if the first reading of the hi-byte (in A) is not equal to current reading in the hi-byte (TH0) try againMOV R7, A; if both readings of the hi-byte are the same move the first reading into R7 - the overall reading is now in R7R6  

Page 82: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

82

Generating delays using the timers• To generate a 50 ms (or 50,000 us) delay we start the

timer counting from 15,536. Then, 50,000 steps later it will overflow. Since each step is 1 us (the timer's clock is 1/12 the system frequency) the delay is 50,000 us.   0

MOV TMOD, #10H; set up timer 1 as 16-bit interval timerCLR TR1 ; stop timer 1 (in case it was started in some other subroutine)MOV TH1, #3CHMOV TL1, #0B0H ; load 15,536 (3CB0H) into timer 1SETB TR1 ; start timer 1JNB TF1, $; repeat this line while timer 1 overflow flag is not setCLR TF1; timer 1 overflow flag is set by hardware on transition from FFFFH - the flag must be reset by softwareCLR TR1 ; stop timer 1

Page 83: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

83

Generating long delays• If the microcontroller has a system clock frequency of 12 MHz then the longest delay

we can get from either of the timers is 65,536 usec.• To generate delays longer than this, we need to write a subroutine to generate a

delay of (for example) 50 ms and then call that subroutine a specific number of times.• ...

MOV TMOD, #10H; set up timer 1 as 16-bit interval timer

• fiftyMsDelay:CLR TR1 ; stop timer 1 (in case it was started in some other subroutine)MOV TH1, #3CHMOV TL1, #0B0H ; load 15,536 (3CB0H) into timer 1SETB TR1 ; start timer 1JNB TF1, $; repeat this line while timer 1 overflow flag is not setCLR TF1; timer 1 overflow flag is set by hardware on transition from FFFFH - the flag must be reset by softwareCLR TR1 ; stop timer 1RET  

• oneSecDelay:PUSH PSWPUSH AR0 ; save processor statusMOV R0, #20 ; move 20 (in decimal) into R0

• loop:CALL fiftyMsDelay ; call the 50 ms delayDJNZ R0, loop ; 20 times - resulting in a 1 second delayPOP AR0POP PSW ; retrieve processor statusRET

Page 84: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

84

Using timers to measure execution time

• Timers are often used to measure the execution time of a programORG 0H

MOV TMOD, #16H ;initialization

SETB TR0 ;starting timer 0… ;main

… ;program

CLR TR0 ; stop timer 0MOV R7, TH0 ; reading timer 0

MOV R6, TL0

Page 85: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

85

Interrupt :

Page 86: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

86

Interrupt Enable Register :

• EA : Global enable/disable.• --- : Undefined.

• ET2 :Enable Timer 2 interrupt.• ES :Enable Serial port interrupt.• ET1 :Enable Timer 1 interrupt.• EX1 :Enable External 1 interrupt.• ET0 : Enable Timer 0 interrupt. • EX0 : Enable External 0 interrupt.

Page 87: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

87

Interrupt handling

• 8051 Interrupt Vector Table

Page 88: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

88

Interrupt Service Routines• ORG 0

JMP main

• ORG 0003H ; external interrupt 0 vector…. ; interrupt handler code for external interrupt 0RETI

ORG 0013H ; external interrupt 1 vector…. ;interrupt handler code for external interrupt 1RETI

ORG 0030H ; main programmain:SETB IT0 ; set external interrupt 0 as edge activated

SETB IT1 ; set external interrupt 1 as edge activated SETB EX0 ; enable external interrupt 0SETB EX1 ; enable external interrupt 1SETB EA ; global interrupt enable …

Page 89: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

89

Examples

• Write a 8051 assembly program that matches 8 switches with 8 LEDs

• Write a 8051 assembly program that uses a two-digit SSD to display the temperature as input from an ADC. Assume that the 0-5V range corresponds to 0-50 °C. The ADC uses RD, WR and INT pins.

Page 90: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

8051 Programming Using C

Page 91: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

91

Programming microcontrollers using high-level languages

• Most programs can be written exclusively using high-level code like ANSI C

• Extensions– To achieve low-level (Assembly) efficiency,

extensions to high-level languages are required

• Restrictions– Depending on the compiler, some restrictions

to the high-level language may apply

Page 92: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

92

Keil C keywords • data/idata:

Description: The variable will be stored in internal data memory of controller.

example:unsigned char data x;//orunsigned char idata y; 

• bdata:Description: The variable will be stored in bit addressable memory of controller.

example:• unsigned char bdata x;

//each bit of the variable x can be accessed as followsx ^ 1 = 1; //1st bit of variable x is setx ^ 0 = 0; //0th bit of variable x is cleared 

• xdata:Description: The variable will be stored in external RAM memory of controller.

example:unsigned char xdata x; 

Page 93: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

93

Keil C keywords• code:

Description: This keyword is used to store a constant variable in code and not data memory.example:unsigned char code str="this is a constant string"; 

• _at_:Description: This keyword is used to store a variable on a defined location in ram.

example:CODE:

• unsigned char idata x _at_ 0x30;// variable x will be stored at location 0x30// in internal data memory 

• sbit:Description: This keyword is used to define a special bit from SFR (special function register) memory.

example:• sbit Port0_0 = 0x80;

// Special bit with name Port0_0 is defined at address 0x80 

Page 94: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

94

Keil C keywords• sfr:

Description: sfr is used to define an 8-bit special function register from sfr memory.

example:sfr Port1 = 0x90;// Special function register with name Port1 defined at addrress 0x90 

• sfr16:Description: This keyword is used to define a two sequential 8-bit registers in SFR memory.

example:sfr16 DPTR = 0x82;// 16-bit special function register starting at 0x82// DPL at 0x82, DPH at 0x83 

• using:Description: This keyword is used to define register bank for a function. User can specify register bank 0 to 3.

example:void function () using 2{// code}// Funtion named "function" uses register bank 2 while executing its code 

• Interrupt:Description: defines interrupt service routinevoid External_Int0() interrupt 0{//code}

Page 95: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

95

Pointers• //Generic Pointer

char * idata ptr;//character pointer stored in data memoryint * xdata ptr1;//Integer pointer stored in external data memory

//Memory Specific pointerchar idata * xdata ptr2;//Pointer to character stored in Internal Data memory//and pointer is going to be stored in External data memoryint xdata * data ptr3;//Pointer to character stored in External Data memory//and pointer is going to be stored in data memory

Page 96: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

96

Writing hardware-specific code• #include <REGx51.h> //header file for 89C51

void main(){        //main function starts        unsigned int i;        //Initializing Port1 pin1        P1_1 = 0; //Make Pin1 o/p        while(1){                //Infinite loop main application                //comes here                for(i=0;i<1000;i++)                        ; //delay loop                P1_1 = ~P1_1;                //complement Port1.1                //this will blink LED connected on Port1.1        }} 

Page 97: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

97

C and Assembly together

• extern unsigned long add(unsigned long, unsigned long);

void main(){        unsigned long a;        a = add(10,30); //calling Assembly function         

while(1);}

Page 98: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

98

C and Assembly together• name asm_test

?PR?_add?asm_test segment code?DT?_add?asm_test segment data

;let other function use this data space for passing variablespublic ?_add?BYTE;make function public or accessible to everyonepublic _add

;define the data segment for function addrseg ?DT?_add?asm_test?_add?BYTE:parm1:  DS 4    ;First Parameterparm2:  ds 4    ;Second Parameter

;either you can use parm1 for reading passed value as shown below;or directly use registers used to pass the value.rseg ?PR?_add?asm_test_add:;reading first argument        mov parm1+3,r7        mov parm1+2,r6        mov parm1+1,r5        mov parm1,r4;param2 is stored in fixed location given by param2

;now adding two variables        mov a,parm2+3        add a,parm1+3;after addition of LSB, move it to r7(LSB return register for Long)        mov r7,a        mov a,parm2+2        addc a,parm1+2;store second LSB        mov r6,a        mov a,parm2+1        addc a,parm1+1;store second MSB        mov r5,a        mov a,parm2        addc a,parm1        mov r4,a        ret

        end

Page 99: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

99

The infinite loop

• A loop with no termination condition or one that will never be met may be unwanted in computer systems, but common in embedded systems.

Page 100: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

100

Example 1

• Generate a 5V peek-to-peek 200μs period square waveform on the DAC output

Page 101: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

101

Example 2

• Generate a 5V peek-to-peek 200μs period sawtooth waveform on the DAC output

Page 102: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

102

Example 3• Generate a 5V peek-to-peek 2ms period sine waveform on the DAC output• code unsigned char Sine[180] = { /* Sine values */• 127,131,136,140,145,149,153,158,162,166,170,175, 179,183,187,191,194,198,202,205,209,212,215,218,

221,224,227,230,232,235,237,239,241,243,245,246, 248,249,250,251,252,253,253,254,254,254,254,254, 253,253,252,251,250,249,248,246,245,243,241,239, 237,235,232,230,227,224,221,218,215,212,209,205, 202,198,194,191,187,183,179,175,170,166,162,158, 153,149,145,140,136,131,127,123,118,114,109,105, 101, 96, 92, 88, 84, 79, 75, 71, 67, 64, 60, 56, 52, 49, 45, 42, 39, 36, 33, 30, 27, 24, 22, 19, 17, 15, 13, 11, 9, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 15, 17, 19, 22, 24, 27, 30, 33, 36, 39, 42, 45, 49, 52, 56, 60, 63, 67, 71, 75, 79, 84, 88, 92, 96, 101,105,109,114,118

• };• /************************************************************• * START of the PROGRAM *• ************************************************************ /• void main (void) {• unsigned char i;• /************************************************************• * Enable the D/A Converter *• ************************************************************ /• ENDAC0 = 1; /* Enable DAC0 */• /************************************************************• * Create the waveforms on DAC0 *• ************************************************************ /• while(1){ /* Run for ever */• for(i = 0; i < 179; i++)• DAC0 = Sine[i];• } * while(1) */• } /* main() */• }

Page 103: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

103

Mixed C/Assembly code (μVision Version 2.06)

• Parameter passing in registers

• Examples:

Page 104: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

104

Function return values

Page 105: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

105

Exampleextern unsigned char add2_func(unsigned char, unsigned char);

void main(){        unsigned char a;        a = add2_func(10,30);        //a will have 40 after execution        while(1);}

;assembly file “add2.asm”

NAME _Add2_func

?PR?add2_func?Add2 SEGMENT CODE

PUBLIC add2_func

RSEG ?PR?Add2_func?Add2

add2_func:

mov a, r7 ;first parameter passed to r7

add a, r5 ;second parameter passed to r5

mov r7, a ;return parameter must be in r7

RET

END

Page 106: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

106

Calling C from AssemblyNAME A_FUNC

?PR?a_func?A_FUNC SEGMENT CODEEXTRN CODE (c_func)PUBLIC a_func

RSEG ?PR?a_func?A_FUNCa_func:

USING 0LCALL c_funcRET

END

void c_func (void){}

Page 107: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

Data Converters

• Analog to Digital Converters (ADC)– Convert an analog quantity

(voltage, current) into a digital code

• Digital to Analog Converters (DAC)– Convert a digital code into

an analog quantity (voltage, current)

Dr. Konstantinos Tatas and Dr. Costas Kyriacou

Page 108: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

108

Video (Analog - Digital)

A/D

AmplifierFilters

Modulator

Image enhancement

and coding

Analog

Digital

Pre-amplifier

Page 109: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

109

Temperature Recording by a Digital System

Sampling & quantization

Coding

Temperature(ºC)

Time

Temperature(ºC)

Time

Page 110: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

110

Need for Data ConvertersDigital processing and storage of physical quantities (sound, temperature, pressure

etc) exploits the advantages of digital electronics

– Better and cheaper technology compared to the analog

– More reliable in terms of storage, transfer and processing

• Not affected by noise

– Processing using programs (software)

• Easy to change or upgrade the system

– (e.g. Media Player 7 Media Player 8 ή Real Player)

• Integration of different functions

– (π.χ. Mobile = phone + watch + camera + games + email +

Page 111: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

111

Signals (Analog - Digital)

2

4

6

8

10

12

14

16

u(V)

1 2 73 4 5 6 8 9 t (S)

D3

D2

D1

D0

0

0

0

1

0

0

1

1

0

0

1

1 0

0

1

1 0

1

1

1

0

0

1

1 1

1

1

1

0

0

1

1

0

1

0

0

Analog Signal • can take infinity values • can change at any time

0100

1001

0110

0101

1010

1110

1111

1100

1000

Digital Signal • can take one of

2 values (0 or 1)• can change only

at distinct times

ADC

2

4

6

8

10

12

14

16

u(V)

1 2 73 4 5 6 8 9 t (S)

DAC

Reconstruction of an analog signal from a digital one

(Can take only predefined values)

1001

0110 0101

1010

11111110

1000

1100

0100

Page 112: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

112

QUANTIZATION ERROR• The difference between the true and quantized value of the analog signal

• Inevitable occurrence due to the finite resolution of the ADC

• The magnitude of the quantization error at each sampling instant is between zero and half of one LSB.

• Quantization error is modeled as noise (quantization noise)

2

4

6

8

10

12

14

16

u(V)

1 2 73 4 5 6 8 9 t (S)

2

4

6

8

10

12

14

16

u(V)

1 2 73 4 5 6 8 9 t (S)

Analog signal value at sampling time: 4.9 V

Quantized Analog signal value: 5.0 V

Quantization error: 5.0 - 4.9 = 0.1 V

Page 113: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

113

SAMPLING FREQUENCY (RATE)

• The frequency at which digital values are sampled from the analog input of an ADC

• A low sampling rate (undersampling) may be insufficient to represent the analog signal in digital form

• A high sampling rate (oversampling) requires high bitrate and therefore storage space and processing time

• A signal can be reproduced from digital samples if the sampling rate is higher than twice the highest frequency component of the signal (Nyquist-Shannon theorem)

• Examples of sampling rates – Telephone: 4 KHz (only adequate for speech, ess sounds like eff)– Audio CD: 44.1 KHz– Recording studio: 88.2 KHz

Page 114: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

114

Digital to Analog Converters • The analog signal at the output of a D/A

converter is linearly proportional to the binary code at the input of the converter.

– If the binary code at the input is 0001 and the output voltage is 5mV, then

– If the binary code at the input becomes 1001, the output voltage will become ......

• If a D/A converter has N digital inputs then the analog signal at the output can have one out of ……. values.

• If a D/A converter has 4 digital inputs then the analog signal at the output can have one out of …… values.

45mV

16

D3 D2 D1 D0Vout(mV)

0 0 0 0 0

0 0 0 1 5

0 0 1 0 10

0 0 1 1 15

0 1 0 0 20

0 1 0 1 25

0 1 1 0 30

0 1 1 1 35

1 0 0 0 40

1 0 0 1 45

1 0 1 0 50

1 0 1 1 55

1 1 0 0 60

1 1 0 1 65

1 1 1 0 70

1 1 1 1 75

Page 115: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

115

Characteristics of Data Converters

1. Number of digital lines

– The number bits at the input of a D/A (or output of an A/D) converter.

– Typical values: 8-bit, 10-bit, 12-bit and 16-bit

– Can be parallel or serial

2. Microprocessor Compatibility

– Microprocessor compatible converters can be connected directly on the microprocessor bus as standard I/O devices

– They must have signals like CS, RD, and WR

• Activating the WR signal on an A/D converter starts the conversion process.

3. Polarity

– Polar: the analog signals can have only positive values

– Bipolar: the analog signals can have either a positive or a negative value

4. Full-scale output

– The maximum analog signal (voltage or current)

– Corresponds to a binary code with all bits set to 1 (for polar converters)

– Set externally by adjusting a variable resistor that sets the Reference Voltage (or current)

Page 116: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

116

Characteristics of Data Converters (Cont…)5. Resolution

– The analog voltage (or current) that corresponds to a change of 1LSB in the binary code– It is affected by the number of bits of the converter and the Full Scale voltage (VFS)– For example if the full-scale voltage of an 8-bit D/A converter is 2.55V the the resolution

is:

VFS/(2N-1) = 2.55 /(28-1) 2.55/255 = 0.01 V/LSB = 10mV/LSB

6. Conversion Time– The time from the moment that a “Start of Conversion” signal is applied to an A/D

converter until the corresponding digital value appears on the data lines of the converter.

– For some types of A/D converters this time is predefined, while for others this time can vary according to the value of the analog signal.

0.1Vo

Vo

7. Settling Time – The time needed by the analog signal at

the output of a D/A converter to be within 10% of the nominal value.

Page 117: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

117

ADC RESPONSE TYPES

• Linear– Most common

• Non-linear– Used in telecommunications, since human

voice carries more energy in the low frequencies than the high.

Page 118: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

118

ADC TYPES• Direct Conversion

– Fast

– Low resolution

• Successive approximation

– Low-cost

– Slow

– Not constant conversion delay

• Sigma-delta

– High resolution,

– low-cost,

– high accuracy

Page 119: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

119

Interfacing with Data Converters • Microprocessor compatible data converters are attached on

the microprocessor’s bus as standard I/O devices.

DAC

CS

Vout

D7

WR

D6

D5

D4

D3

D2

D1

D0 Vref

V(+)

V(-)

8088

Sys

tem

D7

D6

D5

D4

D3

D2

D1

D0

A19

A0

WR

IO/M'

RD

A11

A10

A9

A8

A7

A6

A5

A4

Vout

10K

10K

+5V

Page 120: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

120

Programming Example 1Write a program to generate a positive ramp at the output of an 8-bit D/A converter with a 2V amplitude and a 1KHz frequency. Assume that the full scale voltage of the D/A converter is 2.55V. The D/A converter is in P0 and the WR signal is in P1.1

2V

0V

f = 1KHZ

main(){

do {

for (i=0;i<200;i++)

{

P1_0=1;

P0=i;

delayu(5);

}} while (1)}

200 stepsof 10 mV each==> 2V amplitude

200 steps of 5 us each==> 1ms period or 1KHz frequency

Page 121: Lecture 3 Programming the 8051 Microcontroller Dr. Konstantinos Tatas.

ACOE343 - Embedded Real-Time Processor Systems - Frederick University

121

D/A Converters example

0

4

3

2

1

V (volts)

t (msec )1 5 6 7 8

Write a program to generate the waveform, shown below, at the output of an 8-bit digital to analog converter. The period of the waveform should be approximately 8 ms. Assume that a time delay function with a 1 μs resolution is available. The full scale output of the converter is 5.12 V and the address of the DAC is P0, while the WR signal is in P1.1.

Co

Co

Co

Assuming that an 8-bit A/D converter is used to interface a temperature sensor measuring temperature values in the temperature range 0 - 51.2

, specify: The resolution in of the system in The digital output word for a temperature of 32.5 The temperature corresponding to a digital output word of 01001110