Saturday, September 10, 2011

4 LEDs Controlled by DIP switch

Project Summary
Use a 4 position DIP Switch to turn on and off four LEDs.

I started off with a M430G2452 that was included with the capacitive touch booster pack.


Switch Pin
SW0 P1.2
SW1 P1.3
SW2 P1.4
SW3 P2.0

And for the LEDS.

LED Pin
LED0 P1.0
LED1 P1.6
LED2 P1.7
LED3 P2.5

I had two green LEDs I got a long time ago from Radio Shack. The LEDs have two pins and the long one is the anode and short one the cathode. The anode is where you would apply a positive voltage assuming you are grounding the cathode.

Typically you add a current limiting resistor so you don't burn out the LED. I chose 22ohm resistors. I tried a 1kohm, but it limited the current too much and the led was dim.

I discovered through trial and error that the DIP switch setup needed pull up resistor. This ensured for my case that when the dip switches where open the input pins would go high. I tied the high voltage to the Vcc coming off the MSP430 (Pin 1).

Typically, you seem to be safe using a 5-10khom resistor. The reason for the using a resistor at all is that when the switch is closed you would be tying Vcc to ground. This could burn out a power supply or just waste power.

For my switch pins I used a pull up resistor of 9.1 kohms.

Now on to the code
I used the Code Composer Studio v4 free edition.

//******************************************************************************
//  MSP430G34xx - LED Switch Controller
//
//  Description: Switch Controls 4 LEDs 
//  
//
//                MSP430g24xx
//             -----------------
//         /|\|                 |
//          | |                 |
//          --|                 |
//            |                 |
//      LED<--|P1.0             |
//         SW1|P1.2         P1.6|-->LED
//         SW2|P1.3         P1.7|-->LED
//         SW3|P1.4         P2.5|-->LED
//         SW4|P2.0  
//  Matt L.
//
//  9/4/2011
//  Built with CCE
//******************************************************************************

#include  "msp430g2452.h"

#define LED0 BIT0
#define LED1 BIT6
#define LED2 BIT7
#define LED3 BIT5 //Port2

#define SW0 BIT3
#define SW1 BIT2
#define SW2 BIT4
#define SW3 BIT0  //Port2

#define ON 0x01;
#define OFF 0x00;

void checkSwitches();
void setOutputDirections(void);
void allLEDsOff(void);

void main(void) {
 WDTCTL = WDTPW + WDTHOLD;    // Stop watchdog timer
 P1DIR = (LED0+LED1+LED2);    // Set LED to output direction and SW to inputs
 P2DIR = LED3;                       // Set LED to output direction and SW to inputs
 P1SEL &= ~(SW0+SW1+SW2);   // Select Port 1 dip switch
 P2SEL &= ~(SW3);     // Select Port 2 dip switch
 P1OUT &= ~(LED0+LED1+LED2);   // Set Port 1 LEDs off
 P2OUT &= ~LED3;      // Set Port 2 LED3 off
 
 //Main Loop
 while( 1 ) {
  
  checkSwitches();
  
 }//End of main loop 
}  

//Function void checkSwitches()
//Returns: Nothing
//Description:
/*
 * Checks SW0-4 if the switch is high turn on the LED.
 * If the switch is low turn off.
*/
void checkSwitches(){
 if(P1IN & SW0){
  P1OUT ^= LED0;
 }
 else{
  P1OUT &= ~LED0;
 }
 
 if(P1IN & SW1){ 
  P1OUT ^= LED1;
 }
 else{
   P1OUT &= ~LED1;
 }
 
 if(P1IN & SW2){
  P1OUT ^= LED2;
 }
 else{
  P1OUT &= ~LED2;
 }
 
 if(P2IN & SW3){
  P2OUT ^= LED3;
 }
 else{
  P2OUT &= ~LED3;
 }
  
}


The comments describe what the code does as best as I could feel free to ask questions. Below are pictures of my results.



No comments:

Post a Comment