Universal application program for asynchronous communication between two single-chip microcomputers

Publisher:正在搬砖的河马71Latest update time:2017-12-06 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 MCU P IC 1 Programming (sending part) 
#include  
/*This program implements the asynchronous communication function of the MCU, and this program is the sending part*/ 
unsigned char tran[8]; /*Define an array to store the sending data*/

unsigned char k, data; /*define general registers*/ 
const char table[20]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0XD8, 
0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0x7f, 0xbf, 0x89, 0xff}; 
/*display segment code table without decimal point*/ 
/*spi display initialization subroutine*/ 
void SPIINIT() 

    PIR1=0; 
    SSPCON=0x30;     
    SSPSTAT=0xC0; 
/*Set the SPI control mode, allow SSP mode, and send on the falling edge of the clock, 
which corresponds to the characteristics of "74HC595, when its *SCLK jumps from low to high, the serial input register"*/ 
    TRISC=0xD7; /*SDO pin is output, SCK pin is output*/ 
    TR ISA 5=0; /*RA5 pin is set as output to output display latch signal*/ 

/*Subroutine to assign initial value to array */ 
void fuzhi() 

    for(k=0;k<8;k++) { 
        tran[k]=k+3; 
    } 

/*SCI component initialization subroutine*/ 
void sciint() 

    SPBRG=0X19; /*Set the transmission baud rate to about 9 600 bits/second*/ 
    TXSTA=0X04; /*Select asynchronous high-speed mode to transmit 8-bit data*/ 
    RCSTA=0X80; /*Allow synchronous serial port to work*/ 
    TRISC6=1; 
    TRISC7=1; /*Set RC6 and RC7 as input mode, and present high impedance to the outside*/ 
}     
/*SPI data transmission subroutine*/ 
void SPI LED (data) 

    SSPBUF=data; /*Start sending*/ 
    do { 
              ; 
    }while(SSPIF==0); 
    SSPIF=0; 

/*Display subroutine, display 8 digits*/ 
void display() 

    RA5=0; /*Prepare for latching*/ 
    for(k=0;k<8;k++) { 
        data=tran[k]; 
        data=table[data]; /*Check the displayed segment code*/ 
        SPILED(data); /*Send the displayed segment code*/ 
    } 
    RA5=1; /*Finally give a latch signal to indicate that the display task is completed*/      
}             
/*Main program*/ 
main() 

    SPIINIT(); 
    fuzhi(); /*Assign initial values ​​to the array*/ 
    sciint(); /*Initialize SCI components*/ 
    di(); /*Interrupt disable*/ 
    TXEN=1; /*Send enabled*/ 
    CREN=1; /*Receive data enabled*/ 
       for(k=0;k<8;k++){     
        TXREG=tran[k]; /*Send a character*/ 
           while(1){ 
            if(TXIF==1) break; 
    } /*Wait for writing to complete*/ 
while(1){ 
            if(RCIF==1) break;/*If the response byte is received, the waiting is terminated*/ 
        } 
        RCREG=RCREG; /*Read the response byte and clear RCIF*/     
     }     
    display(); /*Display the sent data*/ 
while(1){ 
    ; 
    } 


2 PIC2 microcontroller programming (receiving part) 
#include  
/*This program implements the asynchronous communication function of the microcontroller. This program is the receiving part and displays the received data on the 8 
*LED*/ 
unsigned char rece[8];/*define an array to store received data*/ 
unsigned char k, data;/*define general registers*/ 
const char table[20]={0xc0, 0xf9, 0xa4 
, 0xb0, 0x99, 0x92, 0x82, 0XD8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0x7f, 0xbf, 0x89, 0xff}; 
/*Display segment code table without decimal point*/ 
/*spi display initialization subroutine*/ 
void SPIINIT() 

;For detailed statements, see the sending program 

/*SCI component initialization subroutine*/ 
void sciint() 

    SPBRG=0X19; /*Baud rate setting is the same as PIC1, which is about 9 600 bits/second*/ 
    TXSTA=0X04; /*Asynchronous high-speed transmission*/ 
    RCSTA=0X80; /*Serial port operation enable*/ 
    TRISC6=1; 
    TRISC7=1; /*Set RC6 and RC7 to input mode, and present high impedance to the outside*/ 

/*SPI data transmission subroutine*/ 
void SPILED(data) 

;Detailed statements are the same as those in the sending program 

/*Display subroutine, display 4 digits*/ 
void display() 

    RA5=0; /*Prepare for latching*/ 
    for(k=0;k<8;k++){ 
        data=rece[k]; 
        data=table[data]; /*Check the displayed segment code*/ 
        SPILED(data); /*Send the display segment code*/ 
    } 
    RA5=1; /*Finally give a latch signal to indicate that the display task is completed*/      

