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.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:edu:zmitac:s2:aih:lab:stm32fan [2024/01/13 19:59] – ktokarzen:edu:zmitac:s2:aih:lab:stm32fan [2024/01/16 09:37] (current) – ktokarz
Line 8: Line 8:
 </table> </table>
 In the stm32duino examples ((https://github.com/stm32duino)), there is a simple program which allows us to control a single LED with PWM. It is named "All-in-one_setPWM" ((https://github.com/stm32duino/STM32Examples/tree/main/examples/Peripherals/HardwareTimer/All-in-one_setPWM)). It uses hardware timer output connected directly to an LED. We will modify this example to control the fan and servo. In the stm32duino examples ((https://github.com/stm32duino)), there is a simple program which allows us to control a single LED with PWM. It is named "All-in-one_setPWM" ((https://github.com/stm32duino/STM32Examples/tree/main/examples/Peripherals/HardwareTimer/All-in-one_setPWM)). It uses hardware timer output connected directly to an LED. We will modify this example to control the fan and servo.
-The code is composed of two files FanSrvTest.h and FanSrvTest.cpp. The *.h file defines pins for the fan and servo, and declares variables for the timer, channels and timer class. +At the beginning of the code, we need to define pins. 
-<code> +<code c>
-#include "Arduino.h" +
- +
-#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
 +</code> 
 +<note> 
 +Pin names use STM naming, not Arduino naming. 
 +</note> 
 +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;        //General hardware instance TIM_TypeDef *SRVInstance;        //General hardware instance
 uint32_t channelSRV, channelFAN; //2 channels uint32_t channelSRV, channelFAN; //2 channels
 HardwareTimer *MyTimServo;       //Hardware Timer class for Fan and Servo PWM HardwareTimer *MyTimServo;       //Hardware Timer class for Fan and Servo PWM
- 
-//Prototypes of funcions 
-void FanSrvSetup(); 
-void fFanSet(int pPercent);      //Change the fan speed (0-100) 
-void fSrvSet(int pAngle);        //Change the servo angle (0-180) 
- 
-#endif 
 </code> </code>
-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 and initial duty cycle for both channels. Fan frequency is not essential, so the base frequency for the timer is set based on the requirements of the servo and equals 50Hz. The initial duty cycle is set to 5%. +At the beginning of the code timer object must be created and two channels activated. Notice that because there is one timer, the base frequency for the fan and servo is the same. Certainly, the duty cycle can be different. 
-The fFanSet() function sets the speed of the fan. The parameter provided is expressed in percentages and can vary between 0 and 100. +<note> 
-The fSrvSet() function sets the angle of the servo. The parameter is expressed in degrees and can vary between 0 and 180. +The frequency used must fulfil the requirements of the servo. The one used in the laboratory stand requires a PWM signal with a pulse sent every 20ms (50Hz). The duty cycle can vary between 500us (microseconds) and 1500us, where 1000us gives 90 degrees of the angle. 
-<code> +</note> 
-#include "FanSrvTest.h" +<code c>
- +
-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), PinMap_PWM);   TIM_TypeDef *SRVInstance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(SERVO_PWM_PIN), PinMap_PWM);
Line 48: Line 38:
   // Configure and start PWM   // Configure and start PWM
   MyTimServo->setPWM(channelSRV,SERVO_PWM_PIN,50,5); // 50 Hertz, 5% dutycycle.   MyTimServo->setPWM(channelSRV,SERVO_PWM_PIN,50,5); // 50 Hertz, 5% dutycycle.
-  MyTimServo->setPWM(channelFAN,FAN_PWM_PIN,50,5);   // 50 Hertz, 5% dutycycle. +  MyTimServo->setPWM(channelFAN,FAN_PWM_PIN,50,50);   // 50 Hertz, 50% dutycycle. 
-}; +</code> 
- +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->setCaptureCompare(channelFAN, FanSpeed, PERCENT_COMPARE_FORMAT); 
-  MyTimServo->setCaptureCompare(channelFAN, pSpeed, PERCENT_COMPARE_FORMAT); //modify duty cycle +  MyTimServo->setCaptureCompare(channelSRV, SrvTime, MICROSEC_COMPARE_FORMAT);
-}; +
- +
-void fSrvSet(int pAngle) +
-{ +
-  if (pAngle<0) pAngle=0;        //check boundaries of angle parameter +
-  if (pAngle>180) pAngle=180; +
-  int i_srv=500+(100*pAngle)/18; //minimal duty cycle is 0,5ms, maximal 1,5ms +
-  MyTimServo->setCaptureCompare(channelSRV, i_srv, MICROSEC_COMPARE_FORMAT);  //modify duty cycle +
-};+
 </code> </code>
en/edu/zmitac/s2/aih/lab/stm32fan.1705172375.txt.gz · Last modified: by ktokarz