程序如下,PC收到的数据都是0XFF,正常来讲应该是0X11的才对,望各位大侠看看程序中哪里出了问题,搞了两天没找出问题!谢谢~~~
//#define MASTER
sfr AUXR = 0X8E;
sfr SPSTAT = 0X84;
#define SPIF 0X80 //SPSTAT.7
#define WCOL 0X40 //SPSTAT.6
sfr SPCTL = 0X85;
#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 SPEED_4 0X00 //CPU_CLK/4
#define SPEED_16 0X01 //CPU_CLK/16
#define SPEED_64 0X02 //CPU_CLK/64
#define SPEED_128 0X03 //CPU_CLK/128
sfr SPDAT = 0X86;
#define ESPI 0X08; //AUXR.3
sbit SPISS = P1^4;
void UartInit() //波特率为9600
{
TMOD=0x20; //定时器1 8位自动重装
TH1=0xfd; //波特率为9600
TL1=0xfd;
TR1=1; //开定时器1
REN=1; //允许串行接收
SM0=0; //串口工作模式1,8位自动重装,波特率可变
SM1=1;
PCON=0x80; //波特率加倍
EA=1; //开总中断
ES=1; //开串行中断
}
void UartSendBYTE( uc8 dat) //串口发送一个字节
{
SBUF=dat;
while(!TI);
TI=0;
}
uc8 RecvUart()
{
while(!RI);
RI=0;
return SBUF;
}
void SpiInit()
{
//SSIG = 1; //忽略SS脚
//SPEN = 1; //允许SPI工作
//DORD = 0; //先传高位MSB
//MSTR = 1; //设置单片机为主机
SPCTL=0XD0; //SPI Control Register SSIG SPEN DORD MSTR CPOL CPHA SPR1 SPR0 1101,0000
SPSTAT=0XC0; //清除中断标志,和写冲突标志,注意是对应位写1才能清零
}
uc8 writebyte(uc8 temp)
{
SPDAT = temp;
while(!(SPSTAT & 0x80)); //判断SPI传输是否完成
SPSTAT = 0xC0; //清除中断标志,和写冲突标志,注意是对应位写1才能清零
return SPDAT;
}
uc8 readbyte()
{
idata uc8 temp;
SPDAT = 0x00;
while(!(SPSTAT & 0x80)); //判断SPI传输是否完成
temp = SPDAT;
SPSTAT = 0xC0; //清除中断标志,和写冲突标志,注意是对应位写1才能清零
return temp;
}
void delay()
{
u16 i;
u16 j;
for(i=0;i<110;i++)
for(j=0;j<1000;j++);
}
void main()
{
UartInit();
SpiInit();
while(1)
{
UartSendBYTE(writebyte(0x11));
delay();
}
} |