简体   繁体   English

MSP430上的摩尔斯电码输入

[英]Morse Code Input on MSP430

I want to use the onboard button of the MSP430G2553 to read in button presses and convert them to Morse Code. 我想使用MSP430G2553的板载按钮读入按钮按下并将它们转换为摩尔斯电码。 I am able to fill the buffer in the program with the correct numbers (0's for dots and 1's for dash's). 我可以用正确的数字填充程序中的缓冲区(0表示点,1表示破折号)。

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. 我现在仅使用从莫尔斯电码转换的数字,因为它们每个只有5位,并使用它来创建密码。

        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. 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. 仅当GPIO位为零时,这才有效。 When it changes back to one, nothing happens in your code. 当它变回1时,代码中什么也没有发生。

  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. 因为pressTime永远不会增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM