STM32读取24位模数转换(24bit ADC)芯片TM7711数据
TM7711是一款国产低成本24位ADC芯片,常用于与称重传感器配合实现体重计的应用。这里介绍STM32读取TM7711的电路和代码实现。TM7711与HX710A是兼容的芯片,而与HX711在功能上有所不同:
- HX711具有双通道信号采样,三种放大倍数,支持设置模拟电压AVDD(也是内部参考电压)输出给外部电路
- TM7711只有单通道信号采样,一种放大倍数,支持内部温度数据读取,支持独立参考电压输入。
市面上的现有模块:
STM32电路连接
TM7711在模拟电压与数字电压部分有要求:
所以如果模拟电压为5V,数字电压DVDD供电也要大于等于5V,对于3.3V典型供电的STM32, 仍然可以进行连接。选择2个具有FT(5V耐压)的管脚,将其中对应时钟输出的管脚配置为Open-drain输出,通过1K欧姆电阻上拉到TM7711的供电电压,将对应数据输入的管脚配置为无上下拉的输入模式,则TM7711可配置为2.7~5.5V的供电范围,不受限于STM32本身为3.3V供电的场景,实现正常通讯。如下图所示:
TM7711测试电路
TM7711典型的应用连接到惠斯通电桥,接收差分电压,由于内部已经设计为128倍信号放大,所以对于5V参考电压输入,最大检测差分电压范围为±20mV。
注意输入信号共模电压方面的要求:
简单测试可以采用如下方式:
当可调电阻器为10欧姆时,IN+和IN-差分电压为(5/(4700+4700+10))*10 = 5.31mV。而IN-端电压为2.49734V,IN+端电压为2.50265V,共模和差模电压都在手册电气范围内,可以微调可调电位器的阻值,调整输出差模电压。
TM7711访问协议
TM7711通过控制时钟管脚输出低电平指示正常工作状态,然后识别数据输入管脚的低电平状态判断当前已完成转换并可读取数据,然后发送出24个时钟波形,并在每个时钟波形的下降沿读取数据位,先读到的是24位采样数据的高位。在24个时钟后,还要发送1到3个时钟波形,即第25~27的时钟,指示后面的ADC转换对应的模式:如下所示:
具体的时序要求如下:
另外通过将时钟输出管脚输出60us以上,可将TM7711进行复位。
STM32工程配置
这里采用STM32F103C6T6和STM32CUBEIDE开发环境,实现TM7711的ADC数据读取代码。
首先配置基本工程和时钟系统:
STM32F103支持USB,可以实现虚拟串口,所以进行USB的配置,采用默认设置接口,另外配置UART2作为可选通讯口。
然后配置UART2:
选择具有FT特征的PB0和PB1作为与TM7711通讯的管脚,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时,10Hz输出模式测试
- 在收到0x02时,温度值读取测试
- 在收到0x03时,40Hz输出模式测试
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 tm7711_rdy (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1)==0)?1:0
#define tm7711_clk_h HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET)
#define tm7711_clk_l HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET)
#define tm7711_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 tm7711_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();
tm7711_clk_h;
PY_Delay_us_t(80); //soft reset tm7711
tm7711_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) //Signal test, 10Hz
{
while(tm7711_rdy) ;
while(!tm7711_rdy) ;
tm7711_data = 0;
PY_Delay_us_t(1);
for(uint8_t i=1;i<=24;i++)
{
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
tm7711_data |= (((uint32_t)tm7711_dout)<<(24-i));
PY_Delay_us_t(1);
}
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
PY_Delay_us_t(1);
while( CDC_Transmit_FS(&tm7711_data, 3) != USBD_OK ) PY_Delay_us_t(1);
}
else if(cmd==0x02) //Temperature test, 40Hz
{
while(tm7711_rdy) ;
while(!tm7711_rdy);
tm7711_data = 0;
PY_Delay_us_t(1);
for(uint8_t i=1;i<=24;i++)
{
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
tm7711_data |= (((uint32_t)tm7711_dout)<<(24-i));
PY_Delay_us_t(1);
}
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
PY_Delay_us_t(1);
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
PY_Delay_us_t(1);
while( CDC_Transmit_FS(&tm7711_data, 3) != USBD_OK ) PY_Delay_us_t(1);
}
else if(cmd==0x03) //Signal test, 40Hz
{
while(tm7711_rdy) ;
while(!tm7711_rdy) ;
tm7711_data = 0;
PY_Delay_us_t(1);
for(uint8_t i=1;i<=24;i++)
{
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
tm7711_data |= (tm7711_dout<<(24-i));
PY_Delay_us_t(1);
}
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
PY_Delay_us_t(1);
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
PY_Delay_us_t(1);
tm7711_clk_h;
PY_Delay_us_t(1);
tm7711_clk_l;
PY_Delay_us_t(1);
while( CDC_Transmit_FS(&tm7711_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,则进行信号采样10Hz输出模式测试(观察工具左侧接收的24位16进制数据):
通过串口工具发送0x02,则进行温度数据测试(观察工具左侧接收的24位16进制数据):
通过串口工具发送0x03,则进行信号采样40Hz输出模式测试(观察工具左侧接收的24位16进制数据):
温度数据可根据手册说明进行解析。
代码实现十进制数据输出,如果要切换为串口printf打印输出,可以参考:
STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)
例程下载
STM32F103C6T6-TM7711例程
–End–