No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:stm32fan
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s2:aih:lab:stm32fan [2024/01/13 20:32] – ktokarz | en:edu:zmitac:s2:aih:lab:stm32fan [2024/01/16 09:37] (current) – ktokarz | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| </ | </ | ||
| In the stm32duino examples ((https:// | In the stm32duino examples ((https:// | ||
| - | The code is composed | + | At the beginning |
| - | **FanSrvTest.h** | + | < |
| - | < | + | |
| - | #include " | + | |
| - | #include < | + | |
| - | + | ||
| - | #ifndef _SRV_TestLib | + | |
| - | #define _SRV_TestLib | + | |
| // PWM timer 2 for Servo and Fan control | // PWM timer 2 for Servo and Fan control | ||
| #define SERVO_PWM_PIN PA0 | #define SERVO_PWM_PIN PA0 | ||
| #define FAN_PWM_PIN PA1 | #define FAN_PWM_PIN PA1 | ||
| + | </ | ||
| + | < | ||
| + | Pin names use STM naming, not Arduino naming. | ||
| + | </ | ||
| + | Both outputs are controlled with the same timer, so only one instance of the timer is needed. It will control two channels one for fan, one for servo. | ||
| + | <code c> | ||
| // PWM variables definitions | // PWM variables definitions | ||
| TIM_TypeDef *SRVInstance; | TIM_TypeDef *SRVInstance; | ||
| uint32_t channelSRV, channelFAN; //2 channels | uint32_t channelSRV, channelFAN; //2 channels | ||
| HardwareTimer *MyTimServo; | HardwareTimer *MyTimServo; | ||
| - | |||
| - | // | ||
| - | void FanSrvSetup(); | ||
| - | void fFanSet(int pPercent); | ||
| - | void fSrvSet(int pAngle); | ||
| - | |||
| - | #endif | ||
| </ | </ | ||
| - | The *.cpp file implements three functions. The FanSrvSetup() function creates a timer instance, assigns channels to timer outputs and pins, and creates a timer class object. After the successful creation, it configures the frequency | + | At the beginning of the code timer object |
| - | The fFanSet() function sets the speed of the fan. The parameter provided is expressed | + | < |
| - | The fSrvSet() function sets the angle of the servo. The parameter is expressed in degrees and can vary between | + | The frequency used must fulfil |
| - | **FanSrvTest.cpp** | + | </ |
| - | < | + | < |
| - | #include " | + | |
| - | + | ||
| - | void FanSrvSetup() | + | |
| - | { | + | |
| //Create Timer instance type based on the servo pin (the same timer is used for fan). | //Create Timer instance type based on the servo pin (the same timer is used for fan). | ||
| TIM_TypeDef *SRVInstance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(SERVO_PWM_PIN), | TIM_TypeDef *SRVInstance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(SERVO_PWM_PIN), | ||
| Line 51: | Line 38: | ||
| // Configure and start PWM | // Configure and start PWM | ||
| MyTimServo-> | MyTimServo-> | ||
| - | MyTimServo-> | + | MyTimServo-> |
| - | }; | + | </ |
| - | + | To modify the duty cycle it is possible to use the function setCaptureCompare() which has the possibility of passing the argument as the value of microseconds or percents. The following lines show examples of setting that fan speed in percents (provided by FanSpeed variable), and servo range in duty cycle time (provided by SrvTime variable). | |
| - | void fFanSet(int pSpeed) | + | <code c> |
| - | { | + | MyTimServo-> |
| - | MyTimServo-> | + | MyTimServo-> |
| - | }; | + | |
| - | + | ||
| - | void fSrvSet(int pAngle) | + | |
| - | { | + | |
| - | if (pAngle< | + | |
| - | if (pAngle> | + | |
| - | int i_srv=500+(100*pAngle)/ | + | |
| - | MyTimServo-> | + | |
| - | }; | + | |
| </ | </ | ||
en/edu/zmitac/s2/aih/lab/stm32fan.1705174359.txt.gz · Last modified: by ktokarz
