已软件模拟,一切正常。
//ADS1256读寄存器
u8 ADS1256_Read_Reg(u8 reg)
{
u8 status;
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7)){;}//wait ready
reg=reg|0x10;
ADS1256_Write_Bits(reg);
ADS1256_Write_Bits(0x0);
delayus(10); //????? T6 时间调整6.5us
status=ADS1256_Read_Bits();
return status;
}
//ADS1256读寄存器
void ADS1256_Write_Reg( u8 reg, u8 senddata)
{
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7)){;}//wait ready
reg=reg|0x50;
ADS1256_Write_Bits(reg);
ADS1256_Write_Bits(0x0);
ADS1256_Write_Bits(senddata);
}
HAL库
uint8_t ADS1256_Read_Reg(uint8_t reg)
{
uint8_t status=0xff;
uint8_t databuffer[2];
while(ADS1256_Read_Ready){;}//wait ready
reg=reg|0x10;
databuffer[0]=reg;databuffer[1]=0;
if(HAL_SPI_Transmit(&hspi2,&databuffer[0],1,1000)!=HAL_OK) {Error_Handler();}
if(HAL_SPI_Transmit(&hspi2,&databuffer[1],1,1000)!=HAL_OK) {Error_Handler();}
delayus(20); //????? T6 时间调整6.5us
if(HAL_SPI_TransmitReceive(&hspi2,&status,&databuffer[0],1,1000)!=HAL_OK) {Error_Handler();}
return databuffer[0];
}
//ADS1256写寄存器
void ADS1256_Write_Reg( uint8_t reg, uint8_t senddata)
{
uint8_t databuffer[3];
while(ADS1256_Read_Ready){;}//wait ready
reg=reg|0x50;
databuffer[0]=reg;databuffer[1]=0;databuffer[2]=senddata;
if(HAL_SPI_Transmit(&hspi2,&databuffer[0],1,1000)!=HAL_OK) {Error_Handler();}
if(HAL_SPI_Transmit(&hspi2,&databuffer[1],1,1000)!=HAL_OK) {Error_Handler();}
if(HAL_SPI_Transmit(&hspi2,&databuffer[2],1,1000)!=HAL_OK) {Error_Handler();}
}
HAL库测试
//ADS1256_Init();
ADS1256_Write_CS_L;
delayms(100);
ADS1256_Write_Reg(0x01,0x01);//状态寄存器初始化
delayus(20);
rbuffer[0]=0;
rbuffer[0]=ADS1256_Read_Reg(0x01);//模拟多路选择器初始化
delayus(20);
向01寄存器写入1,再读出,数据不对
配置
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8Bit;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
Error_Handler();
} |