89s52 micro controller interfacing

49
1 Introduction There are many ‘topics’ that we need to study, understand and then verify to acquire the capability of designing and building microcontroller based systems such as: a 24-Hr Clock or a Taximeter or a Digital Weighing Machine or a Prepaid KWH Meter or a similar. 89S52 Microcontroller Intefacing Golam Mostafa : http://www.krdcbd.com/ : 01726341559 ; [email protected] Now, the question is: Which topics to study and verify first? It is a difficult question and there exits no straightforward answer! However, like many successful people, I have found that the following guidelines could help getting quick interest into the works involving microcontrollers. i. Let us take an example program from Chpater-5 or Chpater-6 of this book. Follow the steps as outlined in the example and observe the results. ii. Open the source code of the program. Let us try to understand the need of every instructions of the program. While studying the program codes, we must remember that the program instructions are closely related with the hardware on which it works. iii. Let us now, try to rewrite the program in our own ways and observe that the new program still provides the same result. iv. This is the beginning point of learning the procedures of microcontroller based system design. To design, develop and build a microcontroller-based system, we need a learning/development tool similar to MicroTalk-8051, which has been pictured at the back of the cover sheet of this book. A development tool is equipped with all kinds of commonly used peripheral controllers, RAM, local hexadecimal keyboard, serial link, breadboard, edge connectors and a good number of ROM and CD-based subroutines. A user places the interface circuitry on the breadboard; connect these with the MCU using the edge connectors and the hookup wires. The user then develops the control program using IBMPC and the assembler. The binary codes are then transferred into the RAM of the trainer using the serial link of the PC. The program could be easily edited and modifies using the local keyboard and the PC. MCU based system design becomes a matter of enjoyment using development system like Microtalk-8951 and the similar. Unfortunately, the development tools are costly items and are not within the reach of the students and the amateur designers. The solution is the development of a student version of the MicroTalk-8051, which is: CKIT-06A. The CKIT-06A is a very low cost miniature microcontroller learning/development system using Atmel 8-bit CISC microcontrollers of type: 89S51, 89S52 and 89S8252. The CKIT-06B supports Atmel 8-bit 20-pin RISC microcontroller of type: AT90S2313. The CKIT-06C supports 8-bit 40-pin RISC MCU of type: AT90S8515. The CKIT is equipped with edge connectors, breadboard, +5V supply and a Line Printer (LPT) cable. A user can develop his circuitry on the breadboard; develop the control program using the IBMPC and assembler. The binary cods are then written into the code memory (EEPROM) of the MCU using the LPT port of the IBMPC and a driver, which has been supplied with the CD that accompanies the CKIT. It is very annoying to develop MCU based system using CKIT because it does not provide any facility to debug a program. If the program works it’s fine; otherwise the modified or the extended codes of the program required to be re-written into the MCU, which reduces the endurance of the EEPROM of the MCU. However, the MCU supports 1000 times write operations within its EEPROM and it is sure that a user would be able to develop his working control program within 100 attempts. 1

Upload: hafiz-ahmed

Post on 22-Apr-2015

529 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 89S52 Micro Controller Interfacing

1 Introduction There are many ‘topics’ that we need to study, understand and then verify to acquire the capability of designing and building microcontroller based systems such as: a 24-Hr Clock or a Taximeter or a Digital Weighing Machine or a Prepaid KWH Meter or a similar.

89S52 Microcontroller Intefacing Golam Mostafa : http://www.krdcbd.com/ : 01726341559 ; [email protected]

Now, the question is: Which topics to study and verify first? It is a difficult question and there exits no straightforward answer! However, like many successful people, I have found that the following guidelines could help getting quick interest into the works involving microcontrollers.

i. Let us take an example program from Chpater-5 or Chpater-6 of this book. Follow the steps as outlined in the example and observe the results.

ii. Open the source code of the program. Let us try to understand the need of every instructions of the program. While studying the program codes, we must remember that the program instructions are closely related with the hardware on which it works.

iii. Let us now, try to rewrite the program in our own ways and observe that the new program still provides the same result.

iv. This is the beginning point of learning the procedures of microcontroller based system design.

To design, develop and build a microcontroller-based system, we need a learning/development tool similar to MicroTalk-8051, which has been pictured at the back of the cover sheet of this book. A development tool is equipped with all kinds of commonly used peripheral controllers, RAM, local hexadecimal keyboard, serial link, breadboard, edge connectors and a good number of ROM and CD-based subroutines.

A user places the interface circuitry on the breadboard; connect these with the MCU using the edge connectors and the hookup wires. The user then develops the control program using IBMPC and the assembler. The binary codes are then transferred into the RAM of the trainer using the serial link of the PC. The program could be easily edited and modifies using the local keyboard and the PC. MCU based system design becomes a matter of enjoyment using development system like Microtalk-8951 and the similar.

Unfortunately, the development tools are costly items and are not within the reach of the students and the amateur designers. The solution is the development of a student version of the MicroTalk-8051, which is: CKIT-06A.

The CKIT-06A is a very low cost miniature microcontroller learning/development system using Atmel 8-bit CISC microcontrollers of type: 89S51, 89S52 and 89S8252. The CKIT-06B supports Atmel 8-bit 20-pin RISC microcontroller of type: AT90S2313. The CKIT-06C supports 8-bit 40-pin RISC MCU of type: AT90S8515. The CKIT is equipped with edge connectors, breadboard, +5V supply and a Line Printer (LPT) cable. A user can develop his circuitry on the breadboard; develop the control program using the IBMPC and assembler. The binary cods are then written into the code memory (EEPROM) of the MCU using the LPT port of the IBMPC and a driver, which has been supplied with the CD that accompanies the CKIT. It is very annoying to develop MCU based system using CKIT because it does not provide any facility to debug a program.

If the program works it’s fine; otherwise the modified or the extended codes of the program required to be re-written into the MCU, which reduces the endurance of the EEPROM of the MCU. However, the MCU supports 1000 times write operations within its EEPROM and it is sure that a user would be able to develop his working control program within 100 attempts.

1

Page 2: 89S52 Micro Controller Interfacing

2 Component Layout of CKIT-06A

1J1

23456789

1011121314151617181920

J24039383736353433323130292827262524232221

P10P11P12P13P14P15P16P17RSTP30P31P32P33P34P35P36P37XT1XT2GND

VccP00P01P02P03P04P05P06P07EA/

ALEPSEN/

P27P26P25P24P23P22P21P20

Y1 U1 CKIT-06Karighar R&D Center

D1

D3 D2

C1

+ -

R2D4

U5 1

+C3

C2

T1

Micr

ocon

trolle

r89

S51,

89S5

2, 89

S825

2

+5V Power SupplyCKIT

Figure-2.1(a): Component Layout Diagram of CKIT-06A

7805

0V

1

2

3

C11000uF +

+

-

-

-

+5V

6V

6V

220V

LV

D2T1

0V C2220uFC3

0.1uF

U5

D3

Figure-2.1(b): Schematic Diagram for +5V Power Supply

2

Page 3: 89S52 Micro Controller Interfacing

3 Circuit Diagrams 3.1 Connection Diagram Between LPT Port and 40-Pin ZIF Socket

3

14

25

24

23

22

`

15

16

17

18

19

20

21`

1

2

3

4

5

6

7

8

9

10

11

12

13

9

LPTCR (037Ah)

CR0 RST

U1: 40 Pin ZIF (89S51)

LPTSR (0379h)

SR4

LPTDR (0378h)

DB7

CR2

COM

LPT Port

MOSI (P15)MISO (P16)SCK (P16)

678

Data In

Data Out

Clock In

GND20Vcc40+5V

XT2

XT1

18

19

Y111.0592MHz

P1010V

D1

+

1001x: GM : 02-05

Figure-3.1: Connection Diagram between LPT Port and 40-pin ZIF Socket

As indicated in the diagram of Fig-3.1, the pins of the 40-pin ZIF socket (assuming that the ZIF contains 89S51 MCU) are permanently connected with the indicated pins of the LPT port of the IBMPC. The P10-pin of the 89S51 is directly connected with the LED, D1. The internal pullup resistor of P10 limits the current. The LED (D1) would be helpful to monitor the status of the functionality of programs.

Page 4: 89S52 Micro Controller Interfacing

3.2 Connection Diagram Between 89S51, J1 and J2 Edge Connectors

J1 J2U1: 40 Pin ZIF (89S51)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

24

23

22

21

28

27

26

25

32

31

30

29

36

35

34

33

40

39

38

37

24

23

22

21

28

27

26

25

32

31

30

29

36

35

34

33

40

39

38

37

P37-RD/

XT2

XT1

GND

P33-INT1/

P34-T0/

P35-T1/

P36-WR/

RST

P30-RxD

P31-TxD

P32-INT0/

P14

P15-MOSI

P16-MISO

P17-SCK

P10

P11

P12

P13

Vcc

AD0-P00

AD1-P01

AD2-P02

AD3-P03

AD4-P04

AD5-P05

AD6-P06

AD7-P07

Vpp-EA/

PROG/-ALE

PSEN/

A15-P27

A14-P26

A13-P25

A12-P24

A11-P23

A10-P22

A9-P21

A8-P20

0V

D1+5V

1001

Y1

Figure-3.2: Connection Diagram between 89S51, J1 and J2 Edge Connectors

The power up reset circuit for the 89S51 is not included in the CKIT-06.

Therefore: i. After storing the program codes in the code memory (the flash) using the LPT port,

the RST-pin could be activated MCU using the LPT port and software instructions. ii. The following reset circuits could be built on the breadboard of the CKIT and be

connected with the appropriate reset pins of the MCUs. The LPT cable should be open.

+5V

0V

+100uF

4k7

Pin-9 of 89S51K1

4

Page 5: 89S52 Micro Controller Interfacing

3.3 Connection Diagram Between 89S8252, J1 and J2 Edge Connectors

J1 J2U1: 40 Pin ZIF (89S8252)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

24

23

22

21

28

27

26

25

32

31

30

29

36

35

34

33

40

39

38

37

24

23

22

21

28

27

26

25

32

31

30

29

36

35

34

33

40

39

38

37

P37-RD/

XT2

XT1

GND

P33-INT1/

P34-T0/

P35-T1/

P36-WR/

RST

P30-RxD

P31-TxD

P32-INT0/

P14-SS/

P15-MOSI

P16-MISO

P17-SCK

P10-T2

P11-T2EX

P12

P13

Vcc

AD0-P00

AD1-P01

AD2-P02

AD3-P03

AD4-P04

AD5-P05

AD6-P06

AD7-P07

Vpp-EA/

PROG/-ALE

PSEN/

A15-P27

A14-P26

A13-P25

A12-P24

A11-P23

A10-P22

A9-P21

A8-P20

0V

D1+5V

1004

Y1

Figure-3.3: Connection Diagram between 89S8252, J1 and J2 Edge Connectors After storing program codes within the MCU, the following reset circuitry (same as for 89S51) should be included. The EA/-pin should also be tied to LH. And then activate the reset switch to run the stored program. Connect the following reset the with the reset pin of the 89S8252 microcontroller. +5V

0V

+100uF

4k7

Pin-9 of 89S8252K1

5

Page 6: 89S52 Micro Controller Interfacing

4 Quick Functional Check of CKIT-06 4.1 Familiarization with ASM51 Assembler and Quick Function Check of the 89S51

CISC (Compact Instruction Set Controller) Microcontroller In this functional check, we would store a small program inside the code memory of the 89S51 microcontroller. When the program is executed, the LED-D1 connected at the P10-pin would start blinking. Let us now carry out the following steps:

1. Read the diagram of Fig-3.1 and 3.2 for the electrical connections between the LPT port, 89S51 MCU and the J1 and the J2 edge connectors of the CKIT.

2. Copy the contents of the folder ‘CKIT’ of the CD that has been supplied with CKIT in the hard disk in the path: C:\CKIT

3. Boot the IBMPC in pure DOS mode and then enter into the path: c:\ckit. 4. Type EDIT at the DOS prompt and then hit the carriage return. A screen would appear called DOS

screen. 5. Now, type the following program statements and save it as: c:\ckit\p41.asm. This program could be

found in the CD that accompanies the CKIT.

$MOD51 ; this file includes the definitions of various symbolic names ; you may open the mode51 file and read the contents ; you may open the asm51.doc file to read about the asm51 assembler $OBJECT(c:\CKIT\p41.OBJ) ; P41.OBJ file contains the Intel-Hex formatted code ; for the Binary code of the program P41.asm. ; read the CKIT manual for the detailed structure of the Intel-Hex ; formatted file. ORG 0000H DB 02H ; after power up the MCU looks for instruction at 0000H DB 00H DB 10H ; the program execution now begins at: 0010H: ORG 0010H MOV SP, #70H ; stack pointer set. See MCU data structure MOV PSW, #00H ; Bank-0 Selection. Fig-21 of the PSPKIT Manual L1: SETB P1.0 ; LED1 is ON LCALL TDELAY ; calling Time Delay LCALL TDELAY LCALL TDELAY ; calling Time Delay LCALL TDELAY ; calling Time Delay LCALL TDELAY CLR P1.0 ; LED1 is OFF LCALL TDELAY ; calling Time Delay LCALL TDELAY LCALL TDELAY ; calling Time Delay LCALL TDELAY LCALL TDELAY ; calling Time Delay LJMP L1 ; repeat TDELAY: MOV R5, #0FFH AGN1: MOV R6, #0FFH AGN2: DJNZ R6, AGN2 DJNZ R5, AGN1 RET END

6

Page 7: 89S52 Micro Controller Interfacing

6. Save the program and exit to DOS. 7. Now convert the P41.asm program into P41.obj file. The p41.obj file would automatically be saved in

