#4 External LED controlled by a GPIO Switch
We tried blinking of an LED. Now we will create a switch using one of the GPIO Pins. The Reset Button in FRDM-K20D50 does not have an alternate function. Hence I wanted to try out a Input switch.Take a look at the [Freescale K20D50M MCU Programming] #1 Helloworld on the Console using Processor Expert for quickstart
Lets get started.
First we need 2 things.
- Identify the PIN for the external LED and a ground pin
- then another PIN to act as a Switch Input and a ground pin
I identified PTC0 and PTB2 respectively.
to see the schematic to identify the PINS. There is a good youtube video which gives an example on K25Z board.
Next we will be adding the BITIO_LDD for the LDD configured to the PTC0 PIN.
Then a BITIO_LDD for the switch on PTB2
Setting up the BITIO_LDD isn't enough for the acting with in PTB2. We need to initialize the GPIO for the PIN. We will add a init_GPIO component and configure it to PTB
Generate the Pex code, and then add then lets add the below logic to toggle the led based on whether a wire between PTB2 and the GRND is connected or not..
The Pin's are connected to cathode, so 0 means high and 1 means low.
Code in main.c
{
if(SW1_GetVal(NULL)==0) // CONNECTED
{
led_SetVal(led_DeviceData);
}
else
{led_ClrVal(led_DeviceData);
}
WAIT1_Waitms(1000);
Now build the code and lets test it.
Here is how the pins are connected on the board
Now lets see what happens with ehe pins at PTB0 is connected, the red wires on the top side is connected.. and you see the LED glowing
Now lets see what happens when one of the wire on top is pulled out..
This is an example of how you use a GPIO pin as a switch to control through software.