STM32读取24位模数转换(24bit ADC)芯片ADS1231数据

STM32读取24位模数转换(24bit ADC)芯片ADS1231数据

ADS1231是一款TI公司出品的24位ADC芯片,常用于与称重传感器配合实现体重计的应用。这里介绍STM32读取ADS1231的电路和代码实现。ADS1231的特点为通过硬件管脚可控制两种采样速率(10SPS和80SPS),及可以控制芯片上下电以实现低功耗过程控制。

ADS1231的内部原理如下图所示(固定128倍输入信号放大增益):
在这里插入图片描述

STM32电路连接

ADS1231与STM32的连接关系设计如下图所示:
在这里插入图片描述
ADS1231的采样模拟接口可以工作在和数字接口不同的电压,如模拟供电 AVDD采用 5V,数字供电采用3.3V,从而与STM32的接口直接连接即可。

ADS1231测试电路

ADS1231典型的应用连接到惠斯通电桥,接收差分电压,由于内部已固定为128倍信号放大,所以对于5V供电(AVDD),最大检测差分电压范围为±20mV。需要注意输入差分信号有共模电压范围要求:
在这里插入图片描述
简单测试可以采用如下方式:
在这里插入图片描述
当可调电阻器为10欧姆时,IN+和IN-差分电压为(5/(4700+4700+10))*10 = 5.31mV。而IN-端电压为2.49734V,IN+端电压为2.50265V,共模和差模电压都在手册电气范围内,可以微调可调电位器的阻值,调整输出差模电压。

ADS1231访问协议

ADS1231可以通过硬件管脚SPEED控制采样速率, 及通过/PWRDONW管脚控制芯片上下电:
在这里插入图片描述
读取数据的时序则为:

  1. 检测nRDY管脚(也是Dout管脚)状态,如为低电平则可以读取数据,如为高电平则不能读取数据 ;
  2. 当数据可读取时, 发送24个时钟,并在每个时钟的下降沿获得采样数据的24位中的各个位,高位优先接收到
  3. 24个时钟之后,多发一个时钟,使得nRDY管脚回到输出高电平状态,在下一次数据可读取时,ADS1231会将信号拉低

STM32工程配置

这里采用STM32G031F8P6和STM32CUBEIDE开发环境,实现ADS1231的ADC数据读取。

首先配置基本工程和时钟系统:
在这里插入图片描述
在这里插入图片描述
配置UART2作为通讯口。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
配置与ADS1231连接的4个管脚:
在这里插入图片描述
保存并生成初始工程代码:
在这里插入图片描述

STM32工程代码

代码主要实现微秒级的时序控制,采用的微秒延时函数参考: STM32 HAL us delay(微秒延时)的指令延时实现方式及优化

测试逻辑采用以下方式:

  1. 串口收到0x01命令,进行10Hz输出测试
  2. 串口收到0x02命令,进行80Hz输出测试

main.c文件完整代码如下:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
//Written by Pegasus Yu in 2023
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{
  __IO uint32_t firstms, secondms;
  __IO uint32_t counter = 0;

  firstms = HAL_GetTick()+1;
  secondms = firstms+1;

  while(uwTick!=firstms) ;

  while(uwTick!=secondms) counter++;

  usDelayBase = ((float)counter)/1000;
}

