PIC microcontroller communication routine

Publisher:Jinyu521Latest update time:2017-12-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. PIC1 microcontroller programming (sending part) 
LIST P=16F877 
#INCLUDE P16F876.INC 
CB LOC K 0X24; reserve three bytes for display 
COUNT; used as counter or register 
ENDC 
ORG 0X0000; program reset entry

NOP 
START GOTO MAIN 
ORG 0X20 
MAIN MOVLW 0X30 ; Initialize RAM contents 
MOVWF FSR ; Start from unit 30H 
MOVLW 0X30 ; Assign value 30H to unit 30H 
MOVWF COUNT 
INTRAM MOVF COUNT, 0 ; Assign 30H to 7FH to units 30H to 7FH 
MOVWF INDF 
INCF COUNT, 1 
INCF FSR, 1 
BTFSS COUNT, 7 
GOTO INTRAM 
BSF STATUS, RP0 ; Initialize SCI components 
MOVLW 0X19 ; Set the transmission baud rate to about 9600 bps 
MOVWF SPBRG 
MOVLW 0X04 ; Select asynchronous high-speed mode to transmit 8-bit data 
MOVWF TXSTA 
BCF STATUS, RP0 
MOVLW 0X80 ; Enable synchronous serial port to work 
MOVWF RCSTA 
BSF STATUS, RP0 
BSF TRISC, 7 ; Set RC6 and RC7 to input mode and disconnect the external circuit  BSF
TRISC, 6 
BCF STATUS, RP0 
MOVLW 0X30 ; Send 30H as the synchronization character 
MOVWF FSR 
MOVF INDF, 0 
MOVWF TXREG ; Write the data to be sent into the transmit buffer TXREG 
BSF STATUS, RP0 
BSF TXSTA, TXEN ; Sending is allowed 
BCF STATUS, RP0 
BSF RCSTA, CREN ; Receive data is allowed 
LOOPTX BTFSS PIR1, RCIF ; Wait for the response byte of PIC2 
GOTO LOOPTX 
MOVF RCREG, 0 ; Read the response byte and clear RCIF 
LOOPTX1 BTFSS PIR1, TXIF ; Send the next byte 
GOTO LOOPTX1 
INCF FSR 
MOVF INDF, 0 
MOVWF TXREG 
BTFSS FSR, 7 ;Have the contents of units 30H~7FH been sent? 
GOTO LOOPTX ;No, continue to send the next byte 
BSF STATUS, RP0 ;If yes, stop sending 
BCF TXSTA, TXEN 
BCF STATUS, RP0 ;Data sending completed 
CALL  LED  ;Call the display subroutine to display the sent data 
END ;Program completed 
2. Microcontroller PIC2 programming (receiving part) 
LIST P=16F876 
#INCLUDE P16F876.INC 
CBLOCK 0X24 
COUNT 
ENDC 
ORG 0X0000 
NOP 
START GOTO MAIN 
MAIN BSF STATUS, RP0 ;Initialization program is the same as the sending subroutine 
MOVLW 0X19 ;Baud rate setting is the same as PIC1 
MOVWF SPBRG 
MOVLW 0X04 ;Asynchronous high-speed transmission 
MOVWF TXSTA 
BCF STATUS, RP0 
MOVLW 0X80 ;Serial port operation enable 
MOVWF RCSTA 
BSF STATUS, RP0


BSF TRISC, 7; Isolate from external circuit 
BSF TRISC, 6 
BCF STATUS, RP0 
MOVLW 0X30; Store the sent data starting from unit 30H 
MOVWF FSR 
BSF RCSTA, CREN; Receive enable 
BSF STATUS, RP0 
BSF TXSTA, TXEN; Send enable 
BCF STATUS, RP0 
WAIT BTFSS PIR1, RCIF; Wait for receiving data 
GOTO WAIT 
MOVF RCREG, 0; Read data 
MOVWF INDF; Store the received response byte into PIC2's RAM 
INCF FSR 
MOVWF TXREG; Send response byte 
LOOPTX BTFSS PIR1, TXIF; Wait for writing to be completed 
GOTO LOOPTX 
BTFSS FSR, 7; Are all data received? 
GOTO WAIT1; No, continue to receive other data 
BCF RCSTA, RCEN; After receiving, turn off the receiving and sending data permission 
BSF STATUS, RP0 
BCF PIE1, TXEN 
BCF STATUS, RP0 
CALL LED; Call the display subroutine to display the received data 
END; Program completed 



 

