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;
}
}
}
}
Previous article:PIC Microcontroller Software Programming Tips
Next article:pic16f877a c language clock program
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
- Hey guys, can you tell me what DFX design is?
- Condensing exclusive technology - Nichicon's on-board charger solution
- Relationship between 12-pulse rectifier input current harmonics and control angle
- Why is the instruction set of the microcontroller RISC but the structure is von Neumann structure? Shouldn't it be Harvard structure?
- Computer Architecture: An Embedded Approach
- The base address can already be distinguished, what is the point of adding an identical offset address?
- EEWORLD University ---- Haiwell IoT Cloud HMI Quick Start Video Tutorial
- MSP430FR6043 Ultrasonic Sensing Evaluation Module
- What are IoT programming tools?
- The pipes kept exploding, and finally the power supply was blown away.