Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 00002 00003 // My first code for the mbed. 00004 // Blinks "HELLO WORLD" out in Morse Code on LED1 00005 // Repeats forever 00006 // 00007 00008 00009 #include "mbed.h" 00010 00011 #define WAIT_time 0.20 // about 5 wpm 00012 #define D() wait(WAIT_time) 00013 00014 DigitalOut myled1(LED1); 00015 00016 00017 void send_dah(); 00018 void send_dit(); 00019 void send_space(); 00020 void send_morse(char); 00021 void blink_out(char *ptr); 00022 00023 00024 char temp1; 00025 char rotate_char; 00026 char loops; 00027 char morse; 00028 00029 00030 00031 // Offset 0x2C for ASCII to Morse code character pattern conversion 00032 // 0xFF means it is not a valid Morse code character 00033 00034 // Characters are packed this way: Bits are rotated right until a "1". After that, "1" is a Dah, "0" is a Dit. 00035 // So, pad the LSBits with "0"s until the 8 bits represent a valid Morse Code charater. 00036 char mcode[100]= 00037 { 00038 0xCE,0xFF,0xAA,0xFF, 00039 0xFC,0xF4,0xE4,0xC4,0x84,0x04,0x0C,0x1C,0x3C,0x7C, 00040 0xFF,0xFF,0xFF,0xFF,0xFF,0x32,0xFF, 00041 00042 0xA0,0x18,0x58,0x30,0x40,0x48,0x70,0x08,0x20,0xE8, 00043 0xB0,0x28,0xE0,0x60,0xF0,0x68,0xB1,0x50,0x10,0xC0, 00044 0x90,0x81,0xD0,0x91,0xD8,0x31, 00045 00046 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 00047 00048 0xA0,0x18,0x58,0x30,0x40,0x48,0x70,0x08,0x20,0xE8, 00049 0xB0,0x28,0xE0,0x60,0xF0,0x68,0xB1,0x50,0x10,0xC0, 00050 0x90,0x81,0xD0,0x91,0xD8,0x31, 00051 }; 00052 00053 00054 00055 int main() { 00056 00057 myled1 = 0; 00058 00059 while(1) { 00060 blink_out("HELLO WORLD"); 00061 send_space(); 00062 00063 } 00064 00065 } 00066 00067 00068 void send_morse(char morse) { 00069 00070 rotate_char = 0x01; // set up mask bit 00071 loops = 0x00; 00072 do { 00073 rotate_char <<= 1; 00074 loops++; 00075 }while ((morse & rotate_char) == 0); 00076 00077 rotate_char <<= 1; 00078 loops++; 00079 00080 do { 00081 if ((morse & rotate_char) == 0) // if it is a zero, send a dit 00082 send_dit(); 00083 00084 else 00085 send_dah(); 00086 00087 rotate_char <<= 1; 00088 loops++; 00089 00090 }while(loops < 8); // there are 8 bits 00091 00092 } 00093 00094 00095 00096 void send_dit() { // dit is on one unit, off one unit 00097 00098 myled1 = 1; 00099 D(); 00100 myled1 = 0; 00101 D(); 00102 00103 } 00104 00105 00106 void send_dah() { // dah is on 2 units, off 1 unit 00107 00108 myled1 = 1; 00109 D(); 00110 D(); 00111 myled1 = 0; 00112 D(); 00113 00114 00115 } 00116 00117 void send_space() { // space between words is 7 units 00118 00119 D(); 00120 D(); 00121 D(); 00122 D(); 00123 D(); 00124 D(); 00125 D(); 00126 00127 } 00128 00129 00130 void blink_out(char *ptr) { 00131 00132 while (*ptr) 00133 {temp1 = *ptr++; 00134 00135 if (temp1 == 0x20) 00136 {send_space(); 00137 temp1 = *ptr++; 00138 }; 00139 00140 temp1-= 0x2C; 00141 morse = mcode[temp1]; 00142 send_morse(morse); 00143 D(); // 2 unit space 00144 D(); 00145 } 00146 } 00147 00148 00149
Generated on Sun Jul 24 2022 02:47:31 by
1.7.2