-/* $Id: ledpwm.c,v 1.1 2010/06/26 19:08:18 simimeie Exp $
+/* $Id: ledpwm.c,v 1.3 2010/07/10 07:34:51 simimeie Exp $
* Functions for led brightness control via PWM (pulse width modulation).
*/
/* Select between Phase correct PWM and Fast PWM mode.
* setting this to 1 selects phase correct mode, everything else is Fast PWM. */
#define PHASECORRECTPWM 1
+
/*
* Our hardware connections are as follows:
* Red -> PD6 / OC0A
* Green -> PD5 / OC0B
* Blue -> PD3 / OC2B
*/
+
+uint8_t ledpwm_re = 0xff, ledpwm_gr = 0xff, ledpwm_bl = 0xff;
+uint8_t ledpwm_bri = 128;
+
void ledpwm_init(void)
{
/* Set OC2B, OC0A and OC0B to output */
- DDRD |= _BV(PD3) | _BV(PD5) | _BV(PD6);
+ 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. */
#else /* not PHASECORRECTPWM */
if (val == 0) {
switch (led) {
- case LEDPWM_REDLED: DDRD &= (uint8_t)~_BV(PD6);
+ case LEDPWM_REDLED: DDRD &= (uint8_t)~_BV(6);
break;
- case LEDPWM_GREENLED: DDRD &= (uint8_t)~_BV(PD5);
+ case LEDPWM_GREENLED: DDRD &= (uint8_t)~_BV(5);
break;
- case LEDPWM_BLUELED: DDRD &= (uint8_t)~_BV(PD3);
+ case LEDPWM_BLUELED: DDRD &= (uint8_t)~_BV(3);
break;
};
} else {
val = 255;
}
switch (led) {
- case LEDPWM_REDLED: DDRD |= _BV(PD6);
+ case LEDPWM_REDLED: DDRD |= _BV(6);
OCR0A = val;
break;
- case LEDPWM_GREENLED: DDRD |= _BV(PD5);
+ case LEDPWM_GREENLED: DDRD |= _BV(5);
OCR0B = val;
break;
- case LEDPWM_BLUELED: DDRD |= _BV(PD3);
+ case LEDPWM_BLUELED: DDRD |= _BV(3);
OCR2B = val;
break;
};
}
#endif /* PHASECORRECTPWM */
}
+
+void ledpwm_set(uint8_t red, uint8_t green, uint8_t blue, uint8_t br) {
+ ledpwm_setled(LEDPWM_REDLED, (((uint16_t)red * br) / 255));
+ ledpwm_setled(LEDPWM_GREENLED,(((uint16_t)green * br) / 255));
+ ledpwm_setled(LEDPWM_BLUELED, (((uint16_t)blue * br) / 255));
+}