 Hello and welcome on our STM48O cube IDE by Ziggs training session. In this part, we will use Timer to control our green LED in PWM generation node. The objective of this session is to configure the timer in a PWM mode to bring the green LED to control it. So instead of using the software code, we will let Timer to do the job and toggle the LED. This time we will use PA5 as an alternate function and we will let Timer2Channel1 to control it. Our system will work on an internal clock 16 MHz divided by 2 this time. So our system clock would be 8 MHz. We would like to keep the same timing like in previous exercises when we controlled green LED by software. To do this, we need 1Hz period with 50% duty cycle PWM signal. Let's start a new project with an STM48O cube IDE. I would use already existing workspace with previous exercises. To start a new project, I can go to file new STM48O project or directly to this icon and select STM48O project as well. It is starting with the target selection. I'm selecting my microcontroller, so it is G071RB. I'm selecting the LQFP64 package which is present on the nuclear board we are using during this hands-on. Selecting next, the next step is project name. I would call it G0PWM and press finish. Now it is initializing the device configuration window which is in fact the STM48O cube MX application or plugin within Eclipse environment. It takes some time. As a result, we should see our G0PWM IOC file which is the configuration file. The file for the configuration of the microcontroller. I can zoom it out for better visibility of the microcontroller. You can use either those plus minus zoom buttons over here or the wheel on the mouse. Okay, the first thing would be the selection of debug interface. So I will go to the system core CIS Serial Wire. I can see PA13 and 14 dedicated for debug interfaces WD. The second thing is a configuration of our PA5 pin which will be controlled by Timer 2, channel 1. We can do it in two different ways. The first is by the selection of the timers. So I would select Timers, Timer 2. From this I would select the clock source as internal clock. I will not select anything from this live mode nor trigger source because we will not use any timer cooperation, timer synchronization of this library. So only the clock source as internal clock and I would use channel 1 as PWM generation channel 1. Please be careful and do not select this PWM generational output because it would generate PWM signal but it will be not connected to the pin but used internally. Okay, so we are selecting PWM generation channel 1 and it is automatically by default routed to PA0 pin which is not our choice. To change it I press control button on my keyboard and left button on mouse. And by clicking the left button on mouse I can see the alternate locations of this function. I can see that one of those is PA5. So I am keeping this control button and left button on mouse pressed and I am just dragging this PA0 pin to PA5 and releasing the left button on mouse and control button on the keyboard. As a result I am migrating my function, my timer to channel 1 location from PA0 to PA5. So this is one of the way how we can do it. The second way is a bit different. So please let me come back to the original setting. I am pressing the left button on mouse and selecting reset state and instead of selecting the roll on the timer I am going to PA5 directly and pressing left button on mouse and I am selecting timer to channel 1. Now I can see the pin over this button. Its label is changed to timer to underscore channel 1 and the color of the pin has changed from grey to yellow. Which means that this pin is reserved, is booked by me and it needs to be configured. To do this I am going to my timer to configuration into the mode window and within channel 1 I am selecting the roll of this pin, which should be PWM generation channel 1. After this operation I can see that the color of the pin has changed to green which means that we've got this pin configured properly. So we've got configuration of the pin. The next step would be the configuration of the timer itself. Before this let's do the short exercise on the clock configuration. As you can see the default settings of the clock configuration is the following. We are using internal clock source, a high speed internal RC, which is 16 MHz, and then this clock is going directly to the system, clocking all other peripherals. What we will do within this very simple exercise will change this 16 MHz within this high speed clock on the bus to 8. I would just change this 16 value over here into 8 and press enter. As you can see application automatically adopted its settings to my target value. All the system is now working on 8 MHz including all the peripherals. Okay, let's come back to our application. So I'm switching back to pinouts and configuration. And now we will focus more on the configuration of the selected timer. I will do some space over here. Mode is already selected, we will not change anything. So the clock source, internal clock and channel 1, PWM generation channel 1. Okay, so the configuration. When the timer configuration we've got quite a lot of fields which we can configure. In our case we need to configure the timer to work in PWM mode with the frequency of 1 Hz and duty cycle 50%. How to do this? We've got the input clock for the timer on the level of 8 MHz. So we need to divide it somehow. For this we've got a few possibilities. The most convenient ones are the pre-scaler. This is the first component we can use here. So we need to downgrade somehow these 8 MHz to smaller value. So I would divide it first by 128. I put 127 because in the register settings there is one edit automatically to the value PSC which we've got in this field. This is why to have division 128 we need to put 127 in this field. Okay, so as a result to the timer we will have instead of 8 MHz we will have 62 500 Hz input. Then the counter will be up counting and the second point we should configure is a period. So the value to which the timer would calculate. And how to do 1 Hz from 62.5 kHz. We need to divide this 62 500 Hz by 62 499. And again we are subtracting 1 from the value to be put due to the fact that we are calculating from 0. So in fact as a result for this we would create 1 Hz signal. So we've got 1 Hz. The only missing point is to set the duty cycle. The duty cycle we are configuring within the pulse and its value should be related to the period value. If you would like to have 50% we need to put half of it so it will be 31 250. In this case as a result of these operations we would have on PA5 pin which would reflect the timer to channel 1 1 Hz signal with 50% duty cycle. Now we are ready to generate the project. So I just press CTRL S, CTRL CIFE. Now we see that something is happening. Okay, let's go to the source. So I'm going to the sources, main.c. And again I can see a lot of user code, beginning user code and sections which are dedicated to my code to be protected against removal during the regeneration. And what I need to do right now is to start the timer. Because my generated application is only the configuration done by me. And I need to start the timer. We need to start the timer only once before while one loop has been called. So I'm using to this purpose user code begin to section. So I would start with HAL team because it's a for the timer. And I need to select the proper functions. As you can see there are quite a lot of them. So I would just limit them by underscore. And let's select something which would generate some PWM for us. Okay, here there are some names. And we can see three. The first one is just starting PWM signal without an interrupt or DMA configuration. Then there is as well start PWM start DMA or IT, which are using DMA or interrupt generation on its functionality. We do not use it. We will select PWM start function. The first argument is a handler to the timer we are using. Here you can see the rest as well, the information that we can select this timer to from our settings. And then the second one is a channel. We will select team underscore channel. And we can see we can select the proper channel. Channel one is our choice. Okay, control save to save the file. And now let's try to build it. So I press the hammer. Okay, and now we can start the debug session. So I press this back icon. I select STM32MCUCC++ application. Press OK. I will not change anything in the configuration. I press OK. And now our application will be switched to the debug perspective. If you press resume button, we can see our green LED tongue link like before, but this time it is controlled directly by the timer. To exit from debug mode, we click terminate. Thank you for watching this video.