From 0f2ea7deaf0cba2f0ca07f6611c5a9e87d531f5b Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Tue, 26 Aug 2025 17:03:24 +0000 Subject: Initial commit --- project_starter_files/Core/Src/dac.c | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 project_starter_files/Core/Src/dac.c (limited to 'project_starter_files/Core/Src/dac.c') diff --git a/project_starter_files/Core/Src/dac.c b/project_starter_files/Core/Src/dac.c new file mode 100644 index 0000000..e03f9ce --- /dev/null +++ b/project_starter_files/Core/Src/dac.c @@ -0,0 +1,83 @@ +/* + * dac.c + * + * Created on: Apr 13, 2022 + * Author: bruce + */ + +#include "dac.h" +#include "main.h" +#include "stm32l4xx.h" + +DAC_HandleTypeDef hdac1; + +/** +* @brief DAC MSP Initialization +* This function configures the hardware resources used in this example +* @param hdac: DAC handle pointer +* @retval None +*/ +void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac) +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(hdac->Instance==DAC1) + { + __HAL_RCC_DAC1_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } + +} + +/** +* @brief DAC MSP De-Initialization +* This function freeze the hardware resources used in this example +* @param hdac: DAC handle pointer +* @retval None +*/ +void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac) +{ + if(hdac->Instance==DAC1) + { + __HAL_RCC_DAC1_CLK_DISABLE(); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4); + } + +} + +void DAC_Init () { + DAC_ChannelConfTypeDef sConfig = {0}; + + /** DAC Initialization + */ + hdac1.Instance = DAC1; + if (HAL_DAC_Init(&hdac1) != HAL_OK) + { + Error_Handler(); + } + + /** DAC channel OUT1 config + */ + sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE; + sConfig.DAC_Trigger = DAC_TRIGGER_NONE; + sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; + sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE; + sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY; + + if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK) + { + Error_Handler(); + } +} + +void DAC_Start () { + HAL_DAC_Start (&hdac1, DAC_CHANNEL_1); +} + +void DAC_Set_Value (uint32_t value) { + HAL_DAC_SetValue (&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); +} -- cgit v1.2.3