STM32读取24位模数转换(24bit ADC)芯片HX711数据
HX711是一款国产低成本24位ADC芯片,常用于与称重传感器配合实现体重计的应用。这里介绍STM32读取HX711的电路和代码实现。
HX711的内部原理如下图所示:
市面上有普通和带屏蔽的两种模块:
STM32电路连接
STM32可直接与HX711进行连接,选择2个具有FT(5V耐压)的管脚,将其中对应时钟输出的管脚配置为Open-drain输出,通过1K欧姆电阻上拉到HX711的供电电压,将对应数据输入的管脚配置为无上下拉的输入模式,则HX711可配置为2.7~5.5V的供电范围,不受限于STM32本身为3.3V供电的场景,实现正常通讯,注意上拉电阻不能太弱,此处不能用10K欧姆的上拉电阻。如下图所示:
HX711测试电路
HX711典型的应用连接到惠斯通电桥,接收差分电压,由于A通道内部已经设计为128倍和64倍信号放大,所以对于5V供电(AVDD),最大检测差分电压范围为±20mV及±40mV,而B通道内部已设计为32倍信号放大,所以对于5V供电(AVDD),最大检测差分电压范围为±80mV。
HX711内部有反馈电路,通过反馈电路决定输出AVDD电压的值,此电压既是内部进行ADC采样的参考高电压,也是可以输出给外部电路的供电参考电压。而AGND为参考低电压与数字地共地,数据手册介绍如下:
通过上面介绍的STM32通过选用FT特性管脚,及采用Open-drain连接方式,STM32和HX711的数字供电可以不同,注意这里只有时钟管脚采用Open-drain连接方式,数据管脚仍然采用普通的无上下拉输入模式,因为HX711数据手册里提到:
注意此芯片在输入信号共模电压方面有要求:
简单测试可以采用如下方式:
当可调电阻器为10欧姆时,IN+和IN-差分电压为(5/(4700+4700+10))*10 = 5.31mV。而IN-端电压为2.49734V,IN+端电压为2.50265V,共模和差模电压都在手册电气范围内,可以微调可调电位器的阻值,调整输出差模电压。需要注意HX711的AVDD电压,在转换前输出,在转换后关闭,不是一直输出。
HX711访问协议
HX711通过控制时钟管脚输出低电平指示正常工作状态,然后识别数据输入管脚的低电平状态判断当前已完成转换并可读取数据,然后发送出24个时钟波形,并在每个时钟波形的下降沿读取数据位,先读到的是24位采样数据的高位。在24个时钟后,还要发送1到3个时钟波形,即第25~27的时钟,指示后面的ADC转换对应的通道和增益, 如下所示:
具体的时序要求如下:
另外通过将时钟输出管脚输出60us以上,可将HX711进行复位。
转换速度方面是通过硬件的RATE管脚高低电平进行设置,拉低为10Hz,拉高为80Hz。另外可以控制芯片采用内部还是外部时钟,一般采用内部时钟将XI管脚拉低即可。
STM32工程配置
这里采用STM32F103C6T6和STM32CUBEIDE开发环境,实现HX711的ADC数据读取。
首先配置基本工程和时钟系统:
STM32F103支持USB,可以实现虚拟串口,所以进行USB的配置,采用默认设置接口,另外配置UART2作为可选通讯口。
然后配置UART2:
选择具有FT特征的PB0和PB1作为与HX711通讯的管脚,PB0作为时钟管脚配置为Open-drain,PB1作为数据管脚配置为输入。
保存并生成初始代码:
STM32工程代码
代码主要实现微秒级的时序控制,采用的微秒延时函数参考: STM32 HAL us delay(微秒延时)的指令延时实现方式及优化
STM32虚拟串口的设置可以参考: STM32 USB VCOM和HID的区别,配置及Echo功能实现(HAL)
编译时需要采用节省存储的编译方式,参考: STM32 region `FLASH‘ overflowed by xxx bytes 问题解决
代码在USB的控制文件里,将USB接收到的字节赋值给全局变量cmd,用来控制逻辑执行:
- 在收到0x01时,A通道128倍增益测试
- 在收到0x02时,B通道32倍增益测试
- 在收到0x03时,A通道64倍增益测试
main.c文件完整代码如下:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 2022
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
__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 PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define hx711_rdy (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1)==0)?1:0
#define hx711_clk_h HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET)
#define hx711_clk_l HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET)
#define hx711_dout HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1)
/* 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 hx711_data;
/* 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();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
PY_usDelayTest();
PY_usDelayOptimize();
hx711_clk_h;
PY_Delay_us_t(80); //soft reset hx711
hx711_clk_l;
PY_Delay_us_t(10);
__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) //Channel A, gain 128
{
while(hx711_rdy) ;
while(!hx711_rdy) PY_Delay_us_t(1);
hx711_data = 0;
PY_Delay_us_t(1);
for(uint8_t i=1;i<=24;i++)
{
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
hx711_data |= (hx711_dout<<(24-i));
PY_Delay_us_t(1);
}
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
PY_Delay_us_t(1);
while( CDC_Transmit_FS(&hx711_data, 3) != USBD_OK ) PY_Delay_us_t(1);
}
else if(cmd==0x02) //Channel B, gain 32
{
while(hx711_rdy) ;
while(!hx711_rdy) PY_Delay_us_t(1);
hx711_data = 0;
PY_Delay_us_t(1);
for(uint8_t i=1;i<=24;i++)
{
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
hx711_data |= (((uint32_t)hx711_dout)<<(24-i));
PY_Delay_us_t(1);
}
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
PY_Delay_us_t(1);
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
PY_Delay_us_t(1);
while( CDC_Transmit_FS(&hx711_data, 3) != USBD_OK ) PY_Delay_us_t(1);
}
else if(cmd==0x03) //Channel A, gain 64
{
while(hx711_rdy) ;
while(!hx711_rdy) PY_Delay_us_t(1);
hx711_data = 0;
PY_Delay_us_t(1);
for(uint8_t i=1;i<=24;i++)
{
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
hx711_data |= (hx711_dout<<(24-i));
PY_Delay_us_t(1);
}
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
PY_Delay_us_t(1);
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
PY_Delay_us_t(1);
hx711_clk_h;
PY_Delay_us_t(1);
hx711_clk_l;
PY_Delay_us_t(1);
while( CDC_Transmit_FS(&hx711_data, 3) != USBD_OK ) PY_Delay_us_t(1);
}
else;
PY_Delay_us_t(100000);
/* 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};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != 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;
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};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
/*Configure GPIO pin : PB0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PB1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* 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 */
STM32代码测试
通过串口工具发送0x01,则进行A通道128倍增益的测试(观察工具左侧接收的24位16进制数据):
通过串口工具发送0x02,则进行B通道32倍增益的测试(观察工具左侧接收的24位16进制数据), 这里B通道输入管脚都接地故无有效信号输入:
通过串口工具发送0x03,则进行A通道64倍增益的测试(观察工具左侧接收的24位16进制数据):
可以看出0x01测试的数值差不多是0x03测试的2倍, 因为增益正好是128对64。
代码实现十六进制数据输出,如果要切换为串口printf打印输出,可以参考:
STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)
例程下载
STM32F103C6T6-HX711例程文章来源:https://www.toymoban.com/news/detail-401501.html
–End–文章来源地址https://www.toymoban.com/news/detail-401501.html
到了这里,关于STM32读取24位模数转换(24bit ADC)芯片HX711数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!