/*  RTTY carrier modem generator by serial programming the AD9850
  by Alastair GW0AJU
*/

int W_CLK=8; // Pin 8 - connect to AD9850 module word load clock pin (CLK)
int FQ_UD=9; // Pin 9 - connect to freq update pin (FQ)
int DATA=10; // Pin 10 - connect to serial data load pin (DATA)
int RESET=11; // Pin 11 - connect to reset pin (RST).
int RTTY_logic=12; // Pin 12 - TTL output of RTTY data signal logic

// 43 char of rtty message with control characters
// message sent; "RYRYRYRYRY DE GW4YCT QRP BEACON TX"
byte info[] = {0x1F, 0x0A, 0x15, 0x0A, 0x15, 0x0A, 0x15, 0x0A, 0x15, 0x0A, 0x15,
0x0A, 0x15, 0x04, 0x1F, 0x09, 0x01, 0x04, 0x1F, 0x1A, 0x13, 0x1B, 0x0A, 0x1F, 0x15,
0x0E, 0x10, 0x04, 0x17, 0x0A, 0x16, 0x04, 0x19, 0x01, 0x03, 0x0E, 0x18, 0x0C,
0x04, 0x10, 0x1D, 0x08, 0x02};

unsigned long freq;
byte angle;

void pulseRESET()
{
digitalWrite(11, LOW);
delayMicroseconds(50);
digitalWrite(11, HIGH);
delayMicroseconds(50);
digitalWrite(11, LOW);
}

void pulseW_CLK()
{
digitalWrite(8, LOW); 
delayMicroseconds(50);
digitalWrite(8, HIGH);
delayMicroseconds(50);
digitalWrite(8, LOW);
}

void pulseFQ_UD()
{
digitalWrite(9, LOW);
delayMicroseconds(50);
digitalWrite(9, HIGH);
delayMicroseconds(50);
digitalWrite(9, LOW);
}


// frequency calc from AD9850 datsheet
// program data = (sys clock) * (frequency tuning word)/2^32
void sendFrequency(float frequency)
{
 {   
      freq = frequency * 4294967295/125000000; // note 125 MHz clock on AD9850
      //Serial.println(freq,HEX);
      //Serial.println(freq,DEC);
      for (int b=0; b<4; b++, freq>>=8) 
    {
      tfr_byte(freq & 0xFF); 
    }   
 }
}

// transfer the phase shift info plus trigger AD9850 internal loading
void phase(byte angle)
  {
      tfr_byte(angle); // Final control byte for AD9850 phase shift keying
      pulseFQ_UD();// Done! Should see output
  }

// transfers a byte, a bit at a time, LSB first to the AD9850
// via serial DATA line to program in the carrier frequency
void tfr_byte(byte data)
{
      Serial.println(data,HEX);
  for (int i=0; i<8; i++, data>>=1) 
        {
          digitalWrite(DATA, data & 0x01);
          pulseW_CLK(); //after each bit sent, CLK is pulsed high
        }
}

void setup()
{
  Serial.begin(9600);
// configure arduino data pins for output
pinMode(FQ_UD, OUTPUT);
pinMode(W_CLK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(RTTY_logic, OUTPUT);

pulseRESET();
pulseW_CLK();
pulseFQ_UD(); // this pulse enables serial mode
}

void loop()
{
  int d;
  int p;
  byte mess;
  int sense;
  sendFrequency(14.061445e6); // mark freq = (space freq + 170Hz)
  delay(1000);
for (d=0 ; d<43 ; d++)  //  d = the number RTTY message + control characters 
    {
      sendFrequency(14.061275e6); // space freq
      delay(20); // start bit low tone space
      mess = info[d];
      //Serial.println(mess,HEX);
      //Serial.println(mess,BIN);
      for (p=0; p<5 ; p++, mess>>=1)
            {
              //Serial.println(mess,BIN);
              if ((mess & 0x01) == 0x01)  // detect for mark tone
                  {
                    sense = 1;
                    //Serial.println(sense,BIN);
                    digitalWrite(RTTY_logic, HIGH);  // TTL RTTY output at pin 12
                    // if FSK modulation is required, keep PSK at a constant angle of zero degrees
                    sendFrequency(14.061445e6); // mark freq = (space freq + 170Hz)
                    // if PSK modulation is required, keep the carrier frequency constant
                    phase(0x00);  // angle of phase shift kept at  zero degrees
                    // for +180 degree shift, replace 0x00 for 0x80 
                    delay(20);   // delay period of data bit transmission
                  }
                else if ((mess & 0x01) == 0x00)  // detect for space tone
                  {  
                    sense =0;
                    //Serial.println(sense,BIN);
                    digitalWrite(RTTY_logic, LOW);   // TTL RTTY output at pin 12
                    // if FSK modulation is required, keep PSK at a constant angle of zero degrees
                    sendFrequency(14.061275e6); // space freq
                    // if PSK modulation is required, keep the carrier frequency constant
                    phase(0x00);  // angle of phase shift kept at zero degrees
                    delay(20);   // delay period of data bit transmission
                 }     
            }
            sendFrequency(14.061445e6); // mark freq = (space freq + 170Hz)
            delay(30);  // delay period for stop bit of 1.5 bits at logic one, mark tone
    }
  // delay period before retransmission of RTTY message, with mark tone signal
  delay(1000);  // delay period of one second
}



