LED BLINKING WITH ATMEGA328P USING ATMEL STUDIO Leave a comment

AVR is a popular microcontroller been used on arduino board but do you know using arduino syntax can delay your work and reduce efficiency so we rather use avr syntax and register directly on arduino microcontroller but for a project that time is not of paramount important arduino syntax can be used directly for the sake of the led blinking tutorial we shall be using atmel studio code directly which will also work on arduino ide by just changing the int main() to void setup() and then do { }while() loop to void loop();

/*
* Include header files for all drivers that have been imported from
* Atmel Software Framework (ASF).
*/
#include
#define F_CPU 16000000UL //16mhz crystal
#include //allow input output function
#include //allow interrupt even though not used in code
#include //allow delay

int main (void)
{
board_init();
DDRB|=(1<<PB5);//set all portd direction to output;
PORTB&=~(1<<PB5);//set pin low

do
{

PORTB|=(1<<PB5); //set PB5 to high
_delay_ms(1000); //wait 1sec
PORTB&=~(1<<PB5); //set PB5 to low
_delay_ms(1000); //wait 1 sec

} while (1);

// Insert application code here, after the board has been initialized.
}

the above code is faster than the one below because we are making use of bit manipulation.,arduino code will still take a-lot of time converting back to the general avr code that time of conversion account for the great difference.

from the above diagram PB5 on the pinout of atmega328p is on digitalpin13 on arduino the code above is like the below code on arduino

void setup()

{
pinMode(13,OUTPUT); //set as output
digitalWrite(13,LOW);//set pin low

}

void loop()
{

digitalWrite(13,HIGH);//set pin high
delay(1000);//wait 1 seconds
digitalWrite(13,LOW);//set pin low
delay(1000);//wait 1 seconds
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Open chat