void sys_spiInit(void)
{
RCC->APB2ENR |= 1<<12; //使能SPI1时钟
SPI1->CR1|=0<<11;//8bit数据格式
SPI1->CR1|=0<<10;//全双工模式
SPI1->CR1|=1<<9; //软件nss 管理
SPI1->CR1|=1<<8;
SPI1->CR1|=0<<7; //MSBFirst
SPI1->CR1|=7<<3; //设置时钟Fsck = Fcpu/256
SPI1->CR1|=1<<2; //SPI 主机
SPI1->CR1|=0<<1; //空闲模式下 SCK为 0 CPOL=0
SPI1->CR1|=0<<0; //数据采样从第1个时间边沿开始
SPI1->CR1|=1<<6; //使能SPI1
}
u8 sys_spiSendRead(u8 byte)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, byte);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1);
}
uint16_t ad7694_ReadData(void)
{
unsigned long DataL = 0;
unsigned long DataH = 0;
unsigned long Ret = 0;
GPIO_SetBits(GPIOA,GPIO_Pin_7);
delay_us(6);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
DataH = sys_spiSendRead(0xFF);
DataH = DataH << 8;
DataL = sys_spiSendRead(0xFF);
Ret = DataH | DataL;
return(Ret) ;
}
AD7694是一个非常哈的A/D转换芯片,加上ADR431基准能够实现非常高的精度采集!(PS:STMF405RG单片机如果使用ST官方库 会导致SPI时钟信号木有,407却可以,所以我直接用寄存器写的) |