/** * @brief DAC Channel1 Triangle Configuration * @param None * @retval None */ static void DAC_Ch1_TriangleConfig(void) { /*##-1- Initialize the DAC peripheral ######################################*/ if(HAL_DAC_Init(&DacHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- DAC channel2 Configuration #########################################*/ sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; if(HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL1) != HAL_OK) { /* Channel configuration Error */ Error_Handler(); } /*##-3- DAC channel2 Triangle Wave generation configuration ################*/ if(HAL_DACEx_TriangleWaveGenerate(&DacHandle, DACx_CHANNEL1, DAC_TRIANGLEAMPLITUDE_1023) != HAL_OK) { /* Triangle wave generation Error */ Error_Handler(); } /*##-4- Enable DAC Channel1 ################################################*/ if(HAL_DAC_Start(&DacHandle, DACx_CHANNEL1) != HAL_OK) { /* Start Error */ Error_Handler(); } /*##-5- Set DAC channel1 DHR12RD register ##################################*/ if(HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL1, DAC_ALIGN_12B_R, 0x100) != HAL_OK) { /* Setting value Error */ Error_Handler(); } }
/// \method triangle(freq) /// Generate a triangle wave. The value on the DAC output changes at /// the given frequency, and the frequence of the repeating triangle wave /// itself is 256 (or 1024, need to check) times smaller. STATIC mp_obj_t pyb_dac_triangle(mp_obj_t self_in, mp_obj_t freq) { pyb_dac_obj_t *self = self_in; // set TIM6 to trigger the DAC at the given frequency TIM6_Config(mp_obj_get_int(freq)); if (self->state != 2) { // configure DAC to trigger via TIM6 DAC_ChannelConfTypeDef config; config.DAC_Trigger = DAC_TRIGGER_T6_TRGO; config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; HAL_DAC_ConfigChannel(&DAC_Handle, &config, self->dac_channel); self->state = 2; } // set triangle wave generation HAL_DACEx_TriangleWaveGenerate(&DAC_Handle, self->dac_channel, DAC_TRIANGLEAMPLITUDE_1023); HAL_DAC_SetValue(&DAC_Handle, self->dac_channel, DAC_ALIGN_12B_R, 0x100); HAL_DAC_Start(&DAC_Handle, self->dac_channel); return mp_const_none; }