PWM for LED implemented

This commit is contained in:
King Kévin 2013-10-13 12:19:17 +02:00
parent 76eccc6c3a
commit 8c128db8d2
1 changed files with 26 additions and 1 deletions

View File

@ -15,7 +15,6 @@ static void ioinit(void)
PORTB |= (1<<nPS_ON); /* witch off power supply */
/* configure peripherals */
DDRD |= (1<<LED); /* LED is output */
DDRB &= ~(1<<IR); /* IR receiver is input */
DDRC &= ~(1<<FAN); /* FAN is input */
@ -30,6 +29,20 @@ static void ioinit(void)
stdout = &uart_output;
stdin = &uart_input;
/* configure LED (on PD6/OC0A) */
DDRD |= (1<<LED); /* LED is output */
/* use phase correct PWM ode (because fast PWM is always on for at least 1 cycle) */
TCCR0A &= ~(1<<WGM01);
TCCR0A |= (1<<WGM00);
TCCR0B &= ~(1<<WGM02);
/* /64 prescale timer */
TCCR0B &= ~(1<<CS02);
TCCR0B |= (1<<CS01)|(1<<CS00);
/* clear on match, because the pin is used as sink */
TCCR0A |= (1<<COM0A1)|(1<<COM0A0);
/* start with LED on */
OCR0A = 0xff;
}
int main(void)
@ -160,6 +173,18 @@ int main(void)
puts("off");
}
break;
case '-':
if (OCR0A>0) {
OCR0A--;
}
printf("decreasing LED: %d\n",OCR0A);
break;
case '+':
if (OCR0A<0xff) {
OCR0A++;
}
printf("increasing LED: %d\n",OCR0A);
break;
}
}