//*******Serial communication program************
//*******Define variables*************
#include
#define unchar unsigned char
#define uint unsigned int
unchar RC_label;//The start bit of the flag is 1 and the end bit is 0
unchar caiji_label;//The acquisition frame format flag is 1 if it is correct and 0 if it is wrong
unchar data RC[100];//Store received data
unchar data TX1[100];//Store own parameters
unchar data TX2[100];//Store error indication ERROR
int RC_buff;//
int i=0,j=0,k=0;//
//*****Variable definition completed************
//*****Subroutine area****************
//*****Serial port initialization program starts**********
void initRS232()
{
//Set TXSTA
CSRC=0;//
TX9=0;//8-bit data transmission
TXEN=1;//transmission enable
SYNC=0;//select asynchronous mode
BRGH=1;//high speed
TRMT=1;//transmission register empty
TX9D=0;//
//Set RCSTA
SPEN=1;//allow serial port to work
RX9=0;//receive 8-bit data
SREN=1;//
CREN=1;//enable continuous reception
ADDEN=0;//
FERR=0;//
OERR=0;//no overspeed error
RX9D=0;//
TRISC=0X80;//set C port
SPBRG=0X19;//set baud rate to 9600
}
//*******initialization 232 completed********
//*******delay subroutine starts********
void delay(int i)
{for(i;--i;) continue;}
//******delay subroutine completed*******
//******interrupt initialization subroutine starts******
void int_interrupt(void)
{
GTE=1;//
PEIE=1;//
RCIE=1;//
}
//*******Interrupt initialization subroutine completed********
//********Interrupt service subroutine started*********
void interrupt receive(void)
{
if(OERR==1)//Judge whether there is an overspeed error
{
CREN=0;
CREN=1;
}
if(RCIF==1)
{
RC_buff=RCREG;
if(RC_buff==0x2a)//Judge whether it is the start mark*
{
RC_label=1;
}
if(RC_buff==0x23)//Judge whether it is the end mark#
{
RC_label=0;
}
}
if(RC_label==1)
RC[k++]=RC_buff;//Store the received data in the arrayelse
if(RC_label==0)
k=0;
if(RC[1]==0XFE&&RC[2]==0XC0&&RC[3]==0X34&&RC[5]==0X12)//Judge whether the data acquisition frame format is correct
caiji_label=1;
else caiji_label=0;
//********Interrupt service subroutine completed **********
//********Parameter sending subroutine started **********
void TX_serve(void)
{
TX1[]={0X2A,0XFD,0X26,0X30,0X31,0X32};//Own parameters are stored in TX1
TX2[]={0X56,0X52,0X52,0X4F,0X52};//Error indication is stored in TX2
if(caiji_label==1)//If the data acquisition frame format is correct, send its own parameter frame
{
for(j=0;j<6;j++)
{
TXREG=TX1[j];
delay(20);
}
}
if(caiji_label==0) //If the data acquisition frame format is wrong, send an error indication
{
for(j=0;j<5;j++)
{
TXREG=TX2[j];
delay(20);
} } }
// ********Parameter sending subroutine completed********** //********Main program starts************** void main() { int_interrupt(); initRS232(); TX_serve(); end }
Keywords:PIC16F877A
Reference address:PIC16F877A and PC serial communication C language source program
//*******Define variables*************
#include
#define unchar unsigned char
#define uint unsigned int
unchar RC_label;//The start bit of the flag is 1 and the end bit is 0
unchar caiji_label;//The acquisition frame format flag is 1 if it is correct and 0 if it is wrong
unchar data RC[100];//Store received data
unchar data TX1[100];//Store own parameters
unchar data TX2[100];//Store error indication ERROR
int RC_buff;//
int i=0,j=0,k=0;//
//*****Variable definition completed************
//*****Subroutine area****************
//*****Serial port initialization program starts**********
void initRS232()
{
//Set TXSTA
CSRC=0;//
TX9=0;//8-bit data transmission
TXEN=1;//transmission enable
SYNC=0;//select asynchronous mode
BRGH=1;//high speed
TRMT=1;//transmission register empty
TX9D=0;//
//Set RCSTA
SPEN=1;//allow serial port to work
RX9=0;//receive 8-bit data
SREN=1;//
CREN=1;//enable continuous reception
ADDEN=0;//
FERR=0;//
OERR=0;//no overspeed error
RX9D=0;//
TRISC=0X80;//set C port
SPBRG=0X19;//set baud rate to 9600
}
//*******initialization 232 completed********
//*******delay subroutine starts********
void delay(int i)
{for(i;--i;) continue;}
//******delay subroutine completed*******
//******interrupt initialization subroutine starts******
void int_interrupt(void)
{
GTE=1;//
PEIE=1;//
RCIE=1;//
}
//*******Interrupt initialization subroutine completed********
//********Interrupt service subroutine started*********
void interrupt receive(void)
{
if(OERR==1)//Judge whether there is an overspeed error
{
CREN=0;
CREN=1;
}
if(RCIF==1)
{
RC_buff=RCREG;
if(RC_buff==0x2a)//Judge whether it is the start mark*
{
RC_label=1;
}
if(RC_buff==0x23)//Judge whether it is the end mark#
{
RC_label=0;
}
}
if(RC_label==1)
RC[k++]=RC_buff;//Store the received data in the arrayelse
if(RC_label==0)
k=0;
if(RC[1]==0XFE&&RC[2]==0XC0&&RC[3]==0X34&&RC[5]==0X12)//Judge whether the data acquisition frame format is correct
caiji_label=1;
else caiji_label=0;
//********Interrupt service subroutine completed **********
//********Parameter sending subroutine started **********
void TX_serve(void)
{
TX1[]={0X2A,0XFD,0X26,0X30,0X31,0X32};//Own parameters are stored in TX1
TX2[]={0X56,0X52,0X52,0X4F,0X52};//Error indication is stored in TX2
if(caiji_label==1)//If the data acquisition frame format is correct, send its own parameter frame
{
for(j=0;j<6;j++)
{
TXREG=TX1[j];
delay(20);
}
}
if(caiji_label==0) //If the data acquisition frame format is wrong, send an error indication
{
for(j=0;j<5;j++)
{
TXREG=TX2[j];
delay(20);
} } }
// ********Parameter sending subroutine completed********** //********Main program starts************** void main() { int_interrupt(); initRS232(); TX_serve(); end }
Previous article:Detailed explanation of port B of pic16f877a
Next article:PIC16F 8-bit microcontroller assembly instruction set
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
Guess you like
- 01. Environment construction and driving 0.96OLED
- 【RT-Thread software package application work】Small alarm clock
- High-side current sampling problem
- Qorvo 100 MHz Envelope Tracking Solution for 5G
- 【Beetle ESP32-C3】3. Adjust LED brightness with knob (potentiometer) (Arduino)
- China's first batch of autonomous driving unmanned commercial licenses issued! Do you dare to sit in an unmanned car? ?
- GDB debugging and DSP
- The IO pin of MSP430 is set as input, but it receives the output signal of the Hall sensor, and the chip-side signal does not switch.
- [Serial] [Starlight Lightning STM32F407 Development Board] Chapter 10 Serial Communication Experiment
- Where do errors in vector network analyzers come from?