Jump to content
43oh

controlling dds with launchpad


Recommended Posts

Hi everyone, I'm new to launchpad and I want to control a DDS ad9850 module using launchpad.  I got this program to work so far. It compiles and uploads OK.  It will set the frequency I want and I have added serial input via USB and it works in that it answers back whatever I send via the serial monitor (using energia).  What I can't figure out, is how to take the data I send via the serial monitor to be put into the field at the bottom of the program ("sendFrequency"). It's now set to 7.255 Mhz If I change the number there, the DDS will change to that frequency, so the link to the serial data from the serial monitor is missing.  Can anyone help?   The program as it is now is shown below:  I'm using a launchpad MSP-EXP430G2 and 430G2553 micro.

 


#include <msp430g2553.h>
/* 
 *modified april 2 2015 by j. forkin to convert arduino to launchpad
 * A simple single freq AD9850 launchpad test script
 * Original AD9851 DDS sketch by Andrew Smallbone at www.rocketnumbernine.com
 * Modified for testing the inexpensive AD9850 ebay DDS modules
 * Pictures and pinouts at nr8o.dhlpilotcentral.com


 * Use freely
 */


#define W_CLK 7       // Pin 7 - connect to AD9850 module word load clock pin (CLK)
#define FQ_UD 10       // Pin 10 - connect to freq update pin (FQ)
#define DATA 9       // Pin 9 - connect to serial data load pin (DATA)
#define RESET 8      // Pin 8 - connect to reset pin (RST).


#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
double freq2;
// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
  for (int i=0; i<8; i++, data>>=1) {
    digitalWrite(DATA, data & 0x01);
    pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
    


  }
}


// frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
void sendFrequency(double frequency) {
  int32_t freq = frequency * 4294967295/125000000;  // note 125 MHz clock on 9850
  for (int b=0; b<4; b++, freq>>=8) {
    tfr_byte(freq & 0xFF);
  }
  tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip
  pulseHigh(FQ_UD);  // Done!  Should see output
}
//added this code to talk serially
void setup() {
  // configure launchpad data pins for output
  pinMode(FQ_UD, OUTPUT);
  pinMode(W_CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(RESET, OUTPUT);


  pulseHigh(RESET);
  pulseHigh(W_CLK);
  pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
}


void loop() {
  
  
  sendFrequency(7255000);  // freq   THIS IS THE FIELD I NEED TO TIE TO THE SERIAL MONITOR INPUT //////////////////
  while(1);
}
 
Edited by bluehash
Please use code tags next time for easy code readability.
Link to post
Share on other sites

To clarify the above topic, can anyone suggest a way to make the serial data sent via the usb port from the pc using the serial monitor function, available to load into a variable that I can plug into the program and use to set the frequency of the dds?  I want to input a number from 1000000 to 30000000.  I can't find an example anywhere.

Thanks

Link to post
Share on other sites

You have to initialize the hardware serial in the setup function:

Serial.begin(9600); (9600 bps is the maximum speed using the launchpad usb serial)

 

then in the loop wait for serial data and when available use Serial.parseInt()

 

something like

void loop() {
  if (Serial.available()) {
    long frequency = Serial.parseInt();
    sendFrequency(frequency);
  }  
}

Have a look at the Communications examples (in File/Examples/4.Communication)

Run the ASCIITable example if you didn't already to make sure the jumpers on the launchpad are set correct, that's a common mistake for beginners.

Link to post
Share on other sites

Thanks for the input;  I tried your suggestion but still no results.  I used the routine in 4. communications for serial event.  The input from the serial monitor does not seem to be assigned to 'frequency'. Number sent from the serial input reflects back and prints of the serial monitor.  I must be missing something.   It's getting late so I will get back to it tomorrow.

Link to post
Share on other sites

This works on my launchpad:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available()) {
    long frequency = Serial.parseInt();
    Serial.println(frequency);
  }  
}

when I type a number, say 1234 in the serial monitor, the launchpad reads it in the frequency variable and prints it back to the serial monitor

 

I find it strange that you see data in the serial monitor when your code doesn't send anything.

Again try the Ascii table example you should see a bunch of lines when you hit reset on the launchpad and try the simpler code above to test reading in a variable.

Link to post
Share on other sites

Thanks, I'll try that.  Just a thought, do I need to jumper the RXD and TXD pins (P1_1 AND P1_2) on the board as well as the TX and RX pins in the jumper field?  I'm using a ver 1.3 board so the jumpers are different than 1.5. 

Link to post
Share on other sites

I don't have a 1.3 board but I don't see why you'd need to connect anything, the microcontroller pins should be connected to both the header and jumper field. If you connected a jumper between RXD and TXD, that explains why you see an echo of what you type in the serial monitor.

Link to post
Share on other sites

I must be missing something.  Is there any way to see what has been loaded into 'frequency'?  In other compilers I've used you could just put 'debug' on the line and it would display values.

I put in the code you suggested and added sendFrequency (frequency); but still nothing. The program does compile and load.   Serial monitor does print what I sent.  It appears that nothing is being loaded into 'frequency' or it is not what I sent from the serial monitor. I put in a number such as 7222222 to see if it was dropping zeros but still no results.  But, if I put in a number directly in the frequency space, the DDS sets to the sendFrequency (7200000); (7.200000 Mhz) correctly.

I just tried something else. I thought maybe since long frequency was declared in the loop routine it would be a local variable and not available outside the function so I instead made it a global variable but still no results.  I then made it equal to 7200000 and on reset the program sets dds to that frequency.  I still can not get the serial input to load into the frequency variable for some reason.

I'll keep plugging along.

Link to post
Share on other sites

The serial monitor is not supposed to print what you send.

You cannot debug with energia, you have to use ti's code composer studio. Now you can load your energia sketches in CCS by the way.

Send a picture of your setup so we can see if something is wrong with your jumpers.

Link to post
Share on other sites

I've uploaded a picture of my board setup to control the DDS on left.  I do think there is something wrong with the serial setup since I loaded and ran the 4. communications example and it did not put out anything when reset to the serial monitor.  I also put some print statements into some code and saw no output to the serial monitor.  Data is definitely not being input into the program. It's probably something simple I've overlooked and I really appreciate your help.  By the way, I set the com port on com1, com2, com3 and com4 with only com4 reflecting back what I typed into the serial monitor.

 

 post-45368-0-57794300-1428609875_thumb.jpg

Link to post
Share on other sites

Thanks, that solved the problem.  The DDS now programs to the frequency I send it via the serial monitor and then reflects it back.  I just now need to have it maintain the frequency as it resets after a couple of seconds.  I used the code you suggested above.  How do I latch the 'frequency' data till I send new data?  It resets 'frequency' to zero and re-sends it  to reset the DDS.  I need to store that data somewhere or don't resend until new data is received from the serial monitor.

 

 

Got it to work.  Not great coding but added a goto to jump over sendFrequency(frequency). if (frequency<1000) goto here: with here: below in loop function.  Now it doesn't send unless valid data is entered and DDS stays on last programmed frequency.

 

Thanks so much for your help! 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...