Advertisement
ossipee

6677 4 wire fan code

Jun 9th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. //Pin 2 to PC fan "Tacho" line (pin 3 on standard connectors) with 10k pullup to 5v.
  2. //pin 11 to PC fan PWM/Input line (pin 4 on standard connectors, absent on 3 pin fans).
  3.  
  4.  
  5. unsigned int counter;
  6. unsigned int lastCounter;
  7.  
  8. unsigned long timeToReset;
  9. void setup() {
  10.   counter = 0;
  11.   lastCounter = 0;
  12.   // put your setup code here, to run once:
  13.   pinMode(2, INPUT);
  14.   attachInterrupt(0, rpm, RISING);
  15.   pinMode(13, OUTPUT);
  16.   //attachInterrupt(0, toggleLED, RISING);
  17.   pinMode(11, OUTPUT);
  18.   setPwmFrequency(11, 1);
  19.   Serial.begin(9600);
  20.   timeToReset = 1000;
  21. }
  22.  
  23. void loop() {
  24.   // put your main code here, to run repeatedly:
  25.   if (millis() > timeToReset) {
  26.     timeToReset = millis() + 1000;
  27.     lastCounter = counter;
  28.     counter = 0;
  29.     Serial.print("RPM: ");Serial.println(lastCounter * 60);
  30.   }
  31.   if (Serial.available()) {
  32.     char input = Serial.read();
  33.     switch (input) {
  34.       case '0':
  35.       analogWrite(11, 0);
  36.       Serial.println("11, 0");
  37.       break;
  38.       case '5':
  39.       analogWrite(11, 128);
  40.       //Serial.println("11, 128");
  41.       break;
  42.       case '8':
  43.       analogWrite(11, 204);
  44.       //Serial.println("11, 204");
  45.       break;
  46.       case '9':
  47.       analogWrite(11, 255);
  48.       //Serial.println("11, 255");
  49.       break;
  50.       case 'f':
  51.       //Serial.print("RPM: ");Serial.println(lastCounter);
  52.       default:
  53.       break;
  54.     }
  55.   }
  56. }
  57.  
  58. void toggleLED() {
  59.   digitalWrite(13, !digitalRead(13));
  60. }
  61. void rpm() {
  62.   counter++;
  63. }
  64. void setPwmFrequency(int pin, int divisor) {
  65.   byte mode;
  66.   if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
  67.     switch(divisor) {
  68.       case 1: mode = 0x01; break;
  69.       case 8: mode = 0x02; break;
  70.       case 64: mode = 0x03; break;
  71.       case 256: mode = 0x04; break;
  72.       case 1024: mode = 0x05; break;
  73.       default: return;
  74.     }
  75.     if(pin == 5 || pin == 6) {
  76.       TCCR0B = TCCR0B & 0b11111000 | mode;
  77.     } else {
  78.       TCCR1B = TCCR1B & 0b11111000 | mode;
  79.     }
  80.   } else if(pin == 3 || pin == 11) {
  81.     switch(divisor) {
  82.       case 1: mode = 0x01; break;
  83.       case 8: mode = 0x02; break;
  84.       case 32: mode = 0x03; break;
  85.       case 64: mode = 0x04; break;
  86.       case 128: mode = 0x05; break;
  87.       case 256: mode = 0x06; break;
  88.       case 1024: mode = 0x7; break;
  89.       default: return;
  90.     }
  91.     TCCR2B = TCCR2B & 0b11111000 | mode;
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement