STM32 Programming from scratch: Timer complementary channel


This behaviour is based on timers.We have two pins of microcontroller used as timer channels. One analog channel is connected to Potentiometer which have one of it's pin connected to GND and other to 3.3 volts. 

And when one is high other is always low and vice versa. This has advanced applications and can be used in applications like square wave inverters.

For getting this waveform I have used STM32f103c8t6 in hardware. I have used code generation tool as stm32cubemx and ide being Keil uVision 5.

In this tutorial I describe how to do settings in stm32cubemx and how to generate code in Keil and later how to edit it and add your register level code in it.

We divide out task in three steps to generate the code and add our code to it.

First of all open stm32cubemx and select stm32f103c8t6 as selected microcontroller.


Edit only RCC,SYS,ADC1 and Timers section.
As shown in green in below image.



Do below settings in RCC:-


Do below settings in SYS if want to use Stlink v2 for debugging: other wise this setting is not required-
Now select this settings in adc1



Now do this setting in advanced timer1(advanced timer is needed for PWM other wise this whole process will not work)

Your pinout  view will look like below:-



In clock configuration do these settings:-





Do below settings in Project Manager:-



Click on generate code


This will generate code for project in Keil uvision. Now navigate to main.c file in Application/User/Core.Open main.c and go to TIM1 code:-



Add register level code there:-



Now add code for  Gpioinit for Pin PA8 and PB13 and code for  adjusting PWM values according to value from potentiometer :-









Now add required functions and variables before start of main function.:-



Start timer and add call to Gpio_Init in main function
also add code in while loop:-




























 

Comments

Popular Posts