AVR LED candle flicker (atmega328)

It’s that time of year again, when you should be thinking about starting your Halloween projects. In the last month ive been trying to move away from arduino and to code streight c on the AVR platform especially the familiar atmega328.

A nice project to do for Halloween is to make a LED candle flicker to move your pumpkin carving into the new age and is also safer then using a candle.

Lets start with the arduino sketch


int yellowLed = 4; // yellowLED

void setup() {


 pinMode(yellowLed, OUTPUT); // Set led as output
}

void loop() {
 analogWrite(yellowLed, random(10)+255); // Randomly pick value from 0 to 255 in increments of 10
}

Nice and easy with very few lines but when you upload the sketch its a whopping 1,532 bytes

Now let’s have a look at the streight C version of the same code

#include <avr/io.h>
#include <stdlib.h>

int main(void)
{

 DDRD |= (1 << PD3); // PD3/pin5 is now an output
 TCCR2A = (1 << COM2B1) | (1 << WGM20); // Timings for the PWM
 TCCR2B = (1 << CS22); // Timings for the PWM

 while (1)
 {

 OCR2B = rand() % 255 + 1; // Randomly pick number between 255 to 1

 }
}

When I upload the code its only 660 bytes which is less then half the size of the arduino counter part

If you want to do the same but on any of the other PWM compatable pins here is the code

For PD3 which is pin 5 on the atmega328


#include <avr/io.h>
#include <stdlib.h>

int main(void)
{

 DDRD |= (1 << PD3); // PD3/pin5 is now an output
 TCCR2A = (1 << COM2B1) | (1 << WGM20); // Timings for the PWM
 TCCR2B = (1 << CS22); // Timings for the PWM

 while (1)
 {

 OCR2B = rand() % 255 + 1; // Randomly pick number between 255 to 1

 }
}

For PD5 which is pin 11 on the atmega328

#include <avr/io.h>
#include <stdlib.h>

int main(void)
{

 DDRD |= (1 << PD5); // PD5/pin11 is now an output
 TCCR0A = (1 << COM2B1) | (1 << WGM20);
 TCCR0B = (1 << CS22);

 while (1)
 {

 OCR0B = rand() % 255 + 1; // Randomly pick number between 255 to 1

 }
}

For PD6 which is pin 12 on the atmega328

#include <avr/io.h>
#include <stdlib.h>

int main(void)
{

 DDRD |= (1 << PD6); // PD/pin12 is now an output

 TCCR0A = (1<<WGM00) | (1<<COM0A1); // Timings for the PWM
 TCCR2A = (1<<WGM00) | (1<<COM0A1); // Timings for the PWM
 TCCR0B = (1<<CS01); // Timings for the PWM
 TCCR2B = (1<<CS01); // Timings for the PWM

 while (1)
 {

 OCR0A = rand() % 255 + 1; // Randomly pick number between 255 to 1

 }
}

For PB1 which is pin 15 on the atmega328

#include <avr/io.h>
#include <stdlib.h>

int main(void)
{

 DDRB |= (1 << DDB1); // PB1/pin15 is now an output

 TCCR1A=0;
 TCCR1A |= (1 << WGM12) | (1 << WGM10);//set fast PWM mode
 TCCR1A |= (1 << COM1A1) | (1 << COM1B1); //non-inverting for both OC1A and OC1B
 TCCR1B |= (1 << CS11); //prescaler=8

 while (1)
 {

 OCR1A = rand() % 255 + 1; // Randomly pick number between 255 to 1

 }
}

For PB2 which is pin 16 on the atmega328

#include <avr/io.h>
#include <stdlib.h>

int main(void)
{

 DDRB |= (1 << PB2); // PB2/pin16 is now an output

 TCCR1A = 0;
 TCCR1A |= (1 << WGM12) | (1 << WGM10);//set fast PWM mode
 TCCR1A |= (1 << COM1A1) | (1 << COM1B1); //non-inverting for both OC1A and OC1B
 TCCR1B |= (1 << CS11); //prescaler=8

 while (1)
 {

 OCR1B = rand() % 255 + 1; // Randomly pick number between 255 to 1

 }
}

For PB3 which is pin 17 on the atmega328

</pre>
<pre>#include <avr/io.h>
#include <stdlib.h>

int main(void)
{

 DDRB |= (1 << PB3); // PB3/pin11 is now an output

 TCCR2A = (1 << COM2A1) | (1 << WGM21) | (1 << WGM20);
 TCCR2B = (1 << CS22);

 while (1)
 {

 OCR2A = rand() % 255 + 1; // Randomly pick number between 255 to 1

 }
}

					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s