Microcontroller For Beginners

AVR microcontroller for beginner......


Download AVR Studio (free) from ATMEL's web(click) and User Manual.(click) For installation see the pdf.(click) After Installation see below.  


Creating a New Project 

when you open AVR studio you will see


Enter New Project you will see


Select Directory where you want to store your project files. Click "Next>>" and choose the debug platform and device as shown:


If you want to use ATmega32, then





 Click "Finish". The main AVR Studio Window should then look something like the following:




Editing and Building the C file

lets write our code... 




Then Save the file and choose "Build" from the "Build" menu (or press <F7>):



The result of building the project will be shown in the "Build" pane and will be something like:




From this window we can see that the code is 134 bytes (compared with 20 bytes for the assembly language version, we will see later)[NOTE: the result you get may be different, depending on the compiler version and optmisation settings.], and that the build was completed with no errors. We are now ready to simulate the code.



Simulating the Code

We have generated the files necessary to simulate the code. To start running the code, select "Start Debugging" from the "Debug" menu:


In the code editor, you'll now see a yellow right-arrow in the left margin. This arrow indicates the current code position:


If the "Processor" panel is not shown, display it. (Right click in the toolbar area and choose to display the "Processor" pane.) You will notice (as shown above) that even though we haven't started stepping through the code yet that the 29 clock cycles have already been executed and the program counter is up to 2A (hexadecimal). Unseen initialisation code has run before main() is reached. This code performs functions like setting the stack pointer, something which had to be done explicitly in the assembly languge program.

Before you step through the code, make sure you expand the I/O View so that you can see the Port B registers:




Single Stepping the Program

As in the assembly language simulation, there are two commands to single step through the code. These are "Step Over" <F10> and "Step Into" <F11>. The difference between these commands is that "Step Over" does not trace into functions. Since our example does not contain any functions, there is no difference between the operation of these commands in this example. 

Now single step down to the last line of code (closing brace of the for loop) by repeatedly pressing the <F11> key or by selecting "Step Into" from the "Debug" menu. Notice how the colour changes from black to red on the registers that change value. This makes it easier to identify which registers change value on each instruction. Continue pressing the <F11> key and see how the binary value in Port B is increased. (It will roll over from value 255 to 0, then to 1, 2, etc.)


You can set breakpoints in C code just like you can in assembly language. Set a break point on the last statement (value++;). A red circle will appear in the left margin of the source view menu as shown. By pressing <F5> or "Run" from the "Debug" menu the program will start running and break (stop) at the statement with the breakpoint.




Opening the Watch View

Open the Watch window by selecting "Watch" from the "View" menu:


When debugging assembly language programs, we could watch register values. When debuggging C programs, we can watch specific variables. Type "value" into the first "Name" box. As you step through the program you will see the value of the variable change. (The location shown is the memory address (in hexadecimal) at which this variable is stored. In some cases variables will be located in RAM, and in other cases they might be located in a general purpose register.


Save your project....................One thing I should say earlier our target is to generate hex file. By default, AVR studio makes .hex in the project directory whenever the "Build" is clicked (off-course if there is no errors). When the studio has build the program successfully, you will find a new folder named "default" in the project directory. The name of hex will the .C file's name you made. You will find it in the "Default"  folder. This hex file can be burned by AVR studio itself. We are doing this with a software named Ponyprog..(See parallel Programmer section) 



Recent Videos

5011 views - 13 comments
1414 views - 1 comment
1089 views - 1 comment
1132 views - 1 comment

Recent Blog Entries

No recent entries