Skip to main content
wsevendays
Associate III
December 3, 2011
Question

What's wrong in PWM (Led)?

  • December 3, 2011
  • 6 replies
  • 1056 views
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
This topic has been closed for replies.

6 replies

Tesla DeLorean
Guru
December 3, 2011
Posted on December 03, 2011 at 22:35

Not GPIOB ! If you want to use PC.8 = TIM3_CH3 (remapped), initialize the GPIOC clock, and GPIOC pin. If you really want in on GPIOB you must not remap it, and it will come out PB.0

pulse width = 999, is pretty much totally ON (a multimeter will read 3.xV). If you want it to pulse 500 might be a better initial value. And probably needs to be much lower to appear DIM.

If you want it to phase DIM/BRIGHT you will need to shovel different cyclic PWM values via DMA or at the update interrupt. 
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
wsevendays
Associate III
December 4, 2011
Posted on December 04, 2011 at 09:49

Thanks. I was wrong.I now

get

to control

how

bright

is

the light.Can you please show how to update value of brightness?

rmteo
Associate
December 4, 2011
Posted on December 04, 2011 at 18:39

You can use the library function but I find it just as easy to use:

TIM_SetCompare3(TIM3, 200);

wsevendays
Associate III
December 4, 2011
Posted on December 04, 2011 at 19:00

Thank you so much

!

You

have helped me

incredibly

.

rmteo
Associate
December 4, 2011
Posted on December 04, 2011 at 19:40

You're welcome.  Actually, what I meant to say is this (which will save you about 12 bytes of code space over the previous way), but either method will work.

TIM3->CCR3 = 200;

wsevendays
Associate III
December 4, 2011
Posted on December 04, 2011 at 22:02

Thanks. A also viewed this on

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32VLDiscovery/DispForm.aspx?ID=422&RootFolder=/public/STe2ecommunities/mcu/Lists/STM32VLDiscovery/Example PWM on STM32-Discovery Blue LED&Source=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32VLDiscovery/flat.aspx?RootFolder%3D%252Fpublic%252FSTe2ecommunities%252Fmcu%252FLists%252FSTM32VLDiscovery%252FExample%2520PWM%2520on%2520STM32-Discovery%2520Blue%2520LED%26FolderCTID%3D0x01200200770978C69A1141439FE559EB459D758000491D59B8574F8049B5DFA3E8B21CBA51%26currentviews%3D591

But my delay was very little so I don't saw changes, now I'm set it to (80000-200000) and all goes perfect.