使用SPI直接扫描方式和中断收发的方式与AD芯片连接时,SPI直接扫描数据正确,而采用SPI中断接收的数据要小一半,是什么原因?比如直接SPI的值是64000,而中断SPI的值是32000
AD使用的是ADS8230,16位数据取3次SPI数据的第7到22位
中断接收的代码:
void SPI1_IRQHandler(void)
{
static u8 spibuf[4];
static u8 spicount=0;
u32 spibuffer;
if( SPI_I2S_GetITStatus( SPI1, SPI_I2S_IT_RXNE ) == SET ) //接收中断
{
spibuf[spicount] = SPI_I2S_ReceiveData(SPI1);
spicount++;
if( spicount >= 3 )
{
CS_ADS_H;
spicount = 0;
spibuffer = (spibuf[0]<<6);
spibuffer = (spibuffer<<2)|spibuf[1];
spibuffer = (spibuffer<<6)|(spibuf[2]>>2);
Adc16bit = spibuffer;
}
}
else //发送空中断
{
if( spicount == 0 )
CS_ADS_L;
SPI_I2S_SendData(SPI1,0);
}
}
直接SPI扫描的代码:
unsigned int Read_ads( void )
{
u8 spibuf[4];
u32 spibuffer;
CS_ADS_L;
spibuf[0] = SPI_WriteByte( 0 );
spibuf[1] = SPI_WriteByte( 0 );
spibuf[2] = SPI_WriteByte( 0 );
CS_ADS_H;
spibuffer = spibuf[0]<<6;
spibuffer = (spibuffer<<2) | spibuf[1];
spibuffer = (spibuffer<<6) | spibuf[2]>>2;
return (u16)spibuffer;
}
unsigned char SPI_WriteByte(unsigned char data)
{
unsigned char Data = 0;
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET); //等待发送缓冲区空
SPI_I2S_SendData(SPI1,data); // 发送一个字节
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET); //等待是否接收到一个字节
Data = SPI_I2S_ReceiveData(SPI1); // 获得该字节
return Data; // 返回收到的字节
}
各位大虾帮忙看下到底是什么情况? |