2017-07-01 08:34 PM
Hello,
I recently migrated to STM32 Platform and I'm trying to learn all the Peripheral features without using Standard Peripheral Libraries.
Problem Statement:
I am not able to get PWM output on timer output channel 2 ( GPIOA1) after configuring general purpose timer 2 of STM32F103c8T6 Micro-controller in PWM mode 2.
My Configuration:
Timer : General Purpose timer 2
Timer Clock: 72 MHz
Duty cycle: 50%
Frequency: 50 Hz
MCU crystal : 8 MHz
I configured the GPIOA in Alternate function output Push Pull mode with maximum frequency not exceeding 50 MHz, I connected an LED bulb to the GPIOA pin 1, but the LED wend on glowing without reduction in the brightness level. The LED was still glowing even after i performed a test with duty cycle set to 0. On the debugger window I could see exactly all the parameters I configured and the Count register was getting incremented. I cannot find information regarding selection of alternate functions for a GPIO Port.
I used Keil Micro vision 4 IDE for compiling and debugging my source code,
I read reference manual for register description and used steps from AN4013 Application note to develop source code
Kindly have a view of my source code and please notify me the corrections that need to be done. The target specifications are mentioned in the source code.
Thank you
.
Solved! Go to Solution.
2017-07-03 06:49 AM
Because you are using PWM2 which means the pulse is negative instead of Positive with PWM1, if you set the counter to zero the pin stays in the high state which will leave the LED turned on. At 50% which is 32767 you may not see the LED blink as it is too fast. You may want to try 65500 as a value and see if dims at that level which leaves the pin low.
Mike
2017-07-03 04:54 AM
Hi
Menon.Rakesh
,I cannot find information regarding selection of alternate functions for a GPIO Port.
Please refer to GPIO chapter in RM0008 and product datasheet.
In your code, it seems you are configuring pins 0 and 1 of port A instead of pins 1 and 2 as per your comment in the code (but I don't think this is the problem as PA1 is properly configured).
Other than AN4013, I recommend you have a look to
(General-purpose timer cookbook).You can also test some ready to use examples based on the Cube package likeSTM32Cube_FW_F1_V1.4.0\Projects\STM3210E_EVAL\Examples\TIM\TIM_PWMOutput.
-Amel
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2017-07-03 05:02 AM
I hate configuring clocks. It's too cryptic to get it right. Anyway I used the STM32Cub software to build your example and put a scope on it and it seems to work just fine.
What I don't see in your code is the configuration of the MCU clock. Also when you put the pulse to zero the pin goes to a high state and don't know if you have the LED configured active low or high.
Mike
2017-07-03 06:49 AM
Because you are using PWM2 which means the pulse is negative instead of Positive with PWM1, if you set the counter to zero the pin stays in the high state which will leave the LED turned on. At 50% which is 32767 you may not see the LED blink as it is too fast. You may want to try 65500 as a value and see if dims at that level which leaves the pin low.
Mike
2017-07-03 07:20 AM
Hello Amel,
I configured the port pin to be gpio: 'alternate function output push-pull ' after reading RM0008, I doubt whether if there is an option to select which alternate function have to be assigned for specific pin, under gpio section. I read about pin remapping but that's not what I really need.
Any how I will also test the ready to use examples which you mentioned.
Regards,
Rakesh Menon
2017-07-03 08:10 AM
Hello Mike,
Also when you put the pulse to zero the pin goes to a high state and don't know if you have the LED configured active low or high.
I configured the LED in active high configuration.
What I don't see in your code is the configuration of the MCU clock.
If you are mentioning about system core clock, it has already been set to 72 MHz inside the header file ''stm32f10x.h ''
The timer clock is also 72 MHz only because the clock branching to timer 2 from APB1 has a multiplier.
I could see that in the debug Window.
Also here is the view of the GPIOA peripheral block:
I couldn't find any clock setting mistakes.
I hate configuring clocks. It's too cryptic to get it right. Anyway I used the STM32Cub software to build your example and put a scope on it and it seems to work just fine.
Can you kindly mention about the GPIOA configuration ( selection of particular GPIOA 1 alternate function ).
Regards,
Rakesh Menon
2017-07-03 09:15 AM
Hello Mike,
Because you are using PWM2 which means the pulse is negative instead of Positive with PWM1, if you set the counter to zero the pin stays in the high state which will leave the LED turned on.
Yeah that's write, I did changed the value to 65000, and I saw the LED getting less brighter, and I did tests with values ranging from 65000 to 65530 in a for loop, and now the change in illumination level is clearly visible.
Thanks and regards,
Rakesh Menon
2019-03-24 07:18 AM
Hi Rakesh,
You'r output compare outputs disabled. You need to set MOE bit on TIM2_BDTR register and maybe RCC for AFIO is needed.
Mustafa