Figure 2-4. Project Window
5.
Add the following code snippet (LED control using push button) in the
main.c
file.
int
main(
void
)
{
/* enable the pull-up function */
MCUCR &= ~(1<<PUD);
/* enable pull-up for button */
PORTC |= 1<<PORTC6;
/* configure LED pin as output */
DDRC |= 1<<DDRC7;
while
(1)
{
/* check the button status (press - 0 , release - 1 ) */
if
(!(PINC & (1<<PINC6)))
{
/*switch on the LED until button is pressed */
PORTC &= ~(1<<PORTC7);
}
else
{
/* switch off the LED if button is released*/
PORTC |= 1<<PORTC7;
}
}
}
6.
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
–
ATmega324PB Xplained Pro kit does not have pull-ups connected and hence internal pull-up
have to be for the button. Set the
MCUCR > PUD
bit as logic zero to enable pull-up.
–
When a pin is configured as input and the respective bit in PORTx is written logic one, the
respective pin is internally pulled up
–
The PINx register is used to return the logic level available on the port pin
Atmel AT06621: Getting Started with Atmel ATmega324PB [APPLICATION NOTE]
Atmel-42626A-Getting-Started-with-Atmel-ATmega324PB_AT06621_Application Note-03/2016
11