void PY_Delay_us_t(uint32_t Delay)
{
  __IO uint32_t delayReg;
  __IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

void PY_usDelayOptimize(void)
{
  __IO uint32_t firstms, secondms;
  __IO float coe = 1.0;

  firstms = HAL_GetTick();
  PY_Delay_us_t(1000000) ;
  secondms = HAL_GetTick();

  coe = ((float)1000)/(secondms-firstms);
  usDelayBase = coe*usDelayBase;
}


void PY_Delay_us(uint32_t Delay)
{
  __IO uint32_t delayReg;

  __IO uint32_t msNum = Delay/1000;
  __IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);

  if(msNum>0) HAL_Delay(msNum);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define ads1231_rdy (HAL_GPIO_ReadPin(GPIOA,  GPIO_PIN_7)==0)?1:0

#define ads1231_clk_h HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET)
#define ads1231_clk_l HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET)
#define ads1231_dout HAL_GPIO_ReadPin(GPIOA,  GPIO_PIN_7)

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t cmd=0;
uint32_t ads1231_data;

uint32_t counter=0;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  PY_usDelayTest();
  PY_usDelayOptimize();

  //hardware reset of ADS1231
  HAL_GPIO_WritePin(ADS1231_nPDWN_GPIO_Port, ADS1231_nPDWN_Pin, GPIO_PIN_RESET);
  PY_Delay_us_t(1000000);
  HAL_GPIO_WritePin(ADS1231_nPDWN_GPIO_Port, ADS1231_nPDWN_Pin, GPIO_PIN_SET);

  __HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);
  HAL_UART_Receive_IT(&huart2, (uint8_t *)&cmd, 1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  if(cmd==0x01) //10SPS
	  {
		  HAL_GPIO_WritePin(ADS1231_SPEED_GPIO_Port, ADS1231_SPEED_Pin, GPIO_PIN_RESET);

	      while(ads1231_rdy) PY_Delay_us_t(1);
	      while(!ads1231_rdy) PY_Delay_us_t(1);
	      ads1231_data = 0;
		  PY_Delay_us_t(1);

		  for(uint8_t i=1;i<=24;i++)
		  {
			  ads1231_clk_h;
			  PY_Delay_us_t(1);
			  ads1231_clk_l;
			  ads1231_data |=  (ads1231_dout<<(24-i));
			  PY_Delay_us_t(1);
		  }

		  ads1231_clk_h;
		  PY_Delay_us_t(1);
		  ads1231_clk_l;
		  PY_Delay_us_t(1);

	      HAL_UART_Transmit(&huart2, &ads1231_data, 3, 2700);

	      counter++;
	      if(counter%10==0) PY_Delay_us_t(1000000);
	  }

	  if(cmd==0x02) //80SPS
	  {
		  HAL_GPIO_WritePin(ADS1231_SPEED_GPIO_Port, ADS1231_SPEED_Pin, GPIO_PIN_SET);

	      while(ads1231_rdy) PY_Delay_us_t(1);
	      while(!ads1231_rdy) PY_Delay_us_t(1);
	      ads1231_data = 0;
		  PY_Delay_us_t(1);

		  for(uint8_t i=1;i<=24;i++)
		  {
			  ads1231_clk_h;
			  PY_Delay_us_t(1);
			  ads1231_clk_l;
			  ads1231_data |=  (ads1231_dout<<(24-i));
			  PY_Delay_us_t(1);
		  }

		  ads1231_clk_h;
		  PY_Delay_us_t(1);
		  ads1231_clk_l;
		  PY_Delay_us_t(1);

		  HAL_UART_Transmit(&huart2, &ads1231_data, 3, 2700);

	      counter++;
	      if(counter%80==0) PY_Delay_us_t(1000000);
	  }


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
  RCC_OscInitStruct.PLL.PLLN = 8;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, ADS1231_SPEED_Pin|ADS1231_SCK_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(ADS1231_nPDWN_GPIO_Port, ADS1231_nPDWN_Pin, GPIO_PIN_SET);

  /*Configure GPIO pins : ADS1231_SPEED_Pin ADS1231_nPDWN_Pin */
  GPIO_InitStruct.Pin = ADS1231_SPEED_Pin|ADS1231_nPDWN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : ADS1231_SCK_Pin */
  GPIO_InitStruct.Pin = ADS1231_SCK_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(ADS1231_SCK_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : ADS1231_nDRDY_DOUT_Pin */
  GPIO_InitStruct.Pin = ADS1231_nDRDY_DOUT_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(ADS1231_nDRDY_DOUT_GPIO_Port, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
	HAL_UART_Receive_IT(&huart2, (uint8_t *)&cmd, 1);
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

代码实现十六进制数据输出,如果要切换为串口printf打印输出,可以参考:
STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)

输出的24位数据为补码格式,进行绝对值提取时按照如下规则:
在这里插入图片描述

测试效果

串口命令0x01输出(间隔1秒输出10个采样值):
在这里插入图片描述
串口命令0x02输出(间隔1秒输出80个采样值):
在这里插入图片描述

例程下载

STM32G031F8P6-ADS1231例程

–End–文章来源地址:https://www.uudwc.com/A/4rBW0/

原文地址:https://blog.csdn.net/hwytree/article/details/130926002

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

h
上一篇2023年09月18日 02:20
下一篇 2023年09月18日 02:27