Advertisement
ScienceGeyser

PWM_Fan_Test

May 13th, 2022
774
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Adafruit_PWMServoDriver.h>
  3. #include <FreqPeriodCounter.h>
  4.  
  5. Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);
  6.  
  7. bool serialVerbose = false;
  8. uint16_t setPoint[16]; // number of set points
  9. uint16_t initsetPointVal = 512; // setPoint initialization value
  10. float setPointRPM = 320.0;
  11. float valueRPM = 0.0;
  12. float counterPeriod = 0.0;
  13. uint16_t pulsePerRev = 4; // May depend on fan motor design. Could be 3 for the 9 mm motors
  14. int numReadings = 1;
  15. #define maxReadings 1000
  16. float readings[maxReadings];
  17. uint16_t readIndex = 0;
  18. float total = 0;
  19. float average = 0;
  20.  
  21.  
  22. /*
  23. 512  =  320 RPM
  24. 640  =  350 RPM
  25. 768  =  660 RPM
  26. 896  =  925 RPM
  27. 1024 = 1150 RPM
  28. 1152 = 1400 RPM
  29. 1280 = 1670 PRM
  30. 1408 = 1930 RPM
  31. 1536 = 2225 RPM
  32. 1664 = 2682 RPM ** probable top stir speed
  33. 2048 =  RPM
  34. 3072 =  7500 RPM ** Starting Speed **
  35. */
  36.  
  37. const byte counterPin = 5;
  38. const byte counterInterrupt = 5; // not sure if this works with D5
  39.  
  40. FreqPeriodCounter counter(counterPin, micros, 0);
  41.  
  42. void setup() {
  43.   SerialUSB.begin(115200);
  44.   delay(1000);
  45.   //while (!SerialUSB) {
  46.   //  delay(1); // wait for serial port to connect. Needed for native USB port only
  47.   //}
  48.   SerialUSB.println("16 channel PWM test Plus Period Counter!");
  49.   //*/
  50.  
  51.   pinMode(counterPin, PIO_EXTINT);
  52.   attachInterrupt(counterInterrupt, counterISR, CHANGE);
  53.  
  54.   for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  55.     readings[thisReading] = 0;
  56.   }
  57.  
  58.   pwm.begin();
  59.   // pwm.setOscillatorFrequency(27000000);
  60.   pwm.setPWMFreq(1000);  // This is the maximum PWM frequency
  61.   // Wire.setClock(400000);
  62.  
  63.   // initialize setPoint array Values
  64.   for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
  65.       setPoint[pwmnum] = initsetPointVal;
  66.   }
  67.   // Set to zero for testing
  68.   for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
  69.       pwm.setPWM(pwmnum, 0, 0);
  70.   }
  71.   delay(5000);
  72.   // Start Spinning
  73.    startSequence(3072, initsetPointVal, 50);
  74.  
  75. }
  76.  
  77. void loop() {
  78.   //delay(100);
  79.   if(counter.ready()){//*/
  80.     counterPeriod = counter.period;
  81.     valueRPM = (1/((counterPeriod*pulsePerRev/1000)/1000))*60;
  82.     SerialUSB.print(valueRPM, 1);
  83.     total = total - readings[readIndex];
  84.     readings[readIndex] = valueRPM;
  85.     total = total + readings[readIndex];
  86.     readIndex++;
  87.     if (readIndex >= numReadings){
  88.       readIndex = 0;
  89.     }
  90.     average = total/numReadings;
  91.     SerialUSB.print("\t");
  92.     SerialUSB.println(average, 1);
  93.    
  94.     if(serialVerbose){
  95.     SerialUSB.print(counter.period);
  96.     SerialUSB.print("\t");
  97.     SerialUSB.print(counter.pulseWidth);
  98.     SerialUSB.print("\t");
  99.     SerialUSB.println(counter.pulseWidthLow);
  100.     }
  101.   }//*/
  102.  
  103. }
  104.  
  105. void startSequence(int startVal, int finalVal, int startDuration){
  106.   for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
  107.       pwm.setPWM(pwmnum, 0, startVal);
  108.     }
  109.   delay(startDuration);
  110.   for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
  111.       pwm.setPWM(pwmnum, 0, setPoint[pwmnum]);
  112.     }
  113. }
  114.  
  115. void counterISR(){
  116.   counter.poll();
  117. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement