新手入门
- 积分
- 27
- 金钱
- 27
- 注册时间
- 2014-9-29
- 在线时间
- 0 小时
|
5金钱
PA7连接 AD7190的DIN引脚
PA8连接AD7190的CS
PA5 连接AD7190的SCK
PA6连接AD7190的DOUT
SPI写入正常,但是读出是数据整体向右移了一位,如AD7190配置寄存器实际值0,80,88,串口输出的数据为128,40,44。补进的最高为DOUT的值
AD7190的时序图
int main(void)
{
SystemInit();
NVIC_Configuration();
bsp_InitUart(); //配置串口
bsp_InitLed(); //配置LED
bsp_InitButton(); //配置GPIO
AD7190_Init() ;
while(1)
{
Start_AD7190();
}
}
void AD7190_Init(void)
{
GPIO_Configuration();
SPI_Tx(); //设置flag_spi的初始值
CLR_CS; //工作在三线模式
SPI_SendByte(T,RESET);
SPI_SendByte(T,RESET);
SPI_SendByte(T,RESET);
SPI_SendByte(T,RESET);
SPI_SendByte(T,RESET);
Delay(50); //复位后,至少等待500us
SPI_SendByte(T,STATUS_REG|READ);
buf[0]=SPI_SendByte(R,DUMMY);
//_______写配置,斩波禁用,选用外部基准电压,单极性模式_______
//_______使能缓冲器,电压增益1(输入范围 +5v),选择AIN1,AINCOM通道______
SPI_SendByte(T,CONFIGURATION_REG|WRITE);
SPI_SendByte(T,REF1);
SPI_SendByte(T,AIN2|AIN3);
SPI_SendByte(T,GAIN_1_5|SINGLE_POLARITY);
SPI_SendByte(T,CONFIGURATION_REG|READ);
buf[2]=SPI_SendByte(R,DUMMY);
buf[1]=SPI_SendByte(R,DUMMY);
buf[0]=SPI_SendByte(R,DUMMY);
printf("配置寄存器 %d \r\n", buf[2]);
printf(" %d \r\n", buf[1]);
printf(" %d \r\n", buf[0]);
SPI_SendByte(T,CONFIGURATION_REG|WRITE);
SPI_SendByte(T,REF1);
SPI_SendByte(T,AIN2|AIN3);
SPI_SendByte(T,GAIN_1_5|SINGLE_POLARITY);
SPI_SendByte(T,CONFIGURATION_REG|READ);
buf[2]=SPI_SendByte(R,DUMMY);
buf[1]=SPI_SendByte(R,DUMMY);
buf[0]=SPI_SendByte(R,DUMMY);
printf("配置寄存器 %d \r\n", buf[2]);
printf(" %d \r\n", buf[1]);
printf(" %d \r\n", buf[0]);
SPI_SendByte(T,STATUS_REG|CONTINUOUS_READ);
buf[0]=SPI_SendByte(R,DUMMY);
//_______写模式,启动连续转换,输出数据速率为50Hz______
//_______启用内部4.92MHz晶振_______
SPI_SendByte(T,MODE_REG|WRITE);
SPI_SendByte(T,CONTINUOUS_CONVERT|IN_OSC);
SPI_SendByte(T,0x00);
SPI_SendByte(T,SPEED_1600);
SPI_SendByte(T,MODE_REG|READ);
buf[2]=SPI_SendByte(R,DUMMY);
buf[1]=SPI_SendByte(R,DUMMY);
buf[0]=SPI_SendByte(R,DUMMY);
printf("模式寄存器 %d \r\n", buf[2]);
printf("%d \r\n", buf[1]);
printf("%d \r\n", buf[0]);
}
u8 SPI_SendByte(unsigned char flag,u8 byte) //因为时钟信号只有在发的时候才产生,所以收数据的时候必须发数据(无效的数据)
{
if(flag==T_flag)
{
if(flag_spi==Tx)
{
while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIy, byte);
return 0;
}
else//SPI由接收状态转成发送状态
{
SPI_Tx();
while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIy, byte);
return 0;
}
}
if(flag==R_flag)
{
if(flag_spi==Full)
{
while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIy, byte);
while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPIy);
}
else//SPI由发送状态转成接收状态
{
SPI_Full();
while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIy, byte);
buf[4]=SPI_I2S_ReceiveData(SPIy);
while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIy, byte);
while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPIy);
}
}
}
vu32 Read_Data_From_AD7190(unsigned char n)
{
vu32 buf_dat,buf_dat1,buf_dat2,buf_dat3;
if(n==1)//处理一个字节
{
buf_dat=SPI_SendByte(R,DUMMY);
return buf_dat;
}
else if(n==4)//处理4个字节
{
buf[3]=SPI_SendByte(R,DUMMY);
buf[2]=SPI_SendByte(R,DUMMY);
buf[1]=SPI_SendByte(R,DUMMY);
buf[0]=SPI_SendByte(R,DUMMY);
printf("shuju %d \r\n",buf[3]);
printf("%d \r\n", buf[2]);
printf("%d \r\n", buf[1]);
printf("%d \r\n", buf[0]);
printf("%d \r\n", buf[4]);
buf_dat3=buf[3]<<24;
buf_dat2=buf[2]<<16;
buf_dat1=buf[1]<<8;
buf_dat=buf_dat3+buf_dat2+buf_dat1+buf[0];
return buf_dat;
}
}
void Start_AD7190(void)
{
uint32_t i;
SPI_SendByte(T,DATA_REG|CONTINUOUS_READ);
if(WAIT_AD7190==0)
{
for(i=0;i<310;i++)
{
// printf("等待 \r\n");
/* Check the parameters */
while(WAIT_AD7190);
SPI_SendByte(T,0x58);
buffer=Read_Data_From_AD7190(4);
}
}
}
void SPI_Full(void)
{
SPI_InitTypeDef SPI_InitStructure;
/* SPI2 configuration ------------------------------------------------------*/
SPI_Cmd(SPIy, DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIy , &SPI_InitStructure);
SPI_Cmd(SPIy, ENABLE);
flag_spi=Full;
}
void SPI_Tx(void)
{
SPI_InitTypeDef SPI_InitStructure;
/* SPI2 configuration ------------------------------------------------------*/
SPI_Cmd(SPIy, DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
//SPI_BiDirectionalLineConfig(SPI_Direction_Tx);
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIy , &SPI_InitStructure);
SPI_Cmd(SPIy, ENABLE);
flag_spi=Tx;
}
|
|