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