|
STM32f051驱动外接ADC芯片ADS1120问题
[复制链接]
使用stm32f051c8t6的SPI1驱动ADS1120芯片,结果程序一直卡在等待nDRDY引脚变为低电平的代码段,于是在ADS1120初始化的函数配置寄存器结束后读取所有寄存器数据,用来检测配置是否正确,结果debug界面中显示的寄存器数据全是0xFF,,,之前一直使用的是HAL库,由于本次使用的板子需要兼容旧版程序因此需要使用固件库,初次使用,参考了不少代码,不知道是不是哪里配置错了,打算明天用示波器看看有关的四个引脚波形是否正确,,有用过ADS1120芯片的大侠给看看吧,多谢了!下面是相关代码:
====程序简述
开发环境:
开发工具:keil V5.20 stm32f0_stdperiph_lib
硬件设备:STM32F051C8T6, ADS1120
========SPI1配置及读写数据函数:
- SPI_InitTypeDef SPI_InitStructure;
- void SPIx_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
- 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_Low;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI1, &SPI_InitStructure);
-
- SPI_Cmd(SPI1, ENABLE);
-
- SPIx_ReadWriteByte(0xff);
- }
复制代码- //SPI1写数据
- void SPI1_WriteBytes(uint8_t *TxBuffer,uint16_t TxLenth)
- {
- uint8_t i;
- while(TxLenth--){
- while( (SPI1->SR & SPI_SR_TXE) == 0 );
- SPI1->DR=*TxBuffer++;
- while((SPI1->SR&SPI_SR_RXNE)==0);
- i=SPI1->DR;
- }
- i++;
- }
- //SPI1读数据
- void SPI1_ReadBytes(uint8_t *RxBuffer,uint16_t RxLenth)
- {
- while(RxLenth--){
- while((SPI1->SR&SPI_SR_TXE)==0);
- SPI1->DR=*RxBuffer;
- while((SPI1->SR&SPI_SR_RXNE)==0);
- *RxBuffer++=SPI1->DR;
- }
- }
- void ADC_WriteBytes(uint8_t *Cmd, uint16_t Count)
- {
- SPI1_WriteBytes(Cmd,Count);
- }
- void ADC_ReadBytes(uint8_t *RxBuffer,uint16_t RxLenth)
- {
- SPI1_ReadBytes(RxBuffer,RxLenth);
- }
- void ADC_ReadWriteBytes(uint8_t *Cmd, uint8_t *Buffer,uint16_t Count)
- {
- // uint8_t i;
- // for(i=0; i<Count; i++)
- // {
- // *Buffer = SPIx_ReadWriteByte(*Cmd++);
- // Buffer++;
- // }
- ADC_WriteBytes(Cmd,1);
- ADC_ReadBytes(Buffer,Count);
- }
复制代码
====读写寄存器函数
- /**
- * [url=home.php?mod=space&uid=159083]@brief[/url] 读取寄存器中的数据.
- * @param *Buffer:读取到的数据指针地址.
- * @param Length:需要读取的数据长度.
- * @note 一次读取多个寄存器数据,从00寄存器开始读取.
- * @retval None
- */
- static void ADS1120_ReadReg(uint8_t *Buffer,uint8_t Length)
- {
- uint8_t cmd;
- CS_L;
- cmd = ADC_CMD_RREG | 0x03;
- ADC_WriteBytes(&cmd,1);
- ADC_ReadBytes(Buffer,Length);
- CS_H;
- }
- /**
- * @brief 写入数据到指定寄存器.
- * @param RegAddr: 指定寄存器地址.
- * @param *Buffer:需要写入的数据指针.
- * @param Length:写入数据长度.
- * @note None.
- * @retval None
- */
- static void ADS1120_WriteReg(uint8_t RegAddr,uint8_t *Buffer,uint8_t Length)
- {
- uint8_t cmd;
- CS_L;
- cmd = ADC_CMD_WREG | (((RegAddr<<2) & 0x0c) |((Length-1)&0x03));
- ADC_WriteBytes(&cmd,1);
- ADC_WriteBytes(Buffer,Length);
- CS_H;
- }
- /**
- * @brief 忙状态判断.
- * @param None.
- * @note 最长等待时间.
- * @retval None
- */
- static uint8_t ADS1120_WaitBusy()
- {
- uint16_t i;
- CS_L;
- i=0;
- while(GPIO_ReadInputDataBit(ADS1120_DRDY_GPIO_PORT,ADS1120_DRDY_GPIO_PIN)){ // 等待就绪(等待DRDY为0)
- Delay();
- i++; if(i>20000)return 1;
- }
- CS_H;
- return 0;
- }
复制代码
====ADS1120初始化函数
- /**
- * @brief ADS1120初始化.
- * @param None.
- * @note 复位芯片及初始化寄存器 .
- * @retval None
- * @note None
- */
- void ADS1120_Init()
- {
- uint8_t cmd;
- uint8_t read[4],buf[4];
-
- ADS1120_WriteCmd(ADC_CMD_RESET); // 复位设备至上电状态(不对SPI进行复位) 20psp需要0.6ms
- Delay();
-
- cmd = 0x28; // 差模输入正负端配置、增益配置
- ADS1120_WriteReg(ADC_REG_00,&cmd,1);
-
- cmd = 0x00; // 数据率、操作模式、转换模式、温度传感器模式、烧毁电流检测
- ADS1120_WriteReg(ADC_REG_01,&cmd,1);
-
- cmd = 0x07; // 内部2.048V参考电压、无滤波、低端电源开关总是开启(?)、恒流源电流1.5mA
- ADS1120_WriteReg(ADC_REG_10,&cmd,1);
-
- cmd = 0x30; // IDAC1->AIN0,IDAC2->AIN3,nDRDY
- ADS1120_WriteReg(ADC_REG_11,&cmd,1);
-
- // 读取寄存器
- ADS1120_ReadReg(read,4);
-
- buf[0] = read[0];
- buf[1] = read[1];
- buf[2] = read[2];
- buf[3] = read[3];
-
- }
复制代码
====读取通道AD值函数
- /**
- * @brief 获取指定通道的AD数据.
- * @param Ch:通道编号.
- * 传入参数可为下述值:
- * @arg 0: RT0
- * @arg 1: RT1
- * @arg 2: RT2
- * @note None.
- * @retval Data: AD数据
- */
- int32_t ADS1120_GetChannelData(uint8_t Ch)
- {
- int32_t result;
- ADS1120_Change_Channel(Ch);
- ADS1120_Start();
- ADS1120_WaitBusy();//16MS p73
- result = ADS1120_Read();
- ADS1120_Stop();
- return result;
- }
复制代码
====读取AD数据函数
- /**
- * @brief 读取ADS1120中的转换数据.
- * @param None.
- * @note None.
- * @retval D:AD数值
- */
- static int32_t ADS1120_Read(void)
- {
- // uint8_t Cmd[3]={ADC_CMD_RDATA,0x00,0x00};
- uint8_t Cmd;
- uint8_t Buf[3];
- int32_t D;
- CS_L;
- ADC_ReadWriteBytes(&Cmd,Buf,3);
- CS_H;
- D=Buf[0];
- D=D*256+Buf[1];
- D=D*256+Buf[2];
- D=D/256;
- return D;
- }
复制代码
|
|