/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F107xC HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 72 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /* Configures Key push-button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /*##-1- Configure the DAC peripheral #######################################*/ DacHandle.Instance = DACx; /*##-2- Configure the TIM peripheral #######################################*/ TIM6_Config(); /* Infinite loop */ while (1) { /* If the Key is pressed */ if (ubKeyPressed != RESET) { HAL_DAC_DeInit(&DacHandle); /* select waves forms according to the Key push-button status */ if (ubSelectedWavesForm == 1) { /* The triangle wave has been selected */ /* Triangle Wave generator -------------------------------------------*/ DAC_Ch1_TriangleConfig(); } else { /* The escalator wave has been selected */ /* Escalator Wave generator -------------------------------------------*/ DAC_Ch1_EscalatorConfig(); } ubKeyPressed = RESET; } } }
/** * @brief DAC configuration * @note For this example, generate a signal on a spare DAC * channel, so user has just to connect a wire between DAC channel * (pin PA.00) and ADC channel (pin PA.00) to run this example. * (this prevents the user from resorting to an external signal generator) * @param None * @retval None */ static void DAC_Config(void) { static DAC_ChannelConfTypeDef sConfig; /* Configuration of DACx peripheral */ DacHandle.Instance = DACx; if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) { /* DAC deinitialization error */ Error_Handler(); } if (HAL_DAC_Init(&DacHandle) != HAL_OK) { /* DAC initialization error */ Error_Handler(); } /* Configuration of DAC channel */ sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY; sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE; sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE; if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL_TO_ADCx_CHANNELa) != HAL_OK) { /* Channel configuration error */ Error_Handler(); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 180 MHz */ SystemClock_Config(); /* Configure LED4 */ BSP_LED_Init(LED4); /* Configure USER Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI); /*##-1- Configure the DAC peripheral #######################################*/ DacHandle.Instance = DAC; /*##-2- Configure the TIM peripheral #######################################*/ TIM6_Config(); /* Infinite loop */ while (1) { /* If USER Button is pressed */ if (ubKeyPressed != RESET) { HAL_DAC_DeInit(&DacHandle); /* select waves forms according to the USER Button status */ if (ubSelectedWavesForm == 1) { /* The triangle wave has been selected */ /* Triangle Wave generator -------------------------------------------*/ DAC_Ch1_TriangleConfig(); } else { /* The escalator wave has been selected */ /* Escalator Wave generator ------------------------------------------*/ DAC_Ch1_EscalatorConfig(); } ubKeyPressed = RESET; } } }
/** * @brief This function will set the DAC to the required value * @param port : the gpio port to use * @param pin : the gpio pin to use * @param value : the value to push on the adc output * @param do_init : if set to 1 the initialization of the adc is done * @retval None */ void dac_write_value(PinName pin, uint32_t value, uint8_t do_init) { DAC_HandleTypeDef DacHandle = {}; DAC_ChannelConfTypeDef dacChannelConf = {}; uint32_t dacChannel; DacHandle.Instance = pinmap_peripheral(pin, PinMap_DAC); if (DacHandle.Instance == NP) return; dacChannel = get_dac_channel(pin); if (!IS_DAC_CHANNEL(dacChannel)) return; if(do_init == 1) { if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) { /* DeInitialization Error */ return; } /*##-1- Configure the DAC peripheral #######################################*/ g_current_pin = pin; if (HAL_DAC_Init(&DacHandle) != HAL_OK) { /* Initialization Error */ return; } dacChannelConf.DAC_Trigger = DAC_TRIGGER_NONE; dacChannelConf.DAC_OutputBuffer=DAC_OUTPUTBUFFER_ENABLE; /*##-2- Configure DAC channel1 #############################################*/ if (HAL_DAC_ConfigChannel(&DacHandle, &dacChannelConf, dacChannel) != HAL_OK) { /* Channel configuration Error */ return; } } /*##-3- Set DAC Channel1 DHR register ######################################*/ if (HAL_DAC_SetValue(&DacHandle, dacChannel, DAC_ALIGN_12B_R, value) != HAL_OK) { /* Setting value Error */ return; } /*##-4- Enable DAC Channel1 ################################################*/ HAL_DAC_Start(&DacHandle, dacChannel); }
/** * @brief This function will stop the DAC * @param port : the gpio port to use * @param pin : the gpio pin to use * @retval None */ void dac_stop(PinName pin) { DAC_HandleTypeDef DacHandle; uint32_t dacChannel; DacHandle.Instance = pinmap_peripheral(pin, PinMap_DAC); if (DacHandle.Instance == NP) return; dacChannel = get_dac_channel(pin); if (!IS_DAC_CHANNEL(dacChannel)) return; HAL_DAC_Stop(&DacHandle, dacChannel); if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) { /* DeInitialization Error */ return; } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization: global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 180 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); DacHandle.Instance = DACx; /*##-0- DeInit the DAC peripheral ##########################################*/ if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) { /* DeInitialization Error */ Error_Handler(); } /*##-1- Configure the DAC peripheral #######################################*/ if (HAL_DAC_Init(&DacHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure DAC channel1 #############################################*/ sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL) != HAL_OK) { /* Channel configuration Error */ Error_Handler(); } /*##-3- Set DAC Channel1 DHR register ######################################*/ if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL, DAC_ALIGN_8B_R, 0xFF) != HAL_OK) { /* Setting value Error */ Error_Handler(); } /*##-4- Enable DAC Channel1 ################################################*/ if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL) != HAL_OK) { /* Start Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }