It feels good to use a computer to control the single-chip microcomputer, and then control other peripherals such as relays. How to achieve it? Come step by step.
The serial port communication of the single-chip microcomputer, that is, the communication between the single-chip microcomputer and the computer, must be realized, and some basic concepts must be understood.
(1) Before implementation, please make sure that the serial port is normal. I have done serial port operation before, and it cannot be controlled after burning. Later, I found out that there is an exclamation mark on the serial port in the device manager. It is caused by a USB port (I use a serial port to USB, for a notebook)
(2) Serial port initialization, in this regard, it is best to have a relevant book to understand the digestion of timers and interrupts, so that it is easy to operate.
one. Set the working mode of the serial port: Set the SCON register.
What is SCON?
The SCON register is an addressable special register of 51 single-chip microcomputer, which is used for the control of serial data communication. Its byte address is: 98H, and its bit address is: 98H~9FH. The content is:
SM0SM1SM2RENTB8RB8TIRI
SM0 SM1: Serial port working mode selection bit.
SM0SM1 working mode function baud rate
00 Mode 0 8-bit synchronous shift register fosc/12
01 Mode 1 10-bit UART variable
10 Mode 2 11-bit UART fosc/64 (or fosc/32)
11 Mode 3 11-bit UART Variable
-------------------------------------------------- ---------
SM2: Multi-computer communication control bit
REN: Receive enable control, 1 is to allow serial port reception, 0 is to disable serial port reception.
TB8: 9th data bit sent.
RB8: 9th data bit received.
TI: Transmit interrupt flag. After sending a frame of data, the hardware will automatically set to 1. The TI bit must be cleared by software.
RI: Receive interrupt flag. After receiving a frame of data, the hardware is automatically set to 1. The RI bit must be cleared by software.
Example: Use the serial port debugging assistant to debug the serial port and send the hexadecimal number to make the corresponding LED light on.
The C program is as follows:
#include
void main()
{
TMOD=0x20; //Set the timer: TImer1 Mode 2 8-bit reload
TH1=0xfd; //Baud rate for 9600 baud @ 11.0592MHz
TL1=0xfd;
SM0=0;
SM1=1;
REN=1; //Control RI, allow serial reception. SM0SM1 and REN are SCON=0x50;
TR1=1; //Start timer 1
while(1)
{
if (RI==1) //If RI is 1, the data sent by the serial port is received
{
RI=0; library // software clear. must
P0=SBUF; //SBUF is a buffer, the receive buffer can only be read but not written, and the transmit buffer can only be written but not read.
/* Both share a byte address, and the two are independent of each other. */
}
}
}
The above are the serial control LED lights, which lights you want to display can be sent through the serial port debugging software.
Example: digital tube display. Only a certain bit is shown here, if there are more than one bit, you can add the relevant program segment by yourself.
#include
void main()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SM0=0;
SM1=1;
REN=1; //Control RI
TR1=1;
while(1)
{
if(RI==1)
{
RI=0;
P0=SBUF; //P0 is the value of the digital tube.
P2=0x06; //8-bit digital tube, the 7th, P2 port is chip selection,
}
}
}
The above program can be debugged and run on AT89s52 MCU+Keil.
Related tools:
Serial debugging assistant, 51 serial communication calculator.exe, can be searched and downloaded on the Internet.
Online Expansion Of Lithium Ion Battery
25Ah Modules Battery,Bluetooth App Battery,Remote Monitoring Battery,Custom Lithium Battery
Wolong Electric Group Zhejiang Dengta Power Source Co.,Ltd , https://www.wldtbattery.com