Tutorial 2 : 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 <inc/hw_types.h> #include <inc/hw_memmap.h> #include <driverlib/gpio.h> #include <driverlib/sysctl.h> 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); } }
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
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
if(!GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4))
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.
Pretty section of content. I just stumbled upon your
weblog and in accession capital to assert that I acquire in fact enjoyed account your blog posts.
Anyway I’ll be subscribing to your feeds and even I achievement you access consistently fast.
It is appropriate time to make some plans for the future and it’s time to be happy.
I have read this post and if I could I desire to
suggest you few interesting things or advice.
Maybe you could write next articles referring to this article.
I desire to read more things about it!
Hi there i am kavin, its my first occasion to commenting anyplace, when i read this paragraph i thought i could also
make comment due to this good post.
Why visitors still make use of to read news papers
when in this technological world the whole thing is accessible
on web?
Your method of describing the whole thing in this paragraph is actually nice,
every one be able to easily be aware of it, Thanks a lot.
If you wish for to get a great deal from this article then you have
to apply such techniques to your won website.
You really make it seem so easy with your presentation but I find this matter to be
really something that I think I would never understand.
It seems too complicated and extremely broad for me.
I am looking forward for your next post, I’ll try to get
the hang of it!
I’m really enjoying the theme/design of your web site.
Do you ever run into any web browser compatibility
problems? A small number of my blog visitors have complained about my blog not operating correctly in Explorer but looks great
in Chrome. Do you have any tips to help fix this problem?
My brother recommended I might like this
blog. He was totally right. This post actually made my day.
You can not imagine simply how much time I had spent for this info!
Thanks!
Howdy! I simply would really like to give a big thumbs up for the good data you’ve got here on this post.
I will probably be returning again to your weblog
more soon.
Hello I am so thrilled I found your website, I really
found you by accident, while I was researching on Bing for something
else, Nonetheless I am here now and would just like
to say thanks a lot for a remarkable post and a all round exciting blog
(I also love the theme/design), I don’t have time to
go through it all at the minute but I have bookmarked it and also added your RSS feeds, so
when I have time I will be back to read a great deal more, Please do keep up the great job.
820704 229680Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how could we communicate? 461803
You can visit Contact Us for contacting us or if you have any questions please visit
Tech Darbar
If you would like to improve your experience simply
keep visiting this web site and be updated with the newest news posted here.
Greetings, I think your website could possibly be having browser compatibility problems.
Whenever I look at your site in Safari, it looks fine however when opening in Internet Explorer, it’s got some overlapping
issues. I simply wanted to provide you with a quick heads up!
Other than that, great site!
You’re so cool! I do not suppose I’ve read through a single thing like that before.
So nice to find another person with a few original thoughts on this issue.
Really.. many thanks for starting this up. This site is one thing that is required on the internet, someone with some originality!
Great delivery. Sound arguments. Keep up the great spirit.
Great delivery. Sound arguments. Keep up the amazing spirit.
Hi there, I desire to subscribe for this website to obtain newest
updates, so where can i do it please help out.
You have some really interesting info here, well written as well as your website is user-fr
It’s amazing to pay a quick visit this web page and reading the
views of all colleagues regarding this piece of writing,
while I am also eager of getting knowledge.
Wow that was odd. I just wrote an really long
comment but after I clicked submit my comment didn’t show up.
Grrrr… well I’m not writing all that over again. Regardless, just wanted to say excellent blog!
What i don’t realize is actually how you are no longer actually a lot
more neatly-liked than you might be now. You are very intelligent.
You know thus considerably in the case of this subject, made
me personally consider it from numerous numerous angles.
Its like women and men don’t seem to be fascinated except it is one thing to accomplish with Lady gaga!
Your individual stuffs excellent. At all times deal with it up!
I think the admin of this site is genuinely working hard for his
web site, as here every material is quality based information.
Hmm is anyone else experiencing problems with the images
on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog.
Any responses would be greatly appreciated.
I think this is one of the most important info for me.
And i am glad reading your article. But wanna remark on some general things,
The site style is ideal, the articles is really great : D.
Good job, cheers
Way cool! Some very valid points! I appreciate you penning this article plus the
rest of the website is very good.
I’m truly enjoying the design and layout of your blog.
It’s a very easy on the eyes which makes it much more pleasant
for me to come here and visit more often. Did you hire out a designer to
create your theme? Great work!
Pretty nice post. I just stumbled upon your weblog and wished to say that I have
really enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and
I hope you write again very soon!
Hi, Neat post. There is an issue along with your web site in web explorer, could test this?
IE still is the marketplace leader and a large part of folks will leave out your
excellent writing because of this problem.
I do not even know how I ended up here, but I thought this post was
good. I don’t know who you are but certainly you are
going to a famous blogger if you aren’t already 😉 Cheers!
Excellent work you have performed, this site is really cool with good information.
Hi there! I’m at work surfing around your blog from my
new iphone 3gs! Just wanted to say I love reading through your blog and look forward to all your posts!
Keep up the great work!
I think the admin of tuis site is genuinely working hard for his website, since here every data is quality
based material.
Yoou are so awesome! I don’t think I’ve truly read through anything like this before.
So wonderful to discover somebody with unique thoughts on this topic.
Seriously.. thanks for starting his up. This website iis one thing that is needed on the
internet, someone wit somme originality!
I just couldn’t depart youur web site before suggesting
that I actually loved the usual info a person provide in your guests?
Is gonna be back ceaselessly in ordedr to check
up on new posts
What’s up, yyes this paragraph iss truly good andd I have learned
lot of things from it concerning blogging. thanks.
I’m not that much of a internet reader to tell the truth however your
site is really nice, keep writing! I’ll proceed and bookmark
your site to come back later. Good luck.
If some one wishes to be updated with latest technologies after that he must be go too see
this site and be up to date daily.
I have learn several excellent stuff here.
Definitely price bookmarking for revisiting. I surprise how
so much effort you putt to make any such great informative website.
I think the admin of this web site is really working hard in favor off his site, for thee reason that here every information is
quality based stuff.
It’s hard to come by educated people for this subject, however, you sound like you know what you’re talking about!
Thanks
I like the valuable info you supply for your articles.
I’ll bookmark your blog and check again right here regularly.
I’m moderately certain I’ll learn many new stuff proper right here!
Good luck for the next!
This design is incredible! You definitely know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.
I really loved what you had to say, and more than that, how you presented it.
Too cool!
I am not sure where you’re getting your information, however good topic.
I must spend a while studying much more or working out
more. Thank you for magnificent information I used to be searching for this information
for my mission.
Good site you have got here.. It’s hard to find high quality writing
like yours nowadays. I seriously appreciate individuals like you!
Take care!!
It’s remarkable in favor of me to have a web site, which is
beneficial in support of my experience. thanks admin
I was pretty pleased to find this website. I wanted to thank you for your time for this particularly fantastic read!!
I definitely appreciated every little bit of it
and i also have you saved to fav to see new things in your website.
Can you tell us more about this? I’d want to find out some additional information.
You really make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!
Very informative article post.Really thank you! Really Great.