Advertisement
Guest User

Arduino CW beacon @ 1337 kHz

a guest
Apr 20th, 2017
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. /*
  2. http://forum.arduino.cc/index.php?topic=8456.0
  3.  
  4. Based on some work found on this board
  5. at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1169088394
  6. and http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1166896036
  7.  
  8. I'm pleased to present a simple beacon, broadcasting Morse code over the radio airwaves.
  9. It works by using an assembly increment on PORTB in the fast loop in the broadcast function.
  10. The least significant bit toggles once per loop, the second toggles every two loops,
  11. the third every four, and so on.
  12. Just run the code and plug a length of wire into Digital Pin 8 and tune your AM dial to around 1337 kHz.
  13. You can also get 1337/2 kHz on Pin 9 and 1337/4 kHz on Pin 10, etc. but this will probably be off your dial.
  14. The LED on Pin 13 flashes the message too, if you like to look at the pretty lights.
  15. */
  16.  
  17. long millisAtStart = 0;
  18. long millisAtEnd = 0;
  19.  
  20. //period of shortest broadcast (256 port changes)
  21. const long period_broadcast = 8;
  22.  
  23. // number of period_broadcasts in one 'dit',
  24. // all other lengths are scaled from this
  25. #define LENGTH_DIT 64
  26.  
  27. const int length_dit = LENGTH_DIT;       //number of periods for dit
  28. const int pause_dit = LENGTH_DIT;        //pause after dit
  29. const int length_dah = 3 * LENGTH_DIT;   //number of persots for dah
  30. const int pause_dah = LENGTH_DIT;        //pause after dah
  31. const int length_pause = 7 * LENGTH_DIT; //pause between words
  32.  
  33. void dit(void);
  34. void dah(void);
  35. void pause(void);
  36. void broadcast(int N_cycles);
  37. void dontbroadcast(int N_cycles);
  38.  
  39. // ### INC ### Increment Register (reg = reg + 1)
  40. #define ASM_INC(reg) asm volatile ("inc %0" : "=r" (reg) : "0" (reg))
  41.  
  42. void setup()
  43. {
  44.   Serial.begin(9600);
  45.   DDRB = 0xFF; //Port B all outputs
  46.   // Do one dit to determine approximate frequency
  47.   millisAtStart = millis();
  48.   dit();
  49.   millisAtEnd = millis();
  50.   Serial.print(millisAtEnd - millisAtStart);
  51.   Serial.print(" ms/dit, freq: ");
  52.   Serial.print( (length_dit + pause_dit) * period_broadcast * 256 / (millisAtEnd - millisAtStart) / 2 );
  53.   Serial.print("kHz ");
  54.   Serial.println();
  55. }
  56.  
  57. void loop()
  58. {
  59.   dah();
  60.   dit();
  61.   dah();
  62.   dah();
  63.   pause();
  64.   dah();
  65.   dit();
  66.   dah();
  67.   dah();
  68.   pause();
  69.   dah();
  70.   dah();
  71.   dit();
  72.   dit();
  73.   pause();
  74.   pause();
  75. }
  76.  
  77. void dit(void)
  78. {
  79.   for (int i = 0; i < length_dit; i++)
  80.   {
  81.     broadcast(period_broadcast);
  82.   }
  83.   for (int i = 0; i < pause_dit; i++)
  84.   {
  85.     dontbroadcast(period_broadcast);
  86.   }
  87. }
  88.  
  89.  
  90. void dah(void)
  91. {
  92.   for (int i = 0; i < length_dah; i++)
  93.   {
  94.     broadcast(period_broadcast);
  95.   }
  96.   for (int i = 0; i < pause_dah; i++)
  97.   {
  98.     dontbroadcast(period_broadcast);
  99.   }
  100. }
  101.  
  102. void pause(void)
  103. {
  104.   for (int i = 0; i < length_pause; i++)
  105.   {
  106.     dontbroadcast(period_broadcast);
  107.   }
  108. }
  109.  
  110. void broadcast(int N_cycles)
  111. {
  112.   unsigned int portvalue;
  113.   for (int i = 0; i < N_cycles; i++)
  114.   {
  115.     portvalue = 0;
  116.     do
  117.     {
  118.       PORTB = portvalue;
  119.       ASM_INC(portvalue);
  120.     }
  121.     while (portvalue < 255);
  122.   }
  123. }
  124.  
  125. void dontbroadcast(int N_cycles)
  126. {
  127.   unsigned int portvalue;
  128.   PORTB = 0x00;
  129.   for (int i = 0; i < N_cycles; i++)
  130.   {
  131.     portvalue = 0;
  132.     do
  133.     {
  134.       ASM_INC(portvalue);
  135.       //add some assembly No OPerations to keep timing the same
  136.       asm volatile ("NOP");
  137.     }
  138.     while (portvalue < 255);
  139.   }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement