Digital I/O Project on AVR® Xplained 328PB

 Objective

This hands-on project steps through a simple example: controlling an LED from a switch on the Xplained board. Controlling a digital I/O pin can be a rewarding project for anyone new to the AVR® Microcontroller. Successfully completing this project confirms that you have completed the software build, the hardware setup, and were able to program the microcontroller successfully. The Studio 7 debugger is introduced at the end to show how to control a circuit through Studio 7 control, and how to view the internal registers within the device.

The ATmega328PB Xplained Mini evaluation kit is a hardware platform for evaluating the ATmega328PB microcontroller. A fully integrated programmer/debugger is included on the board. This provides a seamless integration with Atmel Studio 7.

 Materials

Hardware Tools (Optional)

Tool About Purchase
ATmega328PB-XplainedMini-50px.png
ATmega328PB Xplained Mini
Evaluation Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment

Additional Files

Files

 Procedure

The project controls an on-board LED using the push-button switch that is also on the board. The LED turns ON when the switch is pressed, default state (or unpressed switch) is LED OFF.

1

Task 1 - Project Creation

  • Open Atmel Studio 7 and Select File > New > Project from the main menu.
exec.png
  • Select GCC C Executable Project and give it the name Project1 and then choose a location to save the project on your computer. Click on OK.
device.png
  • The Device Selection window will appear. In the search bar enter 328P and then select the device Atmega328PB and click OK.

2

Task 2 - Main.c

The Main.c program controls the I/O pin through bit settings in the I/O control registers.

  • The main.c file is where the application code is added. The project has a main.c file already created but it only contains a while(1) statement. Modify main.c by entering the lines to match the program below:
int main(void)
{
    DDRB |= 1<<DDRB5;    // Direction of pin PB5 set as Output
while (1)
    {
    if (PINB & (1<<PINB7))    //read PIN PB7
    {
        PORTB &= ~(1<<PORTB5);    // PB7 is low so LED On
    }
    else
    {
        PORTB |= 1<<PORTB5;    // By default PB7 is high, LED Off
    }
     }
}

3

Task 3 - Build and Program Project

  • Select Build > Build Solution from the top menu of Studio 7 to compile the code. You should see a BUILD SUCCEEDED message in the output window of Studio 7.

4

Task 3 - Program Explained Board

The Xplained Board is now ready to be programmed.

  • Connect the Xplained to your computer using the included USB cable.
  • In the Solution Explorer, right-click on the project name and select Properties.
step41.png
  • Under Tool select mEDBG and debugWire as the interface.
step42.png
  • Select Debug > Start Without Debugging. The project will build and the program will be loaded into the Xplained board.

Note: You may see a Firmware Upgrade Message.

step43.png

If you see this message, select Upgrade and when the progress bar is complete, select Close. Restart by selecting Debug > Start Without Debugging.

5

Task 5 - DWEN Fuse

If the DWEN fuse is not enabled an error message is displayed. Click Yes and the Atmel Studio 7 will use the programming connection to set the fuse.

step51.png

6

Task 6 - Program Ready

Wait until Studio 7 completes the programming and the status message says Ready at the lower left corner.

step52.png

7

Task 7 - Execute

Press the switch on the Xplained board and the LED should light up. Release and the LED will shut off.

XplainedMini2.png

8

Task 8 - Debugging

Debugging a device is a useful way of determining how a program is running.

  • Select Debug > Start Debugging and Break. The project will build and the program will be loaded into the Xplained board.
  • The I/O View window will open up showing the various peripherals.
  • Click on the PORTB selection to open the I/O view for PORTB.
step61.png
  • Select Debug > Continue to run the program on the Xplained board. Pressing the switch will light the LED same as prior to debug.
  • Press Debug > Break All to stop the program.
  • The LED can now be controlled by the I/O View. Click on the PB5 LED (blue box in the picture0. When the block is solid (Pin Status 1) the LED will light. When the block is hollow (Pin Status 0) the LED will be off.
step62.png

9

Task 9 - Breakpoints

  • Click on the margin for the command line below the else statement to enable a breakpoint.
step71.png
  • Select Debug > Continue and the application will run on the Xplained board.
  • Press the switch on the Xplained board and the debugger will stop at the breakpoint because the code detects that the switch was pressed.
  • Release the switch and select Debug > Continue again and the board will be reset and start running waiting for you to press the switch.

10

Task 10 - Disable debugWIRE and Close

Once the project is complete, the debugWire fuse needs to be reset in order to program the Xplained board in the future. While still running in debug mode select Debug > Disable debugWire and Close. This will release the debugWire fuse.

 Conclusions

This simple project introduced how to control I/O on the AVR and also showed the basic steps for:

  • How to create a project in Studio 7
  • How to modify main.c
  • How to build and program a device
  • How to start a debug session
  • How to monitor port registers
  • How to use a breakpoint
  • How to disable debugWire mode
© 2017 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.