July 9, 2015

Using switch to blink LED

Overview

This tutorial is a basic tutorial through which I wish to teach you how to take ininput using the Stellaris LaunchPad. For this tutorial, I will be using the one of the two on board switches named SW2 as the input. A switch is something basic and anyone, even someone without any prior knowledge about a micro controller and stuffs will surely understand. If you think so, it is time for you seriously consider what you have actually understood about a button switch. Actually, in real life, the switch is not ideal and is prone to many erroneous phenomena which is generally called as noise. Even though it is not proper to call it as a noise, lets for the time being term it as a noise. Consider a button switch which is connected to your Stellaris LaunchPad. Now Lets say you have connected the other end of the button switch to VCC. Now,for sure,when you press the button a HIGH will be read as the input as it will short the path. What I am interested is actually what happens when I release the button. Will it goback to LOW?? Obviously yes huh? Where else is it supposed to go otherwise? But the answer to the question is that the signal won’t go back to LOW but it will bedisturbed by “electromagnetic noise” and therefore it may switch between HIGH and LOW in a random fashion. Now Inorder to avoid this, in this case, we should use a pull down resistor. The Stellaris LaunchPad actually has it as an internal feature. I will talk about in detail later. So i would strongly suggest that you go through some reference about the pull down resistors, pull up resistors, button debouncing etc. The other prerequisite for this tutorial is the Tutorial 1 about blinking an LED

.


/*
 * Stellaris LAUNCHPAD LM4F120H5QR
 * C++ code to use SW_1 button to turn on an RED LED(PORTF 1)
 */
#include 
#include 
#include 
#include 

int main(void)
{
/*
 * The System clock is run using a 16 Mhz crystal connected to the main oscillator pins of the microcontroller
 * This generates a internal clock signal of 400 Mhz using the PLL
 * The signal is prescaled by the system by 2
 * Now we are defining a prescale of 5 in addition to make the clock frequency 40MHz
 * The system clock frequency must be less than or equal to 80MHz
 */
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
/*
 * Peripherals are enabled with this function.
 * At power-up, all peripherals are disabled.
 * System Clock to the peripheral must be enabled in order to operate or respond to register reads/writes.
 */
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Set PF4(SW_1) as input
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
/*
 * As it is a switch, we should use a pull up resistor.
 * Stellaris can be configured with various pull up/down resistors based on the drive strength(current) specified.
 * Refer to the Stellaris API page number 124 for more configurations
 * Link: www.ti.com/lit/ug/spmu019p/spmu019p.pdf
 */
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU);
//Set PF1(RED LED) as output
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
//Turn of the LED
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);

while(1)
{
//Check whether the button is pressed.
if(!GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4))
//Set PF1 as high. 2 ~ 0b00000010-->mask PF1
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 2);
else
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *