Question
What's wrong in PWM (Led)?
Posted on December 03, 2011 at 10:50
I want to create fading Led with pwm, but my code doesn't works. If I simply set bit 1 to pin my led is lights.Please help me.
I'm use stm32f103vet6.int main(void){ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE ); // Set the Vector Table base address at 0x08000000. NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ); // Configure HCLK clock as SysTick clock source. SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK ); GPIO_StructInit(&GPIO_InitStructure); // Reset init structure // Setup Blue LED on STM32-Discovery Board to use PWM. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Alt Function - Push Pull GPIO_Init( GPIOB, &GPIO_InitStructure ); GPIO_PinRemapConfig( GPIO_FullRemap_TIM3, ENABLE ); // Map TIM3_CH3 to GPIOC.Pin8 // Let PWM frequency equal 100Hz. // Let period equal 1000. Therefore, timer runs from zero to 1000. Gives 0.1Hz resolution. // Solving for prescaler gives 240. TIM_TimeBaseStructInit( &TIM_TimeBaseInitStruct ); TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4; TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1; // 0..999 TIM_TimeBaseInitStruct.TIM_Prescaler = 240 - 1; // Div 240 TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStruct ); TIM_OCStructInit( &TIM_OCInitStruct ); TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; // Initial duty cycle equals 0%. Value can range from zero to 1000. TIM_OCInitStruct.TIM_Pulse = 999; // 0 .. 1000 (0=Always Off, 1000=Always On) TIM_OC3Init( TIM3, &TIM_OCInitStruct ); TIM_UpdateDisableConfig(TIM3,ENABLE); TIM_Cmd( TIM3, ENABLE ); while (1);} #pwm #led #stm32 #stm32f103