PIC16F877A and PC serial communication C language source program

Publisher:炉火旁的YyeLatest update time:2015-04-28 Source: 51heiKeywords:PIC16F877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//*******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

Previous article:Detailed explanation of port B of pic16f877a
Next article:PIC16F 8-bit microcontroller assembly instruction set

Latest Microcontroller Articles
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号