The function is: send a character (such as e) to the microcontroller, and then the microcontroller returns the string "I get e", serial port communication.
Copy code
#include <reg52.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
uchar flag, a;
uchar code table[]=" I get ";
//Serial port initialization
void Serial_Init()
{
TMOD = 0x20; //Set T1 timer working mode 2
TH1 = 0xfd; //T1 timer initial value
TL1 = 0xfd; //T1 timer initial value
TR1 = 1; //Start T1 timer
REN = 1; //Allow serial port to receive
SM0 = 0; //Set serial port working mode 1
SM1 = 1; //Same as above
EA = 1; //Open general interrupt
ES = 1; //Open serial port interrupt
}
void main()
{
Serial_Init();
while(1)
{
if(flag)
{
uchar i;
ES = 0; // First turn off the serial port interrupt to prevent interference
for(i=0; i<7; i++)
{
SBUF = table[i];
while(!TI); // Wait for the transmission to be completed, because after the transmission is completed, TI is set by hardware
TI = 0; // Clear to perform the next transmission
}
SBUF = a; // During serial transmission, the CPU writes data to SBUF, and the capacity of sbuf is very large
while(!TI);
TI = 0;
EN = 1;
flag=0;
}
}
}
void ser() interrupt 4 //Serial port interrupt, receiving, a is equivalent to receiving buffer
{
RI = 0; //After receiving, RI is set to 1 by hardware, and the interrupt is cleared for the next reception
a = SBUF; //When receiving serially, the CPU reads data from SBUF
flag = 1;
}
Copy code
1.SBUF
Quoting Wikipedia:
Full name: serial data buffer
There
are two buffer registers SBUF in the serial port, one is the send register and the other is the receive register, which are completely independent in physical structure. They are both byte-addressed registers, and the byte address is 99H.
This overlapping address is distinguished by read/write instructions: when serial transmission, the CPU writes data to SBUF, and 99H means send SBUF; when serial reception, the CPU reads data from SBUF, and 99H means receive SBUF. MCU serial port program reference.
When I studied before, I had only a partial understanding of SBUF, just copied the code and saw the effect. After re-studying these two days, I got some new understanding.
First of all, it is mentioned in the encyclopedia that there are two buffer registers SBUF in the serial port, one is the send register and the other is the receive register, which are completely independent in physical structure. They are both byte-addressed registers, and the byte address is 99H. In the code segment, SBUF=a or SBUF = table[i] means that the CPU writes data to SBUF, indicating that it is in the sending state. a=SBUF means that the CPU reads data from SBUF, indicating that it is in the receiving state. That is, the overlapping address 0x99H is distinguished by the read/write instruction.
In addition, the capacity of SBUF is very large, and it can hold at least 200 bytes of data (200 characters).
2.TI/RI
First is the encyclopedia of Scon:
SCON (Serial Port Control Register) in 51 single-chip microcomputer represents the serial port control register
scon control register, which is a bit-addressable special register used for serial data communication control. The unit address is 98H, and its structure format is as follows:
SCON SM0 SM1 SM2 REN TB8 RB8 TI RI
bit address 9FH 9EH 8DH 9CH 9BH 9AH 99H 98H Among them,
TI: Transmit interrupt flag bit
Addressable flag bit. In mode 0, after the 8th bit of data is sent, it is set by hardware. In other modes, it is set by hardware before the send or stop bit. Therefore, TI=1 indicates that the frame transmission is completed, and TI can be set to 0 by software.
RI: Receive interrupt flag
Addressable flag. After the 8th bit of data is received, this bit is set by hardware. In other working modes, this bit is set by hardware. RI=1 indicates that the frame reception is completed.
Note: When processing serial port interrupts, TI and RI need to be set to 0 by software. It is impossible to automatically clear to 0 after hardware setting. In addition, when performing buffer operations, ES=0 is required to prevent interrupts.
In the code, SBUF = table[i], that is, after sending, there is while(!TI), that is, waiting for the transmission to be completed, because TI is set by hardware after the transmission is completed, and then TI = 0 is cleared by software to perform the next transmission.
RI is the same. The interrupt is entered because the PC sends information to the microcontroller through the serial port. After the microcontroller completes the reception, RI is set to 1 by hardware, and RI = 0 is cleared by software to perform the next reception.
3.ES
refers to the 5th bit (IE^4) of the interrupt enable register IE, indicating that the serial port interrupt is enabled (ES=1 enables, ES=0 disables). In the code, ES=0 is first set to disable the serial port interrupt to prevent interference. The data to be sent is first placed in SBUF. After all the data is placed, ES=1 is set to enable the serial port interrupt.
Well, if I had been more efficient last night, I could have summarized this earlier and then looked at the CAN communication code. . Here is a summary for myself (actually, I refer to encyclopedias and other materials to express what I understand), which will help the next step.
51 and stm32 have their own strengths and weaknesses. The configuration of stm32 for nrf24l01 is more complicated. .
|