STM32/STM8技术william hill官网
直播中

zhuchuangxian

7年用户 6经验值
擅长:嵌入式技术
私信 关注
[问答]

ADS1256用cubeMX生成项目,用SPI2读出数据错误?

已软件模拟,一切正常。
//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();
  }

回帖(3)

zhuchuangxian

2021-5-15 00:00:03
结案!外部传感器未接,导致程序不报错,数据乱。插上传感器,一切OK!想当然不用插传感器,只是读写参数不会有影响!希望大家不要再跳这个坑!
举报

曼巴

2021-11-22 19:44:12
楼主有没有CubemxLL库写的代码,我最近在调试ADS1256,用LL库一直采样非常不准确
举报

jinyi7016

2021-11-26 11:57:51
大部分是硬件的问题,外接的芯片的连接线,电源等
举报

更多回帖

发帖
×
20
完善资料,
赚取积分