我想使用 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_ */