晚上闲的无聊,突发奇想,做个发报机来玩玩,看了一下莫尔斯电码的资料,程序快写完了才看到别人的写的更简单的程序...就没接着写了
参考资料:https://en.wikipedia.org/wiki/Morse_code
从串口发送文字语句,在PIN13、PIN12输出高低电平,PIN12接上蜂鸣器(我的蜂鸣器是低电平触发)就可以听到Dida声了,听着还挺带感的hhhh
long m1 = 0xBBBB8;
long m2 = 0x2BBB8;
long m3 = 0x3BBA8;
long m4 = 0x2AB8;
long m5 = 0xAA8;
long m6 = 0x3AA8;
long m7 = 0xEEA8;
long m8 = 0x3BBA8;
long m9 = 0xEEEE8;
long m0 = 0x3BBBB8;
long mA = 0xB8;
long mB = 0xEA8;
long mC = 0x3AE8;
long mD = 0x3A8;
long mE = 0x8;
long mF = 0xAE8;
long mG = 0xEE8;
long mH = 0x2A8;
long mI = 0x28;
long mJ = 0xBBB8;
long mK = 0xEB8;
long mL = 0xBA8;
long mM = 0x3B8;
long mN = 0xE8;
long mO = 0x3BB8;
long mP = 0x2EE8;
long mQ = 0xEEB8;
long mR = 0x2E8;
long mS = 0xA8;
long mT = 0x38;
long mU = 0x2B8;
long mV = 0xAB8;
long mW = 0xBB8;
long mX = 0x3AB8;
long mY = 0xEBB8;
long mZ = 0x3BA8;
long mSpace = 0x00;
#define ledPin 13
#define buzzerOut 12
#define delayTime 50
String comData = "";
/*Test
ABCDEFGHIJKLMNOPQRSTUVWXYZ
*/
void setup() {
Serial.begin(9600);
pinMode(buzzerOut, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerOut, HIGH);
}
void loop() {
while (Serial.available() > 0)
{
comData += char(Serial.read());
delay(2);
}
if (comData.length() > 0)
{
Serial.println();
Serial.println();
Serial.print(comData);
Serial.print(" ");
Serial.println(comData.length());
for (unsigned int count = 0; count < comData.length(); count ++) {
ctoM(comData[count]);
}
Serial.println();
comData = "";
}
}
/****************************************************************************/
void ctoM(char singleCharacter) {
//Serial.print(singleCharacter);
//Serial.print(" ");
switch (singleCharacter) {
case'A': mtoD(mA);
break;
case'B': mtoD(mB);
break;
case'C': mtoD(mC);
break;
case'D': mtoD(mD);
break;
case'E': mtoD(mE);
break;
case'F': mtoD(mF);
break;
case'G': mtoD(mG);
break;
case'H': mtoD(mH);
break;
case'I': mtoD(mI);
break;
case'J': mtoD(mJ);
break;
case'K': mtoD(mK);
break;
case'L': mtoD(mL);
break;
case'M': mtoD(mM);
break;
case'N': mtoD(mN);
break;
case'O': mtoD(mO);
break;
case'P': mtoD(mP);
break;
case'Q': mtoD(mQ);
break;
case'R': mtoD(mR);
break;
case'S': mtoD(mS);
break;
case'T': mtoD(mT);
break;
case'U': mtoD(mU);
break;
case'V': mtoD(mV);
break;
case'W': mtoD(mW);
break;
case'X': mtoD(mX);
break;
case'Y': mtoD(mY);
break;
case'Z': mtoD(mZ);
break;
case'0': mtoD(m0);
break;
case'1': mtoD(m1);
break;
case'2': mtoD(m2);
break;
case'3': mtoD(m3);
break;
case'4': mtoD(m4);
break;
case'5': mtoD(m5);
break;
case'6': mtoD(m6);
break;
case'7': mtoD(m7);
break;
case'8': mtoD(m8);
break;
case'9': mtoD(m9);
break;
case' ': ctoD(' ');
break;
}
//Serial.println();
}
void mtoD(long mCharacter) {
int i;
int bitDataLLast, bitDataLast, bitData;
/*
for (i = 0; i < sizeof(mCharacter)*8; i++) {
bit_data = (mCharacter >> i) & 0x01;
digitalWrite(buzzerOut, bit_data);
Serial.print(bit_data);
}*/
//Serial.print("serialOutBIN:");
//Serial.println(mCharacter, BIN);
//Serial.print("bitReadOut:");
for (i = 0; i < 32; i++) {
if (bitRead(mCharacter, 31 - i) == 1)break;
}
for (; i < 32; i++) {
bitDataLLast = bitRead(mCharacter, 33 - i);
bitDataLast = bitRead(mCharacter, 32 - i);
bitData = bitRead(mCharacter, 31 - i);
if ((bitDataLLast == 1) && (bitDataLast == 1) && (bitData == 0))Serial.print("-");
else if ((bitDataLLast == 0) && (bitDataLast == 1) && (bitData == 0))Serial.print(".");
else if ((bitDataLLast == 0) && (bitDataLast == 0) && (bitData == 0))Serial.print("/");
digitalWrite(buzzerOut, 1 - bitData);
digitalWrite(ledPin, bitData);
delay(delayTime);
}
//Serial.print(" ");
}
void ctoD(char singleInterpunction) {
switch (singleInterpunction) {
case ' ': {
digitalWrite(buzzerOut, 0);
Serial.print("-");
digitalWrite(ledPin, 0);
delay(delayTime);
delay(delayTime);
delay(delayTime);
digitalWrite(buzzerOut, 0);
Serial.print(".");
digitalWrite(ledPin, 0);
delay(delayTime);
digitalWrite(buzzerOut, 0);
Serial.print(".");
digitalWrite(ledPin, 0);
delay(delayTime);
digitalWrite(buzzerOut, 0);
Serial.print(".");
digitalWrite(ledPin, 0);
delay(delayTime);
digitalWrite(buzzerOut, 0);
Serial.print("-");
digitalWrite(ledPin, 0);
delay(delayTime);
delay(delayTime);
delay(delayTime);
Serial.print("/");
break;
}
}
}