SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 305 Weeks Ago, 1 Day Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 18 Nov 2017 My Price 10.00

trying to build a lock application using an MSP430.

I am trying to build a lock application using an MSP430. The code I chose to unlock the 8856, but when I enter the combination 8766, the lock still opens.

I am guessing the problem is in how the program verifies the combination. I can't seem to understand why though. How can I fix the problem and why does it happen?

#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 CODE_LENGTH 4

// Function Prototypes
void initButtons(void);
void initLeds(void);
unsigned char readButtons(void);
void setLeds(unsigned char state);
void swDelay(char numLoops);

enum lock_state {
    START = 0,
    WAITING_FOR_INPUT = 1,
    OPEN = 2,
    FAIL = 3,
};

// Declare globals here

// Code to open lock
char lock_code[CODE_LENGTH] = {'8', '8', '5', '6'};

// Main
void main(void)
{
    char curr_key;

    char user_code[4]; // Code entered by user
    char display_code[4]; // Code to display (one * for each digit entered)

    enum lock_state state = START;

    int i;
    int digits_entered = 0;

    WDTCTL = WDTPW | WDTHOLD;        // Stop watchdog timer

    // Useful code starts here
    initButtons();
    initLeds();

    configDisplay();
    configKeypad();

      // *** Intro Screen ***

    GrClearDisplay(&g_sContext); // Clear the display
      GrFlush(&g_sContext);


    while (1)    // Forever loop
      {
        switch(state)
        {
        case START:
            GrClearDisplay(&g_sContext);
            GrStringDrawCentered(&g_sContext, "SecureLock v0.1", AUTO_STRING_LENGTH, 48, 15, OPAQUE_TEXT);
            GrStringDrawCentered(&g_sContext, "#SuperSecure", AUTO_STRING_LENGTH, 48, 25, OPAQUE_TEXT);
               GrStringDrawCentered(&g_sContext, "Enter Code:", AUTO_STRING_LENGTH, 48, 45, OPAQUE_TEXT);
               GrFlush(&g_sContext);

               digits_entered = 0;
               for(i = 0; i < CODE_LENGTH; i++) {
                   display_code[i] = ' ';

               }

            state = WAITING_FOR_INPUT;
            break;
        case WAITING_FOR_INPUT:
            curr_key = getKey();
            if(curr_key) {
                user_code[digits_entered] = curr_key;
                display_code[digits_entered] = '*';
                digits_entered++;
            }
            // Display a series of *'s for each digit entered
            GrStringDrawCentered(&g_sContext, display_code, CODE_LENGTH, 48, 55, OPAQUE_TEXT);
            GrFlush(&g_sContext);

            if(digits_entered < CODE_LENGTH) {
                state = WAITING_FOR_INPUT;
            } else {
                // Check each digit entered to see if it matched the code
                for(i = 0; i < CODE_LENGTH; i++) {
                    if(user_code[i] != lock_code[i]) {
                        state = FAIL;
                    } else {
                        state = OPEN;
                    }
                }
            }
            break;
        case OPEN:   // User entered code correctly!
            // Do some random fun stuff to indicate success (not relevant to challenge)
            BuzzerOn();
            TB0CCR0 = 32;
            TB0CCR5 = TB0CCR0/2;
            setLeds(0x0F);
            GrClearDisplay(&g_sContext);
               GrStringDrawCentered(&g_sContext, "Correct!  Yay!", AUTO_STRING_LENGTH, 48, 45, OPAQUE_TEXT);
               GrFlush(&g_sContext);
               BuzzerOff();
            swDelay(5);
            setLeds(0x00);
            state = START;
            break;
        case FAIL: // User entered an incorrect code!
            // Do some random fun stuff to indicate epic failure (not relevant to challenge)
            BuzzerOn();
            GrClearDisplay(&g_sContext);
               GrStringDrawCentered(&g_sContext, "Wrong!  :(", AUTO_STRING_LENGTH, 48, 45, OPAQUE_TEXT);
               GrFlush(&g_sContext);
            swDelay(5);
               BuzzerOff();
            state = START;
            break;
        }
      }  // 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--;
    }
}

Thanks, this should be an easy question

Answers

(5)
Status NEW Posted 18 Nov 2017 12:11 PM My Price 10.00

-----------  ----------- 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

Not Rated(0)