用ADC0831(串口)测电压,读回来一个8位的数据后(C程序),如何转换成实际电压值?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用ADC0831(串口)测电压,读回来一个8位的数据后(C程序),如何转换成实际电压值?相关的知识,希望对你有一定的参考价值。
还有它测量基准电压值有没上限?用10V行不行?
电压=(最大输入电压/0831的位数)×读回后的值最大输入电压是你的设计电压比如5V,那么分成256份后就是0.02V,将读回后的值×0.02V就得到实际电压值了。若10V太高可采用电阻分压后得到,并且可以设计不同的分压电阻来切换档位。若要求较高可采用运放来完成0.1-N倍放大后即可。 参考技术A void covert(uchar x)
uchar code dispcode[]=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;
disp[0]=dispcode[x/50];
disp[0]=disp[0]+0x80;//加小数点
x=(x%50)*2;//得到小数部分
disp[1]=dispcode[x/10];//第一位小数
disp[2]=dispcode[x%10];//第二位小数
x/250=y/5->y=(5/250)x=x/50也就是说转换的数字量除以50就是实际电压值。 参考技术B 0~255 ----> 0.00 ~ 5.00
可以参考:
http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/738d6b23d0ccdde9d6cae2db.html
ADC值太高,端口串口读不出来
【中文标题】ADC值太高,端口串口读不出来【英文标题】:ADC value is so high and can't read it in port serial 【发布时间】:2021-08-05 14:48:00 【问题描述】:您好,我使用 CubeMX 和 Hal lib 获得了 STM32F01C8T6 的这段代码:
uint32_t value;
while (1)
HAL_ADC_Start (&hadc1);
HAL_ADC_PollForConversion (&hadc1, 1000);
value = HAL_ADC_GetValue (&hadc1);
HAL_UART_Transmit (&huart1,value, 14, 50);
HAL_ADC_Stop (&hadc1);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(500);
ADC 输入为 3.3V,端口串口测试正常。
在调试模式下的值为:536880200,在端口串行中只打印像 !,d@)_+( 之类的坏词。
引脚和 ADC 配置: [1]:https://i.stack.imgur.com/YubLM.png
完整的 main.c 代码:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 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_USART1_UART_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
uint32_t value;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_ADC_Start (&hadc1);
HAL_ADC_PollForConversion (&hadc1, 1000);
value = HAL_ADC_GetValue (&hadc1);
HAL_UART_Transmit (&huart1,value, 14, 50);
HAL_ADC_Stop (&hadc1);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(500);
/* 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_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
Error_Handler();
/* USER CODE BEGIN 4 */
/* 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 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
在调试模式下ADC值太高了,我无法在端口串行中读取它如何修复它?
谢谢。
【问题讨论】:
请阅读the help pages,接受SO tour,阅读How to Ask,以及this question checklist。最后请学习如何创建minimal reproducible example,重点是minimal部分。 常识:您的 ADC 分辨率是多少,考虑到该分辨率,值 536880200 是否有意义? 32 位 delta sigma 是相当罕见的野兽,我非常怀疑它会集成到您的 MCU 中。 【参考方案1】:HAL_UART_Transmit
的第二个参数是指向数据的指针,第三个是数据的大小。
如果要在 UART 上传输二进制数据,需要将 value, 14
更改为 &value, sizeof value
。
如果您想传输 ASCII 文本,那么您需要执行以下操作:
char buffer[16];
int length = snprintf(buffer, sizeof buffer, "%u\n", (unsigned int)value);
if (length > 0)
if (length >= sizeof buffer)
length = ((sizeof buffer) - 1);
HAL_UART_Transmit (&huart1, (uint8_t*)buffer, length, 50);
【讨论】:
以上是关于用ADC0831(串口)测电压,读回来一个8位的数据后(C程序),如何转换成实际电压值?的主要内容,如果未能解决你的问题,请参考以下文章
STC15系列P1.3做ADC-使用内部基准计算外部电压并串口打印