Salve a tutti... Mi lascia perplesso questo codice che volevo cercare di capire insieme a voi..
Dopo aver settato i pin del pwm come output troviamo questi settaggi relativi alla modalità e alla frequenza del pwm nel setup();
// Set up PWM, Phase and Frequency Correct on pin 9 (OC1A) & pin 10 (OC1B) with ICR1 as TOP using Timer1
TCCR1B = _BV(WGM13) | _BV(CS10); // Set PWM Phase and Frequency Correct no prescaling
ICR1H = (PWMVALUE >> 8); // ICR1 is the TOP value - this is set so the frequency is equal to 20kHz
ICR1L = (PWMVALUE & 0xFF);
// Clear OC1A/OC1B on compare match when up-counting
// Set OC1A/OC1B on compare match when downcountin
TCCR1A = _BV(COM1A1) | _BV(COM1B1);
setPWM(leftPWM,0); // Turn off pwm on both pins
setPWM(rightPWM,0);
e fino a qui ci siamo... questa funzione.. per quale motivo settare di nuovo questi registri?
void setPWM(uint8_t pin, int dutyCycle) { // dutyCycle is a value between 0-TOP
if(pin == leftPWM) {
OCR1AH = (dutyCycle >> 8);
OCR1AL = (dutyCycle & 0xFF);
} else if (pin == rightPWM) {
OCR1BH = (dutyCycle >> 8);
OCR1BL = (dutyCycle & 0xFF);
}
}