the path: c:\ckit\p41.obj as per declaration with the include directive $ at the top of the program. Procedures:

i. Type ASM51 and hit the carriage return. ii. Type C:\ckit\p41.asm in response to: Source File drive and name [.ASM]: iii. The program would terminate to DOS prompt with zero errors.

8. Now convert the P41.obj program into P41.bin. Why? The P41.obj program is an Intel-Hex formatted

file, which is suitable to transfer over COM port of the IBMPC. The ISP programming technique of the 89S51 demands that the program codes should be in the Binary format. The P41.obj file would now be converted into P41.bin using the EPW512.exe program (a third party program being used freely with no obligation). The procedures are:

i. Type EPW512 at the DOS prompt. A DOS menu would appear. ii. Follow the menu and load the file: c:\ckit\p41.obj iii. Type I in response of: < B > in, < I > ntel Hex, < M > otorola S Hex iv. Type 0000 in response of: File start seg. (0000) : v. Type 2 in response of: Unused bytes will be < 1 > 00, < 2 > FF, < 3 > Don’t care: vi. Note down the end address as: 0047 (it may vary program to program) vii. Follow the menu. 9. Follow the menu and save the converted file as: c:\ckit\p41.bin. Be careful to type the correct start and

end addresses. 10. Quit the DOS menu and exit to DOS prompt. 11. Connect the LPT cable with the LPT port of the IBMPC. 12. The EA/-pin is already tied with +5V 13. Build the following reset circuit on the breadboard and connect it with the RST pin (pin-9) of the 89S51

MCU. You may omit this circuit if you wish. +5V

0V

+100uF

4k7

Pin-9 of 89S51K1

14. Apply power to the CKIT. 15. Let us now store the codes of the program p41.bin on to the code memory of the 89S51 MCU. The

procedures are: i. At the DOS prompt, type 89S51 and hit carriage return.

ii. Follow the menu iii. Erase the Chip.

16. Write the chip. [type small letter y for Yes and n for No]. Type: Capital letters for digits. 17. Execute the downloaded program by executing P89s51.exe from the path: C:\ckita 18. The LED-D1 should statrt5 blinking.

7

Page 8: 89S52 Micro Controller Interfacing

5 Example Programs and Interfacing Circuits 5.1 Functionality Check of INT0/-pin of 89S51 MCU of the CKIT-06A In this example, we configure Pin-12 of the MCU to operate as ‘Interrupt Input Line’.

12 INT0/ (P32) IE0SWA

0

1IT0=0,1

SWB SWC

EA=1 EX0=1

IRQ : 0003H

IT0

IE0

IT1

IE1TCON (88H) EX0

EA IE (A8H)

1100

cngmet\ch8n\8-9n+5VInernalPullup(5k)

InterruptingDevice (K1)

Figure-5.1: Structure of the INT0/ Interrupt Pin of 89S51

Working Principles: a. Assume that the SWA is at position ‘1’, which could be done by writing LH at IT0-bit of the TCON

register. b. When a High-to-Low going signal appears at Pin-12, the IE0-bit of TCON register is set to LH. c. Assume that the switches SWB and SWC are closed. These switches could be opened and closed as

required by manipulating the relevant bit of the IE register. d. The MCU would be automatically interrupted. e. The MCU would suspend the mainline program and would jump to the ‘Interrupt Service Routine’

due to INTO (ISURINT0) at the address: 0003H. This is known as ‘Vectored Interrupt’. f. If the switches SWB and SWA are opened, then the user can still detect the occurrence of an activating

signal at Pin-12 by reading the IE0-bit and the user can deliver the interrupt services. This is known as ‘Polling Interrupt’.

Functional Check: a. Mainline Program (MLP) located at: 0000H → 0010H. Output: The LED-D1 would be continuously blinking. b. The ISURINTO program is located at: 0003H → 0030H Output: The LEDD1 would blink for 5 times with a long gap in between them. c. At the end of the execution of ISURINT0, the MLP would normally be resumed. Procedures: a. Use the LPT port and store the program C:\CKITA\P51.bin into the 89S51 MCU. b. Run the downloaded program either by activating the LPT port or the RST switch. c. The MLP should start normally. d. After a while, take apiece of hookup wire and gently touch Pin-12 to ground potential. e. The MLP should be suspended. f. The ISURINT0 should start and comes to an end. g. The MLP should resume. h. Repeat the process.

8

Page 9: 89S52 Micro Controller Interfacing

5.2 Functionality Check of Pin-14 as External Event Counter In this example, we configure Pin-14 of the MCU to operate as an ‘External Event Counter’. The Pin-14 would receive external LH-to-LL transitions and the internal Counter-0 would count them.

OSC 1/12 f1=921.6KHz

Y1

C0-T0/=1

SW2SW1

18

19

XT2

XT1

11.0592MHz TF0

534b

P3414

TL0

TH0(8-bit)

Address:8AH

Address:8CH

Counter-0

Figure-5.2: Structure of Counter-0 of MCU in Mode-2 (8-Bit Auto Reload) Operation

Working Principles: 1. Timer/Counter-0 of MCU operates as Counter-0 when the internal switch SW1 is connected to Pin14

and the mode of operation is configured properly with the help of TMOD register. We say that the there is a Timer-0 operation when the SW1 is internally connected with the internal oscillator signal.

2. The open/close condition of SW2 is controlled by TCON register. 3. Initially both TL0 and TH0 are loaded with some user defined values. The counter-0 counts an external

event and increments the TL0. And when the content of TL0 rolls over from all 1s to all 0s, the TF0-bit of the TCON register is set to LH. At the same time, the TL0 is automatically loaded with the content of TH0 register.

4. A user can poll the TF0-bit to know that the counter has been filed up. If interrupt structure is enabled then the setting of the TF0-bit can be brought to the attention of the MCU through vectored interrupt.

Functional Check: We intend to count the negative zero crossing points of the ac line frequency. The counter-0 would be employed to count 50 (fifty) –ve crossing points, which would amount to 1-sec time delay. After each 1-sec time delay, the MCU can drive an external 7-segment display device to indicate amount of time elapsed.

Procedures: 1. Refer to Fig-5.3, build the following zero crossing circuit and the 7-segment display unit on the

breadboard of the CKIT. 2. Download and execute program: C:\CKITA\P52.bin. The DP0 would count the time and be advancing by 1-sec.

D3(on CKIT)

R1 : 1k5

+5V

R21k5

0V

20mS

T0/(P34)14

P00

p

a

P07

+5V

8x560RDP0

ccP20

MCU

1003x : GM : 03-05

Q1: C828

Figure-5.3: Circuit to Detect and Display 1-sec Time Tick from AC Line Frequency

9

Page 10: 89S52 Micro Controller Interfacing

5.3 Functionality Check of P30, P31 Pins as Serial Ports In this example, we configure Pin-10 (P30) and Pin-11 (P31) to operate as serial communication port. The functionality of the serial port would be verified by connecting the MCU with the IBMPC using a 9-pin serial cable as per circuit diagram of Fig-5.4. 4800-Bd, NP, -SPB, 1-STB,8-Ch.

TxD

Rx Buffer RxD

Port-3 : B0H

Address: 99H

Address: 99HTx Buffer

P30P31P32P33P34P35P36P37

P30

P31

10

11

121314151617

U1: 89S51 Microcontroller U2 : MAX232 Holes

12 R1OUT

11 T1IN

R1IN 13

14T1OUT

RXTTL

TXTTL

TXRS

RXRS

1

2

3

4

5

6

7

8

9

COM1 Port of IBMPC

Pins

1

3

2

4

5

6

7

8

9

CD/

DTR/

DSR/

RTS/

CTS/

+5V

TXRS

RXRS

0V15 COM

753x : GM : 10-05

CKIT-06A IBMPCRS<-->TTL 9-Pin Cable

P07

P00

+5V

DP0

a

p

P2.0

cc

15GND VCC

16 +5V0V

8x560R

+1uF

+1uF

1

3C1

4

5

1uF+

16

2

1uF+

6

C2

C3

C4

Figure-5.4: Connection Setup to Check Functionality of Serial Port of MCU

Setup Procedures: 1. Place the zero crossing circuitry and the 7-segment display device (DP0) on the breadboard and

connect it with the MCU as per diagram of Fig-5.4. 2. Place the MAX232 chip (RS ↔ TTL Converter) on the breadboard and connect it with the MCU as per

diagram of Fig-5.4. Also install the capacitors C1-C4 in correct polarity as per diagram of Fig-5.4. 3. Obtain a 9-pin serial port cable and short the lines: 7,8 and 1,4,6 as per circuit diagram of Fig-5.4. 4. Attach the hole-side of the 9-pin connector with the COM1 port of the IBMPC. The other side of the 9-

pin cable should be connected with the MAX232 chip as per circuit diagram of Fig-5.4. Functional Check: 1. Boot the IBMPC in the pure DOS mode and enter into path: C:\CKITA 2. Download (write) the program C:\CKITA\P53.bin into the MCU and execute the program. 3. From the DOS prompt of the PC, execute the program: c:\CKITA\SERCHK.EXE. 4. Press any key from 0-9 in the keyboard of IBMPC and it would be instantly displayed on DP0 of the

trainer. Working Principles: 1. The MCU keeps polling the receiver port and looks for the arrival of a character. The ASCII code of the

character is read from the receiver buffer. It is converted to CC-code and is then displayed on DP0. The MCU also sends the received character back to the IBMPC. The PC displays the character on CRT.

3. The IBMPC receives the ASCII code from the buffer and displays them on the CRT monitor. 4. The IBMPC keeps polling the keyboard and whenever a close key is found, the corresponding ASCII

code is instantly transmitted to the MCU. 10

Page 11: 89S52 Micro Controller Interfacing

5.4 Interfacing 2x16 Character LCD with MCU as a Bus-operated Device Let us interface a 2x16 character LCD unit with the 89S51 MCU of the CKIT-06A. The LCD would be accessed as a ‘Bus Operated’ device. In a bus-operated device, the data read/write operations are carried out using the bus signals (Address, Data and Control), which are automatically generated at the time the instructions are executed. The disadvantage of a bus–operated system is that there is wastage of ‘Port Lines’ of the MCU, which cannot be tolerated in a ‘Single Chip’ solution. In Section-5.5, we would operate the LCD device as a port-operated device in which the bus-signals (Address, Data and Control) are generated artificially using instructions. The advantage of port-operated device is that there is a savings of many port lines, which could be easily used for some other purposes.

2x16 Charracter LCD

DP0 DPF

DP10 DP1F

D0

D1D2

D3

D4

D5

D6

D7

E R-W/ D-I/ VA Vdd Vss

89S51

AD7/P07

AD2/P02AD1/P01AD0/P00

AD3/P03AD4/P04AD5/P05AD6/P06

7891011121314 6 5 4 3 2 1

+5

L+ L-

To S

ee at

Dar

k

0V+5V

Viewing Angle0V R10:1k5

A9(P21)A8(P20)

WR/(P16)RD/(P17)

0V

123456

Y0/Y1/Y2/

12

151413 3

U15: 74LS138 U16: 7486747x-05:GM

A10(P22)

U14

LCDIR: 4000HLCDSR : 4200HLCDDR : 4100HLCDCR : 4300H

A14(P27)

15 16

0V

K1

Figure-5.5: Interfacing Circuit for Bus-operated LCD Device Setup: 1. Refer to Fig-5.5 and place the device U14 (LCD), U15 (74LS138) and U15 (7486) on the breadboard of

the CKIT and connect them together along with 89S51 MCU using the hookup wires. 2. Use LPT and then store the program c:\ckitA\P54a.bin into 89S51. 3. Execute the program P55a.bin, the LCD should show character 3 at DP0 position of 1st Line. 4. Erase the MCU and store c:\ckita\p54b.bin. Execute the program, the LCD shows: 2 at DP2 position of

the 2nd line. 5. Erase and store the program C:\CKITA\P54c.bin. Execute the program. The LCD shows the following

message: Karighar R&D C Tel: 7161846

11

Page 12: 89S52 Micro Controller Interfacing

5.5 Interfacing 2x16 Character LCD with MCU as a Port-operated Device (8-Bit Data) Let us interface a 2x16 character LCD unit with the 89S51 MCU of the CKIT-06A. The LCD would be accessed as a ‘Bus Operated’ device. In a bus-operated device, the data read/write operations are carried out using the bus signals (Address, Data and Control), which are automatically generated at the time the instructions are executed. The disadvantage of a bus–operated system is that there is wastage of ‘Port Lines’ of the MCU, which cannot be tolerated in a ‘Single Chip’ solution. In Section-5.5, we would operate the LCD device as a port-operated device in which the bus-signals (Address, Data and Control) are generated artificially using instructions. The advantage of port-operated device is that there is a savings of many port lines, which could be easily used for some other purposes.

2x16 Charracter LCD

DP0 DPF

DP10 DP1F

D0

D1D2

D3

D4

D5

D6

D7

E R-W/ D-I/ VA Vdd Vss

89S51

AD7/P07

AD2/P02AD1/P01AD0/P00

AD3/P03AD4/P04AD5/P05AD6/P06

7891011121314 6 5 4 3 2 1

+5

L+ L-

To S

ee at

Dar

k

0V+5V

Viewing Angle0V R10:1k5

A9(P21)A8(P20)

747y : 05:GM

U14

15 16

0V

K1

A10(P22)

+5V

8x4k7

Figure-5.6: Interfacing Circuit for Port-operated Device (8-Bit Data Line) Setup: 1. Refer to Fig-5.6 and place the device U14 (LCD) and resistor network RN1 on the breadboard of the

