Ok now you are going to start hardware programming by using AVR microcontroller . you have installed Atmel studio you have a burner(hardware) driver installed on your computer and a microcontroller and hard-wares . now before begin your project first ready your breadboard ,power supply , some male and female headers wires some LEDs ready necessary hard-wares .Connect your hardware as shown in the circuit diagram
Pin 40 is connected to positive pin of led through a 220 ohm resistor . and negative pin of led is connect to ground (-negative terminal of battery). Make sure that pin number 10 is connect to VCC and 11 is connect to ground in every project .but it is not necessary in proteus .
Now open your atmel studio and make a new GCC C executable project , select device atmega16 and write code given below
Now press build the solution or press F7 . you probably got a warning Warning 1 #warning "F_CPU not defined for <util/delay.h>" [-Wcpp] c:program files (x86)atmelatmel toolchainavr8 gccnative3.4.1061avr8-gnu-toolchainavrincludeutildelay.h 90 3 GccApplication28
Just skip the warning or you can modify your code as
Now necessary output files are successfully generated . Now you burn your hex code directly to your hardware or you can just upload your hex file to your proteus circuit .
note: in proteus just goto properties of your microcontroller and then upload the hex file form the debug file of your project name .
ok now the led is toggle (on and off) in every one thousand millisecond (or 1 second ) .
now what you have done
#define F_CPU 1000000UL
first you have define some frequency of the microcontroller (CPU frequency ) which is 1 mega hertz that is 1x106 hertz. UL means unsigned long integer . Do not stuck on this line of code . if you did not understand ignore it ,its just a frequency that the microcontroller clock is based on . go ahead .
#include <avr/io.h> this is avr standard input/output header file which is located in the avr folder
#include <util/delay.h> this is avr delay file for timing purpose which is located in util folder Then in this section you have included some standard avr header file to make operate the program.
Now you have creat a main routine inside the main routine you have did following things
By using the DDRA you can make the ports input or output . here 0b00000001 means 1st pin of PORTA will serve as output and rest are as input .DDRA means data direction register A , so pin number 40 which is PINA0 is make as output .
in this section a infinite loop (while loop) is created so that the led will on and off will goes infinitely . inside it we have sent high voltage to PINA0 using the PORTA=0b00000001; creat a one thousand millisecond delay and sent low voltage to PINA0 using PORTA=0b00000000; so that there is high voltage for one second and there is low voltage for one second . so that while there is voltage the led will blink for one second and if there is no voltage or 0 voltage the led will now turned off for the one second .that’s it .