You can access the source code files via this link.
The project involves controlling the brightness of an LED using PWM. The STM32 microcontroller will generate a PWM signal using the TIMER peripheral and output it to an LED connected to a GPIO pin. The duty cycle of the PWM signal will be determined by the analog voltage level from an ADC channel connected to a potentiometer.
To implement this system, we can use three different methods to read the ADC result and set the PWM duty cycle:
1-ADC is used in blocking mode (polling): In this method, the microcontroller will continuously check the status of the ADC channel in a blocking manner. When the ADC conversion is complete, the microcontroller will move the result to the timer CCR register to set the PWM duty cycle. This method is simple and easy to implement but can be less efficient and may block other tasks from executing.
2- ADC is used in non-blocking mode (Interrupt): In this method, the microcontroller will configure the ADC to generate an interrupt when the conversion is complete. When the interrupt occurs, the microcontroller will move the result to the timer CCR register to set the PWM duty cycle. This method is more efficient and allows other tasks to execute while waiting for the ADC conversion to complete.
3- ADC is used in non-blocking mode (DMA): In this method, the microcontroller will configure the DMA to transfer the ADC result directly to the timer CCR register. This method is the most efficient as it eliminates the need for CPU intervention in moving the ADC result to the timer CCR register.
To get started with this project, we can begin by configuring the ADC to read the analog voltage level from the potentiometer. We can then configure the TIMER peripheral in PWM mode to output the signal to the LED. Next, we can implement the three different methods for reading the ADC result and moving it to the timer CCR register to set the PWM duty cycle.Additionally, we will simulate the circuit design using Proteus software to test the project before implementing it in hardware.
Step One:- Open CubeMX & Create New Project Choose The Target MCU STM32F103C6 & Double-Click Its Name
- Go To The Clock Configuration & Set The System Clock To 8MHz
Configuration for the ADC
PLL,IT,DMA
Modes:
- In the Categories tab, select the ADC1 & enable IN7
- In the Parameter settings tab, Enable the Continuous Conversion Mode
- In the NVIC settings tab, Enable ADC1 and ADC2 global Interrupts
- In the DMA settings tab, enable the DMA for the ADC1 Channel 1
Configuration for the
TIMER PWM
Mode:
- In the Categories tab, select the TIM2 & enable (Internal Clock & PWM Generation Channel 2)
- In the Parameter settings tab, set the Counter Peroid = 65535
- Generate The Initialization Code & Open The Project In CubeIDE
- Write The Application Layer Code
- Open Proteus & Create New Project and click next
- Click on Pick Device
- Search for STM32F103C6 & SW-SPDT & SW-ROT3 & POT & (LED_RED, BlUE, PINK)
- Click on Virtual Instruments Mode then choose OSCILLOSCOPE
- Click on Terminal Mode then choose (DEFAULT & POWER &GROUND)
- finally make the circuit below and start the simulation
If you have any questions or suggestions don't hesitate to leave a comment below
Comments