Probably the only tutorial series on stm32 I really value in the RU-vid. Thank you for opening the registers unlike other videos which dwell on the shortcut cube mx.
Hi. A small detail that I learned from a colleaugue. If you want to divide your clock by 72 you sould load PSC with 72-1 = 71 because counting from 0 to 71 is 72 intervals. You will see that your period will change from 985.4 Hz to 999.8 Hz. That's what you wanted. This is true for all counters (also ARR), if you want N then load value N-1.
I got your code to work with my bluepill. Thanks a lot for the excellent tutorial. I just started learning to program this MCU and I appreciate showing me code that works. It helps a lot with the learning curve this thing has.
thanks for the comment! keep an eye out for my website edwinfairchild.com i will be uploading new content shortly , currently its under construction so its incomplete on most pages but itll get good once i get going
I have enjoyed watching these timer tutorials, and I plan to watch the others about the STM32, as I have a project that needs an MCU and I have limited knowledge and experience with them, so thanks for putting these together! Now, if you could do one on how to use these timer PWM to individually control several small bipolar steppers, say, using some L293D Quad H-bridges to drive the motors, that would be SUPER useful, especially if that could be expanded on to control more motors than the STM32 has timer channels... I feel like that might have something to do with Bitwise operations and Shift registers? After I have that part worked out, I can work on the rest of it.
hi eddie , thank you so much for all your tutorials. your last words are priceless: make your own videos or try to explain all this to other. thats really the best way to learn and show yourself if you are able to explain all this with your own words ! you re so right
Hey man! i've found why the frequency was wrong. The preesclaer is set to "PSC + 1". So you were dividing 72MHz by 73000 resulting in 986Hz. Besides thatt, it's just a great video
Thanks a lot👍 Really helpful tutorial. We didn't have enough time to study it in the semester but you saved me. Implementing this in my project. Works perfectly. Amazing work👍
Great lesson on PWM, the best one i've found so far ! I have just started to learn this concepts and this was really helpfull and informative. By the way, i think you should have tried 72-1 on the PSC register, that might be the thing you were looking for in the end.
Sir, this is a great informative video thank you so much for uploading this. However i got a question, if you want to create a „sinusoidal“ output at least for the motor pretended would you have to set a seperate register which changes the CCR4 register figure every period or how would you do this?
If you just changed the ccr4 register it would only make you signal be wider or shorter it would not change the amplitude like a real sine wave. There is a way . I might make a video on it soon
Eddie Amaya I'm looking forward. Abo is sure. But meanwhile I spotted the mistake myself. I'm trying to understand working and signal generation of a three phase Inverter using Space vector Modulation. In Space Vector modulation, there is no need to create a sinusoidal looking waveform right? There are up and down counting timers with a ccr4 value hitting twice per time period which is enough for the motor to run.
Hey Eddie, thanks for the great videos do you know if there is a way with SWV to show an output waveform of a timer? In my case I am generating a PWM signal and I wanted to use something like SWV trace timeline to view the generated waveform, is this at all possible?
In my opinion the desired frequency is just the counter clock frequency which gives us the time of each count until we reach ARR value , right? whereas the pwm frquency is dependent on arr value.i.e the time period of ARR which translates into frquency.
Thanks man, to me it was very helpful. The counter frequency is fpwm=fosc/((ARR+1)*(PSC+1)), that is why you had a difference on your logic analyzer. I have a question I want to build a synchronous Buck Converter and need to use the PWM on TIM1 ch1..3 with it's complementary outputs and the programmable dead time for that. You talked about the fact that using TIM1 for PWM was not a good idea. The blue pill has these function only on TIM1 ch1..3 so what is the reason that you said that using TIM1 for PWM is not a good idea. Anyone that can help me is appreciated.
Do you have a video on the One Pulse Mode? I need to use an external interrupt and start a timer to generate one pulse each time it is triggered and use a 12bit analog sig to control the pulse width.
I do not have a video on one pulse mode, and I am currently working on a freelance project. But I will get it soon as I'm done in the new few days maybe by the weekend. Sorry
If I want to have a Fixed frequency and the variable duty cycle (the problem is not the duty cycle) the problem is to set a frequency above 15khz which forms utilisations to establish in the code and have a similar output since they always appear only 1khz with the scope
@@roderickdiaz3834 if you are only getting 1Khz even when having different ARR values something is wrong. I dont know what your code is , i dont know what speed the microcontroller is operating at..so idk how i can help
Hello, Thanks for the Video! I am trying to make my own board, but the problem is that when I program the micro using the stlink, it dies (smoking!). The thing is that I've designed Boot0 and Boot1 to GND directly with no resistor, and the OSC32_IN to GND and the OSC32_OUT to sink the current from a pair resistor-led connected to the 3.3v. Idk why it is burning my stm32f103c8 when I program it. Could you help me?
Awesome! Very nice tutorial. Thanks man. Actually your waveform is pretty accurate because your actual prescaler is 72+1=73 and ARR value is 1000+1=1001, So (72Mhz/(73*1001))= 985.3 hz . May I know what kind of logic analyzer is that?
It's a Saleae logic analyzer , but they have better software beta version now but the hardware is too expensive there are cheaper options , this one I got through my school discount
@@EdwinFairchild @Iman Davoudi Just a little hint: Sigrock Pulseview! (Free and Open Source) If you use the Saleae official client with clones it is not legal. I don't know how much difference is between both softwares, since my clone won't even run with the saleae software... Maybe I killed the support while trying to have it running with pulseview and after having it running it wasn't detected on the saleae one. But I am very happy with the clone and pulseview anyway ;-) PS: Thanks for your indepth video! Sadly I try to use CubeMX for configuration and don't get a higher frequency as 8MHz if I change the coresponding clocks in the clock tree ... only if I mess with the PLL Prescaler ...
One, maybe stupid question - I tried to run Timer 1, but all I got is printf output with changing numbers, but can't get anything at the output pin (GPIO A11). Does it can work as a PWM or not? Thanks.
you should really use the resources you have. Open CUBEMX select stm32f103c8 then select timer 1 and look at the channel that corresponds to PA11 and see if that channel can do PWM..... also the manual/datasheet will tell you that TIM1 is an advanced timer versus a general purpose timer so maybe it needs more configuration. You have a lot of tools to help
So it's 4 PWM outputs max? Or is it 16? As I understand it, there are 4 PWM timers so we can have 4 different (or same) time bases. Each timer can output to 4 different OC pins with different duty cycle so we can actually have 16 PWM outputs. Or am I getting something wrong here?
Hi Eddie i want to generate 512khz pwm signal from 48mhz input signal .. Can you help me to find formula which can correctly set prescaler(psc),time period(ARR ),pulse..I tried with few formula but it fails to generate output of 512khz
Hey eddie thx for the video 👍. Btw could you send the link or share the datasheet that youre using. I have download the datasheet from stm but it's not the same with yours. Thx
Yeah that's a good idea, but it's a lot more electronics involved, I was making a DC motor controller a while ago, you just have to take this pwm signal into some logic level mosfets arranged in an h-bridge. And obviously protections for shoot through and induction spikes should be implemented. The code part is the easiest it's just this PWM with some dead time implemented, I think the stm32 has a PWM mode with deadtime implemented to drive stuff like DC motors
Why timer1 of stm32f103c disables moe bits every time? And how can we remap pa0- wakeup pin? timer2 channel 1 is mixed with pa0 wake-up pin. Why stm32f103c timer1 dont give good output over 0.7v when we use over 50khz 0.5 duty cycle.
thanks for your very good demo but its hard for me to understand what i should write first and what should write at the end can you upload the working code that you test ....
You have to watch the entire video series not just the first video. And I don't upload code because then people just copy and paste and it does not stick in their brain.
You selected alternate function for timer4 on PB9 , if there are more than one alternate function on a pin how we will select particular alternate function?
It depends on what chip you are using. Some chips have a register where you select the alternate function . Alternate function is just the mode. There is also an alternate function register that can be used. Again refer to the manual. These videos are somewhat old and the chip itself is very old. I don't use F1 anymore. Will make.new videos soon
@@EdwinFairchild in stm32f407 or stm32f429 there is a register to select alternate function number AF0 or AF1 etc, to select paritcular alernate function if a pin has more than one alternate functions. but in stm32f103c8 what there is no such register, can you please make video of PWM with complementary output? waiting for your video, thankyou very much for such a great register level progamming play list
@@EdwinFairchild in stm32f407 or stm32f429 there is a register to select alternate function number AF0 or AF1 etc, to select paritcular alernate function if a pin has more than one alternate functions(pin has UART, TIM1, ADC1). but in stm32f103c8 what there is no such register( how we will connect pin to TIM1 etc if pin has UART, TIM1, ADC1 etc ), can you please make video of PWM with complementary output? waiting for your video, thankyou very much for such a great register level progamming play list
@@AS-zs7jw in F1 , if you select a pin for alternate function then configure a Timer to use that pin then it will use it. If you configure UART then uART will use it. The point is you don't manually select which alternate fucntion. You just configure to accept alternate function and then when you set up a peripheral that uses it then that's it. It is the programmers job to know which peripherals use it and not to use 2 peripherals on the same pin. If 2 pin has UART, and Timer, and I2C function , then it's your job to make sure you're don't mess up and try to use multiple things on the same pin. And you can refer to the datasheet to see pin functions.
Timers in PWM is how you acheive PWM lol ... its the same thing ... except it sounds weird if you always say "i used timer in pwm mode to fade this LED" so people just say PWM
@@EdwinFairchild no sir there is a difference , which i might guess u do know , have u ever used TIVA , kindly check its datasheet. We have a PWM register to generate PWM instead of doing the same work by using timers . Do let me know if im wrong , ill share the link where PWM is not generated using timers , however by using the PWM module .
@@ShivamKumar-ky6vm ok well yeah now youre talking about a different micro, since the questioned was posed in an STM32 timers video the answer still stand with respect to this context. What other Micros do is a muc broader conversaation. How ever if you get down to the design of that TIVA im sure when you look at the design or a timer and a hardware dedicated pwm peripheral you will see a whole lot of similarities , maybe they offer a softcore on an FPGA so you can get an idea of how similar that will be compared to a timer.
It is a external device. the original one is called saleae logic analyzer and can be connected externaly to the controller. There are cheap clones avaiable as well which nicely runs with a software called pulseview
rewatch @23:19 you are setting bits pin 9 for general purpose output , this is PWM which is an alternate function.... general purpose means just to drive pin high or low...PWM is totaly different. ... 7 and 6 need to be set to Alternate function push pull
@@EdwinFairchild Oh, thanks. I am lost with all that names. What I am used to is thinking about switches inside registers, sorted by numbers above the name, not names itself. Now I am halfway, and it works. Thanks! Here is how now looks working version (still need to "translate" words into numbers for the rest): #include "stm32f10x.h" #include #include "string.h" #include "stdlib.h" #include "stdarg.h" #define DEBUG_UART USART1 void delay(long cycles); void strobe(void); void usart1enable(void); void gpioB1enable(void); void gpioB9enable(void); void timer4enablePWM(void); static void printMsg(char *msg, ...); void delay(long cycles) { while(cycles >0) cycles--; } void strobe() { GPIOB->BSRR = (1CRH &= ~(GPIO_CRH_CNF9_0); //port A9(6) //clkPer/(baudRx16)=72MHZ/9600x16) //468.75 USART1->BRR = 0x1D4C; //468 = 0x1D4, 0.75*16=12 or 0xC, together 0x1D4C USART1->CR1 |= USART_CR1_TE; //whatever USART1->CR1 |= USART_CR1_UE; //whatever } void gpioB1enable() //enabling pin B1 for push-pull, 2 MHz, output { RCC->APB2ENR |=(1
Tweak those PWM's a bit further.. throw on a 10k pot, hook up the gpios to a stereo and get this.. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-sf8gkzmJMAo.html ;-)