From bedbcdc4de680720deff4b6e113a3ec1a0690482 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 22 Jul 2014 15:58:55 +0200 Subject: Speaker and LED pwm Something is not correct here, stack overflow? --- pwm.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'pwm.c') diff --git a/pwm.c b/pwm.c index c38a162..669d9c3 100644 --- a/pwm.c +++ b/pwm.c @@ -1,6 +1,8 @@ /* LED pwm, uses timer0 */ +#include "common.h" + #include #include #include @@ -18,6 +20,11 @@ static uint8_t state = 0; static uint8_t val[2]; static uint8_t init[2]; +static void ledOff () { + PORTB = PORTB & ~((1 << PB6) | (1 << PB7)); + PORTD = PORTD & ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5)); +} + static uint8_t ledToArray (const uint8_t i) { assert (i < PWM_LED_COUNT); if (i >= 2) { @@ -59,10 +66,7 @@ ISR(TIMER0_COMPA_vect) { } if (comphit % 2 == 0) { - /* switch off: we can’t just set to 0 here, since PB6 is used by - * speaker */ - PORTB = PORTB & ~((1 << PB6) | (1 << PB7)); - PORTD = PORTD & ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5)); + ledOff (); } else { PORTB |= val[0]; PORTD |= val[1]; @@ -77,6 +81,8 @@ void pwmInit () { } void pwmStart () { + comphit = 0; + state = 0; /* reset timer value */ TCNT0 = 0; /* set ctc timer0 (part 1) */ @@ -92,6 +98,7 @@ void pwmStart () { void pwmStop () { /* zero clock source */ TCCR0B = 0; + ledOff (); } void pwmSetBlink (const uint8_t i, const uint8_t value) { -- cgit v1.2.3