STM32G491评测(2)DAC
STM32G491内置4通道12位DAC。配合DMA可以高效产生各种波形,
本次评测试用DAC通道1配合DMA数据传输,以查表方式产生1KHZ正弦波。
在CubeMX中选择STM32G491芯片,配置系统时钟150MHZ.
使能DAC1
配置DAC1通道参数
配置DMA参数
使能TIM2
使能TIM6
生成MDK工程
要产生正弦波,需要在MX_DAC1_Init()函数末尾添加代码:
- if (HAL_DACEx_SelfCalibrate(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- sDacConfig = sConfig;
复制代码
main()函数添加代码
- #include "main.h"
- #include "dac.h"
- #include "dma.h"
- #include "tim.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- typedef enum
- {
- DAC_WAVE_SAWTOOTH,
- DAC_WAVE_SINE
-
- } t_wavetype;
- /* 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 */
- __IO uint8_t ubKeyPressed = RESET;
- __IO t_wavetype ubSelectedWavesForm = DAC_WAVE_SINE;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- #define SAWTOOTH_NB_STEPS 60
- uint16_t sinewave[60] = {
- 0x07ff,0x08cb,0x0994,0x0a5a,0x0b18,0x0bce,0x0c79,0x0d18,0x0da8,0x0e29,0x0e98,0x0ef4,0x0f3e,0x0f72,0x0f92,0x0f9d,
- 0x0f92,0x0f72,0x0f3e,0x0ef4,0x0e98,0x0e29,0x0da8,0x0d18,0x0c79,0x0bce,0x0b18,0x0a5a,0x0994,0x08cb,0x07ff,0x0733,
- 0x066a,0x05a4,0x04e6,0x0430,0x0385,0x02e6,0x0256,0x01d5,0x0166,0x010a,0x00c0,0x008c,0x006c,0x0061,0x006c,0x008c,
- 0x00c0,0x010a,0x0166,0x01d5,0x0256,0x02e6,0x0385,0x0430,0x04e6,0x05a4,0x066a,0x0733};
- DAC_ChannelConfTypeDef sDacConfig = {0};
- static void DAC_ChangeWave(t_wavetype wave);
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- 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_DAC1_Init();
- MX_TIM2_Init();
- MX_TIM6_Init();
- /* USER CODE BEGIN 2 */
- if (HAL_TIM_Base_Start(&htim6) != HAL_OK)
- {
- Error_Handler();
- }
- ubSelectedWavesForm = DAC_WAVE_SINE;
- DAC_ChangeWave(ubSelectedWavesForm);
- while (1)
- {
- /* 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};
- /** Configure the main internal regulator output voltage
- */
- HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- 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 = 75;
- 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 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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- static void DAC_ChangeWave(t_wavetype wave)
- {
- uint32_t tmp;
-
- /* Suspend Time Base triggers */
- if (HAL_TIM_Base_Stop(&htim2) != HAL_OK)
- {
- /* Counter enable error */
- Error_Handler();
- }
- if (HAL_TIM_Base_Stop(&htim6) != HAL_OK)
- {
- /* Counter enable error */
- Error_Handler();
- }
-
- switch(wave)
- {
- case DAC_WAVE_SAWTOOTH:
- if (HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_1) != HAL_OK)
- {
- /* DAC conversion start error */
- Error_Handler();
- }
- break;
- case DAC_WAVE_SINE:
- if (HAL_DAC_Stop(&hdac1, DAC_CHANNEL_1) != HAL_OK)
- {
- /* DAC conversion start error */
- Error_Handler();
- }
- break;
- default:
- Error_Handler();
- break;
- }
- /* Re-configure DAC */
- tmp = sDacConfig.DAC_Trigger;
- sDacConfig.DAC_Trigger = sDacConfig.DAC_Trigger2;
- sDacConfig.DAC_Trigger2 = tmp;
- if (HAL_DAC_ConfigChannel(&hdac1, &sDacConfig, DAC_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
-
- /* Generate new wave */
- switch(wave)
- {
- case DAC_WAVE_SAWTOOTH:
- if (HAL_DACEx_SawtoothWaveGenerate(&hdac1, DAC_CHANNEL_1, DAC_SAWTOOTH_POLARITY_INCREMENT, 0, 0x10000/SAWTOOTH_NB_STEPS) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK)
- {
- /* DAC conversion start error */
- Error_Handler();
- }
- break;
- case DAC_WAVE_SINE:
- if (HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1,
- (uint32_t *)sinewave,
- 60,
- DAC_ALIGN_12B_R
- ) != HAL_OK)
- {
- /* DAC conversion start error */
- Error_Handler();
- }
- break;
- default:
- Error_Handler();
- break;
- }
-
- /* Resume Time Base triggers */
- if (HAL_TIM_Base_Start(&htim2) != HAL_OK)
- {
- /* Counter enable error */
- Error_Handler();
- }
- if (HAL_TIM_Base_Start(&htim6) != HAL_OK)
- {
- /* Counter enable error */
- Error_Handler();
- }
- }
复制代码
运行结果
STM32G491DAC.rar
(637 KB, 下载次数: 13)
|