3947 views|0 replies

1379

Posts

0

Resources
The OP
 

Application of general synchronous/asynchronous communication (2) [Copy link]

Single chip dual machine synchronous communication

1 PIC1 microcontroller programming (master control sending)

#include <pic.h>

/* This program realizes the synchronous communication function of two MCUs, which is the main control sending part. The program will display after power on.

* Corresponding characters indicate that the system is working properly. The sent data will be displayed after sending */

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

{

; For detailed program statements, please refer to Section 8.5 of this chapter

}

/* Subroutine to assign initial value 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 baud rate of the transmission to about 9600 bits / second */

TXSTA=0X90 ; /* Select master control mode */

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)

{

; For detailed program statements, please refer to Section 8.5 of this chapter

}

/* 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 */

}

/* Display subroutine, display 8 digits */

void display1()

{

RA5=0 ; /* Ready to latch */

for(k=0 ; k<8 ; k++){

data=0xf9 ; /* Displaying "1" means the system is working normally */

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 */

fuzhi()/* Assign the initial value to the sending array */

sciint() ; /*SCI component initialization */

di() ; /* interrupt disable */

TXEN=1 ; /* Transmitting enabled */

display1()/* Display the corresponding characters, indicating 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 */

} /* Send in loop */

}

2 MCU PIC2 programming (slave reception)

#include <pic.h>

/* This program realizes the synchronous communication function of two MCUs , and is the slave receiving part. It displays the received data

* Displayed on 8 LEDs * /

unsigned char rece[8] ; /* define an array to store received data */

unsigned char k , data ; /* define general register */

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 transfer 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 ; /* 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 */

for(k=0 ; k<8 ; k++) rece[k]=0x03 ;

display() /* Display data indicating normal operation of the system */

while(1) {

while(1){

CREN=1 ; /* Enable 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 , handle it */

CREN=0 ;

CREN=1 ;

}

if(rece[k]==0x00) break ; /*0For synchronization characters , only when "0The following reception is performed only when

}

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 , handle it */

CREN=0 ;

CREN=1 ;

}

rece[k]=rece[k]&0x0F; /* Shield the high bit to prevent interference */

}

CREN=0 ;

display()/* Display received data */

for(i=65535 ; --i ; )continue ;

for(i=65535 ; --i ; ) continue ; /* Give a certain delay before the next round of receiving */

}

This post is from MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list