Using AVR Studio - My first C++ code

This is an extremely simple "Hello World" C++ code for the ATMega32 that you can follow using AVR Studio. You do not need any hardware such as the chip or even the ISP programmer cable because AVR Studio simulates the inputs and outputs, and you can see for yourself how the ports behave. This is just a simple article to show how to install the software and use it. I used an old version of AVR Studio because my computer is old and I did not want to waste time downloading large files that would take ages to install.

Step 1: New Project

Step 1: New Project

Select the "New Project" button.

Step 2: AVR GCC

Step 2: AVR GCC

Select "AVR GCC" and give the project a name, then press the "Next" button.

Step 3: ATMega32 Device

Step 3: ATMega32 Device

Select the "ATMega32" device and press the "Finish" button.

ATMega32 Port A Pinout

ATMega32 Port A Pinout

Here is the ATMega32 pin diagram. Ideally, you want to look over the documentation of this IC to understand its full capabilities. For this article, I shall be using the pins PA0 to PA7.

As these things go, it all boils down to being able make the pins produce +5 V and 0 V to blink LEDs. If you can start out learning that then you are half way there. Once you have made some LEDs blink! Call yourself a genius and get a degree from Cambridge University. Well, it is not that simple, unless your dad is the Chairman of the Board, but at least it gets you part way there. :-)

Hello World!

In this first program we use Port A, which consists of 8 physical pins, pin 33 to pin 40, as shown above. A data bit within the chip represents each physical pin. These bits are grouped together to make the Port A Data Register.

When you set a data bit to binary 1, the associated physical pin will produce 5 V. Hey, it is clever! When the data bit is set to binary 0, the physical pin produces zero volts.

If you set each bit to binary 1 in succession, one after the other, then each physical pin will produce 5-volts in the same succession. If there were LED lights connected to the pins, you would see each LED light up one after the other in succession.

Luckily you do not need to build a circuit with LED lights to see what is happening at the pins in Port A. AVR Studio provides a simulated I/O View that lets you see each bit of Port A.

Code

  1: #include <avr/io.h>
  2: // Example by Peter J. Vis for ATmega32
  3: // This program will turn each pin
  4: // of the PortA ON,sequentially.
  5: 
  6: int main(void)
  7: {
  8: // Set all the Data Direction Register bits
  9: // to Logic 1 which means all bits in PortA
 10: // will be sending data out.
 11: 
 12: DDRA = 0b11111111;
 13:  for ( ; 1==1 ; ) // loop while 1 equals 1
 14:  {
 15: // Send a sequence of binary numbers to the
 16: // PortA. Binary 1 is shifted along to the
 17: // right in each bit.
 18: 
 19:  
 20:  PORTA = 0b10000000;
 21:  PORTA = 0b01000000;
 22:  PORTA = 0b00100000;
 23:  PORTA = 0b00010000;
 24:  PORTA = 0b00001000;
 25:  PORTA = 0b00000100;
 26:  PORTA = 0b00000010;
 27:  PORTA = 0b00000001;
 28:  }
 29: return 1;
 30: }

Data Direction Register (DDR)

Each physical pin on Port A can send data out by making the physical pins produce +5 V or 0 V to represent binary 1 or 0. However, each physical pin can also receive data by sensing the voltage at the pins; however it cannot do both at the same time, and you have to tell it what you are going to do first by setting the mode.

The mode, whether the pin is going to be an input pin or an output pin, is set through another bit in the Data Direction Register (DDR). If the pin is to be used for output then set the corresponding bit in the DDR to binary 1, and if the pin is to be used for input, then set the bit to binary 0.

Building the Program

Building the Program

Copy and paste the program into AVR Studio and select "Build" from the menu option. The results of the building process should look something like above. It should report: Build succeeded with 0 warnings...

Debugging Code

Debugging Code

In the "I/O View" window, select "Port A". This will show registers of Port A and the bits represented by square boxes in the registers.

In the "Debug" menu, select "Start Debugging".

In the "Debug" menu, select "Auto Step".

In the I/O window, as shown above, every box in the DDRA register is grey, which means every pin is set to binary 1 and set for output. Grey means binary one. White means binary zero.

Under that, you should see "Port A" pin output. You should see each box becoming grey and then white in succession.

To stop, open the "Debug" menu and select "Reset".

I am using binary numbers in this example to keep things simple. Once you have mastered this part of the guide you need to learn some terminology. When you are describing the steps to a beginner, you do not say that you are running code. Instead, you say you are debugging code. :-)