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
Previous article:MCD2-DEMO digital tube display PICC-18 example (suitable for beginners)
Next article:PIC digital clock
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- ulab 1.0.0 released
- Nexperia Award Live | Introduction and application of automotive-grade power MOSFET and GaN devices in electric vehicles. Registration is open
- Switching Power Supply Interest Group: Mission 01
- Can a large capacitor be connected in parallel at the output of the LDO to extend the life of the subsequent device?
- How to use J-flash to program the MCU of Huada Semiconductor
- About Analog Switches
- [National Technology N32 MCU Development Package] --N32G432 Series
- MSP430 MCU Example 18 - Timer A generates 4-channel periodic signals
- Low-cost wireless RF communication chip selection
- Does it cost a lot to buy an on-load tapchanger tester?