本文主要是介绍STM32CubeMX 配置 STM32F103 工程:通过DAC输出正弦波,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
说明:STM32CubeMX 配置 STM32F103 工程,通过DAC输出正弦波,参考代码可自动计算频率,自动计算正弦数据。
先参考这篇文章配置时钟、工程输出的设置:
STM32CubeMX 配置 STM32F103 工程:通过DAC生成三角波、噪声-CSDN博客
1.配置DAC
2.配置DMA
3.配置DAC的触发TIM
4.输出设置
5.生成代码
6.打开工程
7.正弦数据生成代码
#if 0//正弦信号#define POINTS 256#define SCALE_FACTOR (4095.0 / 2) // 缩放因子,将[-1, 1]映射到[0, 4095]#define OFFSET 2048 // 偏移量,将[0, 4095]调整到中心#define M_PI 3.14159265uint16_t sine_wave_u16[POINTS];int sine_wave[POINTS];void sina(void){for (int i = 0; i < POINTS; i++){double x = ((double) i / (POINTS - 1)) * 2 * M_PI; // 0到2π之间的值double sin_value = sin(x); // 计算正弦值sine_wave[i] = (int) (SCALE_FACTOR * sin_value + OFFSET); // 缩放和平移正弦值到0~4095范围sine_wave_u16[i] = (uint16_t)sine_wave[i];}}#else#define POINTS 256 //正弦数据点数#define MIN_VALUE 100 //正弦数据最小值#define MAX_VALUE 4000 //正弦数据最大值 #define SCALE ((MAX_VALUE - MIN_VALUE) / 2.0) #define OFFSET MIN_VALUE #define M_PI 3.14159265uint16_t sine_wave_u16[POINTS];int sine_wave[POINTS]; void sina(void) { for (int i = 0; i < POINTS; i++) { double x = ((double)i / (POINTS - 1)) * 2 * M_PI; // 将索引转换为0到2π之间的值 double sin_value = sin(x); // 计算正弦值 sine_wave[i] = (int)((sin_value + 1) * SCALE + OFFSET); // 缩放和平移正弦值到100~4000范围 sine_wave_u16[i] = (uint16_t)sine_wave[i]; } }#endif
8.在main.c函数中添加定时器、DAC启动代码
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2024 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.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dac.h"
#include "dma.h"
#include "tim.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "math.h"
/* 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 */
uint8_t sin_data_updata = 0;
#if 0//正弦信号#define POINTS 256#define SCALE_FACTOR (4095.0 / 2) // 缩放因子,将[-1, 1]映射到[0, 4095]#define OFFSET 2048 // 偏移量,将[0, 4095]调整到中心#define M_PI 3.14159265uint16_t sine_wave_u16[POINTS];int sine_wave[POINTS];void sina(void){for (int i = 0; i < POINTS; i++){double x = ((double) i / (POINTS - 1)) * 2 * M_PI; // 0到2π之间的值double sin_value = sin(x); // 计算正弦值sine_wave[i] = (int) (SCALE_FACTOR * sin_value + OFFSET); // 缩放和平移正弦值到0~4095范围sine_wave_u16[i] = (uint16_t)sine_wave[i];}}#else#define POINTS 256 //正弦数据点数#define MIN_VALUE 100 //正弦数据最小值#define MAX_VALUE 4000 //正弦数据最大值 #define SCALE ((MAX_VALUE - MIN_VALUE) / 2.0) #define OFFSET MIN_VALUE #define M_PI 3.14159265uint16_t sine_wave_u16[POINTS];int sine_wave[POINTS]; void sina(void) { for (int i = 0; i < POINTS; i++) { double x = ((double)i / (POINTS - 1)) * 2 * M_PI; // 将索引转换为0到2π之间的值 double sin_value = sin(x); // 计算正弦值 sine_wave[i] = (int)((sin_value + 1) * SCALE + OFFSET); // 缩放和平移正弦值到100~4000范围 sine_wave_u16[i] = (uint16_t)sine_wave[i]; } }#endif/* 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_DMA_Init();MX_DAC_Init();MX_TIM2_Init();/* USER CODE BEGIN 2 */sina(); //正选信号数据初始化HAL_TIM_Base_Start(&htim2);//HAL_DAC_Start(&hdac,DAC_CHANNEL_1);HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t *)sine_wave_u16, POINTS, DAC_ALIGN_12B_R);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE */if(sin_data_updata){sina();}/* 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};/** 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();}
}/* 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 */
9.TIM文件中添加频率自动计算代码
/* USER CODE BEGIN Header */
/********************************************************************************* @file tim.c* @brief This file provides code for the configuration* of the TIM instances.******************************************************************************* @attention** Copyright (c) 2024 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.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"/* USER CODE BEGIN 0 */
uint16_t user_hz = 1000; //用户自定义频率 单位:hz
uint16_t get_Period(uint16_t hz) //定时器 Period 计算
{return (72000000/(256*hz) - 1);
}
/* USER CODE END 0 */TIM_HandleTypeDef htim2;/* TIM2 init function */
void MX_TIM2_Init(void)
{/* USER CODE BEGIN TIM2_Init 0 *//* USER CODE END TIM2_Init 0 */TIM_ClockConfigTypeDef sClockSourceConfig = {0};TIM_MasterConfigTypeDef sMasterConfig = {0};/* USER CODE BEGIN TIM2_Init 1 */uint16_t user_Period = get_Period(user_hz);/* USER CODE END TIM2_Init 1 */htim2.Instance = TIM2;htim2.Init.Prescaler = 0;htim2.Init.CounterMode = TIM_COUNTERMODE_UP;htim2.Init.Period = user_Period;htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;if (HAL_TIM_Base_Init(&htim2) != HAL_OK){Error_Handler();}sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK){Error_Handler();}/* USER CODE BEGIN TIM2_Init 2 *//* USER CODE END TIM2_Init 2 */}void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{if(tim_baseHandle->Instance==TIM2){/* USER CODE BEGIN TIM2_MspInit 0 *//* USER CODE END TIM2_MspInit 0 *//* TIM2 clock enable */__HAL_RCC_TIM2_CLK_ENABLE();/* USER CODE BEGIN TIM2_MspInit 1 *//* USER CODE END TIM2_MspInit 1 */}
}void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{if(tim_baseHandle->Instance==TIM2){/* USER CODE BEGIN TIM2_MspDeInit 0 *//* USER CODE END TIM2_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_TIM2_CLK_DISABLE();/* USER CODE BEGIN TIM2_MspDeInit 1 *//* USER CODE END TIM2_MspDeInit 1 */}
}/* USER CODE BEGIN 1 *//* USER CODE END 1 */
9.输出波形
这篇关于STM32CubeMX 配置 STM32F103 工程:通过DAC输出正弦波的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!