Hi,
I'm new to this forum, to ATmega (or microcontrollers in general) and to C, so be patient with me... :wink:
My project is to control a led on a STK500 with PWM using a ATmega48. The idea is to have the intensity of the led increase and decrease by pushing switches 3 & 4. See code below. Starting point was code from Joerg Wunsch, which increased/decreased the led automatically, by having the pwm change direction when overflowing. This worked fine before, so my pwm settings should be correct. However, with the code below the led on PB1 just switches on and stays at max intensity.
Does anyone has a clue of what is wrong in the code below?
Thanks,
Leon
#include#include #include #define TIMER1_TOP 1023 /* 10-bit PWM */ #define TIMER1_PWM_INIT _BV(WGM10) | _BV(WGM11) | _BV(COM1A1) #define TIMER1_CLOCKSOURCE _BV(CS10) /* full clock */ enum { UP, DOWN, ZERO}; static uint16_t pwm; static uint8_t direction; ioinit (void) { /* Timer 1 is 10-bit PWM */ TCCR1A = TIMER1_PWM_INIT; TCCR1B |= TIMER1_CLOCKSOURCE; /* Run any device-dependent timer 1 setup hook if present. */ #if defined(TIMER1_SETUP_HOOK) TIMER1_SETUP_HOOK(); #endif /* Set PWM value to 0. */ OCR1A = 0; /* Enable PB1 as output. */ DDRB = 14; PORTB = 14; /* Enable Port D as input. */ DDRD = 0x00; PORTD = 0xff; /* activate internal pull-up*/ /* Enable timer 1 overflow interrupt. */ TIMSK1 = _BV (TOIE1); direction = ZERO; sei (); } ISR (TIMER1_OVF_vect) { switch (direction) { case UP: if (++pwm >= TIMER1_TOP) --pwm; break; case DOWN: if (--pwm <= 0) ++pwm; break; } OCR1A = pwm; } int main (void) { uint8_t led; uint8_t keys; ioinit (); while(1) { keys = ~PIND; if ( keys == 4 ) { direction = UP; PORTB = ~4; } else if ( keys == 8 ) { direction = DOWN; PORTB = ~8; } else { direction = ZERO; PORTB = 14; } } }