Advertisement
Guest User

MicroserverG8_FanCtrl.ino

a guest
May 4th, 2014
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include <TimerThree.h>
  2.  
  3. #define PWM_OUT_PIN 14      //teensy++ 2.0 pin C4  YELLOW WIRE
  4. #define LED_PIN PIN_D6      //teensy++ 2.0 pin D6 (build-in LED)
  5. #define MAX_PWM 1024
  6.  
  7. #define PWM_MEASURE_PIN 0   //teensy++ 2.0 pin D0  GREEN WIRE
  8.  
  9. #define CYCLE_TIME_MILLIS 50
  10. #define SAMPLES 20
  11.  
  12. static double dutyCycleOffset = 0.1;
  13. static double minDutyCycle = 0.9; //10% RPM
  14.  
  15. static unsigned long lastmillis = 0;
  16. static byte readDutyCycleCounter = 0;
  17.  
  18. static double readDuties[SAMPLES];
  19. static double readDutyAvg;
  20.  
  21. static double readDuty;
  22. static double freq;
  23. static long highTime = 0;
  24. static long lowTime = 0;
  25. static long tempPulse;
  26.  
  27. static double targetDuty;
  28.  
  29. static boolean errorState = false;
  30.  
  31. void setup()
  32. {
  33.   pinMode(PWM_OUT_PIN, OUTPUT);
  34.   pinMode(LED_PIN, OUTPUT);
  35.   Timer3.initialize(40);               // 40ยตs period -> 25kHz
  36.   Timer3.pwm(PWM_OUT_PIN, 0);          // setup pwm
  37.  
  38.   pinMode(PWM_MEASURE_PIN,INPUT_PULLUP);
  39.   Serial.begin(115200);
  40. }
  41.  
  42. void loop()
  43. {
  44.  
  45.   if (millis() - lastmillis >= (CYCLE_TIME_MILLIS)){
  46.     errorState = false;
  47.    
  48.     // read PWM
  49.     readPWM();
  50.    
  51.     //Serial.print(readDutyCycleCounter);
  52.     //Serial.print("\t");
  53.     //Serial.println(readDuty);
  54.    
  55.     readDuties[readDutyCycleCounter] = readDuty;
  56.    
  57.     if (readDutyCycleCounter == 0) {
  58.       readDutyAvg = 0;
  59.       for (byte i=0; i<SAMPLES; i++) {
  60.         readDutyAvg += readDuties[i];
  61.       }
  62.       readDutyAvg = readDutyAvg / SAMPLES;
  63.      
  64.     // set PWM
  65.    
  66.     // REGION ERR: < 3% RPM OR PWM FREQ < 20kHz
  67.     if (readDutyAvg < 0.1 || readDutyAvg >= 0.97 || freq < 20000) {
  68.       targetDuty = 0.0;
  69.       errorState = true;
  70.       Serial.println("\terrorState");
  71.     }    
  72.     // REGION 0:  <= 10% RPM
  73.     else if (readDutyAvg >= 0.9) {
  74.       Serial.print("REGION 0: ");
  75.       Serial.println(targetDuty);
  76.       targetDuty = minDutyCycle;
  77.      
  78.     }
  79.     // REGION 1:  10% < PWM <= 20%
  80.     //else if (readDutyAvg >= 0.8) {
  81.     //  targetDuty = readDutyAvg;
  82.     //  Serial.print("REGION I: ");
  83.     //  Serial.println(targetDuty);
  84.     //}
  85.  
  86.     // REGION 2:  20% < PWM <= 35%
  87.     else if (readDutyAvg >= 0.65) {
  88.       targetDuty = readDutyAvg + dutyCycleOffset;    //adding offset -> reducing RPM
  89.       Serial.print("REGION II: ");
  90.       Serial.print(readDutyAvg);
  91.       Serial.print(" + ");
  92.       Serial.print(dutyCycleOffset);      
  93.       Serial.print(" = ");
  94.       Serial.println(targetDuty);
  95.     }
  96.  
  97.    // REGION 3:   >= 35% RPM
  98.    else {
  99.       targetDuty = readDutyAvg;
  100.       Serial.print("REGION III: ");
  101.       Serial.println(targetDuty);      
  102.     }
  103.    
  104.     digitalWrite(LED_PIN, errorState);
  105.     Timer3.setPwmDuty(PWM_OUT_PIN, min(minDutyCycle, targetDuty) * MAX_PWM);
  106.    
  107.     //send debug info to serial
  108.     Serial.print("freq = ");
  109.     Serial.print(freq);
  110.     Serial.print("\treadDutyAvg = ");
  111.     Serial.print(readDutyAvg);
  112.     Serial.print("\ttargetDuty = ");
  113.     Serial.print(min(minDutyCycle, targetDuty));    
  114.     Serial.print("\terrorState = ");
  115.     Serial.print(errorState);    
  116.     Serial.println("");
  117.     }
  118.  
  119.     readDutyCycleCounter++; // increment counter
  120.     readDutyCycleCounter = readDutyCycleCounter % SAMPLES;
  121.     lastmillis = millis(); // Uptade lasmillis
  122.   }
  123. }
  124.  
  125.  
  126. //Takes in reading pins and outputs pwm frequency and readDuty cycle.
  127. void readPWM(){
  128.   highTime = 0;
  129.   lowTime = 0;
  130.  
  131.   tempPulse = pulseIn(PWM_MEASURE_PIN, HIGH, 400);
  132.   if (tempPulse > highTime) {
  133.     highTime = tempPulse;
  134.   }
  135.  
  136.   tempPulse = pulseIn(PWM_MEASURE_PIN, LOW, 400);
  137.   if (tempPulse > lowTime){
  138.     lowTime = tempPulse;
  139.   }
  140.  
  141.   freq = ((double) 1000000)/(double (lowTime+highTime));
  142.   readDuty = (/*100**/(highTime/(double (lowTime+highTime))));
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement