1
0

Add macro to make breathing led to use timer1 if timer3 is in other use

This commit is contained in:
Kai Ryu 2014-07-17 14:16:35 +09:00
parent f82a77cc15
commit 6091b368d6

View File

@ -10,6 +10,18 @@ static uint8_t breathing_led_duration = 0;
void breathing_led_init(void) void breathing_led_init(void)
{ {
#ifdef BREATHING_LED_TIMER1
/* Timer1 setup */
/* CTC mode */
TCCR1B |= (1<<WGM12);
/* Clock selelct: clk/8 */
TCCR1B |= (1<<CS10);
/* Set TOP value */
uint8_t sreg = SREG;
cli();
OCR1AH = (BREATHING_LED_TIMER_TOP>>8)&0xff;
OCR1AL = BREATHING_LED_TIMER_TOP&0xff;
#else
/* Timer3 setup */ /* Timer3 setup */
/* CTC mode */ /* CTC mode */
TCCR3B |= (1<<WGM32); TCCR3B |= (1<<WGM32);
@ -20,28 +32,44 @@ void breathing_led_init(void)
cli(); cli();
OCR3AH = (BREATHING_LED_TIMER_TOP>>8)&0xff; OCR3AH = (BREATHING_LED_TIMER_TOP>>8)&0xff;
OCR3AL = BREATHING_LED_TIMER_TOP&0xff; OCR3AL = BREATHING_LED_TIMER_TOP&0xff;
#endif
SREG = sreg; SREG = sreg;
} }
void breathing_led_enable(void) void breathing_led_enable(void)
{ {
/* Enable Compare Match Interrupt */ /* Enable Compare Match Interrupt */
#ifdef BREATHING_LED_TIMER1
TIMSK1 |= (1<<OCIE1A);
dprintf("breathing led on: %u\n", TIMSK1 & (1<<OCIE1A));
#else
TIMSK3 |= (1<<OCIE3A); TIMSK3 |= (1<<OCIE3A);
dprintf("breathing led on: %u\n", TIMSK3 & (1<<OCIE3A)); dprintf("breathing led on: %u\n", TIMSK3 & (1<<OCIE3A));
#endif
} }
void breathing_led_disable(void) void breathing_led_disable(void)
{ {
/* Disable Compare Match Interrupt */ /* Disable Compare Match Interrupt */
#ifdef BREATHING_LED_TIMER1
TIMSK1 &= ~(1<<OCIE1A);
dprintf("breathing led off: %u\n", TIMSK1 & (1<<OCIE1A));
#else
TIMSK3 &= ~(1<<OCIE3A); TIMSK3 &= ~(1<<OCIE3A);
dprintf("breathing led off: %u\n", TIMSK3 & (1<<OCIE3A)); dprintf("breathing led off: %u\n", TIMSK3 & (1<<OCIE3A));
#endif
} }
void breathing_led_toggle(void) void breathing_led_toggle(void)
{ {
/* Disable Compare Match Interrupt */ /* Disable Compare Match Interrupt */
#ifdef BREATHING_LED_TIMER1
TIMSK1 ^= (1<<OCIE1A);
dprintf("breathing led toggle: %u\n", TIMSK1 & (1<<OCIE1A));
#else
TIMSK3 ^= (1<<OCIE3A); TIMSK3 ^= (1<<OCIE3A);
dprintf("breathing led toggle: %u\n", TIMSK3 & (1<<OCIE3A)); dprintf("breathing led toggle: %u\n", TIMSK3 & (1<<OCIE3A));
#endif
} }
void breathing_led_set_duration(uint8_t dur) void breathing_led_set_duration(uint8_t dur)
@ -60,7 +88,11 @@ static const uint8_t breathing_table[256] PROGMEM = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
}; };
#ifdef BREATHING_LED_TIMER1
ISR(TIMER1_COMPA_vect)
#else
ISR(TIMER3_COMPA_vect) ISR(TIMER3_COMPA_vect)
#endif
{ {
static uint8_t index = 0; static uint8_t index = 0;
static uint8_t step = 0; static uint8_t step = 0;