Morse Code Input on MSP430

767 Views Asked by At

I want to use the onboard button of the MSP430G2553 to read in button presses and convert them to Morse Code. I am able to fill the buffer in the program with the correct numbers (0's for dots and 1's for dash's).

However, when the buffer is then sent to be converted it only returns an error. I am only using numbers translated form Morse Code now as they are only 5 bits each and am using it to create a passcode.

        int main(void) {
        WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer

        InitButtonLED();

        int c;

        while(1){

            c = MorseInOut();
            UARTTransmit(c);
        }
    }

    void InitButtonLED(){

    //P2.0 is the button input
    P1REN |= BIT3;

}

int MorseInOut(){

    int buttonPresses = 0; //number of times the button has been pressed
    int pressTime = 0; //how long the button was pressed
    int buffIt = 0; //buffer iterator
    int buffer[5]; //buffer for long and short characters
    bool current_button = false; // variable to hold the state of the button
    bool last_button_state = false; // state of the button the last time it was read
    int number;

    while(1){

        if(!(P1IN & BIT3)){
        current_button=true; // read button bit
        }

        if ((current_button==true) && last_button_state){ // did the button go down?
                        buttonPresses=buffIt;
        }

        if ((buttonPresses == 5) && (current_button==true)){

            //translates buffer
            number = MorseConvert(buffer);

            return number;

        }

        last_button_state=current_button;

        //determine how long the button was pressed
        if ((current_button==true)  && (buttonPresses >= 1)){

            pressTime=1;

        }

        if ((current_button==true)  && (buttonPresses < 1)){

            pressTime=1;

        }

        //determines if button was held for a long time or short time
        if ((pressTime >= 20000) && (current_button==true) && (buttonPresses >= 0)){

            buffer[buffIt] = 1;
            buffIt++;
            pressTime = 0;

        }

        if ((pressTime < 20000) && (pressTime > 0) && (current_button==true) && (buttonPresses >= 0)) {

            buffer[buffIt] = 0;
            buffIt++;
            pressTime = 0;

        }
    }
}

int MorseConvert(int buffer[]){

    //translates buffer values into integers
    if ((buffer[0] == 1) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1) && (buffer[4] == 1)){
        return 0;
    }

    else if ((buffer[0] == 0) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1) && (buffer[4] == 1)){
        return 1;
    }

    else if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 1) && (buffer[3] == 1) && (buffer[4] == 1)){
        return 2;
    }

    else if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 1) && (buffer[4] == 1)){
        return 3;
    }

    else if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) && (buffer[4] == 1)){
        return 4;
    }

    else if ((buffer[0] == 0) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) && (buffer[4] == 0)){
        return 5;
    }

    else if ((buffer[0] == 1) && (buffer[1] == 0) && (buffer[2] == 0) && (buffer[3] == 0) && (buffer[4] == 0)){
        return 6;
    }

    else if ((buffer[0] == 1) && (buffer[1] == 1) && (buffer[2] == 0) && (buffer[3] == 0) && (buffer[4] == 0)){
        return 7;
    }

    else if ((buffer[0] == 1) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 0) && (buffer[4] == 0)){
        return 8;
    }

    else if ((buffer[0] == 1) && (buffer[1] == 1) && (buffer[2] == 1) && (buffer[3] == 1) && (buffer[4] == 0)){
        return 9;
    }

    else{
        return 99;
    }

}

    #ifndef STATE_H_
#define STATE_H_

#include <stdint.h>
#include <stdbool.h>

void InitButtonLED();
int MorseInOut();
int MorseConvert(int buffer[]);


#endif /* STATE_H_ */
1

There are 1 best solutions below

0
On
  1. This comment is obviously wrong:

    //P2.0 is the button input
    P1REN |= BIT3;
    
  2. if(!(P1IN & BIT3)){
    current_button=true; // read button bit
    }
    

    This has an effect only if the GPIO bit is zero. When it changes back to one, nothing happens in your code.

  3. And this comment is wrong:

    //determine how long the button was pressed
    if ((current_button==true)  && (buttonPresses >= 1)){
        pressTime=1;
    }
    

    because pressTime is never incremented.