/*Main program*/ 
main() 

    SPIINIT(); /*spi display initialization*/ 
    sciint(); /*SCI component initialization*/ 
    di(); /*Interrupt disabled*/ 
    CREN=1; /*Receiving enabled*/ 
    TXEN=1; /*Transmitting enabled*/ 
for(k=0;k<8;k++){ 
while(1){ 
            if(RCIF==1) break; 
} /*Wait for receiving data*/ 
        rece[k]=RCREG; /*Read received data and clear RCIF*/ 
        TXREG=rece[k]; /*Send received data*/ 
while(1){ 
            if(TXIF==1) break; 
} /*Wait for writing to complete*/ 

    display(); /*Display received data*/ 
while(1){ 
        ; 
    } 

8.6 Single-chip dual-machine synchronous communication 
1 Single-chip PIC1 programming (master control sending) 
#include  
/*This program implements the single-chip dual-machine synchronous communication function, which is the master control sending part. After the program is powered on, 
the corresponding character is displayed, indicating that the system is working normally. After sending, display the sent data*/ 
unsigned char tran[8]; /*Define an array to store the sent data*/ 
unsigned char k, data; /*Define general registers*/ 
const char table[20]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 
0XD8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0x7f, 0xbf, 0x89, 0xff}; 
/*Display segment code table without decimal point*/ 
/*SPI display initialization subroutine*/ 
void SPIINIT() 

;For detailed program statements, please refer to Section 8.5 of this chapter 

/*Subroutine to assign initial values ​​to the sending array*/ 
void fuzhi() 

    for(k=0;k<8;k++){ 
        tran[k]=k; 
    } /*Send eight data from 0 to 7*/ 

/*SCI component initialization subroutine*/ 
void sciint() 

    SPBRG=200; /*Set the transmission baud rate to about 9600 bits/second*/ 
    TXSTA=0X90; /*Select the master control mode*/ 
    RCSTA=0X80; /*Allow the synchronous serial port to work*/ 
    TRISC6=1; 
    TRISC7=1; /*Set RC6 and RC7 to input mode, and present high impedance to the outside*/ 
}     
/*SPI data transmission subroutine*/ 
void SPILED(data) 

    ; For detailed program statements, please refer to Section 8.5 of this chapter 

/*Display subroutine, display 8 digits*/ 
void display() 

    RA5=0; /*Prepare for latching*/ 
    for(k=0; k<8; k++){ 
        data=tran[k]; 
        data=table[data]; /*Check the displayed segment code*/ 
        SPILED(data); /*Send the display segment code*/ 
    } 
    RA5=1; /*Finally give a latch signal to indicate that the display task is completed*/      

/*Display subroutine, display 8 digits*/ 
void display1() 

    RA5=0; /*Prepare for latching*/ 
    for(k=0;k<8;k++){ 
        data=0xf9; /*Display "1" indicates that the system is working normally*/ 
        SPILED(data); /*Send display segment code*/ 
    } 
    RA5=1; /*Finally give a latch signal to indicate that the display task is completed*/      

/*Main program*/ 
main() 

    SPIINIT(); /*spi display initialization*/ 
    fuzhi(); /*Assign initial value to the send array*/ 
    sciint(); /*SCI component initialization*/ 
    di(); /*Interrupt disable*/ 
    TXEN=1; /*Send enable*/ 
    display1(); /*Display the corresponding characters to indicate that the system is normal*/ 
while(1){ 
for(k=0;k<8;k++){     
            TXREG=tran[k];/*Send a character*/ 
while(1){ 
                if(TXIF==1) break; 
    } /*Wait for the previous data to be written*/ 
          } 
        display(); /*Display the sent data*/ 
   } /*Loop sending*/ 


2 PIC2 Microcontroller Programming (Slave Reception) 
#include  
/*This program implements the synchronous communication function of the microcontroller dual-machine. It is the slave receiving part and displays the received data 
on 8 LEDs*/ 
unsigned char rece[8]; /*Define an array to store the received data*/ 
unsigned char k, data; /*Define general registers*/ 
unsigned int i; 
const char table[20]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0XD8,0x80, 
0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0x7f, 0xbf, 0x89, 0xff}; 
/*Display segment code table without decimal point*/ 
/*spi display initialization subroutine*/ 
void SPIINIT() 

    ; For detailed program statements, please refer to Section 8.5 of this chapter 

/*SCI component initialization subroutine*/ 
void sciint() 

    TXSTA=0X10; /*Select synchronous slave mode*/ 
    RCSTA=0X90; /*Serial port operation enable*/ 
    TRISC6=1; 
    TRISC7=1; /*Set RC6 and RC7 to input mode and present high impedance to the outside*/ 

/*SPI data transmission subroutine*/ 
void SPILED(data) 

    ;/*For detailed program statements, please refer to Section 8.5 of this chapter*/ 

/*Display subroutine, display 4 digits*/ 
void display() 

    RA5=0; /*Prepare for latching*/ 
    for(k=0;k<8;k++){ 
        data=rece[k]; 
        data=table[data]; /*Get the displayed segment code*/ 
        SPILED(data); /*Send the display segment code*/ 
    } 
    RA5=1; /*Finally give a latch signal to indicate that the display task is completed*/      

/*Main program*/ 
main() 

    SPIINIT(); /*spi display initialization*/ 
    sciint(); /*SCI component initialization*/ 
    di(); /*Interrupt prohibition*/ 
    CREN=1; /*Reception enabled*/ 
for(k=0;k<8;k++) rece[k]=0x03; 
    display(); /*Display data indicating normal operation of the system*/ 
while(1) { 
        while(1){ 
            CREN=1; /*Allow continuous reception*/  
            while(1){ 
                if(RCIF==1) break; 
} /*Wait for receiving data*/ 
            k=0; 
            rece[k]=RCREG; /*Read received data*/ 
            if(OERR==1) { /*If there is an overflow error, process it*/ 
    CREN=0; 
                CREN=1; 

if(rece[k]==0x00) break;/*"0" is a synchronization character. Only when "0" is received, the following reception is performed*/ 

for(k=1;k<8;k++){ 
while(1){ 
                if(RCIF==1) break; 
} /*Wait for receiving data*/ 
            rece[k]=RCREG; /*Read received data*/ 
            if(OERR==1) { /*If there is an overflow error, process it*/ 
    CREN=0; 
    CREN=1; 

            rece[k]=rece[k]&0x0F;/*Shield the high bit to prevent interference*/ 

        CREN=0;  
        display(); /*Display the received data*/ 
        for(i=65535;--i; )continue; 
        for(i=65535;--i; )continue;/*Give a certain delay before the next round of reception*/ 



8.7 Communication between MCU and PC 
1 PC programming 
PC is programmed using Toubr C. The program is as follows: 
#include 
#define port 0x3f8 /*Use serial port 1 for communication*/ 
int ch[15]; 
main () 

  int a; 
  int i,j; 
  int b[6]={88,15,38,26,20,0}; 
  char c; 
  clrscr(); 
  outportb(port+3,0x80); /*Prepare to set the baud rate*/ 
  outportb(port,0x0C); /*Set the baud rate to 9600bps*/ 
  outportb(port+1,0x00); 
  outportb(port+3,0x03); /*8-bit data, no parity check, 1 stop bit*/ 
  outportb(port+1,0x00); /*Turn off interrupt*/ 
  inportb(port+5); /*Read the line status register once to reset it*/ 
  for(;;){ 
    printf("\t\tsend data or receive data: (s or r?)\n\n\n"); 
    c=getchar(); 
     switch(c) { 
       case 's': 
       case 'S': { 
         while(!(inportb(port+5)&0x20));/*Wait if the send holder is full*/ 
         outportb(port,0x01); /*Otherwise send data 01 to notify the microcontroller to prepare for receiving*/ 
         for(i=0;i<6;i++){ /*Send 6 data in total*/ 
          a=b[i]; 
          while(!(inportb(port+5)&0x20)) delay(100);/*Send holder is full, wait*/ 
          outportb(port,a); /*Send a*/ 
          printf("%d\n",a); /*Display a*/ 
          while(!(inport(port+5)&1)); /*Receive data sent back by the microcontroller*/ 
          ch[i]=inport(port); /*save*/ 
  } 
      delay(10); 
      for(j=0; j<8; j++) printf("\n%d\n", ch[j]); /*display the received echo data*/ 
      getch(); 
      break; 
    } 
    case'r': /*receive data*/ 
    case'R':{ 
       while(!(inportb(port+5)&0x20)); 
        outportb(port,0x02); /*send data 02, notify the MCU to send data*/ 
        for(j=0; j<9; j++) { /*receive 9 data in total*/ 
         while(!(inportb(port+5)&1)); 
        ch[j]=inportb(port); 
        } 
        for(j=0; j<9; j++) printf("\n %d\n", ch[j]); 
        getch(); 
        break; 
        } 
     } 
   } 
 } 


Reference address:Universal application program for asynchronous communication between two single-chip microcomputers

Previous article:PIC Microcontroller Software Programming Tips
Next article:pic16f877a c language clock program

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号