#include
sbit RE_DE=P1^0;
#define COUNT 10 // Define the receive buffer size
#define Slaver_NUM 10
unsigned char bdata flag; //define a flag variable in bit addressable
sbit time_over_flag = flag^0; //Receive timeout flag
unsigned char buffer[COUNT]; //define buffer
unsigned char point; //Define buffer position indication
unsigned char Slave_AD[Slaver_NUM]; //Define the effective address storage area
unsigned char ADD_num; //Number of valid addresses
unsigned char idata count_10ms; //Used to indicate how many 10ms interrupts there are
unsigned char idata send_data[7]={
0x31,0x32,0x33,0x34,0x35,0x36,0x37}; //Define the data to be sent, a total of 7 bits
void UART_init(); //Serial port initialization function
void COM_send(void); //Serial port receiving function
unsigned char CLU_checkdata(void); //Calculate check digit function
//------------------------------------------------ ---------------
// Function name: UART_init() serial port initialization function
// Function: When the system clock is 11.059MHZ, set the serial port baud rate to 9600bit/s
// Enable serial port receive interrupt, disable send interrupt, set timer interrupt to enable
//------------------------------------------------ ---------------
void UART_init()
{
// Initialize serial settings
SCON =0x58; //Select serial port working mode 1, open receiving permission, TB8=1
TMOD = 0x21; //Timer 1 works in mode 2, timer 0 works in mode 1
TR1 =1; //Start timer T1
ES=1; //Enable serial port interrupt
PS=1; //Design serial port interrupt priority
// Initialize timer 1
TH1 = 0xfd; //Achieve baud rate 9600 (system clock 11.0592MHZ)
ET1 =0; //Timer 1 interrupt disabled
}
//------------------------------------------------ ---------------
// Function name: timer0_init() Initialize timer 0
// Function: Set the working mode of timer0
//------------------------------------------------ ---------------
void timer0_init()
{
time_over_flag=0;
count_10ms=0;
ADD_num=0;
TL0=0x0F0; //T0 is used to generate a 10ms interrupt
TH0=0x0D8; //50 T0 interrupts generate 1 timeout overflow
ET0=1; //Enable timer 0 interrupt
}
//------------------------------------------------ ---------------
// Function name: system_init() system initialization
// Function: Call the serial port and timer initialization functions to complete system initialization
//------------------------------------------------ ---------------
void system_init(void)
{
//System overall settings
UART_init();
timer0_init();
EA =1; //MCU interrupt is enabled
}
//------------------------------------------------ ---------------
// Function name: com_interrup() serial port receive interrupt processing function
// Function: Receive ten bits of data including the start bit "S" into the data buffer
//------------------------------------------------ ---------------
com_interrupt(void) interrupt 4 using 3
{
unsigned char RECEIVR_buffer;
if(RI) //Handle receive interrupt
{RI=0; // Clear the interrupt flag
RECEIVR_buffer=SBUF; //Receive serial port data
if(point==0) //If the start bit has not been received
{
if(RECEIVR_buffer==0xFE) //Judge whether it is the start flag
{
buffer[point++]=RECEIVR_buffer; //Put the received data into the receiving buffer
}
else
point=0; //No, continue waiting for the start position
}
else if(point>0&&point<10) //Judge whether ten bits of data are received
buffer[point++]=RECEIVR_buffer; //Not enough, put the received data into the receiving buffer
else if(point==10)
{
if(RECEIVR_buffer==0xEF) //Judge whether the end flag is correct
{
buffer[point]=RECEIVR_buffer; //Put the received data into the receiving buffer
Slave_AD[ADD_num++]=buffer[2]; //Put the received address into the address memory
//Indicates that there is a valid device at this address
}
else
point=0; //No, continue waiting for the start position
}
else point=0; //The buffer is full, clear the data in the buffer and receive again
}
if(TI) //Serial port sending interrupt
{
TI=0; // Clear transmit interrupt
}
}
//------------------------------------------------ ---------------
// Function name: timer0_interrup()
// Function: Timer T0 interrupt service routine
// Function description: T0 interrupts once every 10ms, and sets time_over_flag=1 for 50 consecutive interrupts;
//------------------------------------------------ ---------------
timer0_interrupt(void) interrupt 1 using 2
{
count_10ms++;
if(count_10ms==50)
{
ET0=0; //Disable timer T0 interrupt
TR0=0; //Stop timer T0
time_over_flag=1; //Set the receive timeout flag
count_10ms=0x00; //10ms counter reset
}
else
{
TL0=0x0F0; //Reload timer initial value
TH0=0x0D8;
}
}
//------------------------------------------------ ---------------
// Function name: COM_send() serial port sending function
// Function: Send out the ten-bit data in the data buffer
//------------------------------------------------ ---------------
void COM_send(void)
{
RE_DE=1; //Set MAX483 to the sending state
for(point=0;point=10,TI=1;point++) //Continuously send ten bits of data
//Send all the data in the buffer to the serial port
{
SBUF=buffer[point];
TI=0;
}
RE_DE=0; //Set MAX483 to receive state
}
//------------------------------------------------ ---------------
// Function name: write_buffer()
// Function: Write ten bits of data to the send buffer
//------------------------------------------------ ---------------
void write_buffer(unsigned char slaver_add)
{
unsigned char i;
TB8=1; //Open multi-machine communication mode
buffer[0]=0xFE;
buffer[1]=slaver_add;
for(i=2;i<9;i++) //Continuously send ten bits of data
//Send all the data in the buffer to the serial port
{
buffer[i]=send_data[i-2];
}
buffer[9]=0xEF;
}
//------------------------------------------------ ---------------
// Function name: Main function
// Function: Schedule a sub-function to complete the communication process
//------------------------------------------------ ---------------
void main(void)
{
unsigned char i=0;
system_init(); //System initialization
do{ //Check if there is any corresponding device for addresses 0 to 10
write_buffer(i++); //Write and query the sending information of device No. i
COM_send(); //Call the sending function to complete the sending
timer0_init(); //Complete a query, re-initialize timer 0, and prepare for the next query
}
while(time_over_flag&&i<10);
}
Previous article:Implementation of SPI bus of 51 single chip microcomputer
Next article:MCU tutorial: 51 MCU infrared remote control and display on LCD1602
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- About MSP430 Common Program Architecture - My Understanding
- [ATmega4809 Curiosity Nano Review] ASF does not support ATmega4809
- Review summary: Domestic RISC-V Linux board Fang Xingguang VisionFive
- EEWORLD University Hall----Principles of Automatic Control Lu Jingchao, Northwestern Polytechnical University
- Advantages and prospects of CC1312R compared with CC1310
- Open source FabGL graphics library for ESP32
- The problem of combining Arm and Linux
- Bluetooth Protocol Analysis_Protocol Architecture
- How to calculate the power of the power amplifier under drain bias?
- Read the good book "Operational Amplifier Parameter Analysis and LTspice Application Simulation" + Power Supply Waveform View