[mw_shl_code=c,true]u8 SPI1_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)//等待发送区空
{
retry++;
if(retry>200)
return 1; //wrong
}
SPI_I2S_SendData(SPI1, TxData); //通过外设SPIx发送一个数据
retry=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位
{
retry++;
if(retry>200)
return 1;
}
return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据
}[/mw_shl_code]
[mw_shl_code=c,true] //write 4 register starting at address 01H(CONFIG1)
CS_LOW;
spi_buffer[0] = 0x41; //
spi_buffer[1] = 0x03;
spi_buffer[2] = 0x96; //CONFIG1, 250SPS
spi_buffer[3] = 0xc0; //CONFIG2
spi_buffer[4] = 0xe0; //CONFIG3
spi_buffer[5] = 0x00; //LOFF
for(i=0;i<6;i++)
{
while(SPI1_ReadWriteByte(spi_buffer
) == 1);
}
CS_HIGH;
//Read from 4 registers, starting at 0x01
//0x96, 0xC0, 0xE0, 0x00, //Global Settings across Channels 01h-04h(4)
CS_LOW;
spi_buffer[0] = 0x21;
spi_buffer[1] = 0x03;
for(i=0;i<2;i++)
{
while(SPI1_ReadWriteByte(spi_buffer) == 1);
}
for(i=0;i<4;i++)
{
reg = SPI1_ReadWriteByte(Dummy_Byte);
}
for(i=0;i<4;i++)
printf("
reg[%02x] = 0x%02x
",i,reg);
CS_HIGH;[/mw_shl_code]
如代码,采用例程里的SPI1_ReadWriteByte()函数,首先往四个寄存器里写值,然后直接读取寄存器的值。可是结果不对,可能是什么原因呢?
一周热门 更多>