Skip to content

Instantly share code, notes, and snippets.

@l3kn
Created October 5, 2012 19:21
Show Gist options
  • Save l3kn/3841814 to your computer and use it in GitHub Desktop.
Save l3kn/3841814 to your computer and use it in GitHub Desktop.
Simple .c LED-Fader
#include<avr/io.h>
#include<util/delay.h>
void sleep(uint8_t millisec)
{
while(millisec)
{
_delay_ms(1);/* 1 ms delay */
millisec--;
}
}
int main()
{
uint8_t pwm_to = 100;
uint8_t pwm_is = 0;
int8_t pwm_fade_dir = -1;
DDRC |=1<<PC2; /* PC2 will now be the output pin */
while(1)
{
for(uint16_t i=0;i<=16000;i++)
{
if(pwm_is==pwm_to)
{
PORTC |=(1<<PC2); /* PC2 HIGH */
}
pwm_is++;
if(pwm_is==100)
{
pwm_is=0;
PORTC &= ~(1<<PC2); /* PC2 LOW */
}
}
pwm_to+=pwm_fade_dir;
if(pwm_to == 0 || pwm_to == 100)
{
pwm_fade_dir*=-1;
}
}
return 0;
}
/*
avr-g++ -g -Os -DF_CPU=16000000 -Wall -mcall-prologues -mmcu=atmega328p -fno-exceptions -o blink.obj blink.c
avr-objcopy -R .eeprom -O ihex blink.obj blink.hex
avrdude -P /dev/ttyUSB0 -b 19200 -c avr911 -p m328p -U flash:w:blink.hex
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment