background image

Add the following code snippet (LED control using push button) in the .c file.

int

 main(

void

) {

    

/* configure LED pin as output */

    DDRB |= 1<<DDRB5; 

    

while

(1) {

        

/* check the button status (press - 0 , release - 1 ) */

        

if

(!(PINB & (1<<PINB7))) {

        

/*switch off (0) the LED until key is pressed */

            PORTB &= ~(1<<PORTB5); 

        } 

else

 {      

            

/* switch on (1) the LED*/

              

            PORTB |= 1<<PORTB5; 

        }

    }

}

5.

Code explanation:

Each PORT has three registers DDRx, PORTx, and PINx

The DDRx register is used to configure the port pin direction. 1 - Output; 0 - Input.

If one pin is configured as output pin and if the respective bit in PORTx is written logic one,
the respective port pin is driven high. If the same bit is written logic zero, the pin will be driven
low.

The PINx register is used to return the logic level available on the port pin

In this example code the PB7 Button is used as input and the PB5 LED0 as output

Here the LED0 is controlled based on the pushbutton status

As long the button is in pressed state (0) the LED0 will not glow (0)

On releasing the button (1) the LED0 will glow (1 - default)

Figure 2-4 Project Window

6.

In order to debug this project, configure the Tool and Interface in the Project properties. To open the
project properties, go to 

Project

 menu → 

Properties

. In the project properties, go to 

Tool

 tab →

Under the 

Selected Debugger/Programmer

, select the tool as mEDBG and interface as

Atmel AT08401: Getting Started with Atmel ATmega328PB [APPLICATION NOTE]

Atmel-42482A-Getting-Started-with-ATmega328PB_AT08401_Application Note-08/2015

12

Summary of Contents for ATmega328PB Xplained Mini

Page 1: ...d microcontroller Features Getting started with the ATmega328PB microcontroller and tools Getting started with Atmel ATmega328PB Xplained Mini Kit and Atmel Studio 6 2 This application note contains a...

Page 2: ...ard Identification of Xplained Mini Kit 8 1 5 2 Connect the ATmega328PB Xplained Mini UART to the mEDBG COM Port 9 1 6 Programming and Debugging 9 1 6 1 Programming the ATmega328PB Xplained Mini by us...

Page 3: ...C oscillator Security with fuses and lock bits Atmel QTouch Library support Compatibility between devices portability Peripheral touch controller Clock Failure Detection CFD Note For detailed informat...

Page 4: ...ize number of peripheral channels number of I O pins etc for this device can be found http www atmel com devices ATMEGA328PB aspx tab parameters Atmel AT08401 Getting Started with Atmel ATmega328PB AP...

Page 5: ...tions tab the recommended application areas not limited to for this device can be found http www atmel com devices ATMEGA328PB aspx tab applications In the Tools tab all the related tools like IDE pro...

Page 6: ...In the Documents tab all the kit related documents like schematics and user guide can be found Atmel AT08401 Getting Started with Atmel ATmega328PB APPLICATION NOTE Atmel 42482A Getting Started with A...

Page 7: ...mel Studio Webpage The Atmel Studio installer free IDE is available at http www atmel com tools ATMELSTUDIO aspx 1 4 2 Atmel Studio Microsite To learn more about Atmel Studio refer to the following mi...

Page 8: ...udio 6 2 SP2 Note There is no need to install part pack for Atmel Studio future versions 3 Launch the Atmel Studio 4 Connect the ATmega328PB Xplained Mini to the USB port and it will be visible in the...

Page 9: ...tings are 9600 baud N 8 1 The COM port settings can be changed by using the Device Manager 1 6 Programming and Debugging This section helps to program and debug the ATmega328PBXplained Mini kit by usi...

Page 10: ...nd Break 6 Atmel Studio will display an error message if the DWEN fuse in the ATmega328PB is not enabled click YES to make Studio set the fuse using the ISP interface 7 A debug session is started with...

Page 11: ...C executable Project template name the project and click OK to get the device selection wizard Figure 2 2 New Project Wizard 3 The Device Selection wizard will display as shown in Figure 2 3 Device Se...

Page 12: ...e pin will be driven low The PINx register is used to return the logic level available on the port pin In this example code the PB7 Button is used as input and the PB5 LED0 as output Here the LED0 is...

Page 13: ...d on ATmega328PB Xplained Mini as shown in Figure 2 6 Start without Debugging on page 13 and Figure 2 7 Start Debugging and Break on page 13 Figure 2 6 Start without Debugging Figure 2 7 Start Debuggi...

Page 14: ...mentation for various products http www atmel com webdoc Atmel Gallery https gallery atmel com Production Selection Guide Atmel MCU Selector on http www atmel com Ordering Samples and Buying evaluatio...

Page 15: ...ry Doc Rev Date Comments 42482A 08 2015 Initial document release Atmel AT08401 Getting Started with Atmel ATmega328PB APPLICATION NOTE Atmel 42482A Getting Started with ATmega328PB_AT08401_Application...

Page 16: ...ITS BUSINESS INTERRUPTION OR LOSS OF INFORMATION ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Atmel makes no representatio...

Reviews: