Advertisement
Guest User

Arduino FM Modulation with AD9850

a guest
Feb 21st, 2014
3,625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. //More info http://zissisprojects.wordpress.com/arduino-sdr-ad9850
  2.  
  3. #include <SPI.h>
  4. uint32_t frequency = 694000;                  //The desired frequency must be divided by 50 ex. 34,7MHz/50 = 694000
  5. uint32_t tword = frequency * 3436 / 100;      //tuning word calculation
  6. byte W[5] = {0,0,0,0,0};
  7. const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);  //these are the prescalers for the ADC sampling rate.
  8. const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
  9. const unsigned char PS_16 = (1 << ADPS2);
  10. int mic = 512;
  11.  
  12. void setup()
  13.           {
  14.            
  15.             DDRB = B01100000;                          //portb has outputs, PB5(may be used for reset) PB6(FU_UD pin)
  16.             PORTB = 0x00;
  17.            // Serial.begin(115200);                    // everything can be controlled by serial comm
  18.              pinMode (A0,INPUT);                       // here comes the audio frequency in
  19.             SPI.setDataMode(SPI_MODE0);                // mode 0 seems to be the right one
  20.             SPI.setClockDivider(SPI_CLOCK_DIV2);       // this is pretty fast
  21.             SPI.setBitOrder(LSBFIRST);                 // AD9850 wants LSB first
  22.             SPI.begin();                              
  23.                                                        // set up the ADC
  24.             ADCSRA &= ~PS_128;                         // remove bits set by Arduino library
  25.             ADCSRA |= PS_32;                           // setting the sampling rate at 16MHz/32 this makes the analogRead() complete in around 40μs
  26.            
  27.           }
  28.  
  29. void loop()
  30.   {    mic = analogRead(A0);                           //reading the AF signal
  31.        frequency = 694000 + 3*mic-1536;                //this is the Frequency Modulation. Desired frequency is divided by 50 and then around 75khz deviation is calculated depending on the input amplitude
  32.        tword = frequency * 1718;                       //calculating the tuning word for AD9850
  33.               W[0] = (byte) tword;
  34.               W[1] = (byte) (tword >> 8);
  35.               W[2] = (byte) (tword >> 16);             //converting it to bytes
  36.               W[3] = (byte) (tword >> 24);
  37.               W[4] = 0; //phase zero
  38.                                                        //start sending with spi interface
  39.                  PORTB = B01000000;
  40.                  PORTB = 0x00;                         //pulse FU_UD
  41.                 for (int j = 0; j<5;j++)
  42.            {
  43.              SPI.transfer(W[j]);                       //send the word
  44.            }
  45.                  PORTB = B01000000;
  46.                  PORTB = 0x00;                         //pulse FU_UD
  47.          
  48.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement