Multi-machine communication based on RS-485 of 51 single-chip microcomputer

Publisher:老实巴交的大叔Latest update time:2015-09-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//-----------------------Function declaration, variable definition---------------------

#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);

}

Reference address:Multi-machine communication based on RS-485 of 51 single-chip microcomputer

Previous article:Implementation of SPI bus of 51 single chip microcomputer
Next article:MCU tutorial: 51 MCU infrared remote control and display on LCD1602

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号