AVR pulsing LED: avrledtest.c
ian – Tue, 2006 – 05 – 16 15:03
Based heavily off the sample code on http://www.captain.at/electronics/atmel-programmer/
This has been tested on an ATmega32 with a 4MHz crystal oscillator.
#include < avr/io.h>
void delay_ms(unsigned char ms)
/* badly mangled delay_ms */
{
unsigned short outer1, outer2;
outer1 = 1;
while (outer1) {
outer2 = 1000;
while (outer2) {
while ( ms ) ms--;
outer2--;
}
outer1--;
}
}
int main(void)
{
/* enable PD5 as output */
DDRD |= 1 << PD5;
unsigned char bright = 0;
char up = 1;
TCCR1A |= 1 << COM1A1; // clear OC1A/OC1B on compare match during upcount; set on match during downcount
TCCR1A |= 1 << WGM10; // PWM, phase correct, 8-bit
TCCR1B |= 1 << CS10; // internal clock, 1:1 prescale
while (1) {
delay_ms(0);
TCNT0 = 0;
if (up)
++bright;
else
--bright;
OCR1A = bright;
if (bright == 255)
up = 0;
if (bright == 0)
up = 1;
}
return 0;
}
