最近在用spi调试TI 的ADS芯片,读12个寄存器,出现:第一个数据为0,后续11个数据正确。
这样读寄存器的,先发送两个opcode,然后再读取寄存器数据。
[mw_shl_code=c,true] val1 = SPI1_Send_byte(SPI_Tx_buf[0]);
delay_us(10);
val2 = SPI1_Send_byte(SPI_Tx_buf[1]);
delay_us(10);
val3 = SPI1_Receive_byte();
delay_us(15);[/mw_shl_code]
SPI 为全双工,8bit,CPOL=0,CPHA=1,主机模式。
关于读写数据函数,
[mw_shl_code=c,true]uint16_t SPI1_Send_byte(uint16_t data)
{
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);
return SPI_I2S_ReceiveData(SPI1);
}
uint16_t SPI1_Receive_byte(void)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0x00);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}[/mw_shl_code]
我尝试,短接MOSI和MISO引脚,按一下测试:
[mw_shl_code=c,true]uint8_t rev_buff[20];
uint8_t sed_buff[20]= {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
for(i=0;i<15; i++)
{
rev_buff = SPI1_Send_byte(sed_buff);
delay_ms(1);
}[/mw_shl_code]
接收到的数据为:
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14
这啥情况? 路过大侠,帮忙回答解决下问题啊。
关于SPI的配置:
[mw_shl_code=c,true] SPI_I2S_DeInit(SPI1);
SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low ;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft ;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE);[/mw_shl_code]
|