Advertisement
Guest User

Untitled

a guest
May 30th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | None | 0 0
  1. #include <JTEncode.h>
  2.  
  3. // DDS pins:
  4. #define W_CLK 12      
  5. #define FQ_UD 8
  6. #define DATA 10
  7. #define RESET 9
  8.  
  9. // pin for PA control: 1 = TX ON ; 0 = TX OFF
  10. #define TXPIN 5
  11.  
  12. // base frequency
  13. uint32_t frequency = 14076000;
  14.  
  15. ///////////////////////////////////////////////////////////////////////
  16.  
  17. #define MODE_JT65 1
  18. #define MODE_JT9 2
  19. #define MODE_WSPR 3
  20.  
  21. #define JT65_SYMBOLTIME 372
  22. #define JT65_SYMLENGTH 126
  23. #define JT65_SYMSHIFT 2.6917
  24.  
  25. #define JT9_SYMBOLTIME 580
  26. #define JT9_SYMLENGTH 85
  27. #define JT9_SYMSHIFT 1.736
  28.  
  29. #define WSPR_SYMBOLTIME 682
  30. #define WSPR_SYMLENGTH 162
  31. #define WSPR_SYMSHIFT 50.33701670912
  32.  
  33. int SYMBOLTIME=0;
  34. int SYMLENGTH=0;
  35. double SYMSHIFT=0;
  36. int DELAYAFTER=0;
  37.  
  38.  
  39. int freq_shift = 0;
  40.  
  41. #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
  42.  
  43. void tfr_byte(byte data)
  44. {
  45.   for (int i=0; i<8; i++, data>>=1) {
  46.     digitalWrite(DATA, data & 0x01);
  47.     pulseHigh(W_CLK);  
  48.   }
  49. }
  50.  
  51. void sendFrequency(double frequency) {
  52.   int32_t freq = frequency * 34.359738368;
  53.   for (int b=0; b<4; b++, freq>>=8) {
  54.     tfr_byte(freq & 0xFF);
  55.   }
  56.   tfr_byte(0x000);
  57.   pulseHigh(FQ_UD);
  58. }
  59.  
  60. JTEncode jtencode;
  61. int x=0, mode, messagelen;
  62. char message[15];
  63.  
  64. uint8_t symbols[162];
  65.  
  66. void setup() {
  67.   pinMode(FQ_UD, OUTPUT);
  68.   pinMode(W_CLK, OUTPUT);
  69.   pinMode(DATA, OUTPUT);
  70.   pinMode(RESET, OUTPUT);
  71.  
  72.   pinMode(TXPIN, OUTPUT);
  73.   digitalWrite(TXPIN, LOW);
  74.  
  75.   pulseHigh(RESET);
  76.   pulseHigh(W_CLK);
  77.   pulseHigh(FQ_UD);
  78.  
  79.   Serial.begin(9600);
  80.  
  81. }
  82.  
  83. int serialInt() {
  84.   int ret = 0;
  85.   char c;
  86.   while(1) {
  87.     while(!Serial.available()) {}
  88.     c = Serial.read();
  89.     if(!(c>='0' && c<='9')) {
  90.       return ret;
  91.     }
  92.     else {
  93.       ret = ret*10 + c-'0';
  94.     }
  95.   }
  96. }
  97.  
  98. void readSerialMessage() {
  99.  
  100.   uint8_t x=0;
  101.   char c;
  102.  
  103.   while(1) {
  104.     while(!Serial.available()) {}
  105.     c = Serial.read();
  106.     if(c == '\n' || c == '\r') {
  107.       message[x] = 0x00;
  108.       messagelen =  x;
  109.       return;
  110.     }
  111.     else {
  112.       message[x] = c;
  113.       x++;
  114.     }
  115.    
  116.   }
  117.  
  118. }
  119.  
  120.  
  121. // transmitting in JT
  122. void tx_jt() {
  123.  
  124.   // enable PA
  125.   digitalWrite(TXPIN, HIGH);
  126.  
  127.   for(x=0;x<SYMLENGTH;x++) {  // transmit all symbols
  128.    
  129.     sendFrequency(frequency+freq_shift+symbols[x]*SYMSHIFT);
  130.     delay(SYMBOLTIME);
  131.    
  132.   }
  133.  
  134.   // disable PA
  135.   digitalWrite(TXPIN, LOW);
  136.  
  137. }
  138.  
  139. void loop() {
  140.  
  141.   {   // read mode, frequency and message from serial port
  142.     mode = serialInt();
  143.     Serial.print("Mode OK: ");
  144.     Serial.println((mode==1)?"JT65":"JT9");
  145.     readSerialMessage();
  146.     Serial.print("Message is: ");
  147.     Serial.println(message);
  148.     freq_shift = serialInt();
  149.     Serial.print("TX freq: ");
  150.     Serial.println(frequency+freq_shift);
  151.   }
  152.  
  153.  
  154.   // load settings for chosen transmission mode, encode message and start TXing
  155.   switch(mode) {
  156.     case MODE_JT65:
  157.     {
  158.       SYMBOLTIME = JT65_SYMBOLTIME;
  159.       SYMLENGTH = JT65_SYMLENGTH;
  160.       SYMSHIFT = JT65_SYMSHIFT;
  161.      
  162.       jtencode.jt65_encode(message, symbols);
  163.  
  164.       tx_jt();
  165.       break;
  166.     }
  167.     case MODE_JT9:
  168.     {
  169.       SYMBOLTIME = JT9_SYMBOLTIME;
  170.       SYMLENGTH = JT9_SYMLENGTH;
  171.       SYMSHIFT = JT9_SYMSHIFT;
  172.  
  173.       jtencode.jt9_encode(message, symbols);
  174.  
  175.       tx_jt();
  176.       break;
  177.     }
  178.   }
  179.   // back to base frequency
  180.   sendFrequency(frequency);
  181.  
  182.   // restart the program
  183.   asm volatile ("  jmp 0");
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement