HI,
For the PWM output from STM32F4, I am able to set PULSE LENGTH (dutycycle) and FREQUENCY of PWM by changing values in
"TIM_OCInitStructure.TIM_Pulse = TIM_PULSE"
and
"TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD;"
.
In my program, values in TIM_PULSE defines dutycycle and values in TIM_PERIOD defines the frequency.
Next, in-order to develop the user interface control. which means the input values from my GUI should change the values in TIM_PULSE and TIM_PERIOD.
Could you pls give me some coding idea to update these values whenever the user want to change?
My full code:
#define APB2_FREQ 84000000 * 2 #define PWM_FREQAPB2 25000 //20 KHZ uint8_t Duty2=50; #define TIM_PRESCALER2 0 #define TIM_PERIOD2 ((APB2_FREQ)/(PWM_FREQAPB2))-1 // Autoreload reg value #define TIM_PULSE2 ((TIM_PERIOD2 + 1) * Duty2)/ 100 - 1 void TIM_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; // Enable TIM10 Clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE); //TIM CHANGE // Enable GPIOF Pins that are used for on board LED's RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); // change 1 /* Initialise pins 13, 14 and 15 D - relating to on board LED's*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOF, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOF, GPIO_PinSource6, GPIO_AF_TIM10); } void PWM_Config() { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; // Setup timer defaults TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD; //PWM Frequency = 20KHz TIM_TimeBaseStructure.TIM_Prescaler = TIM_PRESCALER2; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM10, &TIM_TimeBaseStructure); /* Configure timer for PWM - channel 1*/ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // Mode-> PWM1 for set, PWM2 for clear TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = TIM_PULSE; // dutycycle //notice the number 1 in TIM_OC1Init TIM_OC1Init(TIM10, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM10, ENABLE); //TIM_ARRPreloadConfig(TIM3, ENABLE); /* Start timer*/ TIM_Cmd(TIM10, ENABLE); } void PWM_SetDC(/*uint16_t channel, uint16_t dutycycle*/) { uint16_t channel; //uint16_t dutycycle; if (channel==1) { //TIM3->CCR1 = 2000; } else { } }
KINDLE HELP!
How to update TIM_PULSE2 and TIM_PERIOD2 from the User Interface