PIC single chip dual machine asynchronous communication (dhd0)
1 PIC1 microcontroller programming (sending part)
#include
/*This program implements the asynchronous communication function between two MCUs. This program is the sending part*/
unsigned char tran[8]; /*define an array to store the sent data*/
unsigned char k, data; /*define general register*/
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, and "74HC595, when
*When SCLK jumps from low to high, the characteristics of the serial input register correspond to */
TRISC=0xD7; /*SDO pin is output, SCK pin is output*/
TRISA5=0; /*RA5 pin is set as output to output display latch signal*/
}
/*Assign initial value to array subroutine*/
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; /*Enable 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 transfer subroutine*/
void SPILED(data)
{
SSPBUF=data; /*Start sending*/
do {
;
}while(SSPIF==0);
SSPIF=0;
}
/*Display subroutine, display 8 digits*/
void display()
{
RA5=0; /*Ready to latch*/
for(k=0;k<8;k ) {
data=tran[k];
data=table[data]; /*get the displayed segment code*/
SPILED(data); /*Send display segment code*/
}
RA5=1; /*Finally, a latch signal is given to indicate that the task is completed*/
}
/* Main program */
main()
{
SPIINIT();
fuzhi(); /*Assign initial value to the array*/
sciint(); /*SCI component initialization*/
di(); /*interrupt disable*/
TXEN=1; /*Send enabled*/
CREN=1; /*Receive data allowed*/
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 response byte, clear RCIF*/
}
display(); /*Display the sent data*/
while(1){
;
}
}[page]
2 PIC2 microcontroller programming (receiving part)
#include
/*This program implements the asynchronous communication function between two MCUs. This program is the receiving part and displays the received data on 8* LEDs*/
unsigned char rece[8];/*define an array to store received data*/
unsigned char k, data;/*define general register*/
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 procedure
}
/*SCI component initialization subroutine*/
void sciint()
{
SPBRG = 0X19; /*Baud rate setting is the same as PIC1, about 9 600 bits/second*/
TXSTA=0X04; /*Asynchronous high-speed transmission*/
RCSTA=0X80; /*Serial port enable*/
TRISC6=1;
TRISC7=1; /*Set RC6 and RC7 to input mode, and present high impedance to the outside*/
}
/*SPI data transfer subroutine*/
void SPILED(data)
{
; For detailed statements, see the sending procedure
}
/*Display subroutine, display 4 digits*/
void display()
{
RA5=0; /*Ready to latch*/
for(k=0;k<8;k ){
data=rece[k];
data=table[data]; /*get the displayed segment code*/
SPILED(data); /*Send display segment code*/
}
RA5=1; /*Finally, a latch signal is given to indicate that the task is completed*/
}
/* Main program */
main()
{
SPIINIT(); /*spi display initialization*/
sciint(); /*SCI component initialization*/
di(); /*interrupt disable*/
CREN=1; /*Receive enabled*/
TXEN=1; /*Send 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 the received data*/
while(1){
;
}
Previous article:Application of PIC microcontroller in aircraft refueling system
Next article:PIC microcontroller CRC check program
- 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
- Patent application
- 【Portable Programmable Meter】Project Submission
- There is a basic question about AD9
- After current sampling, the inverted ratio is amplified by 2 times (U9A), and then the output is connected to the form of a hysteresis comparator (U9B), which is...
- DSP-BIOS usage from environment to building the first project
- File status after dup and fork functions are executed
- EEWORLD University Hall----Live Replay: Explore the Black Technology of Network Cameras with Infineon
- Tesla Coil 2 "Low Power Discharge + Detailed Pictures" Welcome to guide
- Problems with the multi-function Bluetooth remote control
- Problems in FPGA Experiments