简体   繁体   中英

STM32 - why the code does not modify the registers like it is supposed to?

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.

Enable the clock for the DAC in the RCC. Exactly which bit of which register should be set is documented in the reset and clock control chapter of the reference manual for your microcontroller.

As long as the clock of a peripheral is not enabled, all of its registers read 0, and the peripheral is not usable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM