2928|1

1万

帖子

25

TA的资源

版主

楼主
 

用microbit发报 [复制链接]

 用B键或者用pin2做触摸键,发送信号。pin0控制蜂鸣器,pin1控制马达。A键显示或清楚显示。

  1. ##########################
  2. #   v 1.0 Initial Public Release
  3. #   This is a working CW Radio. 'B' is key. 'A' shows message received in text.
  4. #   Connect Speaker or Headphones to Pin 0 and Ground
  5. #   Or connect Piezo Buzzer or Haptic Motor for Silent operation to pin1 and ground
  6. #   pin2 is capacitive touch keying.
  7. #   Copyright (C) 2017 Revd. John Goodman M0RVJ,
  8. #
  9. #   This program is free software: you can redistribute it and/or modify
  10. #   it under the terms of the GNU General Public License as published by
  11. #   the Free Software Foundation, either version 3 of the License, or
  12. #   (at your option) any later version.
  13. #
  14. #   This program is distributed in the hope that it will be useful,
  15. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. #   GNU General Public License for more details.
  18. #
  19. #   You should have received a copy of the GNU General Public License
  20. #   along with this program.  If not, see <[url]http://www.gnu.org/licenses/>L[/url]
  21. #   
  22. #   This uses some public domain code by Giles Booth.
  23. #   See here [url]http://www.suppertime.co.uk/blogmywiki/2016/05/microbit-morse-code-transmitter/[/url]
  24. ###########################
  25. from microbit import *
  26. import radio
  27. import music

  28. radio.on()
  29. radio.reset()

  30. #Customise these settings for tx, pwr, channel and speed.
  31. radio.config(power=7)
  32. radio.config(channel=98)
  33. WPM = 15
  34. txenabled = True #in case you want to practice without sending
  35. sidetone = 550

  36. dotlength = int( 60000 / ( WPM * 50 ) )
  37. dashlength = dotlength * 3
  38. interelement = dotlength
  39. interletter = dotlength * 2
  40. interword = dotlength * 7
  41. DOT_THRESHOLD = dotlength * 2
  42. DASH_THRESHOLD = dotlength * 5
  43. WORD_THRESHOLD = dotlength * 7

  44. DOT = Image("00000:00000:00900:00000:00000")
  45. DASH = Image("00000:00000:09990:00000:00000")            
  46. ANT = Image("90909:09990:00900:00900:00900")        

  47. morse = {
  48.     "A":".-",
  49.     "B":"-...",
  50.     "C":"-.-.",
  51.     "D":"-..",
  52.     "E":".",
  53.     "F":"..-.",
  54.     "G":"--.",
  55.     "H":"....",
  56.     "I":"..",
  57.     "J":".---",
  58.     "K":"-.-",
  59.     "L":".-..",
  60.     "M":"--",
  61.     "N":"-.",
  62.     "O":"---",
  63.     "P":".--.",
  64.     "Q":"--.-",
  65.     "R":".-.",
  66.     "S":"...",
  67.     "T":"-",
  68.     "U":"..-",
  69.     "V":"...-",
  70.     "W":".--",
  71.     "X":"-..-",
  72.     "Y":"-.--",
  73.     "Z":"--..",
  74.     "0":"-----",
  75.     "1":".----",
  76.     "2":"..---",
  77.     "3":"...--",
  78.     "4":"....-",
  79.     "5":".....",
  80.     "6":"-....",
  81.     "7":"--...",
  82.     "8":"---..",
  83.     "9":"----.",
  84.     ".":".-.-.",
  85.     ",":"--..--",
  86.     "/":"-..-.",
  87.     "?":"..--.."
  88.     }
  89. # reverse dict.
  90. decodemorse = {v: k for k, v in morse.items()}

  91. # convert string to cw
  92. def EncodeMorse(message):
  93.     m = message.upper()
  94.     enc = ""
  95.     for c in m:
  96.         enc = enc + morse.get(c," ")
  97.         if morse.get(c," ") != " ":
  98.             enc = enc + " "
  99.     return enc
  100.    
  101. # function to flash and play cw
  102. def FlashMorse(pattern):
  103.    for c in pattern:
  104.        if c == ".":
  105.            display.show(DOT)
  106.            pin1.write_digital(1)
  107.            music.pitch(sidetone, dotlength)
  108.            display.clear()
  109.            pin1.write_digital(0)
  110.            sleep(interelement)
  111.        elif c == "-":
  112.            display.show(DASH)
  113.            pin1.write_digital(1)
  114.            music.pitch(sidetone, dashlength)
  115.            display.clear()
  116.            pin1.write_digital(0)
  117.            sleep(interelement)
  118.        elif c == " ":
  119.            sleep(interletter)
  120.    return


  121. def ReceiveCW():     
  122.     display.show(ANT)
  123.     message = ''
  124.     started = running_time()
  125.     while True:
  126.         waiting = running_time() - started
  127.         received = radio.receive()
  128.         if received:
  129.             FlashMorse(EncodeMorse(received))
  130.             message += received
  131.         if button_b.is_pressed():
  132.             return # breakin to immediate keying
  133.         if pin2.is_touched():
  134.             return # breakin to immediate keying
  135.         if button_a.was_pressed():
  136.             display.scroll(message) # nb blocks receive while showing...
  137.             message = ''
  138.         if waiting > WORD_THRESHOLD * 2:
  139.             display.show(ANT)
  140.         if len(message) > 15: #keep message buffer short...
  141.             message = message[1:16]
  142.         if accelerometer.was_gesture("shake"):
  143.             display.scroll("CW TRX by M0RVJ")
  144.             
  145. def Keyer():
  146.     buffer = ''
  147.     message = ''
  148.     started = running_time()
  149.     while True:        
  150.             waited = running_time() - started
  151.             key_down_time = None
  152.             while button_b.is_pressed(): ## button B keying
  153.                 if not key_down_time:
  154.                     key_down_time = running_time()
  155.                 music.pitch(sidetone, -1, pin0, False)
  156.                 pin1.write_digital(1)
  157.                 while True:
  158.                     if not button_b.is_pressed():
  159.                         music.stop()
  160.                         pin1.write_digital(0)
  161.                         break
  162.             while pin2.is_touched(): ## touch keying
  163.                 if not key_down_time:
  164.                     key_down_time = running_time()
  165.                 music.pitch(sidetone, -1, pin0, False)
  166.                 pin1.write_digital(1)
  167.                 while True:
  168.                     if not pin2.is_touched():
  169.                         music.stop()
  170.                         pin1.write_digital(0)
  171.                         break
  172.             key_up_time = running_time()
  173.             if key_down_time:
  174.                 duration = key_up_time - key_down_time
  175.                 if duration < DOT_THRESHOLD:
  176.                     buffer += '.'
  177.                     display.show(DOT)
  178.                 elif duration:
  179.                     buffer += '-'
  180.                     display.show(DASH)
  181.                 started = running_time()
  182.             elif len(buffer) > 0 and waited > DASH_THRESHOLD:
  183.                 display.clear()
  184.                 character = decodemorse.get(buffer, '?')
  185.                 buffer = ''
  186.                 display.show(character)
  187.                 if txenabled:
  188.                     radio.send(character)
  189.                 message += character
  190.             if waited > WORD_THRESHOLD * 2:
  191.                 return
  192.                
  193. while True:
  194.     Keyer()
  195.     ReceiveCW()
复制代码


链接已隐藏,如需查看请登录或者注册



最新回复

  详情 回复 发表于 2017-6-23 20:58
点赞 关注
 
 

回复
举报

721

帖子

1

TA的资源

一粒金砂(高级)

沙发
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表