 Hello, this is Lab 2 of our STM32-C0 MOOC. In this lab, we will look at the timer peripherals of the STM32-C0 and its PWM mode. The objective of this lab is to learn about the more advanced peripherals of the STM32-C0, which is the timer. In this lab, we are going to configure a timer in a PWM mode to bring the LED that we previously controlled with a GPIO. PWM stands for Pulse Width Modulations. PF5, which is connected to the green LED on your nuclear board, has a Timer Channel Atalent Function, which is Timer 1 Channel 1, and will be using it. This is an overview of the Timer Peripheral. It provides timing resources internally and externally. Internally to the STM32 for triggers and time-based purposes. Externally for outputs and inputs on what we refer to as Timer Channels. In this example, we use an external channel output for waveform generations or PWM for pulse width modulation. Let's get started. So we are going back to STM32-CUBID. So if you don't have it open, please open it again. And we're going to create a new project, a file new STM42 project. Then from this view, as before, we're going to type STM32-C031-C6-T6, which is the STM42 that is on your nuclear board, and that you can see here by the way. So we're going to select that. Next, we're going to give a name to the project. So STM42-C0-PWM, pour exemple. And finish. OK. In the STM32-CUBID project, we are going to add the Timer 1 channel 1 to blink the LD4, the green LED that is connected to PA5. So in the Pinouts and Configuration tab, you will expand the Timers category, click on Timer 1, and then select the internal clock for source clock, and set the Timer 1 to PWM generation CH1 for channel 1. In the Pinout and Configuration tab, we're going to expand Timers, select Team 1 for Timer 1. So for the clock source, we'll use internal clock. So we will configure this later on, but we'll use internal clock. Then, so this is channel 1 CH1, we're going to select, so scroll down, the PWM generation CH1. And now, we have one IO that has been configured as Team 1 channel 1. By default, the tool configured Timer 1 channel 1 to PA0. We want to remap it to PA5. To do this, we're going to use a remaping feature of the STM42-CUBID. So you're going to put your mouse over PA0, then on your keyboard, press Control, and at the same time, hold it down with the Control left click on your mouse. This will show the different IOs, and we're going to show you how to do it. We want to remap Timer 1 channel 1, or Team 1 channel 1, to PA5. To do this, we're going to use the remaping feature and this will show the different IOs where we can remap the feature Timer 1 channel 1. So the attend function. So now to remap it, you're going to drag and drop from PA0 to PA5 here. So still, you know why holding Control key and your left click of your mouse. And now you can drag and drop it. So as you can see now, we have a PA5 that is Timer 1 channel 1 attend function. So if you want to see, there you go. For the Timer parameters, so let's say that we want to generate a Timer PWM output signal with a one second period, or one hertz, and a 50% duty cycle. The Timer input clock frequency, or TPCLK, is set to 6 MHz as an example. We will set the pre-scaler for the Timer to 128. The resulting Timer counter clock, or CK underscore CNT, equal TPCLK divided by the pre-scaler. So 6 MHz divided by 128, which gives us 46,875 Hz. To get one hertz, or one second period, the counter period needs to be set to 46,875. The counter period equals the Timer counter clock divided by the period T. So equal 46,875 divided by one. To get 50% duty cycle, the pulse needs to be set to the counter period so 23,447. First, in the clock configuration, we will set TPCLK to 6 MHz. Now, in the Pinout and Configuration tab, we will configure the Timer one. First, go to the parameter settings of Timer one, so Tim one. For the period, we'll configure the one hertz Timer. So first, we'll set the value that is here, the pre-scaler, or PSE. And the PSE value is actually the pre-scaler minus one. So we will set it as 128 minus one, which is 127. Then for the counter period, we'll set it as 46,875 as we calculated before. Now, for the PWM generation channel one, so the channel one PWM, we will set the pulse to 23,437 right here. First, we're going to configure the clock configuration. So to do this, click on clock configuration tab and we want to set TPCLK to 6 MHz. So press 6 and then press Enter. So this will set the clock configuration to have TPCLK, which is the input clock of the Timer Peripheral, Timer one, to 6 MHz. Now go back to the Pinout and Configuration tab. So Timer one. And we are going to the parameter settings, so in the configuration. So you can expand a little bit to see more what is going on. So we are in the parameter setting tab and we are going to input the data to configure our timer. So the pre-scaler, as we said before, is going to be set to 127. Now for the counter period, we're going to set it to 46,875. Center and now scroll down to the PWM generation channel one. So we want to set the pulse to 23,437. Perfect. So we have finished our configuration. Now we can save our project. So this will also generate the code. Press File, yes, save. And yes, we're going to also change the perspective to a C and C++ perspective because we're going to add some code. So press yes. This will open main.C as you can see here in the user code section 2 of our main function, so main.C. We're going to add a function here to launch the timer and the channel one PWM. So this is a function from our HAL library. So this is HAL underscore team underscore PWM underscore start. And then as a parameter, you're going to give the handler of the timer one, which is h team one. And then as a second parameter, team underscore channel underscore one. So this will launch the PWM signal on channel one of timer one. Now save your file. File save or just the icon right there. And now we can build the project. So remember the icon right here, the hammer. So click on it. This will build your project. Okay. Build zero errors, zero warning. This is great. Now you can plug your board. So your nuclear board, if it's not plugged already. So plug it again to your laptop or to your host machine. And now we can enter the debug word. So remember this guy here. So this icon, the bug. So that will permit. So click on it. That will permit to enter the debug session. Going to switch perspective to be debug session. And now you can execute a code. So on resume. And now you can see on your board the green LED that is blinking at one hertz, you know, like period with a 50% duty cycle. That's perfect. We can now stop, you know, the execution of the code clicking here on this termination icon. And you can close this project. Close project.