Search Microcontrollers

Thursday, August 23, 2012

MSP430G2 - Pull up / down resistors

When using GPIO lines as inputs, you normally need to use pullup or pulldown resistors.
Most of the modern mcus now have them built in, selectable via software.

To understand why you need them I propose a simple and funny experiment with the MSP430 Launchpad.

Let's setup an extremely simple application, something that flashes the leds if we press the S2 button on the lanchpad.

The key thing here is that we are using a gpio input (p1.3) to detect whether the s2 button is pressed or not.
The flashing leds will just provide feedback on the fact that, yes, the button was pressed.

In order to show why we need pull resistors we will not configure one at the beginning

/*
 * main.c
 */

#include <msp430g2553.h>

void clockConfig()
{
 BCSCTL1 = CALBC1_16MHZ; // Set DCO
 DCOCTL = CALDCO_16MHZ;
 BCSCTL2= DIVS_2 + DIVM_0; // divider=4 for SMCLK and 1 for MCLK
}

void pinConfig()
{
  P1DIR |= BIT0  + BIT6; // leds = output
  P1DIR &= ~BIT3; // s2 = input
  P1OUT &= ~BIT0; // turn leds off
  P1OUT &= ~BIT6;
}

void main(void)
{
 WDTCTL = WDTPW + WDTHOLD;
 clockConfig();
 pinConfig();
 while (1)
 {
if ((P1IN & BIT3)>0) // if button bit is high
{ // flash leds
  P1OUT |= BIT0;
   P1OUT &= ~BIT6;
__delay_cycles(10000000);
  P1OUT &= ~BIT0;
   P1OUT |= BIT6;
         __delay_cycles(10000000);
}
 }


This experiment could eventually lead to unpredictable results (no worries, nothing will blow up -but hey, if it does don't call it on me! :P -), but hopefully we will be able to demonstrate the issue.

Now, the "magic" part :

Compile, load and run this simple software, let it run for a few seconds and watch the leds.
Nothing interesting happening there, right?
Ok, now without pressing it, touch with a finger the side of the button S2, you should survive this action.... unless a tiny and extremely poisonous scorpion was hiding behind it.

What happens?
The first thing is that you did not die, which is already a success for the experiment, but the most important thing is that eventually (depending on electrical charges on your body) you might have been able to trigger the state of pin p1.3.

While it looks pretty cool, we normally don't want that.
An input line left alone that way would eventually capture electrical charges from the surrounding environment, acting like an antenna.
The potential of such antenna would then be "floating" and it's difference to ground can be enough sometimes to reach a "1" logic state.

If we permanently connect a resistor (could be something like a 10K ohm) to ground we force -with a tiny current- the potential to level with the ground.
The amount of current needed to ensure this is in general very small, unless you have some extreme cases like you are designing circuitry for a nuclear weapon and such... but that's ok, I actually had to declare I will not use Code Composer Studio to create mass destruction weapons already.

Note : Isn't it funny' there is a country in the World were apparently you can buy an assault rifle with few question asked, but it gets tricky to download a C compiler just in case you could use it to produce weapons, which, admittedly, is the main usage of a C compiler :) 
Peace and love, stop using your compilers to kill people around the world (and smartly declaring it in online export forms at the same time)! 
Ah, I will ask you to sign some kind of declaration where you promise no to use my idea of hiding tiny scorpions behind push buttons.

Back to our resistors, the MSP430G2 enables them with the register P1REN, so let's just add the line

P1REN |= BIT3; 

in the pinconfig procedure.
Re-build, reload, ad re-execute and try again to touch the side of the button.
Yeah, I know, it was much more fun before, right?
 

No comments: