-void ledpwm_init(void)
-{
- /* Set OC2B, OC0A and OC0B to output */
- DDRD |= _BV(3) | _BV(5) | _BV(6);
- /* Set compare output mode for OC2B in timer counter control register: */
-#if (PHASECORRECTPWM == 1)
- /* select phase correct PWM mode (1). WGM2 bit is 0 so we don't need to touch TCCR2B. */
- TCCR2A |= _BV(COM2B1) | _BV(WGM20);
-#else /* not PHASECORRECTPWM */
- /* set output at BOTTOM (=0), clear it on compare match.
- * select fast PWM mode 3. WGM2 bit is 0 so we don't need to touch TCCR2B. */
- TCCR2A |= _BV(COM2B1) | _BV(WGM20) /*| _BV(WGM21)*/;
-#endif /* PHASECORRECTPWM */
- /* select clock source: cpu clock without prescaler. */
- TCCR2B |= _BV(CS20);
- /* Default brightness: PWM 0 */
- OCR2B = 0;
- /* Set compare output mode for OC0A and OC0B in timer counter control register: */
-#if (PHASECORRECTPWM == 1)
- /* select phase correct PWM mode (1). WGM2 bit is 0 so we don't need to touch TCCR0B. */
- TCCR0A |= _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00);
-#else /* not PHASECORRECTPWM */
- /* set output at BOTTOM (=0), clear it on compare match.
- * select fast PWM mode 3. WGM2 bit is 0 so we don't need to touch TCCR0B. */
- TCCR0A |= _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00) | _BV(WGM01);
-#endif /* PHASECORRECTPWM */
- /* select clock source: i/o clock without prescaler. */
- TCCR0B |= _BV(CS20);
- /* Default brightness: PWM 0 */
- OCR0A = 0;
- OCR0B = 0;
+/* To do a 16-bit write, the high byte must be written before the low byte.
+ * For a 16-bit read, the low byte must be read before the high byte */
+static void ledpwm_programnextstep(void) {
+ uint16_t nextval = 0xffff;
+ uint8_t i;
+ for (i = 0; i < 3; i++) {
+ if ((ledpwm_val[i] > ledpwm_curoreg) && (ledpwm_val[i] <= nextval)) {
+ nextval = ledpwm_val[i];
+ }
+ }
+ OCR1AH = nextval >> 8;
+ OCR1AL = nextval & 0xff;
+ ledpwm_curoreg = nextval;