Serial communication of 51 single-chip microcomputer (III.)
has been read 47 times 2015-12-29 00:21 | Personal Category: 51 single-chip microcomputer | single chip microcomputer, communication
Use the serial port to send and receive simultaneously operation: The computer displays the result source program:
/*
Project name: Serial send and receive experiment
Project content: Serial sending and receiving at the same time,
The computer sends data to the microcontroller to control the P0 port, thus controlling
LED light off, single-chip computer to send data to computers, computers
Show up
Date: December 23, 2015
Project leader: YUAN
*/
#include <reg52.h>
typedef Unsigned CharUChar8;
typedef Unsigned IntUInt16;
void Delayms(UInt16Valms);
void Uartinit(void);Serial initialization
void Uartreceivedata(void);Receive data
void Uartsenddata(void);The serial port sends the data, namely the monolithic computer sends the data
voidMain(void)
{
/* Initialization: Includes timer interrupt settings,
Serial port register settings, baud rate setting */
Uartinit();
While(1)
{
/* Receive the data sent by the computer and send it to the LED control port */
Uartreceivedata();
Uartsenddata();
Delayms(500);
}
}
void Delayms(UInt16Valms)
{
UInt16 Uival,Ujval;
For(Uival=0;Uival<Valms;Uival++)
For(Ujval=0;Ujval<113;Ujval++);
}
void Uartinit(void)
{
Tmod&= 0x0f; Just change what you want to change.
Tmod|= 0x20;Set timer 0 to working mode 2
TL1= 0xfd;
TH1= 0xfd;Set baud rate to 9600bps
TR1= 1;Allow T1 to start counting
SCON&= 0x5f;
SCON|= 0x50;Set the serial port to work in mode 1, allowing data to be received
}
void Uartreceivedata(void)
{
If the receive interrupt is 1, then there is a send
If(RI)
{
RI= 0;
P0=Sbuf;//send data to P0 control led
}
}
void uartsenddata ( void)
{
static uChar8 num = 0 //send data and accumulate
sbuf = Num num++;
ti = 0
}
/span>
Collection sharing invitation to use props| edit | Delete |
Serial communication of 51 single-chip microcomputer (III.)