想实现计算机通过串口向主单片机发送一串数据,主单片机串口每收到一个字节就立刻把收到的通过spi发到从单片机;同时主单片机把收到的从单片机字节发送到计算机,求求各位大佬帮帮孩子,为什么实现不了
#include "STC15F2K60S2.H"
#include "intrins.h"
#define MASTER
#define FOSC 18432000L
#define BAUD (256-FOSC/32/115200)
#define SPIF 0x80
#define WCOL 0x40
#define SSIG 0x80 //SPCTL.7
#define SPEN 0x40 //SPCTL.6
#define DORD 0x20 //SPCTL.5
#define MSTR 0x10 //SPCTL.4
#define CPOL 0x08 //SPCTL.3
#define CPHA 0x04 //SPCTL.2
#define SPDHH 0x00 //CPU_CLK/4
#define SPDH 0x01 //CPU_CLK/16
#define SPDL 0x02 //CPU_CLK/64
#define SPDLL 0x03 //CPU_CLK/128
sbit SPISS = P1^6;
void UartInit(void) //4800bps@12.000MHz
{
SCON = 0x50; //8???,?????
AUXR |= 0x40; //???1???Fosc,?1T
AUXR &= 0xFE; //??1?????1???????
TMOD &= 0x0F; //?????1?16???????
TL1 = 0x8F; //??????
TH1 = 0xFD; //??????
ET1 = 0; //?????1??
TR1 = 1; //?????1
}
void InitSPI()
{
SPDAT = 0;
SPSTAT = SPIF | WCOL;
#ifdef MASTER
SPCTL = SPEN | MSTR;
#else
SPCTL = SPEN;
#endif
}
void SendUart(unsigned char dat)
{
while (!TI);
TI=0;
SBUF = dat;
}
unsigned char RecvUart()
{
while (!RI);
RI = 0;
return SBUF;
}
unsigned char SPISwap(unsigned char dat)
{
#ifdef MASTER
SPISS = 0;
#endif
SPDAT = dat;
while (!(SPSTAT&SPIF));
SPSTAT = SPIF|WCOL;
#ifdef MASTER
SPISS = 1;
#endif
return SPDAT;
}
void main()
{
UartInit();
InitSPI();
while(1)
{
#ifdef MASTER
SendUart(SPISwap(RecvUart()));
#else
ACC=SPISwap(ACC);
#endif
}
}
|