先上主角儿。板载的ST-LINK V3有点抢眼了。。。 拿到开发板后,一直在纠结做什么好,工作之余就翻开资料看看,发现STM32G431有三个可编程运放,对于我们工作上经常用到小信号放大的情况,倒是一个不错的方案。可以省掉一个运放及外围电路。 刚开始被搞懵了,手册上只讲了三种模式 而CubeMX中有一堆模式可选 本来从手册的字面意思上,比较好选的,但到了CubeMX中,无从下手了,没办法,只好再去啃手册。 本来想用差分输入方式的,但它这个好像不支持(也没再去深究),目前就用单端输入方式。 下面正式开始:(因为MCU属于新款的,所以该升级到最新版本的软件工具要升到最新版,否则可能不支持) 一、利用STM32CubeMX创建并配置工程 直接选择NUCLEO-G431RB板,并初始化为缺省模式(这种方式和板子最搭)。 二、配置OPAMP2,选择PGA Not Connected 模式,Gain我选择的是64(最大)。配置后,会看到PA6和PA7被分配成了OPA的输出端和输入端。因为我要连接到内部的ADC,这里其实是不需要输出的,不过我暂时还没找到怎么把OUT管脚用于其他功能。 三、配置ADC2,选择通道为VOPAMP2 Channel。下面的参数就不细说了,和其他系列的MCU基本差别不大,再配置一下DMA,AD转换需要用到DMA。 四、配置完成,生成代码,运行效果。通过板载的虚拟串口发出温度数据,没有做数据处理以及校准,精度还是可以的。
以下为main.c中的代码,其它代码为CubeMX生成的,未作改动。 - /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2019 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"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include <string.h>
- #include <stdio.h>
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- #define ADC_DMA_LENGTH 32
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc2;
- DMA_HandleTypeDef hdma_adc2;
- UART_HandleTypeDef hlpuart1;
- OPAMP_HandleTypeDef hopamp2;
- /* USER CODE BEGIN PV */
- uint32_t AdcData[ADC_DMA_LENGTH];
- uint16_t RealTemp = 0;
- uint32_t VolmV = 0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_LPUART1_UART_Init(void);
- static void MX_OPAMP2_Init(void);
- static void MX_DMA_Init(void);
- static void MX_ADC2_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /*******************************************************************************
- ** 函数名: Quick_Sort
- ** 功 能: 选择排序法
- ** 参 数: unsigned int *a 待排列的数组指针
- ** unsigned int i 待排列的数组起始元素下标
- ** unsigned int j 待排列的数组结束元素下标
- ** 返 回: 无
- ** 备 注:
- *******************************************************************************/
- void Quick_Sort(int *a, int i, int j)
- {
- int m,n,temp;
- int k;
- m = i;
- n = j;
- k = a[(i+j) / 2];/*选取的参照*/
- do{
- while((a[m] < k) && (m < j))
- {/*从左到右找比k大的元素*/
- m++;
- }
- while((a[n] > k) && (n > i))
- {/*从右到左找比k小的元素*/
- n--;
- }
- if(m <= n)
- {/*若找到且条件满足,则交换*/
- temp = a[m];
- a[m] = a[n];
- a[n] = temp;
- m++;
- n--;
- }
- }while(m <= n);
- if(m < j)
- {
- Quick_Sort(a, m, j);/*运用递归*/
- }
- if(n > i)
- {
- Quick_Sort(a, i, n);
- }
- }
- uint32_t Get_Temp(uint32_t *pArray)
- {
- uint32_t Value = 0;
- uint32_t SortArray[ADC_DMA_LENGTH];
-
- for(uint32_t i=0; i<ADC_DMA_LENGTH; i++)
- {
- SortArray[i] = *(pArray + i);
- }
- Quick_Sort((int *)SortArray, 0, ADC_DMA_LENGTH-1);
-
- for(uint32_t i=4; i<(ADC_DMA_LENGTH-4); i++)
- {
- Value += SortArray[i];
- }
- Value /= (ADC_DMA_LENGTH-8);
- //换算成mV值(0.1mV)
- Value *= 33000;
- Value /= 4096;
- //换算成温度值,1000度对应41mV
- Value *= 100;
- Value /= 41;
- //最后把增益除掉
- Value /= 64;
- Value += 60;//没有做校准,所以程序里直接加上一个偏置值
- return Value;
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- static char TempStr[64];
- /* 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_LPUART1_UART_Init();
- MX_OPAMP2_Init();
- MX_DMA_Init();
- MX_ADC2_Init();
- /* USER CODE BEGIN 2 */
- HAL_ADC_Start_DMA(&hadc2, (uint32_t *)AdcData, ADC_DMA_LENGTH);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- // HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
- RealTemp = Get_Temp(AdcData);
- sprintf((char *)TempStr, "\r\nthe temperature is %d ℃\r\n\0", RealTemp);
- HAL_UART_Transmit(&hlpuart1, (uint8_t *)TempStr, strlen(TempStr), 500);
- HAL_Delay(500);
- /* 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};
- /** Configure the main internal regulator output voltage
- */
- HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- 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_DIV4;
- RCC_OscInitStruct.PLL.PLLN = 85;
- 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 busses 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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the peripherals clocks
- */
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_ADC12;
- PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
- PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief ADC2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_ADC2_Init(void)
- {
- /* USER CODE BEGIN ADC2_Init 0 */
- /* USER CODE END ADC2_Init 0 */
- ADC_ChannelConfTypeDef sConfig = {0};
- /* USER CODE BEGIN ADC2_Init 1 */
- /* USER CODE END ADC2_Init 1 */
- /** Common config
- */
- hadc2.Instance = ADC2;
- hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV16;
- hadc2.Init.Resolution = ADC_RESOLUTION_12B;
- hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc2.Init.GainCompensation = 0;
- hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
- hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- hadc2.Init.LowPowerAutoWait = DISABLE;
- hadc2.Init.ContinuousConvMode = ENABLE;
- hadc2.Init.NbrOfConversion = 1;
- hadc2.Init.DiscontinuousConvMode = DISABLE;
- hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc2.Init.DMAContinuousRequests = ENABLE;
- hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
- hadc2.Init.OversamplingMode = DISABLE;
- if (HAL_ADC_Init(&hadc2) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_VOPAMP2;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
- sConfig.SingleDiff = ADC_SINGLE_ENDED;
- sConfig.OffsetNumber = ADC_OFFSET_NONE;
- sConfig.Offset = 0;
- if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN ADC2_Init 2 */
- /* USER CODE END ADC2_Init 2 */
- }
- /**
- * @brief LPUART1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_LPUART1_UART_Init(void)
- {
- /* USER CODE BEGIN LPUART1_Init 0 */
- /* USER CODE END LPUART1_Init 0 */
- /* USER CODE BEGIN LPUART1_Init 1 */
- /* USER CODE END LPUART1_Init 1 */
- hlpuart1.Instance = LPUART1;
- hlpuart1.Init.BaudRate = 115200;
- hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
- hlpuart1.Init.StopBits = UART_STOPBITS_1;
- hlpuart1.Init.Parity = UART_PARITY_NONE;
- hlpuart1.Init.Mode = UART_MODE_TX_RX;
- hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&hlpuart1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN LPUART1_Init 2 */
- /* USER CODE END LPUART1_Init 2 */
- }
- /**
- * @brief OPAMP2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_OPAMP2_Init(void)
- {
- /* USER CODE BEGIN OPAMP2_Init 0 */
- /* USER CODE END OPAMP2_Init 0 */
- /* USER CODE BEGIN OPAMP2_Init 1 */
- /* USER CODE END OPAMP2_Init 1 */
- hopamp2.Instance = OPAMP2;
- hopamp2.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
- hopamp2.Init.Mode = OPAMP_PGA_MODE;
- hopamp2.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
- hopamp2.Init.InternalOutput = ENABLE;
- hopamp2.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE;
- hopamp2.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO;
- hopamp2.Init.PgaGain = OPAMP_PGA_GAIN_64_OR_MINUS_63;
- hopamp2.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
- if (HAL_OPAMP_Init(&hopamp2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN OPAMP2_Init 2 */
- HAL_OPAMP_Start(&hopamp2);
- /* USER CODE END OPAMP2_Init 2 */
- }
- /**
- * Enable DMA controller clock
- */
- static void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMAMUX1_CLK_ENABLE();
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- }
- /**
- * @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_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : LD2_Pin */
- GPIO_InitStruct.Pin = LD2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
- }
- /* 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 */
- /* 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,
- tex: 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****/
复制代码
|