#include <TimerOne.h>
#include <IRremote.hpp>
#include "api.h"
// This example creates a PWM signal with 25 kHz carrier.
//
// Arduino's analogWrite() gives you PWM output, but no control over the
// carrier frequency. The default frequency is low, typically 490 or
// 3920 Hz. Sometimes you may need a faster carrier frequency.
//
// The specification for 4-wire PWM fans recommends a 25 kHz frequency
// and allows 21 to 28 kHz. The default from analogWrite() might work
// with some fans, but to follow the specification we need 25 kHz.
//
// http://www.formfactors.org/developer/specs/REV1_2_Public.pdf
//
// Connect the PWM pin to the fan's control wire (usually blue). The
// board's ground must be connected to the fan's ground, and the fan
// needs +12 volt power from the computer or a separate power supply.
#define PWM_FREQ 40 // 1/25000Hz = 40us
#define IR_RECEIVE_PIN 2 // Signal Pin of IR receiver
const int PIN_PWM_FAN = 9;
#define IR_PLUS 2
#define IR_MINUS 152
#define IR_PLAY 168
#define IR_ZERO 104
#define IR_ONE 48
#define IR_TWO 24
#define IR_THREE 122
#define IR_FOUR 16
#define IR_FIVE 56
#define IR_SIX 90
#define IR_SEVEN 66
#define IR_EIGHT 74
#define IR_NINE 82
void divideClockSystem(uint16_t div) {
if (div > 256)
div = 256;
div = log(div) * M_LOG2E;
Serial.println(div);
cli(); // disabilita gli interrupt
CLKPR = _BV(CLKPCE);
CLKPR = (uint8_t)div;
sei(); // enable global interrupt
}
void dCSdatman(uint16_t div) {
if(div>256 || (div & (div-1))!=0) {
Serial.println("Valore errato!");
return;
}
uint8_t n = 0;
while(div >= 2) {
div >>= 1;
n += 1;
}
Serial.println(n);
/*cli(); // Disabilita gli interrupt.
CLKPR =_BV(CLKPCE);
CLKPR = n;
sei(); // Riabilita gli interrupt.*/
}
void setup(void) {
IrReceiver.begin(IR_RECEIVE_PIN);
//timer1Init();
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
uint16_t c = (F_CPU/100000 * PWM_FREQ) / 20;
Serial.println(c);
dCSdatman(1);
}
void blink() {
static bool ledState = LOW;
static uint32_t saveMillis = millis();
//if (millis() - saveMillis > 100) {
//saveMillis = millis();
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
delay(100);
//}
}
void timer1Init() {
TCCR1B = _BV(WGM13); // set mode as phase and frequency correct pwm, stop the timer
TCCR1A = 0; // clear control register A
ICR1 = (F_CPU/100000 * PWM_FREQ) / 20;
TCCR1B = _BV(WGM13) | _BV(CS10);
pinMode(PIN_PWM_FAN, OUTPUT); // set pin 9 as output
TCCR1A |= _BV(COM1A1);
}
void setDutyPercent(uint8_t dc) {
uint32_t dutyCycle = ICR1 * ((dc / 100.0) * 1023);
//dutyCycle *= dc;
OCR1A = dutyCycle >> 10;
//Timer1.pwm(PIN_PWM_FAN, (dc / 100.0) * 1023);
}
void loop(void) {
blink();
uint8_t command = 0;
// Checks received an IR signal
if (IrReceiver.decode()) {
command = IrReceiver.decodedIRData.command;
//translateIR();
Serial.println(command);
IrReceiver.resume(); // Receive the next value
}
switch (command) {
case IR_ONE:
//divideClockSystem(1);
break;
case IR_TWO:
//divideClockSystem(2);
break;
case IR_THREE:
//divideClockSystem(4);
break;
case IR_FOUR:
//divideClockSystem(8);
break;
case IR_FIVE:
//divideClockSystem(16);
break;
case IR_SIX:
//divideClockSystem(32);
break;
case IR_SEVEN:
//divideClockSystem(64);
break;
case IR_EIGHT:
//divideClockSystem(128);
break;
case IR_NINE:
//divideClockSystem(256);
break;
case IR_PLUS:
speedInc();
Serial.println(getSpeedPercent());
break;
case IR_MINUS:
speedDec();
Serial.println(getSpeedPercent());
break;
case IR_ZERO:
// notturno sinlet mode
setSpeedPercent(SPEED_MIN);
Serial.println("Silent Mode 25%");
break;
}
// slowly increase the PWM fan speed
//
/*for (uint8_t dutyCycle = 30; dutyCycle < 100; dutyCycle++) {
Serial.print("PWM Fan, Duty Cycle = ");
Serial.println(dutyCycle);
setDutyPercent(dutyCycle);
delay(100);
}*/
}