Getting started with Atmel SAM4S-EK2

LED 3

We'll now try to control the LED using a button on the board.

For this project, we'll be using ioport driver to control the LED and button. So go ahead and add the ioport driver from ASF Wizard and initialise it in the main.c file.

Find a button in the sam4s_ek2.h file. I have chosen GPIO_PUSH_BUTTON_1.

Before using functions of ioport driver for any pins, we have to specific the direction of the ioport that we'll be using. Here the LED should be given an output direction and button an input direction since we are controlling the LED using the button.

The function used to set the direction of an ioport is

ioport_set_pin_dir(pin, direction)

So, add the following

ioport_set_pin_dir(LED0_GPIO, IOPORT_DIR_OUTPUT);
ioport_set_pin_dir(GPIO_PUSH_BUTTON_1, IOPORT_DIR_INPUT);

Now we have to specify the mode for the button.

ioport_set_pin_mode(GPIO_PUSH_BUTTON_1, IOPORT_MODE_PULLUP);

For more information about mode PULLUP

Add a while loop which continuously checks the pin level of the button and consequently turn the LED on/off.

while (true) {
    ioport_set_pin_level(LED0_GPIO,
        ioport_get_pin_level(GPIO_PUSH_BUTTON_1));
}

Click here for project code