MCU dual-machine synchronous communication 
1. MCU PIC1 programming (master control sends) 
LIST P=16F876 
#INCLUDE P16F876.INC 
CBLOCK 0X24; Reserve three bytes for display 
COUNT; Used as counter or temporary register 
ENDC 
ORG 0X0000; Program reset entry 
NOP 
START GOTO MAIN 
ORG 0X0100 
MAIN MOVLW 0X30 
MOVWF FSR; The following will initialize the RAM content starting from unit 30H 
MOVLW 0X30; Assign the value 30H to unit 30H 
MOVWF COUNT 
INTRAM MOVF COUNT, 0; Assign 30H~7FH to units 30H~7FH 
MOVWF INDF 
INCF COUNT, 1 
INCF FSR, 1 
BTFSS COUNT, 7 
GOTO INTRAM 
BSF STATUS, RP0 ; Initialize the SCI component 
MOVLW 0X19 ; Set the transmission baud rate to about 9600 bps 
MOVWF SPBRG 
MOVLW 0X94 ; Select synchronous high-speed mode to transmit 8-bit data 
MOVWF TXSTA 
BCF STATUS, RP0 
MOVLW 0X80 ; Enable the synchronous serial port to work 
MOVWF RCSTA 
BSF STATUS, RP0 
BSF TRISC, 7 ; Set RC6 and RC7 to input mode and disconnect the external circuit 
BSF TRISC, 6 
BSF STATUS, RP0 
MOVLW 0X30 ; Transfer the content starting from unit 30H to PIC2 
MOVWF FSR 
MOVF INDF, 0 
MOVWF TXREG ; Write the data to be sent into the transmit buffer TXREG 
BSF STATUS, RP0 
BSF TXSTA, TXEN ; Sending permission 
BCF STATUS, RP0 
TX1 BTFSS PIR1, TXIF ;Wait for the previous data to be written 
GOTO TX1 
INCF FSR ;Prepare to send the next data 
MOVF INDF, 0 
MOVWF TXREG ;Write the new data into TXREG 
BTFSS FSR, 7 ;Judge whether the contents of all 30H~7FH units have been sent? 
GOTO TX1 ;If not, continue to send other bytes 
TX2 BTFSS PIR1, TXIF ;Wait for all the data required to be sent to be sent, and then 
GOTO TX2 ;Write a byte to TXREG so that the last data can be sent smoothly 
MOVWF TXREG 
NOP ;After a delay of several microseconds, turn off the transmission permission 
NOP 
NOP 
NOP 
NOP 
BSF STATUS, RP0 
BCF TXSTA, TXEN 
BCF STATUS, RP0 
CALL LED ;Call the display subroutine to display the sent data 
END ;Program completed

2. PIC2 microcontroller programming (slave reception) 
LIST P=16F876 
#INCLUDE P16F876.INC 
CBLOCK 0X24 
COUNT 
ENDC 
ORG 0X0000 
NOP 
START GOTO MAIN 
ORG 0X0100 
MAIN BSF STATUS,RP0 
MOVLW 0X10 ;Select synchronous slave mode 
MOVWF TXSTA 
BCF STATUS,RP0 
MOVLW 0X80 ;Serial port enable 
MOVWF RCSTA 
BSF STATUS,RP0 
BSF TRISC,7 ;Disconnect with external circuit 
BSF TRISC,6 
BCF STATUS,RP0 
MOVLW 0X30 ;Store received data from unit 30H 
MOVWF FSR 
BSF RCSTA,CREN ;Receive enable 
WAIT BTFSS PIR1,RCIF ;Wait for reception 
GOTO WAIT 
MOVF RCREG, 0; read received data 
MOVWF INDF; store received data into PIC2 RAM 
INCF FSR 
BTFSS FSR, 7; have all data been received? 
GOTO WAIT; no, continue to receive other bytes 
NOP; after a delay of several microseconds, clear the receive enable bit 
NOP 
NOP 
NOP 
BCF RCSTA, RCEN 
CALL LED; call the display subroutine to display the received data 
END; program completed 



 

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 9 600 bps*/ 
outportb(port 1,0x00); 
outportb(port 3,0x03); /*8 data bits, parity check, 1 stop bit*/ 
outportb(port 1,0x00); /*Disable interrupt*/ 
inportb(port 5); /*Read the line status register once to reset it*/ 

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 MCU 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 MCU*/ 
ch[i]=inport(port); /*Save*/ 

delay(10); 
for(j=0;j<8;j ) /*Display the received echo data*/ 
printf("\n%d\n",ch[j]); 
getch(); 
break; 

case'r': /*Receive data*/ 
case'R': 

while(!(inportb(port 5)&0x20)); 
outportb(port,0x02); /*Send data 02 to 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; 




2. MCU Programming 
LIST P=16F876 
#INCLUDE P16F876.INC 
CBLOCK 0X24 
COUNT 
TEMP 
ENDC 
ORG 0X0000 
NOP 
START GOTO MAIN 
ORG 0X020

MAIN MOVLW 0X30 ;Initialize RAM unit 
MOVWF FSR 
BCF STATUS,RP0 
MOVLW 0X22 
MOVWF COUNT 
INTRAM MOVF COUNT,0 
MOVWF INDF 
INCF COUNT,1 
INCF FSR,1 
BTFSS FSR,7 
GOTO INTRAM 
BANKSEL TXSTA ;Set the baud rate to 9 600 bps 
MOVLW 0X19 
MOVWF SPBRG ;Asynchronous high-speed mode 
MOVLW 0X04 
MOVWF TXSTA 
BCF STATUS,RP0 
MOVLW 0X80 ;Enable serial port 
MOVWF RCSTA 
MOVLW 0X30 
MOVWF FSR 
BSF RCSTA,CREN ;Receive enable 
BSF STATUS,RP0 
BSF TXSTA,TXEN ;Transmit enable 
BCF STATUS,RP0 
WAIT BTFSS PIR1,RCIF ; Receive PC command 
GOTO WAIT 
MOVF RCREG, 0 
MOVWF TEMP 
MOVWF INDF 
INCF FSR 
DECFSZ TEMP ; If the received data is 1, receive 
GOTO TXW ; Otherwise send data 
MOVLW 0X06 
MOVWF TEMP 
WAIT1 BTFSS PIR1, RCIF ; Receive one data 
GOTO WAIT1 
MOVF RCREG, 0 
MOVWF COUNT 
MOVWF TXREG 
LOOPTX BTFSS PIR1, TXIF ; Send the received data back to PC 
GOTO LOOPTX 
MOVF COUNT, 0 
MOVWF INDF 
INCF FSR 
DECFSZ TEMP 
GOTO WAIT1 
GOTO OVER ; Processing completed 
TXW MOVLW 0X09 ; Send 9 data to PC 
MOVWF TEMP 
MOVLW 0X30 
MOVWF FSR 
TXW0 MOVF INDF, 0 
MOVWF TXREG 
INCF FSR 
TXW1 BTFSS PIR1, TXIF 
GOTO TXW1 
DECFSZ TEMP 
GOTO TXW0 
OVER BCF RCSTA, CREN; Communication task completed, enable receiving and sending 
BSF STATUS, RP0 
BCF TXSTA, TXEN 
BCF STATUS, RP0 
CALL LED; Call the display subroutine to display the sent (or received) data 
END; Program completed 


Reference address:PIC microcontroller communication routine

Previous article:MCD2-DEMO digital tube display PICC-18 example (suitable for beginners)
Next article:PIC digital clock

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号