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 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 AboutInstallers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment

Additional Files

Files

 Procedure

The project controls an onboard 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.

2

Task 2 - Main.c

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

  • Themain.c file is where the application code is added. The project has amain.c file already created but it only contains awhile(1) statement. Modifymain.c by entering the lines to match the program below:
int main(void){    DDRB |= 1<<DDRB5;    // Direction of pin PB5 set as Outputwhile (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

  • SelectBuild > 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 theSolution Explorer, right-click on the project name and selectProperties.
step41.png
  • UnderTool, selectmEDBG anddebugWire as the interface.
step42.png
  • SelectDebug > 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. If you see this message, selectUpgrade and when the progress bar is complete, selectClose. Restart by selectingDebug > Start Without Debugging.

step43.png

5

Task 5 - DWEN Fuse

If the DWEN fuse is not enabled an error message is displayed. ClickYes 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.

  • SelectDebug > Start Debugging and Break. The project will build and the program will be loaded into the Xplained board.
  • TheI/O View window will open up showing the various peripherals.
  • Click on thePORTB selection to open the I/O view for PORTB.
step61.png
  • SelectDebug > Continue to run the program on the Xplained board. Pressing the switch will light the LED the same as before debug.
  • PressDebug > Break All to stop the program.
  • The LED can now be controlled by the I/O View. Click on thePB5 LED (blue box in the picture). 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 theelse statement to enable a breakpoint.
step71.png
  • SelectDebug > 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 selectDebug > 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 to program the Xplained board in the future. While still running in debug mode, selectDebug > 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 modifymain.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