STC12C5A 硬件SPI 控制 NRF24L01
最近在做一个无线对讲系统,无线通信模块儿为24L01.主控为STC12C5A60S2.24L01为spi通信,用12C5A模拟了SPI读写函数,无线正常收发,函数如下:
uchar SPI_RW(uchar byte)
{
uchar bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI = (byte & 0x80); // output 'byte', MSB to MOSI
byte = (byte << 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
byte |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(byte); // return read byte
}
我想,既然12C5A有硬件SPI,就应该用上,于是写了SPI函数如下:
void spi_init(unsigned char n) //STC 硬件SPI初始化,n为分频数,影响SPI的通信速度
{
switch(n)
{
case 4: SPCTL=0xfc; break;
case 16:SPCTL=0xfd; break;
case 64:SPCTL=0xfe; break;
case 128:SPCTL=0xff; break;
}
SPSTAT=0xc0;
}
uchar SPI_RW(uchar x)
{
SPDAT=x;
while(!(SPSTAT&0x80));
SPSTAT=0xc0;
return SPDAT;
}
SPI初始化CPUclk的128分频,大约170k左右,但是通信不成功……
请大师帮忙分析一下原因可能在哪呢? 再降速试试 经过试探,最终发现是SPI配置有误,改为case 4: SPCTL=0xd0; break;
即可 devcang 发表于 2012-10-27 12:18 static/image/common/back.gif
再降速试试
改为case 4: SPCTL=0xd0; break;即可
学习啦! 学习啦!有空也想试试!
页:
[1]