PIC single-chip dual-machine asynchronous communication program

Publisher:平凡幸福Latest update time:2012-10-30 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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){

;

}

Reference address:PIC single-chip dual-machine asynchronous communication program

Previous article:Application of PIC microcontroller in aircraft refueling system
Next article:PIC microcontroller CRC check 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号