我想使用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_ */
最佳答案
此评论显然是错误的:
//P2.0 is the button input
P1REN |= BIT3;
if(!(P1IN & BIT3)){
current_button=true; // read button bit
}
仅当GPIO位为零时,这才有效。
当它变回1时,代码中什么也不会发生。
这个评论是错误的:
//determine how long the button was pressed
if ((current_button==true) && (buttonPresses >= 1)){
pressTime=1;
}
因为
pressTime
永远不会递增。