InaMORAta: Difference between revisions
| Line 19: | Line 19: | ||
==Control (PWM)== | ==Control (PWM)== | ||
We set up a 25K PWM signal on | We set up a 25K PWM signal on pin 8. We can't use the standard Arduino PWM system, which operates at lower frequencies (490Hz and 980Hz). We operate directly on timer registers, using timer 4: | ||
<pre> | <pre> | ||
const word PWM_FREQ_HZ = 25000; | const word PWM_FREQ_HZ = 25000; | ||
TCCR4A = 0; | |||
TCCR4B = 0; | |||
TCNT4 = 0; | |||
ICR4 = (F_CPU / PWM_FREQ_HZ) / 2; | |||
TCCR4A |= (1 << COM4A1) | (1 << WGM41); | |||
TCCR4B |= (1 << WGM43) | (1 << CS40); | |||
</pre> | </pre> | ||
This <tt> | This <tt>ICR4</tt> value comes out to 320. We can then set the desired PWM <tt>duty</tt> (0–255) with: | ||
<pre> | <pre> | ||
OCR4C = duty; | |||
</pre> | </pre> | ||