' Random Morse code generator (actually, pseudo random) ' David Gwillim, KB2TQX ' 7 March 2007 ' 224 bytes #picaxe 08m ' variables ' NOTE: morse_bits MUST be assigned b0 as bitN variables are used symbol morse_bits = b0 ' w0 symbol ascii_char = b1 symbol char_count = b2 ' w1 symbol top_bit = b3 symbol bit_count = b4 ' w2 symbol startbit_flag = b5 symbol dah_length = b6 ' w3 symbol char_group = b7 symbol delaymS = w5 ' must be a word size variable ' constants symbol run_pin = pin2 ' if this pin is: high=run, low=pause symbol dit_length = 8 ' dit length in 10mS units symbol pitch = 101 ' frequency used for dit and dah in sound command symbol sound_pin = 1 ' pin use for sound output symbol group_size = 5 ' number of characters sent in each group symbol group = 2 ' 0=letters, 1=numbers, 2=all ' preload Morse bit patterns lookup table into EEPROM ' ! " # $ % & ' ( ) * + comma - period / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ eeprom (%01101100,%01101101,%00000000,%11110110,%00000000,%00000000,%01100001,%00101001,%01010010,%00000000,%00110101,%01001100,%01011110,%01101010,%00101101,%00100000,%00110000,%00111000,%00111100,%00111110,%00111111,%00101111,%00100111,%00100011,%00100001,%01000111,%01010101,%00000000,%00101110,%00000000,%01110011,%01100101,%00000110,%00010111,%00010101,%00001011,%00000011,%00011101,%00001001,%00011111,%00000111,%00011000,%00001010,%00011011,%00000100,%00000101,%00001000,%00011001,%00010010,%00001101,%00001111,%00000010,%00001110,%00011110,%00001100,%00010110,%00010100,%00010011,%00000000,%00000000,%00000000,%0000000,%01110010) main: dah_length = dit_length * 3 ' dah length in 10mS units. char_count = 0 ' initialize character counter char_group = group play_loop: check: if run_pin = 0 then sleep 2 goto check end if random w6 ascii_char = b12 ' byte random variable - lower 8 bits of w6 select char_group case 0 if ascii_char < "A" OR ascii_char > "Z" then play_loop case 1 if ascii_char < "0" OR ascii_char > "9" then play_loop else if ascii_char < "!" OR ascii_char > "_" then play_loop end select ' Get Morse bit pattern. ascii_char contains ASCII, morse_bits contains Morse bit pattern. First 1=start bit, then 1=dit and 0=dah select ascii_char case "!" to "Z" let ascii_char = ascii_char - "!" read ascii_char,morse_bits else morse_bits = 0 endselect if morse_bits != 0 then gosub PlayMorseLetter inc char_count if char_count = group_size then delaymS = dah_length * 40 ' inter-word delay in mS (>=3 dahs) pause delaymS char_count = 0 else delaymS = dah_length * 10 ' dah length in mS. Also inter-character delay pause delaymS end if endif goto play_loop PlayMorseLetter: ' morse_bits contains the Morse bit pattern let bit_count = 0 ' initialize the bit counter let startbit_flag = 0 ' initialize startbit found flag ' find the start bit and play Morse character nextbit: let top_bit = bit7 ' save top bit value let bit7 = 0 ' clear top bit inc bit_count ' can only process 8 bits if bit_count = 9 then out if top_bit = 1 and startbit_flag = 0 then let startbit_flag = 1 ' set the start bit flag goto skip endif if startbit_flag = 0 then skip ' still looking for start bit if top_bit = 1 then ' we are in Morse bit pattern sound sound_pin,(pitch,dit_length) else sound sound_pin,(pitch,dah_length) endif delaymS = dit_length * 10 ' dit length in mS. Also inter-element delay pause delaymS ' inter-element delay. Pause one dit length skip: let morse_bits = morse_bits * 2 ' shift left goto nextbit out: return