gpt4 book ai didi

c - MSP430 上的摩尔斯电码输入

转载 作者:行者123 更新时间:2023-11-30 15:26:12 25 4
gpt4 key购买 nike

我想使用 MSP430G2553 的板载按钮读取按钮按下情况并将其转换为摩尔斯电码。我能够用正确的数字填充程序中的缓冲区(0 代表点,1 代表破折号)。

但是,当缓冲区被发送进行转换时,它只会返回一个错误。我现在只使用从莫尔斯电码翻译而来的数字,因为它们每个只有 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. 这个评论显然是错误的:

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

    仅当 GPIO 位为零时,这才有效。当它变回 1 时,您的代码中不会发生任何事情。

  3. 这个评论是错误的:

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

    因为 pressTime 永远不会增加。

关于c - MSP430 上的摩尔斯电码输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com