Using AT89C52 MCU as a multi-function peripheral device

Publisher:古泉痴迷者Latest update time:2011-09-30 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The widespread use of AT89C52 (abbreviated as 89C52) has greatly reduced the price of single-chip microcomputers. At present, the market retail price of 89C52 is lower than any of the special interface chips such as 8255, 8279, 8253, 8250, etc.; and the functions of 89C52 actually far exceed the above chips. Therefore, it is economically cost-effective to use 89C52 as an interface chip. In the system designed by me, 89C52 is designed as a multi-function programmable interface, and its related programs are solidified in the Flash ROM on the chip. This chip (hereinafter referred to as the multi-function interface) has the following functions: ① There is a dedicated keyboard/display interface; ② There is a full-duplex asynchronous serial communication interface; ③ There are two 16-bit timers/counters. In this way, one 89C52 chip undertakes the work of three special interface chips; not only does it greatly reduce the cost, but also optimizes the hardware structure and software design, bringing a lot of convenience to users.
1 Hardware Structure
When 89C52 is used as an interface, the interface itself must occupy as few hardware resources as possible, and the connection lines between the interface and the main system should be as few as possible, so that less host pins can be occupied and more resources can be left for the system. In this design, serial communication is used between the host and the multi-function interface. There are four connection lines between the host and the multi-function interface: CE, CLK, DAT, and INTR. CE is used as the chip select signal input terminal. When this pin is low, the host can transmit data with the multi-function interface. CLK is the clock input terminal. When the host sends an instruction to the multi-function interface, the level of this pin rises to indicate that the data is valid. DAT is the serial data input/output terminal. INTR is the output terminal of the multi-function interface. When the data in the multi-function interface is ready, INTR sends a low level to the host to notify the host that the data is ready and can be read. This signal can be queried by the host and can also be used as an interrupt request signal sent to the host.
The P0, P1, and P2 ports of 89C52 are used as keyboard/display interfaces. Keyboard input and display output are performed using the program scanning method. Port P0 is used as the field port, port P1 is used as the column input port of the keyboard, and port P2 is used as the word port of the display and the row scan output port of the keyboard. Since the current of the word port of the display is large, port P2 needs to be driven by current. This multi-function interface can connect up to 8 LED displays and 1 8×8 chain disk matrix. Its hardware principle is shown in Figure 1.
P3.2, P3.3, P3.6, and P3.7 are used as CE, CLK, DAT, and INTR signals respectively; while P3.0 and P3.1 are used as asynchronous serial communication data lines RXD and TXD; P3.4 and P3.5 are OUT0/T0 and OUT1/T1 signal lines respectively. When the timer works in continuous pulse mode, this pin can output continuous pulses with programmable on-off ratio and frequency; when working in counter mode, this pin is used as an external count pulse input terminal to input count pulses.
Since the multi-function interface receives instructions using software, the time periods t1, t2, t3, t4, and t5 in the figure are relatively long, and the time period t1-t4 is required to be between 10μs and 1000μs. There are two types of instructions: write instructions and read instructions. In write instructions, whether it is the opcode or the operand, the data transmission direction is from the host to the multi-function interface; while for read instructions, the data direction of the opcode part is from the host to the multi-function interface, and the data direction of the operand part is from the multi-function interface to the host.
t5 is the time interval required between the opcode and the operand in the read instruction, and t5 should be in the range of 30μs to 1000μs. During the read operation, when the rising edge of the CLK clock arrives, the multi-function interface sends the data to the DAT pin.
3 Instruction encoding
The multi-function interface has a total of 13 instructions. The instruction length varies from 1 byte, 2 bytes, 3 bytes, and 4 bytes.
(1) Reset instruction
The machine code is 00H. When the multi-function interface receives the reset command, all characters are blanked, all timers are cleared, and the timing interrupt and serial interrupt are turned off. Its status is similar to the system power-on reset.
(2) Display data command
This command sends display data to the display buffer of the multi-function interface. This is a 2-byte command, and its command encoding is as follows:

D7

D6

D5

D4

D3

D2

D1

D0

0

0

0

1

0

0

0

0

D7

D6

D5

D4

D3

D2

D1

D0

dip

0

0

0

d3

d2

d1

d0

Where dip is the decimal point control bit, a2, a1, a0 are bit addresses, d3, d2, d1, d0 are characters to be displayed, and their formats are listed in Tables 1 and 2.
(3) Flashing command
This command controls the flashing properties of each digital tube. d7~d0 correspond to digital tubes 1~8 respectively, 1 is flashing, and 0 is not flashing.

D7

D6

D5

D4

D3

D2

D1

D0

0

0

0

1

0

0

0

0

D7

D6

D5

D4

D3

D2

D1

D0

d7

d6

d5

d4

d3

d2

d1

d0

(4) Circular left shift instruction
The instruction code is 11H. This instruction shifts all displays from left to right by 1 bit.
Table 1

a2

a1

a0

Display bit

0

0

0

1

0

0

1

2

0

1

0

3

0

1

1

4

1

0

0

5

1

0

1

6

1

1

0

7

1

1

1

8

Table 2

d3

d2

d1

d0

Display Characters

0

0

0

0

0

0

0

0

1

1

0

0

1

0

2

0

0

1

1

3

0

1

0

0

4

0

1

0

1

5

0

1

1

0

6

0

1

1

1

7

1

0

0

0

8

1

0

0

1

9

1

0

1

0

-

1

0

1

1

H

1

1

0

0

L

1

1

0

1

P

1

1

1

0

E

1

1

1

1

null

(5) Circular right shift instruction
The instruction code is 12H. This instruction shifts all displays from right to left by 1 bit.
(6) Read keyboard instruction
The instruction is a 2-byte instruction, and the operand is the key value read. The key values ​​of each keyboard are shown in Figure 1. The instruction format is as follows:

D7

D6

D5

D4

D3

D2

D1

D0

0

0

0

1

0

0

1

1

D7

D6

D5

D4

D3

D2

D1

D0

d7

d6

d5

d4

d3

d2

d1

d0

(7) Continuous pulse output instruction
This instruction programs the timer. It makes OUT0/OUT1 output continuous pulses. The on-off ratio and frequency of the pulses can be set by programming. The instruction is 4, and the format is as follows:
Byte 1

D7

D6

D5

D4

D3

D2

D1

D0

0

0

0

1

0

0

1

1/0

Byte 2

D7

D6

D5

D4

D3

D2

D1

D0

M3

M2

M1

M0

N3

N2

N1

N0

Byte 3

D7

D6

D5

D4

D3

D2

D1

D0

f15

f14

f13

f12

f11

f10

f9

F8

Byte 4

D7

D6

D5

D4

D3

D2

D1

D0

f7

f6

f5

f4

f3

f2

f1

f0

Where M:N is the on-off ratio of the pulse, and the setting range of M or N is 1 to 10. f15 to f0 is the frequency setting, and its setting range is 1 to 9999Hz. It is set in BCD code. In the instruction opcode, D0 sets timer 0 or timer 1 to work.
(8) Timer mode instruction
This instruction is a 3-byte instruction. The opcode is 22H or 23H, where D0 determines whether to operate timer 0 or timer 1. The second and third bytes are the timing time.
(9) Counter mode instruction
This instruction is a 3-byte instruction. The opcode is 24H or 25H, where D0 determines whether to operate timer 0 or timer 1. The second and third bytes are the initial count value.
(10) Read counter instruction
It is a 3-byte instruction. The opcode is 26H or 27H, and the second and third bytes are the count values ​​read from the counter. The D0 bit of the opcode determines whether to operate counter 0 or counter 1. This instruction reads the current count value of the 16-bit counter.
(11) Serial send command
This command initializes asynchronous serial communication. Its command format is as follows:

Byte 1

Byte 1

D7

D6

D5

D4

D3

D2

D1

D0

0

0

1

1

0

a2

a1

a0

Table 2 Bytes

D7

D6

D5

D4

D3

D2

D1

D0

b1

b0

d5

d4

d3

d2

d1

d0

Byte 3

D7

D6

D5

D4

D3

D2

D1

D0

d7

d6

d5

d4

d3

d2

d1

d0

Byte 4

D7

D6

D5

D4

D3

D2

D1

D0

d7

d6

d5

d4

d3

d2

d1

d0

Serial communication is carried out in batch transmission. Each instruction transmits one data frame. In the instruction, the first and second bytes are used to initialize the serial communication, and the data after the third byte are sent. b1 and b0 are used to determine odd parity, even parity or no parity. The operands d5, d4, d3, d2, d1, and d0 of the second byte are used to set the number of data in the frame. A maximum of 64 characters can be sent in one frame. a2, a1, and a0 set the baud rate of the serial communication. The baud rates are listed in Table 3.

Table 3

a2 a1 a0

Baud rate

0 0 0

110

0 0 1

150

0 1 0

300

0 1 1

600

1 0 0

1200

1 0 1

2400

1 1 0

4800

1 1 1

9600

(12) Serial receive command

The instruction format is as follows:

Byte 1

D7

D6

D5

D4

D3

D2

D1

D0

0

0

1

1

1

a2

a1

a0

Byte 2

D7

D6

D5

D4

D3

D2

D1

D0

b1

b0

d5

d4

d3

d2

d1

d0

Byte 3

D7

D6

D5

D4

D3

D2

D1

D0

d7

d6

d5

d4

d3

d2

d1

d0

Byte 4

D7

D6

D5

D4

D3

D2

D1

D0

d7

d6

d5

d4

d3

d2

d1

d0

The instruction encoding is similar to serial transmission, and the data after the third byte is the received data.

(13) Read status instruction

The instruction format is as follows:

D7

D6

D5

D4

D3

D2

D1

D0

0

1

0

0

0

0

0

0

D7

D6

D5

D4

D3

D2

D1

D0

0

0

0

d4

d3

d2

d1

d0

When keyboard input, counter return, etc. or serial communication is interrupted, the multi-function interface will send a negative pulse to INTR. After responding, the host should send a read status instruction to query the status flag to determine which status flag is active. Among them, d0 is the keyboard input flag, d1 is the counter 0 return to zero action. d2 is the counter 1 return to zero flag. d3 is the serial reception end flag, and d4 is the serial transmission end flag.

4. Software Design of Multi-Function Interface

In the main program, display scanning and keyboard scanning are performed. If there is keyboard input, the key number of the keyboard matrix is ​​found. The key number is stored in the key value register, and the status flag is set. At the same time, the INTR negative pulse is sent to notify the host to get the key value.

The host operates the multi-function interface through instructions. The host must first set CE to 0. The falling edge sends an external interrupt to the multi-function interface through P3.2. In the interrupt service program, the multi-function interface performs instruction fetch and decoding operations, and transfers to the corresponding processing channel through the scattered transfer instruction. Then the interrupt returns. At this point, the process of instruction fetch-decoding-execution of an instruction is completed. The program returns to the main loop to continue display scanning and keyboard scanning.

The so-called instruction fetch operation is actually the process of communication between the multi-function interface and the host. Since software is used for communication, the widths of t1, t2, t3, t4, and t5 have a certain range.

After receiving the operation instruction from the host, the serial communication between the multi-function interface and the peripherals and the operation of the related timers are carried out in interrupt mode. At the end of the interrupt, the status flag is set and the INTR negative pulse is sent to the host. This negative pulse can be queried by the host or used as an interrupt request signal to the host. After completing the above work, return to the main program.

89C 52 has three timers, among which T2 is used as the baud rate generator for serial communication. In this way, T0 and T1 can be lent to users as timers. When T2 is in the baud rate generator mode, the TCLK bit or RCLK bit in the T2CON register can be set. The initial count value is loaded into TH2 and TL2, so that T2 starts counting from this initial value, but FT2 is not set. RCAR2H and RCAP 2L After the constants in are set by software, the overflow rate of T2 is strictly unchanged, thus making the baud rate of serial communication very stable.

5 Application Examples

In the online supplement of this journal (http://www.elecfans.com), the host is given as AT 89C The program is used to wait for keyboard input, and then convert the keyboard code into decimal and send it to the LED digital tube for display.

Reference address:Using AT89C52 MCU as a multi-function peripheral device

Previous article:Design of a Serial Data Acquisition/Transmission Module for Single Chip Microcomputer
Next article:Indoor formaldehyde testing system based on 51 single chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号