I am using STM32F072C8T6 microcontroller with HAL library. I write a program to send out an analog voltage through the DAC pin of the microcontroller but it does not work. I ran the debugger, and I could see that none of the DAC registers changed when I stepped through the code. Does anyone know if I miss something in the code?
I take over the project from other. He generated the project configuration from CubeMX. However, I don't have the project .ioc file (CubeMX file), so I have to add the DAC functions manually instead of using CubeMX. What I did is I uncommented the #define HAL_DAC_MODULE_ENABLED in stm32f0xx_hal_conf.h, and add the stm32f0xx_hal_dac.c and stm32f0xx_hal_dac_ex.c into the Drivers folder.
Here are the code for the DAC in main.c:
DAC_HandleTypeDef hdac;
int main(void){
HAL_Init();
SystemClock_Config();
DAC_ChannelConfTypeDef sConfig = {0};
hdac.Instance = DAC;
if (HAL_DAC_Init(&hdac) != HAL_OK)
{
Error_Handler();
}
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_DAC_Start( &hdac, DAC_CHANNEL_1);
HAL_DAC_SetValue( &hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 2048);
while(1){
}
}
The DAC output should be 1/2*3.3V = 1.65V. However the actual voltage is 0V, and all the DAC registers remain 0x00. I have also tried to create a new project with CubeMX, and the DAC works perfectly with this new project so the hardware is not the problem.