InaMORAta: Difference between revisions

Line 19: Line 19:


==Control (PWM)==
==Control (PWM)==
We set up a 25K PWM signal on digital output pin 9. We can't use the standard Arduino PWM system, which operates at lower frequencies (490Hz and 980Hz). We operate directly on timer registers:
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;
   TCCR1A = 0;
   TCCR4A = 0;
   TCCR1B = 0;
   TCCR4B = 0;
   TCNT1 = 0;
   TCNT4 = 0;
   TCCR1A |= (1 << COM1A1) | (1 << WGM11);
   ICR4 = (F_CPU / PWM_FREQ_HZ) / 2;
   TCCR1B |= (1 << WGM13) | (1 << CS10);
  TCCR4A |= (1 << COM4A1) | (1 << WGM41);
  ICR1 = 16000000 / (2 * PWM_FREQ_HZ);
   TCCR4B |= (1 << WGM43) | (1 << CS40);
</pre>
</pre>
This <tt>ICR1</tt> value comes out to 320. We can then set the desired PWM <tt>duty</tt> (0–100) with:
This <tt>ICR4</tt> value comes out to 320. We can then set the desired PWM <tt>duty</tt> (0–255) with:
<pre>
<pre>
   OCR1A = (word)(duty * 320) / 100
   OCR4C = duty;
</pre>
</pre>