Tutorial 2 : Using Switch to Blink LED
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.
The code was provided before hand so that you will actually get a better understanding of the code. Otherwise it would be really boring and most of you will find it irresistible to wait for the final code.
Step 1 : Setup up clock and Enable Clock
The button switch SW2 on your Stellaris LaunchPad is actually connected to the PF4 of the LM4F120H5QR. So obviously, you will have too use the PORTF. As explained in the last tutorial, the SysCtlClockSet is used to set up the clock, whereas the SysCtlPeripheralEnable is used to enable the System Clock supply to the GPIOF which we would actually require to use our switch which is connected to PF4. I wont be explaining about the code snippets that we have already taught in the earlier tutorials again.
Step 2 : Setting Up Input
Once the system clock to the particular port has been enabled, the next thing that we have to do is to make the particular pin on the micro controller an input pin so that we can use it to interface our button. So this line of code will help us get it done
elegantly. I believe that the function is self explanatory.
Step 3 : Enabling Pull Up Resistor
The GPIOPadConfigSet function is actually used to the maximum current that is supposed to flow through the input pin and whether a pull up is required or not. You may wonder why i am using a pull up ressistor and not a pull down ressistor.
Actually if you go through the schematic of the LaunchPad board, you will find that the Switch is actually connected to ground and not VCC. so therefore, when the button is released, i want to ensure that the state is HIGH and that is the reason why we go for a pull up resistor. More specifically standard weak pull up. There is another mode called opendrain pull up also available. The push-pull output actually uses two transistors. Each will be on to drive the output to the appropriate level: the top transistor will be on when the output has to be driven high and the bottom transistor will turn on when the output has to go low.
The open-drain output lacks the top transistor. When the output has to go high you simply turn off the bottom transistor, but the line is now pulled high only by the pullup resistor.Your micro allows you to select between the two types, which means that by setting some bits in some register you actually enable/ disable the top transistor and enable/disable the pullup (if internal, otherwise you just disable the top transistor and have to use an external pullup).
The advantage of the push-pull output is the higher speed, because the line is driven bothways. With the pullup the line can only rise as fast as the RC time constant allows. The R is the pullup, the C is the parasitic capacitance, including the pin capacitance and the board capacitance.The push-pull can typically source more current. With the open-drain the current is limited by the R and R cannot be made very small, because the lower transistor has to sink that current when the output is low; that means higher power consumption.However, the open-drain allows you to cshort several outputs together, with a common pullup. This is called an wired-OR connection. Now you can drive the output low with any of the IO pins. To drive it high all ouputs have to be high. This is advantageous in some situations, because it eliminates the external gates that would otherwise be required.
Step 4 : Reading From an Input Pin
This statement actually checks whether the Pin is pressed. I think that the function is self explanatory. Actually, we are checking whether it is low, because of the reason that the button is actually grounded at the other end and therefore, when pressed we are expecting a LOW signal.