AVR®: Finding Documentation and Turning On an LED
In This Video
- Find the device datasheet, "Xplained Mini User's Guide", and its schematics.
- Start a new project in MPLAB® X IDE.
- Demonstrate how to efficiently use the datasheet to understand how to configure a pin and turn on an LED.
Reference Materials
Procedure
Start MPLAB X
Start MPLAB X and plug in the Xplainined Mini to one of your USB ports.
If the Xplained Mini is enumerated properly, you should see the following screen with the Kit Window opened showing the ATmega328PB Xplained Mini board.
Create the Project
Select File > New Project from the menu at the top.
A New Project dialog box will appear. Select Standalone Project from the options and then select Next.
At the Select Device screen, select the following for the device Family, Device, and Tool. Select Next.
Now select the XC8 Compiler Toolchain and select Next
Next, we'll create a name for our project. Enter "Getting_Started_with_AVR" in the Project Name dialog box and then select Finish
MPLAB X IDE has created our project and populated the user interface with several folders.
Right-click on the Source Files folder and hover over New and then select avr-main.c
Change the name of the File Name at the top of the dialog box to main and then select Finish.
MPLAB X has now created a main document as part of our project and it should be open for you to edit. This file is where we'll enter our code to turn on the led of the ATmega328PB Xplained Mini. As you can see, it populated the file with some of the things we will need in our project. The avr/io.h header file has been included which contains definitions we'll need along with the main while loop for our application code to reside in.
Find the documents
the documents we need to get started programming the board
In the kit window that opened when the ATmega328PB was connected, there is a section indicated by External Links located below the board image and description. If not already expanded, select that section to expose the links to the ATMega328PB Mini User Guide, schematics, and datasheet.
LED PIN
- Find what pin the LED is connected to so that we can turn on the correct pin.
The ATmega328PB Xplained Mini User manual as well as the ATmega328PB Xplained Mini Schematic shows that the LED is connected to pin PB5 through a 1k ohm resistor. In order to turn the LED on, we will need to make PB5 high.
In order to determine how to do that, we will consult the ATmega328PB datasheet. PB5 is a GPIO pin (General Purpose Input Output) and the best place to start in the datasheet is section 17 which discusses I/O-Ports and how to configure them.
From the datasheet, each port pin consists of three register bits: DDxn, PORTxn, and PINxn. The DDxn bit in the DDRx register selects the direction of this pin.
Set Pin Direction
First we set the direction of the pin and in this case, we want it to be an output. In C Programming, we can accomplish this with the following line of code:
Breakdown
- DDRB is a register in the ATmega328PB microcontroller that controls the data direction of the digital I/O pins on port B. Each bit in the register corresponds to a pin on the port, with bit 0 corresponding to pin PB0, bit 1 to pin PB1, and so on.
- |= is the bitwise OR assignment operator. It performs a logical OR operation between the current value of PORTB and the value on the right-hand side of the operator and then stores the result back into PORTB.
- (1 << PB5) is a bit-shift operation that moves the value 1 to the left by the number of bits specified by the PB5 constant, which is 5. This evaluates to 0b00100000.
When we perform the bitwise OR operation between DDRB and (1 << PB5), the result will be that the bit corresponding to pin PB5 in DDRB is set to 1, while the other bits remain unchanged. This sets the data direction of pin PB5 to be an output.
Now we want to set the pin high so that it can drive the Led.
Breakdown
- PORTB is a register in the ATmega328PB microcontroller that controls the state of the digital output pins on port B. Each bit in the register corresponds to a pin on the port, with bit 0 corresponding to pin PB0, bit 1 to pin PB1, and so on.
- |= is the bitwise OR assignment operator. It performs a logical OR operation between the current value of PORTB and the value on the right-hand side of the operator, and then stores the result back into PORTB.
- (1 << PB5) is a bit-shift operation that moves the value 1 to the left by the number of bits specified by the PB5 constant. In this case, PB5 is defined as the value 5, so (1 << PB5) evaluates to 0b00100000.
This line of code sets the bit corresponding to pin PB5 in the PORTB register to 1 while leaving the other bits unchanged. This effectively sets the output voltage of pin PB5 to the logic HIGH level.
Combine the Code
- Let's put these lines of code together in MPLAB X.
int main(void) {
DDRB |= (1 << PB5); // set PB5 as output pin
while (1) {
PORTB |= (1 << PB5); // set PB5 high
}
}
Place the two lines of code into the main file and select the button indicated below to make and program the target device.
If everything was done as indicated, the led on the board should light continuously.