This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:practical:hardware:sut:stm32:emb1a_1 [2024/03/03 09:01] – [Start] ktokarz | en:iot-open:practical:hardware:sut:stm32:emb1a_1 [2024/03/25 08:23] (current) – [Project information] pczekalski | ||
---|---|---|---|
Line 25: | Line 25: | ||
#include < | #include < | ||
</ | </ | ||
- | The hardware timer uses internal timer modules and allows us to define channels attached to the timer. | + | The hardware timer library |
=== Step 1 === | === Step 1 === | ||
- | Include the library, and define PIN assignments to channels and PWM frequency (100Hz): | + | Include the library, and define PIN assignments to channels and PWM frequency (50Hz): |
<code c> | <code c> | ||
#include < | #include < | ||
- | // Pins definition for RGB LED | + | // Pin definition for the fan |
- | # | + | # |
- | #define LED_PWM_BLUE | + | |
- | #define LED_PWM_RED | + | |
- | # | + | # |
</ | </ | ||
- | GPIO pins controlling | + | The GPIO pin controlling |
=== Step 2 === | === Step 2 === | ||
Line 44: | Line 42: | ||
<code c> | <code c> | ||
// PWM variables definitions | // PWM variables definitions | ||
- | HardwareTimer *MyTimLED; | + | HardwareTimer *MyTimFan; //Hardware Timer class for Fan |
- | uint32_t | + | uint32_t |
</ | </ | ||
- | Instantiate the timer object and initialise the 3 channels | + | Instantiate the timer object and initialise the fan channel |
<code c> | <code c> | ||
- | //Create Timer instance type based on the chosen LED pin (the same timer is used for all LEDs). | + | //Create Timer instance type based on the fan pin (the same timer is used for servo if needed). |
- | TIM_TypeDef *Instance | + | TIM_TypeDef *FanInstance |
| | ||
- | // | + | // |
- | channelR | + | channelFAN |
- | channelG = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_GREEN), | + | |
- | channelB = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_BLUE), PinMap_PWM)); | + | |
- | // Instantiate HardwareTimer object. Thanks to ' | + | // |
- | MyTimLED | + | MyTimFan |
- | // Configure and start PWM | + | //Configure and start PWM |
- | MyTimLED-> | + | MyTimFan-> |
- | MyTimLED-> | + | |
- | MyTimLED-> | + | |
</ | </ | ||
- | To modify the intensity of the chosen LED after initialisation use the setCaptureCompare function. In the following example, the duty_cycle_value can vary between 0 and 100. | + | To modify the fan's rotation speed after initialisation use the setCaptureCompare function. In the following example, the duty_cycle_value can vary between 0 and 100. |
<code c> | <code c> | ||
- | MyTimLED-> | + | MyTimFan-> |
+ | }; | ||
</ | </ | ||
=== Step 3 === | === Step 3 === | ||
- | Write a loop for each colour (R, G, then B) to light the colour | + | Write a loop to change |
- | <note important> | + | <note important> |
- | Mind to compose code to increase | + | Mind to compose |
<code c> | <code c> | ||
- | // Increase | + | // Increase |
- | for (int duty_cycle_value = 0; duty_cycle_value <= 50; duty_cycle_value++) { | + | for (int duty_cycle_value = 0; duty_cycle_value <= 100; duty_cycle_value++) { |
- | // Gradually increase duty cycle for Red LED | + | // Gradually increase duty cycle for fan speed |
- | | + | |
delay(20); // Delay for smooth transition | delay(20); // Delay for smooth transition | ||
} | } | ||
- | |||
- | delay(100); | ||
- | |||
- | // Decrease brightness | ||
- | for (int duty_cycle_value = 50; duty_cycle_value >= 0; duty_cycle_value--) { | ||
- | // Gradually decrease duty cycle for Red LED | ||
- | MyTimLED-> | ||
- | delay(20); // Delay for smooth transition | ||
- | } | ||
- | |||
</ | </ | ||
==== Result validation ==== | ==== Result validation ==== | ||
- | You should be able to observe the pulsing colours of the RGB LED, increasing and decreasing brightness linearly. | + | You should be able to observe the changes in the fan's rotation speed. It is advisable to connect this scenario with the scenario for reading the pressure measurements. |
===== FAQ ===== | ===== FAQ ===== | ||
- | **What is the maximum number of channels? | + | **What is the maximum number of channels? |
\\ | \\ | ||
- | **What is the maximum bit resolution for PWM?**: Maximum resolution is 16 bits. Note that we can express the duty cycle in a variety of formats. In the presented example we expressed it in percentage so it varies between 1 and 100. It can be also expressed in ticks, Hertz, microseconds, | + | **What is the maximum bit resolution for PWM?**: Maximum resolution is 16 bits. Note that we can express the duty cycle in a variety of formats. In the presented example we expressed it in percentage so it varies between 1 and 100. It can be also expressed in ticks, microseconds, |
\\ | \\ | ||
- | **What PWM frequency should I use? | + | **What PWM frequency should I use? |
+ | <WRAP noprint> | ||
===== Project information ===== | ===== Project information ===== | ||
{{: | {{: | ||
Line 120: | Line 106: | ||
{{: | {{: | ||
</ | </ | ||
- | + | </ | |
- | + |