The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I'm trying to design a program in C to demonstrate the lab board buttons and LEDs: button S1 should turn the red LED on when pressed and off when unpressed, S2 should turn on the yellow LED, and so on for S3 and S4. My program works, but it doesn't behave properly when you I press more than one button at a time/... can you help?
I am using code composer studio and an MSP430 board. There is som/************** ECE2049 DEMO CODE ******************/
/************** 20 August 2016 ******************/
/***************************************************/
#include <msp430.h>
/* Peripherals.c and .h are where the functions that implement
* the LEDs and cap touch buttons are implemented. It is useful
* to organize your code by putting like functions together in
* files. You include the header associated with that file(s)s
* into the main file of your project. */
#include "peripherals.h"
#define BUTTON_S1 0x01
#define BUTTON_S2 0x02
#define BUTTON_S3 0x04
#define BUTTON_S4 0x08
#define LED_RED 0x08
#define LED_YELLOW 0x04
#define LED_BLUE 0x02
#define LED_GREEN 0x01
#define LED_NONE 0x00
// Function Prototypes
void initButtons(void);
void initLeds(void);
unsigned char readButtons(void);
void setLeds(unsigned char state);
// Main
void main(void)
{
char ret_val;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
initButtons();
initLeds();
configDisplay();
configKeypad();
GrClearDisplay(&g_sContext); // Clear the display
GrFlush(&g_sContext);
while (1) // Forever loop
{
// Read the state of all 4 lab board buttons
ret_val = ~readButtons() & 0x0f;
// If a button was pressed
if(ret_val) {
if(ret_val == BUTTON_S1) { // If S1 is pressed, turn on the red LED
setLeds(LED_RED);
}
if (ret_val == BUTTON_S2) { // S2 -> Yellow LED
setLeds(LED_YELLOW);
}
if (ret_val == BUTTON_S3) { // S3 -> Blue LED
setLeds(LED_BLUE);
}
if (ret_val == BUTTON_S4) { // S4 -> Green LED
setLeds(LED_GREEN);
}
} else {
setLeds(LED_NONE); // If no buttons are pressed, turn off the LEDs
}
} // end while (1)
}
void initButtons(void)
{
// Configure buttons as outputs using internal pull up resistors
// Logic 0 = Button Pressed; Logic 1 = Not Pressed
// Note order of buttons on board!
// Button 1: P7.0
P7SEL &= ~BIT0;
P7DIR &= ~BIT0;
P7REN |= BIT0;
P7OUT |= BIT0;
// Button 2: P3.6
P3SEL &= ~BIT6;
P3DIR &= ~BIT6;
P3REN |= BIT6;
P3OUT |= BIT6;
// Button 3: P2.2
P2SEL &= ~BIT2;
P2DIR &= ~BIT2;
P2REN |= BIT2;
P2OUT |= BIT2;
// Button 4: P7.4
P7SEL &= ~BIT4;
P7DIR &= ~BIT4;
P7REN |= BIT4;
P7OUT |= BIT4;
}
unsigned char readButtons(void)
// Return the state of the buttons as {B4,B3,B2,B1}
// in the lower nibble of the return value
// Note the order of the buttons on the board!
//
// smj -- 27 Dec 2015
{
char b1 = (P7IN & BIT0);
char b2 = (P3IN & BIT6) >> 6;
char b3 = (P2IN & BIT2) >> 2;
char b4 = (P7IN & BIT4) >> 4;
char ret = (b4 << 3) | (b3 << 2) | (b2 << 1) | (b1);
return ret;
}
void initLeds(void)
{
// Configure LEDs as outputs, initialize to logic low (off)
// Note the assigned port pins are out of order test board
// Red P6.2
// Green P6.1
// Blue P6.3
// Yellow P6.4
// smj -- 27 Dec 2016
P6SEL &= ~(BIT4|BIT3|BIT2|BIT1);
P6DIR |= (BIT4|BIT3|BIT2|BIT1);
P6OUT &= ~(BIT4|BIT3|BIT2|BIT1);
}
void setLeds(unsigned char state)
{
// Turn on 4 colored LEDs on P6.1-6.4 to match the hex value
// passed in on low nibble state. Unfortunately the LEDs are
// out of order with 6.2 is the left most (i.e. what we think
// of as MSB), then 6.1 followed by 6.3 and finally 6.4 is
// the right most (i.e. what we think of as LSB) so we have
// to be a bit clever in implementing our LEDs
//
// Input: state = hex values to display (in low nibble)
// Output: none
//
// smj, ECE2049, 27 Dec 2015
unsigned char mask = 0;
// Turn all LEDs off to start
P6OUT &= ~(BIT4|BIT3|BIT2|BIT1);
if (state & BIT0)
mask |= BIT4; // Right most LED P6.4
if (state & BIT1)
mask |= BIT3; // next most right LED P.3
if (state & BIT2)
mask |= BIT1; // third most left LED P6.1
if (state & BIT3)
mask |= BIT2; // Left most LED on P6.2
P6OUT |= mask;
}
void swDelay(char numLoops)
{
// This function is a software delay. It performs
// useless loops to waste a bit of time
//
// Input: numLoops = number of delay loops to execute
// Output: none
//
// smj, ECE2049, 25 Aug 2013
volatile unsigned int i,j; // volatile to prevent optimization
// by compiler
for (j=0; j<numLoops; j++)
{
i = 50000 ; // SW Delay
while (i > 0) // could also have used while (i)
i--;
}
}
ething wrong with the logic, but I can't figure out what...
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly