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 |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
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.
1
Task 1 - Project Creation
- Open Atmel Studio 7 and select File > New > Project from the main menu.
- 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.
- 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.
- Under Tool, select mEDBG and debugWire as the interface.
- 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. If you see this message, select Upgrade and when the progress bar is complete, select Close. Restart by selecting Debug > Start Without Debugging.
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.
- Select Debug > Continue to run the program on the Xplained board. Pressing the switch will light the LED the same as before 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 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.
9
Task 9 - Breakpoints
- Click on the margin for the command line below the else statement to enable a breakpoint.
- 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 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
Table of Contents
|