July 9, 2015

Using switch to blink LED……

Step 1 : Setup up clock and Enable Clock

SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

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

GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);

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

	GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU);

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

	GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU);

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.


Leave a Reply

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