Untitled

By Gruff Water Vole, 1 Month ago, written in php.
  1. long millisAtStart=0;
  2. long millisAtEnd=0;
  3.  
  4. const long period_broadcast=8; //period of shortest broadcast (256 port changes)
  5.  
  6. #define LENGTH_DIT 64
  7. //number of period_broadcasts in one 'dit',
  8. //all other lengths are scaled from this
  9.  
  10. const int length_dit=LENGTH_DIT;//number of periods for dit
  11. const int pause_dit=LENGTH_DIT;//pause after dit
  12. const int length_dah=3*LENGTH_DIT;//number of persots for dah
  13. const int pause_dah=LENGTH_DIT;//pause after dah
  14. const int length_pause=7*LENGTH_DIT;//pause between words
  15.  
  16. void dit(void);
  17. void dah(void);
  18. void pause(void);
  19. void broadcast(int N_cycles);
  20. void dontbroadcast(int N_cycles);
  21.  
  22. // ### INC ### Increment Register (reg = reg + 1)
  23. #define ASM_INC(reg) asm volatile ("inc %0" : "=r" (reg) : "0" (reg))
  24.  
  25. void setup()
  26. {
  27.  
  28.   Serial.begin(9600);
  29.   DDRB=0xFF;  //Port B all outputs
  30.   //Do one dit to determine approximate frequency
  31.   millisAtStart=millis();
  32.   dit();
  33.   millisAtEnd=millis();
  34.   Serial.print(millisAtEnd-millisAtStart);
  35.   Serial.print(" ");
  36.   Serial.print((length_dit+pause_dit)*period_broadcast*256/(millisAtEnd-millisAtStart)/2);
  37.   Serial.print("kHz ");
  38.   Serial.println();
  39. }
  40.  
  41. void loop()
  42. {
  43.   dah();
  44.   dit();
  45.   dah();
  46.   dah();
  47.   pause();
  48.   dah();
  49.   dit();
  50.   dah();
  51.   dah();
  52.   pause();
  53.   dah();
  54.   dah();
  55.   dit();
  56.   dit();
  57.   pause();
  58.   pause();
  59. }
  60.  
  61. void dit(void)
  62. {
  63.   for(int i=0;i<length_dit;i++)
  64.   {
  65.     broadcast(period_broadcast);
  66.   }
  67.   for(int i=0;i<pause_dit;i++)
  68.   {
  69.     dontbroadcast(period_broadcast);
  70.   }
  71. }
  72.  
  73.  
  74. void dah(void)
  75. {
  76.   for(int i=0;i<length_dah;i++)
  77.   {
  78.     broadcast(period_broadcast);
  79.   }
  80.   for(int i=0;i<pause_dah;i++)
  81.   {
  82.     dontbroadcast(period_broadcast);
  83.   }
  84. }
  85.  
  86. void pause(void)
  87. {
  88.   for(int i=0;i<length_pause;i++)
  89.   {
  90.     dontbroadcast(period_broadcast);
  91.   }
  92. }
  93.  
  94. void broadcast(int N_cycles)
  95. {
  96.   unsigned int portvalue;
  97.   for (int i=0;i<N_cycles;i++)
  98.   {
  99.     portvalue=0;
  100.     do
  101.     {
  102.         PORTB=portvalue;
  103.         ASM_INC(portvalue);
  104.     }
  105.     while(portvalue<255);
  106.   }
  107. }
  108.  
  109. void dontbroadcast(int N_cycles)
  110. {
  111.   unsigned int portvalue;
  112.   PORTB=0x00;
  113.   for (int i=0;i<N_cycles;i++)
  114.   {
  115.     portvalue=0;
  116.     do
  117.     {
  118.         ASM_INC(portvalue);
  119.         //add some assembly No OPerations to keep timing the same
  120.         asm volatile ("NOP");
  121.     }
  122.     while(portvalue<255);
  123.   }
  124. }