Assuming that you have followed the previous tutorials and set up environment for AVR development using Atmel Studio, let us now see how to create a new project in Atmel Studio 7 (AS7). Please take note that while Installing Atmel Studio it will ask permission for installing packages for 8/32bit AVR micro-controllers as well as ARM series, make sure you select the packages for 8/32bit avr micro-controllers.
Step 1
After starting the AS7, you will see the welcome screen in which you can see lots of tools in toolbar and 'Start Page' in tab section. Under that tab look for 'Start' under which lie the 3 options New Project, Open Project and New Example Project. Click on 'New Project' or you can go in file menu and select project under new sub-menu.
Now that we have created our first project in Atmel Studio, we need to write some code to make the LED blink. Before moving to actual coding of microcontroller's port pins we need to load headers for libraries. Libraries are set of codes that are to be used again and again. Libraries make our life easy but internally they can be a maze of complex codes.
In our basic example of blinking the LED we need to load libraries for pin-outs of our Atmega microcontroller and for generating delays.
#include <avr/io.h>
#include <util/delay.h>
Atmega328/P has 3 IO ports, we need to select one Port Pin to which we have to interface the LED. Lets say we want to blink LED on pin PB5 i.e 5th pin of Port B which is also known as pin 13 on Arduino UNO.
For this we have to set PB5 I/O pin as output. This has to be done by setting the corresponding DDRB(Data Direction Register B) bit High or '1'. Following snippet of code is used to set PB5 as output:
Atmel Studio 7 home screen |
Step 2
Once you choose to create a new project in Atmel Studio, a dialog box will pop up to ask what programming language you want to choose. Select 'GCC C Executable Project' as we don't want to make a library project or write the code in C++.Setting up new project |
Step 3
Once the programming language is selected you will be asked for selecting device. He make sure you choose Atmega328/P and click 'Ok'. With this step you have created a new project in atmel studio 7 and you should be able to see a tab in which we have to write the code.Select the device i.e your micro-controller to be programmed |
Coding tab for new project |
In our basic example of blinking the LED we need to load libraries for pin-outs of our Atmega microcontroller and for generating delays.
#include <avr/io.h>
#include <util/delay.h>
Atmega328/P has 3 IO ports, we need to select one Port Pin to which we have to interface the LED. Lets say we want to blink LED on pin PB5 i.e 5th pin of Port B which is also known as pin 13 on Arduino UNO.
For this we have to set PB5 I/O pin as output. This has to be done by setting the corresponding DDRB(Data Direction Register B) bit High or '1'. Following snippet of code is used to set PB5 as output:
DDRB |= (1<<PORTB5); //declare PORTB5/Pin19 as output by setting corresponding register bit high
OR
/*
DDRB = 0b00100000; //set PB5 as output
*/
To set PB5 as output you can directly set 5th bit of DDRB register as high or by setting the corresponding pin port register high of PB5 pin. Since we only have to blink the LED, micro-controller needs to execute this part of code only once.
Now that PB5 is set as output we need to set it cycle it between HIGH and LOW to blink the LED. This can be simply done by pushing corresponding PORTB register to HIGH or LOW as required.
Lets say we have to Keep the LED on for 1s and off for 1 second. For this we have to set PORTB pin 5 to HIGH for 1 second and LOW for another 1 second to switch off the LED.
PORTB = 0b00100000; //set PORTB5 HIGH [Binary Representation]
_delay_ms(1000);
PORTB = 0x00; //set all pins on PORTB5 LOW [Hexadecimal Representation]
_delay_ms(1000);
Since we want to continuously blink our LED at interval of 1 second we need to execute it continuously by using the while loop which is equivalent to 'void loop' in Arduino. This while loop is to be placed inside a main loop, which is per-defined. The code which is inside the main loop but outside the while loop is executed only once. For example the code where we declare PORTB5 as output is to be executed only once after the micro-controller starts, so it will be placed outside the while loop whereas the code where we toggle the Pin PORTB5 from LOW to HIGH and HIGH to LOW after every 1000ms is to be placed in while loop.
int main(void)
{
DDRB = 0b00100000; //set PB5 as output
while (1)
{
PORTB = 0b00100000; //set PORTB5 HIGH [Binary Representation]
_delay_ms(1000);
PORTB = 0x00; //set all pins on PORTB5 LOW [Hexadecimal Representation]
_delay_ms(1000);
}
}
Now that we typed our code, it is time that we compile it and upload it to our Atmega328. To compile the code, in toolbar look for 'Build' and click on it under which you will fin multiple options, click very first option called 'Build Solution' or you can just press F7 button on Keyboard to save the hassle and make some use of those Function keys.
Compiling the Code
If you have coded everything properly then your code will compile successfully. If it does, then Hurray! If you check the folder where you previously saved the folder there will be a new folder inside called 'Debug', just open it to find multiple files one of them will be having 'hex' extension. This is the hex file that is to be uploaded to our micro-controller to blink that LED. You might want to open that hex file using some text editor and have a look at its content.List of generated files after successful compilation |
Now that the hex file is generated we need to upload it to the controller. Just hookup the USBasp to your Atmega microcontroller and start AVRDUDESS on your computer. If You haven't done with setting up with USBasp on your system or have to connect it to mcu, I will suggest you refer to this tutorial.Upload Hex File To MCU |
Click here to download the complete Atmel Studio solution to blink the LED. Tweak the code as per your needs and try getting output at different port pins o
f ATmega328.
this is a code for blinking an LED ... 0B0010000 looks so complex ... can give advive for beginers in this area
ReplyDeleteActually it is a way of representation for output at port in binary format. '0B0010000', here if you start from right to left the first 0 from write indicates first port pin for micro-controller. In this case pin 0 of PORTB also PORTB0. Each bit of register PORTB represents each physical pin of PORTB. Writing '1' gives logic output HIGH; '0' gives logic output LOW. If I wanted to glow LED at PORTB0 I can write PORTB = 0b00000001. Alternatively you can use Decimal system or Hexadecimal System as well to get outputs. Eg. PORTB = 255; will make all of the pins of PORTB go HIGH. (convert 255 to binary system). Hope this helps you for now. I will make a separate post for this topic. Thank You for bringing it to attention!
DeleteYou can type this code in arduino ide, compile it & program the atmega328 using usbAsp.
ReplyDelete