CKIT and connect them together along with 89S51 MCU using the hookup wires. 2. Use LPT and then store the program c:\ckitA\P55a.bin into 89S51. 3. Execute the program P55a.bin, the LCD should show character 7 at DP0 position of 1st Line. 4. Erase the MCU and store c:\ckita\p55b.bin. Execute the program, the LCD shows: A at DP2 position

of the 2nd line. 5. Erase and store the program C:\CKITA\P55c.bin. Execute the program. The LCD shows the following

message: Karighar R&D C Tel: 7161846 6. The readers may study program p55c.asm in consultation with the data sheet-timing diagram of LCD.

12

Page 13: 89S52 Micro Controller Interfacing

5.6 Interfacing 2x16 Character LCD with MCU as a Port-operated Device (4-Bit Data)

2x16 Charracter LCD

DP0 DPF

DP10 DP1F

D0

D1D2

D3

D4

D5

D6

D7

E R-W/ D-I/ VA Vdd Vss

89S51

AD7/P07

AD4/P04AD5/P05AD6/P06

7891011121314 6 5 4 3 2 1

+5

L+ L-

To S

ee at

Dar

k

0V+5V

Viewing Angle0V R10:1k5

A9(P21)A8(P20)

748y : 06:GM

U14

15 16

0V

K1

A10(P22)

+5V

4x4k7

Figure-5.7: Interfacing Circuit for Port-operated Device (4-Bit Data Line) Setup: 1. Place the LCD and RN1 on the breadboard of the CKIT. 2. Use hookup wire and connect the LCD and RN1 with the 89S51 MCU as per circuit diagram of Fig-5.7. 3. Store the program c:\ckita\p56a.bin into the MCU and execute it. 4. The LCD should shoe character 7 at DP0 position of the 1st line. 5. Erase the chip and then store program p56b.bin. 6. Execute the program p56b.bin. 7. The LCD should show the character C at DP2 position of the 2nd line. 8. Erase the MCU. 9. Store the program c:\ckita\p56c.bin. 10. Execute the program: C:\ckita\p56c.bin. 11. The LCD should show the following message on the display:

Karighar R&D C Tel: 7161846

13

Page 14: 89S52 Micro Controller Interfacing

5.7 Interfacing 7-Segment Display Devices and Walking 1’s Keyboard using 8279 Controller In any microcomputer system, the display unit and the keyboard are the two most important IO devices. The display is usually built using 7-segment display devices and the keyboard is made using PCB mountable ON/OFF keys. The 8279 are a MPU/MCU bus compatible versatile IO controller that allows interfacing both the 7-sement display devices and the ON/OFF keys. However, in MCU applications, the display and the keyboard are driven using the built-in port lines, as we will find in Section-5.8.

DP0 DP7 DP8 DPF

cc0 cc7 cc8 ccFB0B1B2B3A0A1A2A3

abcd

h

efg

ABCEn/

ABCEn

S0/

S7/

S0/

S7/

ABC

S0/

S7/

RL0RL1RL2RL3RL4RL5RL6RL7

K00 K07

SL0Sl1

SL2SL3

D0D1D2D3D4`D5D6D7

AD0/P00

AD7/P07

RD/WR/WR/(P36)

RD/(P37)

CS/A (C-D/)

A14(P27)A8(P21)

CLKALE

IRQ

RST0V

89S51 U12: 8279

U13: 74138 U14: 74138

U15: 74138

1200: Gm: 03-06

Figure-5.8: 8279-Driven 7-Segment Display Unit and Keyboard Setup: 1. Consult data sheets for 8279, 74138 to get their pin numbers and signal names. 2. Place the U12, U13, U14, U15 , CC-type 7-Segment devices and the keyboard on the breadboard. 3. use hookup wire and connect the devices of the breadboard with the 89S51 MCU as per circuit

diagram of Fig-5.8. 4. Store the program C:\ckita\p57a.bin into the MCU and execute it. The display should the changing

pattern of: blank, 0, 1, 2, 3,………,E,F , ., 0, 1….. 5. Erase the chip and then store the program c:\ckita\p57b.bin. Execute the program. 6. Press the keys and find that the DP0DP1 positions show the Scan Code of the pressed down key.

14

Page 15: 89S52 Micro Controller Interfacing

5.8 Interfacing 7-Segment Display Devices and Walking 1’s Keyboard using MCU Port Lines

DP0 DP7 DP8 DPF

cc0 cc7 cc8 ccFabcd

h

efg

ABC

S0/

S7/

P10P11

P17

K00 K07

P00

P0789S51

U13: 74138

1200x: Gm: 03-06

+5V8x4k7

ABCD

S0/

SF/

P20

P23

P24

P26P25

U12: 74154

P21P22

Figure-5.9: Port Line Driven 7-Segment Display Unit and Keyboard Setup: 1. Consult data sheets for 8279, 74154 and 74138 to get their pin numbers and signal names. 2. Place the U12, U13 and CC-type 7-Segment devices and the keyboard on the breadboard. 3. Use hookup wire and connect the devices of the breadboard with the 89S51 MCU as per circuit

diagram of Fig-5.8. 4. Store the program C:\ckita\p58a.bin into the MCU and execute it. The display should the changing

pattern of: blank, 0, 1, 2, 3,………,E,F , ., 0, 1….. 5. Erase the chip and then store the program c:\ckita\p58b.bin. Execute the program. 6. Press the keys and find that the DP0DP1 positions show the Scan Code of the pressed down key.

15

Page 16: 89S52 Micro Controller Interfacing

5.9 Generation of PWM (Pulse Width Modulated) Signal using Port-operated 8253

GM: 03-05: 744xU1 : 89S51 8253

PIT

MCU

50Hz Line

Oscilloscoipe

Ch1

Ch2One Shot

GT0CK0OT0

GT1CK1OT1

GT2CK2OT2

ALE of 8951 : 1.10592MHz

CT0(00B)

CT1(01B)

CT2(02B)

D1D0

D2D3D4D5D6D7

P00P01P02P03P04P05P06P07

39

36

3837

35343332

P20P21P23

A0A1CS/

RD/WR/

P24P25

VccGNDGND

Vcc 4020

212224

2526

1224

2223

192021

87654321 11

109

141512

161817

+5V

R2: 1k5R1: 1k5D3

20mS

x

ALE 30

Q1 : C828

y

0VC1: 104

CT2(03H)

Figure-5.10: Generation of PWM Signal using Port-operated 8253 Programmable Timer

Theory: ‘Pulse Width Modulation’ means ‘Changing the ON Time (Mark) Width of a Pulse’. In Fig-5.10, let us see that the period of the signal of Ch-2 of the oscilloscope is fixed and it is 20mS. But the OFF Time (Space), x is variable and hence the ON Time (Mark) y is also variable. Here, we have a pulse of variable width, which could be used (and of course after suitable conditioning) to control the rotating speed of a fan, the luminescence of a dim light and so on. In this example, we will learn and practice the procedures of generating a PWM signal using the Timer-0 of the 8253.

Brief Architecture of 8253: There are 3 identical timers/counters inside the 8253 chips, which could be configured to operate in various mode of operation. In this example, we would initialize Timer-0 as a ‘Programmable One-shot’ to produce a PWM signal similar to one as shown in Fig-5.10. A square wave signal (SWS) of 20mS duration is obtained from the ac line frequency using a ‘Zero Crossing Circuit’ consisting of D3, C1, R1, R2 and Q1. When a rising edge of the SWS arrives at the GATE0-pin of Timer-0, the OUT0-pin immediately assumes LL state and remains Low until the content of the internal Counter-0 is emptied. Here, we see that a PWM signal could be obtained by writing variable data inside the 16-bit Counter-0 of the 8253. The readers are referred to data sheet for the detailed study of the architecture and programming of the 8253.

Setup: 1. Gently solder a piece of wire with the anode of the D3 of the CKIT-06A. Place the components R1, R2

and Q1 on the breadboard and connect them together using hookup wires as per circuit diagram of Fig-5.10. This circuit is known as Zero Crossing Detector (ZCD).

2. Place an 8253 chip on the breadboard and connect it various pins with the ZCD and the MCU of CKIT. 3. Connect an oscilloscope with the 8253 as per directions of the diagram of Fig-5.10. 4. Store the program c:\ckita\p59.bin inside the 89S51 MCU and execute it. 5. Observe in the oscilloscope that the width of the oneshot pulse is continuously changing. 6. Open the program C:\CKITA\P59.ASM and study it with the assistance of a teacher. 7. Operate Timer-1 as a 20KHz square wave generator.

16

Page 17: 89S52 Micro Controller Interfacing

5.10 Interfacing Multiple 8253 as Bus-operated Devices In Section-5.9, we operated a single 8253 as a port-operated device in which the address, data and control signals for the 8253 have been generated artificially using the port lines of the MCU. In a bus-operated device, the address, data and control (RD/ and WR/) signals are automatically asserted by the MCU concurrently with the execution of program instructions. U1 : 89S51

U2: 8253

PIT-1

MCU

50Hz Line

Oscilloscoipe

Ch1

Ch2One Shot

GT0CK0OT0

GT1CK1OT1

GT2CK2OT2

ALE of 8951 : 1.10592MHz

CT0(8000H)

CT1(8100H)

CT2(8200H)

D1D0

D2D3D4D5D6D7

P00P01P02P03P04P05P06P07

39

36

3837

35343332

A8(P20)A9(P21)

A14(P27)

A0A1CS/

RD/WR/

RD/(P36)WR/(P37)

VccGNDGND

Vcc 4020

212227

1617

1224

2223

192021

87654321 11

109

141512

161817

+5V

R2: 1k5R1: 1k5D3

20mS

x

ALE 30

Q1 : C828

y

0VC1: 104

CT2(8300H)

U3: 8253

PIT-2

GT0CK0OT0

GT1CK1OT1

GT2CK2OT2

CT0(4000H)

CT1(4100H)

CT2(4200H)

D1D0

D2D3D4D5D6D7

A0A1CS/

RD/WR/

VccGND

23

192021

87654321 11

109

141512

161817

CT2(4300H)22

2412

A15(P27) 28

Figure-5.11: Interfacing Multiple 8253 as Bus-operated Devices

Working Principles: The 8253 devices are interfaced with the 89S51 MCU as bus-operated devices. The U2 and U3 are assigned the following addresses for their various internal registers:

Register Name Address for U1 Address for U2 Counter-0 8000H 4000H Counter-1 8100H 4100H Counter-2 8200H 4200H Mode Register 8300H 4300H

When the MCU asserts address 8000H on the bus, the A15-pin assumes LH and A14-pin assumes LL. The device U1 is selected and U2 is deselected. The opposite occurs at address 4000H 17

Page 18: 89S52 Micro Controller Interfacing

5.11 Converting 6-Digit BCD-to-BINary In many situations relating to instrument design such as Weighing Machine, we would be required to convert BCD number into Binary and vice versa. The readers may study the Fig-63.10 for the applications of BCD2BIN, BIN2BCD and BMULT subroutines. In this section we would practice the procedures of converting 6-digit BCD number into equivalent BINary number. The input (000000 – 999999) and the output (000000H – 0F423FH) numbers would be displayed on a LCD display.

Setup: 1. Install a 2x16 character LCD panel on the breadboard and connect it with the MCU of the CKIT-06A as

per circuit diagram of Fig-5.7. 2. Open the program c:\ckita\p518.asm and find that the input BCD and output BIN values are

associated with RAM locations as follows: L1X: MOV 54H, #12H ; Input BCD Higher Byte

MOV 53H, #34H ; next Byte of BCD MOV 52H, #56H ; lowest byte of BCD

L2x: MOV 48H, #00H ; to hold Higher Byte of BINary MOV 47H, #00H ; to hold next byte of BINary MOV 46H, #00H ; to hold lower byte of BINary

3. Store the program C:\CKITA\p518.bin into MCU and execute it. The as:

InpBCD : 123456 OtpBIN : 01E240 4. Change the input BCD to 999999. Assemble the program P518.asm and convert to P518.bin. Store the

program P518.bin into MCU and execute it. The LCD should show as:

InpBCD : 999999 OtpBIN : 0F423F Working Principles of P518.asm

Input BCD (000000-999999)Internal RAM Input BCD Data Order

5354

x1x0x3x2

MSDigits

LSDigits

Ouput BINar y (000000H-0F423FH)Internal RAM Input BCD Data Order

46

48

y1y0y3y2

MSDigits

LSDigitsSDBCD2BIN

Calling at: .............512

x5x4

52

y5y447

Figure-5.12: Data Structure for Converting Six Digit BCD Number into Binary

1. Extract x0 in the form of 0X0 from x1x0 and get (00H – 09H) taking positional weight as: 01H for 1.

Update output binary table: 48 - 46 2. Extract x1 in the form of 0x1 from x1x0 and get (00H – 5AH) taking positional weight as: 0AH for 10.

Update output binary table : 48 - 46. 3. Extract x2 in the form of 0X2 from xx3x2 and get (0000H – 0384H) taking positional weight as: 64H for

100. Update output binary table: 48 - 46 4. Extract x3 in the form of 0x3 from x3x2 and get (0000H – 2328H) taking positional weight as: 03E8H for

1000. Update output binary table : 48 - 46. 5. Extract x4 in the form of 0X4 from x5x4 and get (000000H – 015F90H) taking positional weight as:

2710H for 10000H. Update output binary table: 48 - 46 6. Extract x5 in the form of 0x5 from x5x4 and get (000000H – 0DBBA0HH) taking positional weight as:

0186A0H for 100000. Update output binary table : 48 - 46.

18

Page 19: 89S52 Micro Controller Interfacing

5.12 Converting 40-Bit BINary Number into Equivalent BCD using Horner Rule

Setup: 1. Install a 2x16 character LCD panel on the breadboard and connect it with the MCU of the CKIT-06A as

per circuit diagram of Fig-5.7. 2. Open the program c:\ckita\p519.asm and find that the input BIN and output BCD values are

associated with RAM locations as follows: LOAD: BIN2BCD: MOV 68H, #12H ; Input BIN Hbyte MOV 5DH, #00H ; to hold BCD HByte MOV 67H, #34H ; MOV 5CH, #00H MOV 66H, #56H ; MOV 5BH, #00H MOV 65H, #78H MOV 5AH, #00H MOV 64H, #90H ; Input BIN Lbyte MOV 59H, #00H MOV 58H, #00H ; to hold BCD LByte

3. Store the program C:\CKITA\p519.bin into MCU and execute it. The as:

Bin: 1234567890 Bcd: 078187493520

4. Change the input BIN to FFFFFFFF. Assemble the program P519.asm and convert to P519.bin. Store the program P51.bin into MCU and execute it. The LCD should show as:

Bin: FFFFFFFFFF Bcd: 099511627775

Working Principles:

Input BINary (0000000000H - FFFFFFFFH)Internal RAM Input BCD Data Order

6566 x5x4

x3x2

MSDigits

LSDigits

Ouput BCD (000000000000- 099511627775)Internal RAM Input BCD Data Order

5C5D

y5y4y7y6

MSDigits

LSDigits

BIN2BCD

Calling at: .............

514x

6768

x7x6

x1x05A5B

y3y2y1y0

39 32 23 16 815 7 064H65H66H68H

Input : 40-Bit Binary Data

3 158H59H5CH5DH

Ouput: 12-Digits BCD Data

BIN2BCD

028911 10

x9x8

64

y9y8y11y10

5958

67H31 24

5AH4567

5BH

Figure-5.13 : Data Structure for 40-Bit Binary to BCD Conversion

Input BINary = b39b38,…………….,b1b0

Output BCD = b39x239 + b38 x 238 + ………………. + b1 x 21 + b0 x 20

= (((((( ……((((b39)2 + b38)2 + ……………+ b3)2 + b2) 2 +b0

When we carefully observe the above expression while moving from right to left, we notice that the bit values (say b0) are always added with ‘something’, which is multiplied by 2. This feature is not seen for bit b39. To offer the same feature to the bit b39, we may rewrite the above expression as:

Output BCD = (((((( ……((((IBCDx2 + b39)2 + b38)2 + ……………+ b3)2 + b2) 2 +b0

Where, IBCD (Initial Partial BCD) is assigned the value of 00.

This expression is known as ‘Horner Rule’ due to W.G. Horner, who used it in the year of 1819 (the original rule belongs to Isaac Newton in 1711) for algebraic simplification. This is a particular case of the general-purpose polynomial of the form: A(x) = anxn + an-1xn-1 + …………..+ a1x1+ a0 x0

19

Golam Mostafa (the author) of Ahsanullah University of Science and Technology (AUST), Dhaka for the 1st time in Bangladesh in the year of 1998, carried out the low-level implementation of the Horner Rule to convert a 32-bit BINary number into equivalent BCD number in connection with the design of ‘Digital Weighing Machine’ using 8085/8086/8051 architectures.

Page 20: 89S52 Micro Controller Interfacing

5.13 16-Bit by 16-Bit Unsigned Binary Multiplication (BMULT) Setup: 1. Install a 2x16 character LCD panel on the breadboard and connect it with the MCU of the CKIT-06A as

per circuit diagram of Fig-5.7. 2. Open the program c:\ckita\p520.asm and find that the input and output numbers are associated with

RAM locations as follows: INPUT_1: OUTPUT: MOV 59H, #78H ; Input1 Hbyte MOV 5FH, #00 ; to hold Output HByte MOV 58H, #56H ; Input1 LByte MOV 5EH, #00H MOV 5DH, #00H INPUT_2: MOV 5CH, #00H MOV 5BH, #12H ; Input2 Hbyte MOV 5AH, #34H ; Input2 LByte

3. Store the program C:\CKITA\p520.bin into MCU and execute it. The as:

Inputs: 7856,1234 Output: 088E7D78

4. Change the inputs to FFFFH and FFFFH. Assemble the program P520a.asm and convert to P520a.bin. Store the program P520a.bin into MCU and execute it. The LCD should show as:

Inputs: FFFF,FFFF Output: FFFE0001

Working Principles:

4041424344454647

4Bc1c0c3c2

r1'r0'r3'r2'

r3''r2''r5''r4''

r3'''r2'''r5'''r4'''r5''''r4''''

4A

r7''''r6''''

Input-2: Multiplicand (0000H - FFFFH)

Ouput; Product (00000000H- FFFE0001H)Internal RAM Input BCD Data Order

5E5F

r5r4r7r6 MSDigits

LSDigits

Calling at: .............

5C5D r3r2

r1r0

BIN2BCD

Internal RAM Input BIN Data Order

5A5B x3x2 MSDigits

LSDigitsx1x0

Internal RAM Input BIN Data Order

5859 y3y2 MSDigits

LSDigitsy1y0

Input-1: Multiplier (0000H - FFFFH)

X

y1y059H

51958H

y3y2

5AHx1x0

5BHx3x2

BMULTCarry

Carry Carry

5CH5DH

5EH5FHX

Figure-5.14: Data Structure to Compute 16-Bitx16-Bit Binary Multiplication

x3x2 x1x0y3y2 y1y0

`r3'r2' r1'r0'r5''r4'' r3''r2''

r5'''r4''' r3'''r2'''

= (x1x0) x (y1y0) : L1= (x3x2) x (y1y0) : L2= (x1x0) x (y3y2) : L3

r7''''r6'''' r5''''r4'''' = (x3x2) x (y3y2) :L4r1r0

c1c0

519

: Multiplier: Multiplicand

c3c2r3r2r5r4r7r6

A-reg

B-reg

yy

xx

MUL AB

A-reg

B-reg

Low 8-BitsHigh8-Bits

519

Figure-5.15: 8051 Register Model for BMULT Figure-5.16: Computing Mechanism for BMULT

20

Page 21: 89S52 Micro Controller Interfacing

5.14 Performing m-Bit by n-Bit Division using GCR-Method on 8-Bit Machine Introduction: There may be occasions where we might need to divide an m-bit (say 40-it, 00576E7BFFHH) number by an n-bit (say 32-bit, 19B24DE6H) number using an 8-bit architecture machine such as 8051. The author encountered such situation while developing a ‘Prepaid KWH Meter’ using ADE7756/89S8252 devices. The energy measurement chip ADE7756 outputs a 40-bit code, as a basic measure of energy while the 89S8252 is an 8-bit machine. To extract the quanta (nx0.01) kwh, the author had to find a way of dividing m-bit by n-bit without violating the allowable % error. The result is the GCR-Method (Count Residual method due to Golam Mostafa), whose working principle is explained briefly with the help of an experimentally verified example:

1. Assume 140w electric load is ON for 5-sec 2. The 5-sec accumulated energy is: 576E7BFFH = 1466858495 = X 3. Assume 40w load is ON 5-sec and accumulated energy is: 19A5516EH = 430264686 = Y

Y1 = FF x FF x FF x 1A = 19B24DE6H = 431115750 % error = (Y-Y1)/Y = (430264686 – 431115757)/ (430264686) = 0.20%

4. Quanta (nx0.01) kwh: = {(Reading of Step-2)/ (Reading of Step-3)} x {(0.01/36000) x (40x5)} = (X / Y1) x (0.01/B4H) = (X/FF) x (1/FF) x (1/FF) x (1/B4) x 0.01 kwh

5. X/FF(z8) = q1 – z9 1AH ;divider is Z8, remainder is named z9 and is saved in 1AH. : 57C642 - 41 q1/FF(z6) = q2 – z7 1BH : 581E - 60 q2/FF(z4) = q3 – z5 1CH : 58H – 76H q3/B4(z2) = q4 – z3 1DH : 00 – B4H q4/1A = q5 – z1 1EH ; : 00 – 1AH

6. The quanta kwh is q5 7. The integrated Residual is:

R = ((((z1) z2 + z3) z4 + z5) z6 + z7) Z8 + z9 = z1z2z4z6z8 + z3z4z6z8 + z5z6z8 + z7z8 + z9

8. For the given data of Step-4, we should have: q5 = quanta = 00H R = 00H + 56F907A8H + 751476H + 5FA0H + 41H = 576E7BFFH

Binary Division Process using GCR-Method:

08

RAM Loc

09

RAM Loc

0A0B0C

0D0E0F101112131415

Divisor

Dividend

Residue

Quuotientas Countsin the formof: 0X(00 - 0F)

Con

y1y0x1x0x3x2x5x4x7x6

ConVariable Variable

RC0C1C2C3C4C5C6C7BDIV MERG

RAM Loc Con

08090A0B0C

C1C0C3C2C5C4C7C6

Variable

QuuotientasPackedHex

764x : GM : 10-05

T1 T2 T3

0D x9x8

16

0D C9C8

C8C917

21 Figure-5.17: Data Structure for 40-Bit by 8-Bit Binary Division using GCR-Method

Page 22: 89S52 Micro Controller Interfacing

5.15 Read/Write with Data EEPROM of 89S8252 using Program Instructions Looking at Fig-A.5, we find that the AT89S8252 MCU contains 2K Data EEPROM along with security bit. The DATA EEPROM occupies the space: 0000H – 07FFH. A user may store ‘System Confidential Data’ inside the data EEPROM and then set the security bit. Once the security bit is set, there is no way left to access the content of the data EEPROM using the ‘Conventional Programmer’ or the ‘ISP Port’. This feature of the 89S8252 allows it for building ‘Prepaid Energy Meter’ and the similar products.

There are three ways of writing data inside the data EEPROM and these are:

1. Using conventional ROM Programmer putting the MCU OFF-line (not running). 2. Using Program Instructions of the MCU itself keeping the MCU ON-line (running). 3. Using ISP Port of the MCU keeping the chip OFF-line.

Writing into Data EEPROM using Program Instructions: 1. Install the AT89S8252 MCU on the CKIT-06A. 2. Install the LCD on the breadboard of the CKIT as per circuit diagram of Fig-5.5. The LCD is an output

device, which allows a user to monitor the input and output variables. 3. Let us store the program C:\ckita\P522a.bin into the MCU and execute it. The program does the

following: a. It writes 12H, 34H , 56H into locations 00EAH, 00EBH and 00ECH respectively. b. It reads back the locations 00EAH, 00EBH and 00ECH. c. The LCD shows as follows:

Write: 12,34,56 Read: 12,34,56

Data Structure: I

08

09

0A

12H

34H

56H

22

mmediate Data RAM Locations EEPROM Locations

00EA

00EB

00EC

RAM Locations

EA

EB

EC

LCD

Write:12,34,56

Read: 12,34,56

1201:GM:03-06

Figure-5.18: Data Structure for the Process of Writing into EEPROM of 89S8252 MCU Working Principles: Let us open the program c:\ckit\p522a.asm and we find that the writing process into the EEPROM of the 89S8252 MCU involves the following steps:

1. The data 12H, 34H and 56H are written into RAM locations 08H – 0AH for matter of convenience. However, the data could be directly written into the EEPROM.

2. The rules for performing data read/write operations into the EEPROM of the MCU are clearly stated into the data sheets of the MCU. We follow the data sheets and copy the contents of RAM locations 08H-0AH into EEPROM locations 00EAH-00ECH.

3. The 3-byte data are read back from the EEPROM and are saved into RAM locations EAH-ECH. 4. The content of the locations EAH-ECH are displayed on the LCD to visualize that the data have

been correctly written/retrieved into/from of the EEPROM of the MCU.

Page 23: 89S52 Micro Controller Interfacing

5.16 Functionality Check of the SPI (Serial Peripheral Interface) Port of 89S8252. The four port pins (P1.4-P1.7) of the 89S8252 microcontroller could be configured to operate as a ‘High Speed Synchronous Serial Peripheral Interface Port (SPI Port)’ to exchange data with compatible devices like:

i. Another 89S8252 microcontroller ii. AT25040 Serial EEPROM iii. ADE7756 Energy Measurement Chip (EMC)

In this section, we shall carry out a functional test of the SPI port of the 89S8252 MCU using an ADE7756 EMC chip. The set up consists of:

i. Placing the EMC chip on the breadboard of the CKIT-06A. ii. Placing an 89S8252 MCU into the 40-pin socket of the CKIT iii. And then connecting the 89S8252 and the ADE7756 as per circuit diagram of Fig-5.19.

89S8252 MCU ADE7756 EMC

SCKDOUTDIN

SCK(P17)MISO(P16)MOSI(P15)

876

181920

RST/5P14(SS/) 1 +5V32

AVddAVdd

REFIN/REFOUT 9+

C147uF

C20.1

8AGND10DGND

Y13.58MHz

CLKOUT

CLKIN

16

15

V2PV2N

76 Vol Channel

V1PV1N

45 Curr Channel

0V CS/17SAG/IRQ/ZXCFNC

867 : GM : 03-05

Figure-5.19: Connection Diagram of MCU and EMC to Test SPI Port To be sure that the SPI Port of the MCU is functioning properly, we shall perform data read/write operations on the ‘Mode Register’ of the ADE7756 chip. The particulars of the Mode Register are: i. Size : 16-bit ii. Operation Mode : Read and Write iii. Content after Power up Reset : 000CH Reading Mode Register after Power UP: (c:\ckita\p523a.asm) Store the binary codes of the program c:\ckita\p523a.bin into the 89S8252 MCU and execute it. The LCD should show: MR_Read:000CH Writing 1234H (New Data) into Mode Register and Reading it Back (p523b.asm) Download program p523b.bin and execute it. The LCD should show: MR_Read:1234H.

23

Page 24: 89S52 Micro Controller Interfacing

6.1 24-Hr Clock

Procedures: 1. Use telephone hook up wires and build the following circuit on the breadboard and connect them with

the MCU as per diagram of Fig-6.1. i. 20mS TT (Time Tick) acquisition circuit from the line frequency ii. Time Display Unit using CC-type 7-Sement Display devices

D3(on CKIT)

R1 : 1k5

+5V

R21k5

0V

20mS

T0/(P34)14

P00

p

a

P07

+5V

8x560R HRS MIN SECDP0

DP1DP2

DP3DP4

DP5

cc cc cc cc cc cc

P20P21P22P23P24P25

MCU1002x : GM : 03-05

Q1: C828

Figure-6.1: CKIT-06A Based Circuit for the 24-Hr Clock System 2. Boot the PC in pure DOS mode and enter in the path: C:\CKITA 3. Connect the CKIT with the PC using the LPT port. 4. Store the program C:\CKITA\CK24HR.BIN in the code memory of the MCU. 5. Execute the program: CK24HR. 6. The 24-Hr Clock should run. If the clock does not work, adjust the time delay of the program.

Design Analysis of the 24-Hr Clock System: A: Data Structure

BCD_TIME

BCD2CCConverter

SUR:BCD2CC

52

53

54

SEC

MIN

HRS3F

CC_TIME

3E

3D

3A

3B

3C

SEC

MIN

HRS

DisplaySUR:CCX7S

1002x : GM : 03-05

Figure-6.2: Data Structure for 24-Hr Clock System

24

Page 25: 89S52 Micro Controller Interfacing

Chapter-6.2 Development of Taximeter using PSPKIT

Mileage

Fare

Waiting Mn

Tk

Km

: Karighar R&D Centre : Bangladesh :

Mileage

Fare

Waiting Mn

Tk

Km

CARMET-35 : Karighar R&D Centre : Bangladesh HRDTOPAY

KTST

WTST

W-KERR

LEDTST

R STISP

Wheel GearBox

Sensor

Vehicle

Wheel

998X CKIT-06A Taximeter

Figure-62.1: Conceptual Design Layout of Taximeter using CKIT-06A

62.1 Introduction

The author has already designed, developed, built and field tested taximeters that are really commercially feasible. These taximeters are now on the road of getting in into the market. However, the author wishes that the interested readers should come forward with new ideas of building exciting taximeters having extraordinary features like remote telemetry, graphic display and etc. In the following pages, the author has presented the design concepts (both hardware and software) in modular forms. It is hoped that the readers would work on these modules and convert these into functional objects. At the end, all these modules could be integrated together to realize the ‘Actual Taximeter’. The University students may take it as theses/project works. And if so, the author may sometimes arrange a National Competition on ‘Design of Microcontroller Based Digital Taximeter’. 62.2 Brief Working Principles of CARMET

Karighar R&D Center has, for the 1st time in Bangladesh, successfully designed, developed and prototyped a ‘Metering System’ for the Taxi/CNG Cabs. The Company has given the name CARMET-35, which stands for ‘Metering System for the Taxi Cars developed in the month of May of the year 2003’. The design layout of the CARMET is depicted below in Fig-61.1.

The CARMET is built around an 8x51 Microcontroller and an 8279 peripheral controller (Fig-15.7). The 8x51 receives status signal of the ‘Wheel (running or not)’ through a sensor, which is attached with the gearbox of the engine. It then computes the ‘Distance’, ‘Fare’ and ‘Waiting Time/Speed’ according to some predefined algorithm. The 8279 drive the Digital Fare Meter (DFM), Digital Distance Meter (DDM) and the Digital Wait Meter (DWM). The DFM, DDM and DWM have been built using CC-type 7-segment display devices. The DDM is incremented by 10m and the corresponding increase in the DFM is (for the CNG auto):

a. Tk 1.00 for 200m distance traveled or b. Tk 1.00 for 2min waiting time or c. Tk 1.00 for the combination of distance and waiting making an equivalence of 200m.

25

Page 26: 89S52 Micro Controller Interfacing

Functions of the Front Panel Keys of the Taximeter

Front Panel Switch Full Name Function RST Reset To reset the meter LED TST Led Test To check that the 7Segment devices are OK HRD Hired To indicate that the Taxi is hired by a passenger. The fare field

shows the flat fare. The meter is enabled to acquire distance, compute fare and waiting time.

TO PAY Journey Ends Make Payment

When the taxi arrives at the final destination, the taxi driver pushes this switch to freeze all the activities of the meter. The meter shows: Total Fare, Distance and Waiting Time. This switch is only active when the taxi is in the waiting state.

K TST Examine K Factor of Meter

K-Factor is the value that corresponds to the number of pulses that the speed transducer generated when it was first installed in the taximeter and the taxi was moved over 1Km distance (We used Highway Km Posts).

W TST Determine W (Wheel) Factor of Taximeter

With the help of the W TST switch, an inspector can time to time check the functional condition of the: : Meter itself, : Speed pulse carrying cable, : Tire (Excessive Wear, Not Fully inflated) In this test, the taxi is moved over known 1km distance and the transducer counts are recorded.

W-K ERR Determine Error between W and K Factors

To certify that the meter and the wheel of the vehicle are in operational condition, then the difference between W- and K-Factors must be less than 1%. Pressing of the W-K Error switch would compute the percentage of errors and display it on the 7Segment display.

ISP In System Programming Port

The Vehicle Ordinances of GOB may time to time change the fare rates. ISP Programming Port allows updating new data within the meter without removing the MCU from the meter itself.

62.3 Taximeter Setup using CKIT-06A

26

B+

B-

12V

0V

M1

Voltage Regulator

+5V

0V

+5V0VSPulse

Vcc

GND

M3M2

Transducer `Pulse Shaper &Conditioning Logic

Generator

Q1/

Q2

T0/

INT0/

P11

M4

6MHz

Microcontroller : 89S51

M5

CS/

A

D

C

A

D

C

Display Controller : 8279

User Data p,...,a

M7

Fare Distance Waiting

SL3-SL0 Decoders: 2x138

1006: GM:02=2005

M8

Decoders:1x138

SL3-SL0

S2-SL0S0/ S1/ S2/

RL2

RL3

HRD

W

LED

PAY

K

W-K

M9

M10Keyboard

IRQINT1

DR : 2000HCR: 2100H

DP0-DP5 DP6-DP11 DP12-DP15

Figure-62.2: Taximeter Setup using PSPKI

Page 27: 89S52 Micro Controller Interfacing

62.4 Hardware Module-1 (Installing One-shot Circuit) Let us build the one-shot circuit of Fig-62.3 on the breadboard. It is left as an exercise for the readers to explore the working principles of the one-shot circuit.

27

Figure-62

R1=1K C1= 0. 1u

76

11 CLR/ 16 Vcc

109

GND

80V

+5V

K1

1/2U174LS 123

Q1 5

Q1/ 12

755x

C3 = 0.1uF

R2=3k3 C2= 1000uF

1514

3

21

GND

80V

+5V

2/2U174LS 123

Q2 13

Q2/ 4

A

B

C

CLR/

+

S peed Pulses D

E

T0(P34)

INT0(P32)

P1.1Waiting

IRQ

+5V

0V

R3: 1k5

R4 : 1k5INT1(P33)

Q1 : C828 F

G

Figure-62.3: One-shot Circuit to Condition the Speed Pulses

The One-shot Circuit: The package contains two pieces of identical re-triggerable one shots. The schematics and its connection with the transducer and microcontroller are shown in Fig-62.3.

The duration of the transducer signal ‘Sig’ varies with the speed of the vehicle (See Example-62.1). To produce the same type of pulse out-of-Sig, the ‘Sig’ information is pulse-shaped by passing it through the one shot circuit of 1/2Uz. The low-going edge of the signal ‘Sig’ of the transducer fires the one shot 1/2U1 to produce a pulse of duration 0.15mS (=150uS). The inverted output of 1/2U1 is labeled as ‘speed Pulses’ and is connected at T0(P34) terminal of the MCU Considering the CNG vehicle, the transducer produces about 1500-1700 pulses for 1km of distance traveled. As long as the wheel is turning, there is always presence of speed pulses at P34-pin. The MCU receives the pulses, calibrate these, computes the fare and then display the fare and the distance. The absence of the ‘Speed Pulses (let us call it WTP)’ indicates that the wheel of the vehicle is not turning and the MCU must recognize that the ‘Waiting Event’ has started. Is it always true? Not necessarily! It is true that there will be no speed pulse when the vehicle enters into ‘Waiting State.’ But the absence of the speed pulse does not necessarily mean that the wheel of the vehicle is not turning. There may be occasions when the vehicle is moving at a very slow speed, and in that case, the occurrence of the speed pulse would also be very rare. Therefore, the event of the absence of the speed pulse cannot be used as an input to the MCU for triggering the ‘Waiting Cycle.’ The valid waiting state of the vehicle is recognized in the following way: The speed pulse signals are converted to ‘High Level Logic’ by re-triggering the 2/2U1 one shot by the falling edge of every speed pulse. The width of the Q2 output is chosen in such a way so

Page 28: 89S52 Micro Controller Interfacing

that the 2/2U1 always receives re-triggering pulses even at the acceptable slowest speed of the vehicle (See Example-62.1). This arrangement allows the MCU to recognize the valid ‘waiting state’ or ‘wheel turning state’ by monitoring the ‘Logic State’ of the status signal at a the port pin, P1.1. If the vehicle moves at a speed, which is lower than the acceptable limit, there will be no speed pulse at the appropriate time to re-trigger the 2/2U1 one shot. As a result, the ‘status signal’ will drop to ‘Logic Low State’ and the MCU will be interrupted at the INT0 pin. The MCU will jump to the ISUBINT0 to start with the processing of the ‘waiting cycle’, which involves (i) counting of the current waiting time, (ii) fare calculation and updating by combining the current waiting time with the distance traveled (iii) monitoring the end of the waiting time.

Example-62.1: A CNG auto is moving at a speed of 10km/hour. The transducer of this particular CNG produces 1500 WTP for 1km of mileage. (a) Show that the CARMET will not trigger the waiting cycle. (b) Calculate the minimum speed of the CNG at which the waiting cycle of the meter will be activated.

Solution: (a) Average WTP/Sec = (1500x10) / (60x60) = 4 WTP pulses.

Assuming smooth speed of the CNG, the WTP separation time is = 0.25 S The 1st WTP will trigger the 2/2U1 to assert 1.5S pulse at Q2. The 2nd WTP will re-trigger the 2/2U1 at the elapse of only 0.25S of the 1st Q2 pulse. Because the 2/2U1 is a re-triggerable one shot, the Q2 will begin with a fresh 1.5S pulse. After 0.25S, the 3rd WTP arrives and so on… Thus, we see that the Q2 will never drop to low state and the MCU will never execute the waiting cycle.

(b) For the MCU to activate the waiting cycle, the Q2 of 2/2Uz must drop to low state. This requires a speed of the CNG, which fails to re-trigger the 2/2U1 at the appropriate time (before the expiry of the 1.5S width of the Q2). Therefore, the time separation between the two successive WTP pulses must be ≥ 1.5S. So, WTP/Sec = 0.7. Therefore the CNG speed is : (0.7x60x60) /1500 = 1.68km/hour.

62.5 Hardware Module-2 (Installing Speed Transducer) 1. Collect the speed transducer of Fig-62.4a and connect it with the one-shot circuit of the

breadboard as per following specifications:

Transducer Terminals One-shot Terminals Signal Names 1 (Red Color) A +5V 2 (Green Color) C 0V 3 (Black Color) B Signal (Speed Pulse)

28

760

(1)+5V-RED0V-GRN (2)Sig-BLK (3)

(4)Shield

(1)

(3)(2)

+5V

0VSig

(4)Shield

Bush Bearings

Hall

Effect

Devic

e

760

Spindle

Figure-62.4a: Pictorial View of Transducer Figure-62.4b: Equivalent Circuit of Transducer 2. Apply power to the one-shot circuit. Connect Ch-1 of an oscilloscope at terminal point D and

Ch-2 at terminal point E. Turn the spindle of the transducer and check that there appears pulses at D-point. Point-E remains at LH state.

3. Stop turning the spindle. There should not be any pulses at D-point and E-point should assume LL state.

The Transducer: It is a low-cost tachometer using hall-effect devices. The shaft of the transducer is mechanically coupled with the shaft of the gearbox of the engine of the vehicle. The constructional structure of the tachometer is shown in Fig-62.4b.

Page 29: 89S52 Micro Controller Interfacing

62.6 Software Module-1 (p626.asm) Task: Develop program codes, which when executed will produce the following result.

Display Field Name Value Comment DP0-DP5 Fare 00.00 Initial Fare DP6-DP11 Distance 0.00 Initial Distance DP12-DP15 Waiting 0.00 Initial Waiting

After the execution of p626.asm, let us press the HRD-key. We should see the following readout on the 7Segment display of the trainer.

Display Field Name Value Comment DP0-DP5 Fare 12.00 Initial Flat Fare DP6-DP11 Distance 0.00 Initial Distance DP12-DP15 Waiting 0.00 Initial Waiting

In the design of any instrument, we need to follow some guidelines, which are: (i) List the all possible input and output variables (ii) List the memory locations, which are used to store all these variables (iii) List the memory locations, which are being used as flags, counters and etc. (iv) And now draw a table of Fig-61.6 what we call ‘Data Structure’.

Let us take the help of Figure-A.7 (Apndix-A) and build the following data structure of Fig-62.5. 527a : GM : 10-05

18T1 19 SCWM

303132333435363738

3A39

3B3C3D3E3F

T2

STWM

STDM

STFM(CCode)

T3

5051525354555657

(CCode)

(CCode)

(BCD)SECMIN

SEC

MIN

DPFDPEDDDDDC

DP0DP1DP2DP3DP4DP5DP6DP7DP8DP9DPADPB

DPEDPF

1/1001/10

110

1001000

1/100

11/10

10100

1000

STWMBCD

STDMBCD

STFM(BCD)

ContentsLoc

DP0DP1DP2DP3DP4DP5DP6DP7DP8DP9DPADPBDPCDPD

SEC

1/10,1/100MIN

1, 101000,100

1000,1001, 10

1/10,1/100

Rem ark

SUR10

SCWM : Software Current Waiting Tim e MeterSTWM : Software Total Waiting Time MeterSTDM : Software Total Distance MeterSTFM : Software Total Fare MeterDFM : Digital Fare MeterDDM : Digital Dstance MeterDWM : Digital Waiting Time Meter

U19: 8279

DFM

DP0DP1DP2DP3DP4DP5

DDM

DWM

DP6DP7DP8DP9DPADPBDPCDPDDPEDPF

DFM

DDM

DWM

1 3 8 9

2 3 3

0 3 7

p-aDP0 DP5

DP6 DPB

DPC DPF

Table

SUR11

6D

6F BCDSPEEDT4

2F

2AT5 SPEED

CCcode

DP0

Fare (Tk)

Distance (Km)

Wait(Min)

Speed(Km/h)

LCD Panel

7Segment Display

29

Page 30: 89S52 Micro Controller Interfacing

30

Instruction Codes for P626.asm. L1: Initialize display unit for 16-digits and clear by calling SUR8 L2: Write 000000 to STFM of T3 of Fig-62.5 Write 000000 to STDM of Fig-62.5 Write 0000 to STDM of Fig-62.5 L3: Convert BCD code of T3 into CC-code and save them in T2 by calling SUR10 L4: Purge T2 to erase the unnecessary leading Os by calling SUR : DELLZS L5: Insert decimal points at the appropriate places of T3 by calling SUR : PLPOINTS L6: Transfer CC-code of T2 into DP0-DP15 via 8279 by calling SUR11 L7: NOP L8: keep polling the keyboard of the trainer If (pressed down key = HRD key) Goto L9 Else Goto L8 L9: Show: 12.00 at the Fare Field of the 7Segment display

0.00 at the distance filed 0.00 at the Waiting Field

L10: wait here

62.7 Software Module-2 (p627.asm) [Measuring Distances from the Speed Pulse] Add the following codes at the appropriate places of program p626.asm to bring the following affect in the functioning of the program P627.asm.

1. Let us press 17 pulses pressing the breadboard switch K1, the Distance Field of the 7Segment display would show: 0.01.

2. Apply another set of 17 pulses, the distance meter would show: 0.02 and so on…. 3. Apply very speedy pulses from using a high frequency oscillator. 4. Keep counting until the display shows: 9999.99. This would verify that the distance-

updating algorithm is working fine! …. …. L1: the input pulses are entering to Counter-0 of the MCU. Now let us configure the Counter-0

as follows: a. It will keep counting the speed pulses and at the end of counting 17 (11H) pulses, it will

produce a flag named TF0 in the TCON-register of the MCU. b. Keep polling the TF0-bit and when found set, update the distance by 10m. We have

learnt previously that a CNG auto produces about 1700 pulses for 1km distance. There fore. There will be 17 pulses for 10m of distance.

c. Keep measuring the distance, update it and display it. d. The above method of counting the speed pulses is known as ‘Polling Method’.

P627b.asm (Measuring Speed Pulse on Interrupt Method) L1: Configure Couter-0 for counting 17 pulses to rollovers from all 1s to 0s. L2: Enable Timer-0/Counter-0 interrupts logic L3: Write codes for ISURTFO (Timer-0/Counter-0 overflow Interrupt) to update the distance. L4: Continue …….

Page 31: 89S52 Micro Controller Interfacing

62.8 Software Module-3 (P628.asm) [Computing Distance Fare and Update] Add codes with program p627.asm so that we observe that: 1. Distance would keep updating by 10m. 2. There will be no change in fare for distance <2.00 km. 3. When the distance just arrives at 2.00 Km, the fare would be change to Tk 13.00. 4. After that for each distance of 200m, the fare would be updated by Tk 1.00. 5. The above program behavior is represented by the following flow chart of Fig-62.6.

Inerrupt Service Routine: ISURTF0

Increase Mileageby 0.01km

fb0=0

Mileage=2km?

Display:Mileage, Fare,Total Wait Tim e

Return to MLP

Y

N1 ---> fb0

Y

Increase Fare

NMileage >=2km

1SL1:

1SL2:

1SL3:

1SL4:

1SL5:

1SL6:

1SL7:

526a : GM; 10-05

Increase Fare

address of fb0 = 00H

Figure-62.6: Simplified Flow Chart for Updating Fare

Let us convert the flow chart of Fig-62.6 into program instructions and then execute. ISL2: if (fbo = 0) Goto ISL3 Else Goto ISL6 ISL3: if ((53H) = 02) Goto ISL7 Else Goto ISL4 ISL4: Take action as indicated in flow chart ISL5: Return to MLP ISL6: See program Codes in the CD Isl7 See program codes in the CD.

31

Page 32: 89S52 Micro Controller Interfacing

62.9 Software Module-4 (P629.asm) [Computing Waiting Time, Waiting Time Fare and Update]

Add codes with program p628.asm so that we observe that: 1. Distance would keep updating by 10m. 2. There will be no change for distance <2.00 km. 3. When the distance arrives at about 1.70 Km, disconnect the speed pulse source. 4. The taximeter should immediately enter into ‘Waiting State’. The ‘Waiting Time Field’

would start counting the ‘Waiting Time’ by 1-sec. 5. Keep waiting until the Waiting Time is 2-Min (Waiting Time Field shows: 2.00). Let us

observe that at the elapse of 2-Min waiting time, the fare is update4d by Tk 1.00. The additional Tk 1.00 comes from the fare of 120-sec (2-Min) waiting time @ Ps 50/1-Min.

6. Wait for another 1Min and observe that the fare is updated by Tk 1.00. 7. The readers may notice that Ps 50 is updating the fare for a waiting time of 2-Min. The 2-Min

waiting time is not a random or convenient choice rather it is a design criterion, which must be respected to see that the fare for 2-Min waiting time is exactly equal to the fare for 200m. In Software Module-5, we shall see the combined affect of ‘Distance Drive’ and ‘Waiting Time Drive’ to update the fare by Tk 1.00.

Instruction Codes: L1: add codes to initialize Timer-1 of the MCU to set the TF1-flag bit of TCON register at the end of every

approximately 8.9mS. Why 8.9mS? [

OSC 1/12 f1=921.6KHz

Y1C1-T1/=0

SW2SW1 TL1

(5-bit)TH1

(8-bit)

18 XT2

19 XT1

11.0592MHz

13-BitMode-0

TF1

534c

8DH8BH

Figure-62.7: Mode-0 Operation of Timer-1 to Generate 8.9mS Time Quanta

f1 = 921.6 x 1000 = 921600 Hz. Time duration for each pulse = 1/921600 Sec. Timer-1 will count 213 = 8192 pulses to rollover from all 1s to 0s. Each time, there is a rollover, the TF1-flag bit set. Therefore, time difference between two Tf1-flag bits = 8192x(1/921600) Sec = 0.0088Sec = 8.9mS. ]

L2: if (p1.1-bit = LL) Goto Waiting State at L3 Else Goto L2 L3: Start Timer-1 L4: (if time counter = 70H) ; 70H = 112 x 8.9 = 996.8mS ≈ 1Sec Goto L5 Else Goto L4 L5: Update Waiting Time by 1Sec ; Reset Time Counter Goto L4

32

Page 33: 89S52 Micro Controller Interfacing

33

62.10 Software Module-5 (p6210.asm) [Computing Fare for Combined Affect of Distance and Waiting Time]

1. - 2. Bring necessary changes in the mainline program so that the meter behaves as follows:

a. Advance the distance by 1.87 Km and then disconnect the speed pulses. b. The meter has entered into waiting state. Can you say after how long of waiting time,

the fare would be updated by Tk 1.00. This is to say that the Fare Field would show: Tk 13.00? Ans: 1Min 18Sec. How?

Current Distance covered : 1.870 Km Remaining distance to cover 2km : 0.130 Km = 130m = 13x10m 200m distance charges : Ps 100 10m distance would charge : Ps 5 130m distance would charge : Ps 65 = 13x Ps 5

60 (6x10)-Sec waiting time charges : Ps 50 6-Sec waiting time would charge : Ps 5

Therefore, the equivalent waiting time for the uncovered 130m is: 13x6 = 78-Sec = 1Min 18Sec.

c. Keep waiting for 1Min 18Sec and we would observe that Tk 1.00 augments the fare filed. This is one of the design criterions that must be implemented while designing a digital taximeter. Nm

62.11 Software Module-6 (p6211.asm)

[Computing Fare for Combined Affect of Waiting Time and Distance] 1. - 2. Bring necessary changes in the mainline program so that the meter behaves as follows:

c. Advance the distance by 1.230 Km and then disconnect the speed pulses. d. The meter has entered into waiting state. Wait for 54Sec and then connect the speed

pulse source. Can you say after how much of distance coverage, the fare would be updated by Tk 1.00. Tk 13.00? Ans: 680m. How?

Current Distance covered : 1.230 Km Remaining distance to cover 2km : 770m

Elapsed waiting time : 54Sec = 9x6 Sec Elapsed waiting time would charge : Ps 45

Ps 5 is charged for : 10m Ps 45 would be charged for : 90m Therefore, the additional distance that has to be covered to make the fare field showing Tk 13.00 is : 770m – 90m = 680m!

62.12 Software Module-7 (p6212.asm) [Speed (Km/hr) Computation and Display]

1. - 2. Now bring necessary change in the mainline program so that it behaves as follows:

a. The DP12-DP15 devices of the display unit are configured to work as Speed Meter with readout as: xxx.x Km/Hr.

Page 34: 89S52 Micro Controller Interfacing

34

a. Apply speed pulses by pressing the switch K1, which is located on the breadboard. We would observe that the DP12-DP15 of the display unit shows some value of the speed in Km/Hr. Keep pressing K1 at a higher rate; we will see that the value of the speed is also changing.

b. Apply speed pulses from J10-12 connector, we see that the speed is about: 202.5 Km/Hr. c. Apply speed pulses from J10-13. The speed is about: 405.0 Km/Hr.

Instruction Codes: L1: Timer-1 should have been initialized in mainline program to generate 8.9mS time quanta.

This time could be used to divide the 10m distances to compute the speed. L2: if (TF0 is set meaning the taxi has covered 10m distance) Compute Speed and display at DP12-DP15 as: xxx.x Km/hr L3: Continue computing distance and display it

Speed Measurement Principles: Let us assume that Timer-1 is configured to generate 8.9mS time quanta. To measure speed (Km/Hr), we need to know about time that has elapsed while the taxi traveled 10m distances. Let us also assume that the Timer-1 has generated ‘n number of Time Ticks (TT)’ during 10m distances. One TT corresponds to 8.9mS time. Now, we may write as:

Speed = 10m/(nxTT) [ 1 TT = (1/f1) x 8192 Sec Refer Fig-61.8 f1 = Y1/12 = 11.0592/12 ] Speed = [[(10/1000) ]x3600 ] / (n xTT) Km/hr = (1/n) x (11059200 x10x3600)/(1000 x 12 x 8192) Km/hr = (10/n) x 405 Km/Hr = 405/(n/10) Km/hr = (405x10)/(n/10) xxx.x.x Km/hr

; Multiply by 10 would allow displaying the speed as having one digit after the decimal point.

= 4050/(n/10) = 0FD2H/(n/0AH) ; xxx.x Km/hr

Implementation: 1. Make arrangement in the program codes to count ‘n’ 2. Compute n/0AH by calling division SUR13 at 1A00H and SUR14 at 1AA0H.. 3. Compute 0FD2H/(n/0A) by calling SUR13and SUR14. 4. Convert the Binary Speed into BCD by calling SUR15 at: 1B9BH. 5. Display the BCD speed on DP12-DP15 as: xxx.x Km/hr.

Now bring necessary change in the program P6212.asm so that DP12-DP15 shows 0.0 Km/hr when the speed pulses are not injected. 62.13 Software Module-8 (p6213.asm)

[Combined Computation and Display of Speed and Waiting Time] 1. - 2. Now bring necessary change in the mainline so that the meter behaves as follows:

a. The DP12-DP15 devices of the display unit are configured to work as Speed Meter with readout as: xxx.x Km/Hr.

Page 35: 89S52 Micro Controller Interfacing

35

d. Apply speed pulses by pressing the switch K1, located on the breadboard, we would observe that the DP12-DP15 of the display unit shows some value of the speed in Km/Hr. Keep pressing K1 at a higher rate, we see that the value of the speed is changing.

e. Apply speed pulses from J10-12 connector, we see that the speed is about: 202.5 Km/Hr. f. Apply speed pulses from J10-13. The speed is about: 405.0 Km/Hr. g. Disconnect the speed pulse source. Now, the meter should enter into waiting state and

counts seconds. The waiting time would appear on DP12-DP15 in the form of: xx.xx. h. Reconnect the speed pulse source. Now the meter should show speed (Km/hr) on DP12-

DP15 positions.

Instruction Codes: L1: if (speed pulse missing) Goto L3 L2: Update distance Compute speed and display at DP12-DP15 L3: Update waiting time and display at DP12-DP15 L4: Goto L1

62.14 Software Module-9 (p6214.asm) [Complete Taximeter based on 89S51 and 8279]

Combine together all the software modules/routines so far developed in order to transform the CKIT-06A into a Digital taximeter. The taximeter must comply with the specifications of the following table.

Drive Result Remarks Only Distance Drive. Waiting OFF Fare: 13.00 at 2.00 Km

Fare: 14.00 at 2.20 Km Fare: 15.00 at 2.40 Km …………………….

DP12-DP15 Shows speed (Km/hr)

Only Waiting Drive. Distance Off Fare: 13.00 at 0.00 Km. Waiting :20.00 Fare: 14.00 at 0.00 Km. Waiting: 22.00 Fare: 15.00 at 0.00 Km. Waiting: 23.00

DP12-DP15 Shows waiting Time

Distance and Waiting Combined - Get waiting: 0.55, distance: 1.91 Km - Get waiting 0.55, distance: 2.11 Km - Get waiting: 0.55, distance: 2.31 Km

Fare: 13.00 Fare: 14.00 Fare: 15.00

Calculation: 55Sec/6=9 ≅ 9x10 = 90m 02.00 – 00.09 = 01.91m

Waiting and Distance Combined -Get distance: 1.75 Km. Waiting: 2.30 -get distance:1.75, waiting: 4.30

Fare: 13.00 Fare: 14.00

Calculation; 2.00- 1.75=0.25 km 250/10=25=25x6=150Sec 150Sec ≅ 2.30 sec

Distance and Waiting Combined -Get distance 2.25 Km, waiting 0.00 -Get distance 2.25 Km, waiting: 1.30 -get distance 2.25Km, waiting:n3.30

Fare: 14.00 Fare: 15.00 Fare: 16.00

Calculation: 200-50 =150/10=15≅90Sec ≅ 150m

Waiting and Distance Combined -get dist: 2.27 - get wait: 0.18 - get dist: 2.37 -Get distance: 2.57 -get dist: 2.57, wait: 2.18

Fare: 14.00 Fare: 14.00 Fare: 15.00 Fare: 16.00 Fare: 17.00

Calculation: 18sec/6≅3x10=30m 200m-70m=130m 130m-30m = 100m 270m+100m = 370m

Page 36: 89S52 Micro Controller Interfacing

62.15 Single Chip Digital Taximeter Given below a conceptual block diagram for a single chip taximeter. This meter would offer the following advantages over the taximeter of Fig-62.2.

1. Lower IC counts. 2. Good visibility of the display unit during daylight. 3. Almost no cross talk with the electrical firing system of the engine. 4. The meter immediately enters into waiting state after pressing the Hired switch.

B+ 12V

0V

M1Voltage Regulator

+5V

0V

Vcc

GND

M5

M2

Transducer

Q1/

Q2

T0/

INT0/

P11

M6

11.0592MHz

Microcontroller : 89S51

User Data p,...,a

M9

Fare Distance/Waiting

1008: GM:02=2005

M7PowerDriver

P0

P1

P2

2003 2003

2804

+9V

M8

0VP35

+5

HIRED /PAY

SW1

+9V

+12

0V

Power Conditioner

+10VCOMSPulse

P30

Vcc

+5V Switch

0V

SP

Signal Conditioner Pulse Shaper

M3 M4

M10

Figure-62.8: Single Chip Version of the Digital Taximeter Functional Descriptions: 1. M1 provides immunity to logic power supply during the electric firing of the engine. 2. M3 minimizes the cross talks between the signal lines and the engine firings. 3. M4 allows the MCU to decide if the car is moving or not. 4. M10 allows the meter to enter into waiting state at the activation of the ‘Hired’ switch. 5. M5 is a voltage regulator to provide +5V for the logic and +9V for power buffers. 6. M7, M8 are the power buffers and provide good visibility for the 7-segment display devices

in the daylight. 7. M9 is the 7-segment type display unit. 8. M2 is an electronic transducer that picks up the speed pulses while the car moves.

36

Page 37: 89S52 Micro Controller Interfacing

6.3 Development of Digital Weighing Machine using CKIT-06A 6.3.1 Hypothetical Pictorial View of the 8951-Based Digital Weighing Machine

Heartbeat

Power Sw

Keyboard

RST9 9. 9 9 Tk

Kg

TkCost

Weight

Rate/Kg

Display

1 9. 9 9 0

1 9 9 8. 8 0

231

RTEBKS

1 324 5 67 8 9

0

PanWeight

Figure-63.1: Hypothetical Pictorial View of 8951 Based Digital Weighing Machine (DWM) using CKIT-06A

The goods are placed on the pan. The weight of the goods is displayed on the ‘Weight Filed’ of the display unit. The display unit is made using –segment display devices.

The rate (Tk/Kg) of the goods is entered from an interactive keyboard, which is attached with the front panel of the DWM. The rate is displayed on the ‘Rate Field’ of the display unit. The cost of the goods is displayed on the ‘Cost Field’ of the display unit.

The DWM continuously – i. Reads the weight ii. Reads the rate iii. Computes the cost iv. Displays the Weight, Rate and Cost. v. Blinks the Heartbeat LED ON/OFF to indicate that the DWM is active.

The rate is acquired on interrupt basis. The DWM remains in rate acquisition mode until the 4-digit entry for the rate is complete. During rate acquisition period the weight and cost are not updated. At the end of rate entry, the DWM updates the weight, the rate and the cost.

6.3.2 Conceptual Hardware Block Diagram of DWM Based on CKIT-06A

Rate fromKe yboard

Digital DisplayTotal Cost

Weight

Rate/Kg

Tk

Kg

Tk

Weight

LoadCell

AMPADC

8051 MCU

Hard + Soft Wares

M1 M2 M3

M4

M5

901/2005

Figure – 63.2: Conceptual Hardware Block Diagram of DWM

37

Page 38: 89S52 Micro Controller Interfacing

6.3.3 Design Level Hardware Block Diagram of DWM Based on CKIT-06A

EPROM

RAM

KDICKe yboard

We ight

LCICLoadce ll

U17U19

8279

4433

MCU

8051

U2 U19

8279

2764

6264

DataData

Data

Data

Data

U10

U8DP12

-DP15

DP6-

DP10

DP0-

DP5

DDIC Total Cost in Tk

We ight in Kg

Rate /Kg in Tk

S canCodes

Analog

ScanCodes

BCD

Display Data

Digital Display

Data

Data

901/2005

IRQRateData

1 324 5 67 8 9

0 RSRTEBKS CC-

Codes

CC-Codes

Figure- 63.3: Design Level Hardware Block Diagram of DWM Based on CKIT-06A 6.3.4 Load Cell Details

Output 20mV / 20Kg. Wt.

+ Excitation (Green)

- Excitation (Black)

+ Sense (Blue)

- Sense (Yello)

Load Cell

Weight

+ Output (Red)

+ Output (White)

+ -

GM: loadcellcircuit323: 15/12/01

-5V

+5V

Figure-63.4: Load Cell

There are three single point load cells primarily intended for use in weighing scales although they may be used in many other applications. The force applied to the load face of the cell causes a deflection of the body, which is measured by the bridge of four strain gauges. A screened lead, 6-wire system is used or best accuracy. When used for weighing scales the load cell can be filleted with a platform up to the maximum size. The output of the cell is independent of the point through which the load acts. [Courtesy: RS Catalog] 38

Page 39: 89S52 Micro Controller Interfacing

Sl. No.

Capacity RS Stock No.

Technical Specifications Instrument Amplifier RS Stock No

1 2 Kg 632-736 2 20 kg 632-742 3 100 kg 645-805

a. Total Error : 0.0025% of the rated load b. Recommended Excitation : ± 5V (10V) c. Output at Rated Load : 2 mV/V ± 10% d. Zero Return : 0.025% of the Rated Load e. Zero Balance : ± 5% of Rated Load f. Safe Overload : 150% of the Rated Load g. Ultimate Overload (for failure) : 300% of the Rated Load h. Insulation : >106 ohm i. Output Impedance : 350 ± 3 ohm

308-815 302-463

Figure-63.5: Technical Specifications of Load Cells [Courtesy: RS Catalog]

6.3.5 Instrumentation Amplifier

39

3

2

6

5

1

7

2

3

1

0V

-

+

+

+-

-

R1 10kR2 10k

R3 5k

R4 10k

R5 10k

R6 10k

R7 10k

1/1 U1LM358

1/2 U1

1/1 U2LM358

+12V

-12V

8

4

Gain = 100(V2 - V1) / ( (R1+R2+R3) ) /R3)(R7/R6)

- V1

+V2

323b

Figure-63.6: Discrete Instrumentation Amplifier 20k

12

3

+

-

4 GSS1

11 GSS2

+ V2

- V1

8

A10

9A20

6

OF1

GST1

5

7

OF2

10

GST2

+12V

-12V

2

13

0V

14-V

+VCOM

N/C

N/C

1 (OUT)

R1: 20k

323

R2: 20k

INA101

Figure-63.7: IC Instrumentation Amplifier

Page 40: 89S52 Micro Controller Interfacing

6.3.6 Connection Diagram of ADC MC14433 with 89S51 MCU of CKIT-06A

DS1DS2DS3DS4

Q0Q1Q2

19181716

202122

GND13

0V

+H

L

Input0-2V

C310.1uF

C3447uF

+5V

Vref2

R85k

C33 0.1uF

24Vcc

4433

GM:648a: 06-04:10-05

Vx

Vag

Vee

-5V

CLK1

CLK2

10

11R12300k

R11 470k

3

1

456

R1R1/C1C1

(Mylar)

Q3 23

78

CO1C02

C32 0.1uF(Offset)

EOCDU

OR/

14915

U17

0V

0VC17 0.1uF

C37 0.1uF12

External5k Poton theBreadboardRx

U2:8051

J81235

1234

WQ3

WQ1WQ0

DS1

WQ2

DS2DS3DS4

J8-9

J8-10

U19:8279 DP6DP7

DP8DP9

LCD Panel

P17P16P15

J2-

P14

P13P12P11P10

when Vx > Vref

DS1DS2

DS3DS4

0V

+5V

Figure-63.8: Connection Diagram of ADC MC14433 with 8951 Microcontroller

6.3.7 Single Chip Design of DWM using 89S51 Microcontroller

P17-P10

P07 - P00

P23-P20

P27 - P24

P32 - P30

Load Cell Amplifier ADC Microcontroller 4 to16 MUX

PWR BUF

PWR BUF

PWR BUFDisplay

DP0-DP7

DisplayDP8 - DP15

Keyboard

Weight

ROMRAM

GM:648a: 06-04:10-05

Figure-63.9: Single Chip Design of Digital Weighing Machine

40

Page 41: 89S52 Micro Controller Interfacing

6.3.8 Computation Flow Chart for the 89S51 Based Digital weighing Machine

Binary-to-BCD

Converter

BCD-to-CCcode

Converter

16-bit x 16-bitBinary

Multiplication

BCD-to-B inaryConverter

BCD-to-B inaryConverter

BCD-to-CCcode

Converter

BCD-to-CCcode

Converter

ScanCode -to-BCD

Converter

Unpacked BCDto

BCD Converter

14

320

6587

RENBKS

9

RES

Walking0's

Keyboard8279-Based

LoadCe ll

20mV/20Kg

Amplifie r,ADC andInterface

Controller101A,

MC14433Port-1

Software ModuleInstruction Codes

Software ModuleSubroutine

Software ModuleInstruction Codes

Software ModuleSUR: 7

Software ModuleSubroutine

Software ModuleSubroutine

Software ModuleSubroutine

Software ModuleSubroutine

Hardware ModuleKeyPad

Hardware Module Hardware ModuleDP6 - DP10

Hardware ModuleDP0 - DP5

Hardware ModuleDP12 - DP15

Software ModuleSubroutine

Goods

A

4-Bytes Scan Codes(stream) for Rate Data00.00-99.99 Tk/Kg.Example : 99.99 Tk/Kg.13H,13H, 13H, 13H

B

4-Bytes Unpacked BCDfor Weight Data : 00.00-19.99 Kg.Example : 19.99 Kg.1000, 1001, 1001, 1001

C

C

2-Bytes Packed-BCD19 99 D

D

Each 16-Bits Binary

E

FE

E

4-Bytes Common Cathode Codes06, 6F, 6F, 6F

4-Bytes CommonCathode Codes6F, 6F, 6F, 6F

G

Cost:32-Bits Binary

Cost:4-Bytes Packed-BCD

Cost:Rounded to6-Bytes CommonCathode Codes

Weight in Kg.

Cost in Tk

Rate in Tk/Kg.

GM:dwmflow:1201 : 902 , 01/2005

07CFH

270FH

0130FE21H19988001

2-Bytes Packed-BCD99 99

CCcodeTransferre d to

Display

Software ModuleSubroutine

CCcodeTransferre d to

Display

Software ModuleSubroutine

CCxfer toDisplay

Software ModuleSubroutine

Figure-63.10: Computation Flow Chart for Digital weighing Machine 41

Page 42: 89S52 Micro Controller Interfacing

6.3.9 Acquisition/Computation Flow Chart of Digital Weighing Machine

Compute BINCOST = BINRATE x BINWT

Initialization:Stack, Bank, Port-1 as Input,Vector Link INT0

Initialization:Initial Rate 00.00, Initial Weight 00.000

8279 Init., Flag Bit Init, Interrupt Init

Obtain Packed BCD Wt from UpBCD Weightand Save at: T5C of Data Structure

Convert BCDRATE and BCDWT intoBINRATE and BINWT

Compute BCDCOSTand saving at T5A of Data Structure

Convert BCDRATE, BCDWT andBCDCOST into CCcoded Data and save at T3of Data Structure. Place Decimal Points at T3

Remove Leading Zeros of Cost. TransferCCdata of T3 into Display RAM of 8279

1. Show: _ _ . _ _ at the RateField with a Blinking Cursor2. Set fb1 to LH3. Init Cursor Position, Digits toPrint4. Enable Interrupt

ISURINT0:L1: C100H

L 2:

L3 :

L4:

L14:

L15:

SUR

BCD2BIN

L17:

L18:

L20:

L22:

L23:

SUR

BIN2BCD

SUR

BCD2CC

SUR

CCX8279

294

The Product rate (Tk/Kg.) is acquired from the keyboard on interrupt basis in is shown here to the right of Main Line Program. The interrupt ocurs at INT0pin of the MCU. The vector direction is: KeyPress ---> INT0 -----> 0013H ---> C0F0H ----> ISURINT0. The mail line program is located at C1000H. TheMPU reads the rate, the weight and then computes the total cost using EPROM resident subroutins.

Wt >20Kg?

N

Y

Blank WtFieldL5:

Acquire UpBCD Wt: DS1, DS2, DS3, DS4and save at: 58H, 59H, 5AH, 5BH

SUR

BMULT

HearBeat LED ON/OFF via P3.4 Port Pin SURTDELAY

Main Line ProgramInterrupt Subroutine toAcquire Rate (Tk/Kg)

fb1= LH?

Make Arificial RETI and Makesthe ISURINT0 as an Artificial

Main Line ProgramL24:

N L25:

REN Cmnd?

Y

L23A;

Cursor ON/OFF

N

EnableInterrupt

L25A:

Y

REN Key?

N L26A:

Print Rate DigitsSave Scan Code

Print Done?

N

Y

AdjustPrint Position

YL28:

1. Obtain BCDRATE from SCANRATE2. Save at T5C

1. Adjust Cursor Pos2. Adjust Digits Print3. Enable Interrupt

Figure-63.11 : Data Acquisition/Computation Flow Chart for the Digital Weighing Machine

42

Page 43: 89S52 Micro Controller Interfacing

Chapter - 6.4 Remote Controlled Speed Regulation System

POS1 25mS

POS2

POS3

100Hz ZCD

D7-D0P17-P10WR/P32

AC

CS0/

POS4 25mS

POS5

POS6

D7-D0WR/

CS1/

POS7 25mS

POS8

POS9

D7-D0WR/

CS2/

POS16 250mS

POS17

POS18

D7-D0WR/

CS5/

POS10 25mS

POS11

POS12

D7-D0WR/

CS3/

POS13 25mS

POS14

POS15

D7-D0WR/

CS4/

PA5-PA0PB5-PB0PC5-PC0

PA0PA1

PA2

PA3

PA4

PA5

PB0PB1

PB2

PB3

PB4

PB5

PC0

PC1PC2

PC3PC4

PC5

WR/D7-D0

CS6/

CS7/-CS0/CS/

220VAC N

TR1

TR2

TR3

TR4

TR5

TR6

TR7

TR8

TR9

TR10

TR11

TR12

TR13

TR14

TR15

TR16

TR17

TR18

PIT8253

Microcontroller Based Remote Controlled Multichannel Speed Regulation System

2KEEPROM

64 ByteRAM

64 ByteSFR

Microcontrollerxx51

P36 P35P37

RxD-P30

RemoteCommand andControl Signals

PDEC74138

MBSD\REMOTE1: GM:01-03

25mS

50mS

25mS

25mS

25mS

25mS

25mS

25mS

25mS

25mS

25mS

25mS

RemoteMonitoring Signals

TxD-P31

P34-P33 A

A

A

A

A

AABC

PIO8255

PIT8253

PIT8253

PIT8253

PIT8253

PIT8253

ZCD

A

MCUAT89C0251

LL

L

LL

L

LL

L

LL

L

LL

L

LL

L

Figure-64.1: Remote Controlled Speed Regulation System

43

Page 44: 89S52 Micro Controller Interfacing

Appendix-A Architecture of ISP-Mode CISC Microcontroller What is a Microcontroller?

The answer is that it is a ‘Single Chip Based Controller’ and hence the name ‘Microcontrolling Unit (MCU) ‘ or simply a ‘Microcontroller’. The 89S51 chip of the Atmel Company presents itself as an example of a single chip Microcontroller This chip internally contains in partial amount the functions of the following hardware resources. a. An 8-bit CPU with an 8-bit Accumulator (A-register) A 1-bit CPU called ‘Boolean Processor’ with a 1-bit Accumulator, the Carry.

b. 4 Kbytes EEPROM with Security Bit, which when Fused prevents a user from reading the memory content

c. 128 Bytes RAM d. 2-Channel Timing/Counting Functions e. 2-Channel External Hardware Interrupts with Built-in Interrupt Priority Resolver. f. Full Duplex Serial IO Controller

g. 28 (max: 32) IO Lines, which can be individually programmed as input or output. h. ISP (In System Programming) Programming Capability using LPT port of IBMPC.

The conceptual view of the 89S51 microcontroller is shown in Fig-A.1.

697

4K EEPROM

128 ByesRAM

Tim in/Counting

T-C1/T-C0/

INT0/INT1/ Interrupt

Resolver

8-Bit CPU

TxDRxD

Serial IO

PIO

P07-P00

P17-P10

P27-P20

P37-P30

P0

P1

P2

P3

Figure-A.1: Conceptual View of the 89S51 Microcontroller

The Atmel Microcontrollers The Atmel Company of the USA has launched a series of microcontroller chips, which offer very cost effective solutions to many industrial control applications. These chips have borrowed the ‘Instruction Set’ and the CPU from the 8051 of the Intel Company. It is also fully compatible with the Intel 8-Bit peripheral controllers. The following are the commonly used MCUs of the Atmel Company:

a. 892051 : A 20-pin version with 1 Kbytes EEPROM for program codes. b. 89S51 : 4K Code Memory with In-System Programming (ISP) capability. . c. 89S52 : 8K Code Memory but ISP capability.

44 d. 89S8252 : 8K Code Memory, 2K Data Memory, ISP capability and SPI Port.

Page 45: 89S52 Micro Controller Interfacing

89S51 Architecture

18

32,3334,35,36,37,38,3940

9

19

20

8,7,6,5,4,3,2,1

RST

XTAL2 XTAL1

GNDP07-P00

P17-P10

P27-P20

P37-P30

Vcc

698Y = 6MHz

EA/-(Vpp)31

28,27,26,25,24,23,22,21

17,16,15,14,13,12,11,10ALEPSEN/NC

3029

ExernalPort LinesOperated

IODevices

67

10

12

141516

18

2122

25

4012345

89

11

13

17

1920

2324

2627282930313233343536373839

P10P11P12P13P14P15/MOSIP16/MISOP17/SCKRSTP30-RxDP31-TxDP32-INT0/P33-IN1/P34-TC0/P35-TC1/P36-WR/P37-RD/XTAL2XTAL1GND P20-A8

P21-A9P22-A10P23-A11P24-A12P25-A13P26-A14P27-A15

PSEN/ALE-PG/EA/-VppP07-AD7P06-AD6P05-AD5P04-AD4

P01-AD1P00-AD0

Vcc

89S51 698

P03-AD3P02-AD2

Figure-A.2: Physical Pin Diagram Figure-A.3: Port Lines Operated 8951 Based System

18

32,3334,35,36,37,38,3940

9

19

20

8,7,6,5,4,3,2,1

RST

XTAL2

XTAL1

GNDAD7-AD0

A15-A8

P17-P10

Vcc

Y = 6MHz

EA/31+5V

28,27,26,25,24,23,22,21

699

AD7-AD0

ALE 30 ALE

PSEN/ 29

A15-A8

PSEN/

WR/RD/

1617

WR/RD/

RxDTxD

1011RxD

TxD

TC0/TC1/

INT0/INT1/

1213

1415

ExternalModem

ExternalPort

External BusOperated ROM, RAMand Port

Data/ControlP1

21SFRs128 ByteRAM

4kROM

Figure-A.4: Bus Lines Operated 89C51 Based System Accessing External ROM, RAM and IO Devices

Fig-A.3 is an application of the 89S51, in which all the internal four ports consisting of 32 (4x8) IO lines have been used as simple IO lines to accomplish data transfer operations with the peripheral devices.

Fig-A.4 depicts a system in which the MCU is accessing external ‘Program Memory’, ‘Data Memory’, ‘Memory-mapped Port’ and also supports the functions of ‘Interrupt’, ‘Counting’, ‘Serial IO’ and ‘Parallel IO’.

45

Page 46: 89S52 Micro Controller Interfacing

Internal Architecture of 89S8252 CISC Microcontroller

divide by12

11.0

592

MH

z 8-BItCPU

8-BITAccumulator (A)

M1 M2

M5

1-BItCPU

1-BITAccumulator (C)

M3

M6

In Sys.Prog. Interface

SPIInterface

M4

M7

SerialCom munication

Timer/Counter

M8

M12Rx, Tx

TC0-2

CodeEEPROM(FLASH)

0000 - 1FFF

DataEEPROM

0000 - 7FFF Lower Block ofRAM (00-7F)

SFR (80-FF)Upp RAM (80-FF)

for 89X52 only

Exter. Interrupt

M13INT0-1

M9 M10 M11

M13

M16

M14 M15

M17

Port-0

Port-1

Control Matrix(Sequence Generator)

P00-P07

P10-P17

Addree BusInterface A00-A15

Data Buffer D0-D7

M18

M20

Port-2 Write ControlLogic

Port-3

P20-P27

P30-P37

M19

WR/

M21

Read ControlLogic

RD/PSEN/

SS/, SCKMOSI,MISO

SCKDataOut,DataIn

783 : GM : 1-11-05

Security Bit Security Bit

Figure-A.5: Internal Architecture of 89S8252 CISC Microcontroller

46

Page 47: 89S52 Micro Controller Interfacing

Internal Details of 89S8252 Microcontroller

P37

RD/

P36

WR/

P35

C1/

P34

C0/

P33

INT1/

P32

INT0/

P31

TxD

P30

RxD

10

11

12

13

14

15

16

17

Port-3Latches

ProcessingModule-1

D0D1D2D3D4D5D6D7

A0A1A2A3A4A5A6A7

P00P01P02P03P04P05P06P07

Port-0Latches

(8)

A8A9

A10A11A12A13A14A15

P20P21P22P23P24P25P26P27

Port-2Latches

(8)

Port-1Latches

P10P11

P17

P12P13P14P15P17

(8)

ProcessingModule-2

0000

1FFF

80P0*

SP

DP0L

DP0H

-

81

83

TCON

PCON*

TMOD

TL0

TL1

TH0

TH1

-

P1*

-

SCON*

SBUF

-

P2*

-

IE*

-

P3*

-IP*

PSW*

-

ACC*

B*

-

82

87

88

89

8A

8B

8C

8D

90

98

91

A8

A0

B0

B8

D0

E0

F0

FF

RegisterSet

InternalRAM

80

7F

2F

20

BitSpace

BK3

BK2

BK1

BK000

08

10

181F

17

0F

07

Internal8K Flash

PC = 16-Bit A15-A00

MOVX @R1 (R0), A: R1 or R0 = 8-Bit

MOVX @DPTR, A: DPTR = 16-Bit A15-A00

A7-A0A15-A08 by P2

cngmet\ch8\8-6: 784

29

30

PSEN/

ALE

Registers marked by * areBit Addressable

ISP and SPIInterface

xx52

C8

CDT2Tim er-2

Internal2K EEPROM

0000

07FF

DP1LDP1H

8485

47Figure-A.6: Internal Details of 89S8252 CISC Microcontroller

Page 48: 89S52 Micro Controller Interfacing

Recommended Data Structure for Atmel CISC Microcontrollers

48

00 Bank-007

0F08

10

20

17

FlagTable

3F40

761

Bank-1

Bank-2181F Bank-3

27

2F

000107081018

1F171F

2027282F3037

787F30 DPF

DP0

CC Code for Digit of DP0CC Code for Digit of DP1CC Code for Digit of DP2CC Code for Digit of DP3

CC Code for Digit of DPF

4F DP0

DPFUnpacked Hex of format: 0X

Unpacked Hex of format: 0X50

57

Packed Hex of format: XX

DP0DP1

DPEDPF

Packed Hex of format: XX586B6C6D6E6F70

FF

CRP (Curs Pos)/ PRP (Print Pos)

SP

NPD (Digits Printd left of CursCTP (Cursor Type)

Key Sccan Code

384048505860

7068

3F474F575F676F77

0203040506

797A7B7C7D7E

Internal RAM Internal SFRs of the MCU

80

FF

818283

P0

TCON

SPDP0LDP0H

PCON

84

TH0

NOT ACCESSIBLE8D

TMOD

TH1

89

8788

TL1TL08A

8B8C

8E - 8FP190

NOT ACCESSIBLE91 - 97

SCON98SBUF99

F1

NOT ACCESSIBLE

F0

E1E0

B

NOT ACCESSIBLE

ACC

EF

NOT ACCESSIBLE

D1

DF

PSWD0

NOT ACCESSIBLE9A

9FA0

P3

NOT ACCESSIBLEA1

A7A8 IE

NOT ACCESSIBLEA9

AFB0

P2

NOT ACCESSIBLEB1

B7B8 IP

NOT ACCESSIBLE

B9

CF

8087

8887

9097

989F

A8

A0

B7

A7

AF

- -

B0

B8BF

F0F7

E0

D0

E7

D7

R0R7R0R7R0R7R0R7

Scratch pad Memory Space

T2CONT2MODRCAP2LRCAP2HTL2TH2

C8C9

CACBCCCD

8952

only

85DP1LDP1H

NOT ACCESSIBLE86

7F80

Accessible by indirect addreesas: MOV A, @R0

Figure-A.7: Recommended Data Structure for Atmel CISC Microcontrollers

Page 49: 89S52 Micro Controller Interfacing

49