/* IDE 1.8.19 Arduino Mega2560 & UNO (wobei UNO nicht getestet) */ #include // https://github.com/janelia-arduino/Streaming Stream &out {Serial}; const byte pinFan {11}; void setup(void) { Serial.begin(250000); Serial.println(F("\nuC Reset ####\n")); //readT1Register(Serial); // Debug resetTimer1(); //readT1Register(Serial); // Debug initPwmPin(pinFan); //readT1Register(Serial); // Debug initTimer1To25kHz(); //readT1Register(Serial); // Debug changeT1Duty(pinFan, 33); // Pin, Duty [%] //readT1Register(Serial); // Debug } /* Default TCCR1A 1010.0001 // COM1A1, COM1B1, WGM10 (Clear on Compare, 8Bit PWM Phase Correct)) TCCR1B 11 // CS11, CS10 (Prescaler 64) TCCR1C 0 ICR1 0 OCR1A 0 OCR1B 0 */ void loop(void) { } void initPwmPin (const uint8_t pin) { switch(pin) { #if defined(__AVR_ATmega328P__) case 9: TCCR1A |= _BV(COM1A1); pinMode(pin, OUTPUT); break; // UNO Pin 9 case 10: TCCR1A |= _BV(COM1B1); pinMode(pin, OUTPUT); break; // UNO Pin 10 #endif #if defined(__AVR_ATmega2560__) case 11: TCCR1A |= _BV(COM1A1); pinMode(pin, OUTPUT); break; // Mega2560 Pin 11 case 12: TCCR1A |= _BV(COM1B1); pinMode(pin, OUTPUT); break; // Mega2560 Pin 12 #endif default: break; } } void changeT1Duty (const uint8_t pin, uint16_t duty) { if (duty > 100) duty = 100; duty = ICR1*duty/100; switch(pin) { #if defined(__AVR_ATmega328P__) case 9: OCR1A = duty; break; // UNO Pin 9 case 10: OCR1B = duty; break; // UNO Pin 10 #endif #if defined(__AVR_ATmega2560__) case 11: OCR1A = duty; break; // Mega2560 Pin 11 case 12: OCR1B = duty; break; // Mega2560 Pin 12 #endif default: break; } } void initTimer1To25kHz (void) { changeTimer1Mode(10); ICR1 = 320; // macht mit Prescaler 1 -> 25kHz startTimer1(1); // Prescaler 1 } void stopTimer1 (void) { // delete Clock Select Bits, Timer is stopped TCCR1B &= ~( _BV(CS12) | _BV(CS11) | _BV(CS10) ); } void startTimer1 (const unsigned int prescaler) { // set new Prescaler Clock Select Bits switch (prescaler) { case 1 : TCCR1B |= _BV(CS10); break; case 8 : TCCR1B |= _BV(CS11); break; case 64 : TCCR1B |= _BV(CS11) | _BV(CS10); break; case 256 : TCCR1B |= _BV(CS12); break; case 1024 : TCCR1B |= _BV(CS12) | _BV(CS10); break; default : break; // otherwise timer remains stopped } } void changeTimer1Mode (const uint8_t mode) { if ((mode <=15) && (mode != 13)) { stopTimer1(); deleteTimer1Mode(); switch (mode) { case 0 : ; break; // Normal case 1 : TCCR1A |= _BV(WGM10); break; // PWM, Phase Correct 8-Bit case 2 : TCCR1A |= _BV(WGM11) ; break; // PWM, Phase Correct 9-Bit case 3 : TCCR1A |= _BV(WGM11) | _BV(WGM10); break; // PWM, Phase Correct 10-Bit case 4 : TCCR1B |= _BV(WGM12); ; break; // CTC, OCRnA case 5 : TCCR1B |= _BV(WGM12); TCCR1A |= _BV(WGM10); break; // Fast PWM 8 Bit case 6 : TCCR1B |= _BV(WGM12); TCCR1A |= _BV(WGM11) ; break; // Fast PWM 9 Bit case 7 : TCCR1B |= _BV(WGM12); TCCR1A |= _BV(WGM11) | _BV(WGM10); break; // Fast PWM 10 Bit case 8 : TCCR1B |= _BV(WGM13) ; ; break; // PWM, Phase & Frequency Correct, ICRn case 9 : TCCR1B |= _BV(WGM13) ; TCCR1A |= _BV(WGM10); break; // PWM, Phase & Frequency Correct, OCRnA case 10 : TCCR1B |= _BV(WGM13) ; TCCR1A |= _BV(WGM11) ; break; // PWM, Phase Correct, ICRn case 11 : TCCR1B |= _BV(WGM13) ; TCCR1A |= _BV(WGM11) | _BV(WGM10); break; // PWM, Phase Correct, OCRnA case 12 : TCCR1B |= _BV(WGM13) | _BV(WGM12); ; break; // CTC, ICRn case 13 : break; // Reserved case 14 : TCCR1B |= _BV(WGM13) | _BV(WGM12); TCCR1A |= _BV(WGM11) ; break; // Fast PWM, ICRn case 15 : TCCR1B |= _BV(WGM13) | _BV(WGM12); TCCR1A |= _BV(WGM11) | _BV(WGM10); break; // Fast PWM, OCRnA default : break; // otherwise no change of the configuration } } } void deleteTimer1Mode (void) { // delete all WGM Bits TCCR1A &= ~( _BV(WGM11) | _BV(WGM10) ); TCCR1B &= ~( _BV(WGM13) | _BV(WGM12) ); } void resetTimer1 (void) { // delete all TCCR1B = 0; TCCR1A = 0; TCCR1C = 0; TIMSK1 = 0; ICR1 = 0; OCR1A = 0; OCR1B = 0; } void readT1Register (Stream &out) { out << "TCCR1A " << _BIN(TCCR1A) << endl; out << "TCCR1B " << _BIN(TCCR1B) << endl; out << "ICR1 " << ICR1 << endl; out << "OCR1A " << OCR1A << endl; out << "OCR1B " << OCR1B << endl; out.println(); }