Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
topgun00
PostPosted: Mar 01, 2012 - 02:40 PM
Rookie


Joined: Oct 06, 2011
Posts: 37


Hi guys...
I want to obtain 5KHz sinwave generator with DDS AD9834...this is my code but doesn't work...why???what is the error???

Code:

#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/interrupt.h>



//------------------------- SPI ---------------------------------//
#define SPI_SS    PB4
#define SPI_MOSI  PB5
#define SPI_MISO  PB6
#define SPI_SCK     PB7
#define SPI_DDR   DDRB
#define SPI_PORT  PORTB


void spi_master_init(void) {
   
   // set MOSI, SCK, SS output
   SPI_DDR = _BV(SPI_MOSI) | _BV(SPI_SCK) | _BV(SPI_SS);
   // enable SPI, Master; set clock rate fck/16
   SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0);

   // set SS to high => slave is not selected for data transfer
   SPI_PORT |= _BV(SPI_SS);
}


void spi_master_begin_transmission() {
   SPI_PORT &= ~_BV(SPI_SS);
}

void spi_master_end_transmission() {
   SPI_PORT |= _BV(SPI_SS);
}

//------------------------- AD9834 ----------------------------------//
void spi_ad_16(unsigned short data) {
   
   unsigned char MSdata = ((data >>8) & 0x00FF); // most significative bit
   unsigned char LSdata = (data & 0x00FF);

   spi_master_begin_transmission();

   SPDR = MSdata;

   while(!(SPSR & (1<<SPIF))); //ciclo attesa

   SPDR = LSdata;

   while(!(SPSR & (1<<SPIF))); //ciclo attesa

   spi_master_end_transmission();

}

void AD9834_init() {
   
   spi_ad_16(0x2300);    
   spi_ad_16(0x4F5C);   
   spi_ad_16(0x400A); 
   
   spi_ad_16(0x8000); 
   spi_ad_16(0x8000); 
   spi_ad_16(0xC000); 
   spi_ad_16(0xE800);

   spi_ad_16(0x2200); 
}




//------------------------- MAIN ------------------------------------//

int main(void) {
   
   spi_master_init();
   
   AD9834_init();

   while(1) {}

}


MCLK is connected to CLKO of MCU ATMEGA644P...
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 01, 2012 - 03:18 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18524
Location: Lund, Sweden

Quote:

but doesn't work

What does it do then?

Output a 4 KHz sine wave?
Output a 5 KHz square wave?
Jump about singing "Rule Britannia"? Very Happy
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Mar 01, 2012 - 03:55 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21248
Location: Orlando Florida

Probably sits there like an inert hunk o silicon.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
topgun00
PostPosted: Mar 01, 2012 - 04:03 PM
Rookie


Joined: Oct 06, 2011
Posts: 37


no. In output there is only noise!!!
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 01, 2012 - 04:53 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18524
Location: Lund, Sweden

I'm a digital person first-and-foremost, so taker this with as large a grain of salt you find suitable (but I'm sure those analogue guys here would also like to know):

Tell us how you are measuring. An oscilloscoppe? DSO or an analogue one?

Show complete schematics, including all power supplies etc.

If you power up the AD9834 and measure before and after the AVR has talked to it, is there any observable difference in the noise (i.e. more or less white etc)?
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
DocJC
PostPosted: Mar 01, 2012 - 04:56 PM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6841
Location: Cleveland, OH

Johan,

I think he does not have any output signal at all, so just noise as the output pin isn't being driven.

I don't think he is stating that there is just too much noise on top of the signal.

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Mar 01, 2012 - 04:58 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62221
Location: (using avr-gcc in) Finchingfield, Essex, England

Also use the scope to probe the SPI interface lines from the AVR to the DDS - is it actually communicating as you expect?

I note you are not doing anything specifically with CPOL and CPHA (so you will get the default). Is this the right one of the four SPI modes for the device?

Also the example in the AD9834 for setting the 28 bit frequency shows the first command as 0x2000 not 0x2300? You might also want to comment all the other commands you have there as I don't and you in 6 months won't know what the intention of all those magic numbers are!

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 01, 2012 - 04:59 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18524
Location: Lund, Sweden

Hey Doc!

I didn't have much specific/strong hypotheses in mind. Just threw out/up what came across the frontal lobe and jotted it down hoping the OP would start thinking in broader terms than just "it must be the code".

OK, come on all you guys who understands the black art of mastering more than two levels on a signal!

Quote:
6 months

Bah! For me it was 6 minutes for many years, but lately I'm more and more close to 6 seconds.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Mar 01, 2012 - 05:32 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21248
Location: Orlando Florida

What's the 5KHz sin wave go to? If human ears are involved I betcha you cant find too many folks that can tell you if a 5KHz tone is a sin or a square, because the square wave has 0dB of 10KHz and and -12dB of 15KHz (3rd harmonic), but only dogs and young kids that havent blown out their cochlear hairs with ipods can hear 15KHz.

_________________
Imagecraft compiler user


Last edited by bobgardner on Mar 01, 2012 - 08:48 PM; edited 1 time in total
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
ka7ehk
PostPosted: Mar 01, 2012 - 07:17 PM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12035
Location: Tangent, OR, USA

First step: Verify that comms between micro and DDS are working.

1) Is the SPI clock running at the DDS MDATA pin?
2) Is there data on SDATA pin at the DDS?
3) Is the chip enable FSYNC going low while data is being transmitted?
4) Is a full 16 bit word being sent with the FSYNC pin staying low
between bytes?

Then, is the DDS wired up properly:

1) Power and ground on ALL of the pertinent pins?
2) Is there an Rset?
3) Are there bypass and compensation caps as shown in Fig 2 of data sheet?

Jim

_________________
Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
topgun00
PostPosted: Mar 05, 2012 - 04:08 PM
Rookie


Joined: Oct 06, 2011
Posts: 37


I verified all comms between MCU and DDS but don't work...maybe there is some error in the programm of SPI. This is the code SPI...:

Code:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/interrupt.h>

#define SPI_PORT   PORTB
#define SPI_SS      PB4

void SPI_Init (void) {

   DDRB = ( (1<<DDB5) | (1<<DDB7) | (1<<DDB4) );

   SPCR = ( (1<<SPE) | (1<<MSTR) | (1<<SPR0) );


   }

void SPI_Start (void) {

   SPI_PORT &=~ (SPI_SS);

}

void SPI_Transmit (char myString[]) {
      
      for (int i=0; myString[i] !=0; i++) {

      SPDR = myString[i];
      
      while (!(SPSR &(1<<SPIF)));
      }
}

void SPI_End (void) {

   SPI_PORT = (1<<SPI_SS) ;

}

int main (void) {

   SPI_Init();
   
   SPI_Start();

   char myString[]= "hello";

   SPI_Transmit(myString);
   
   SPI_End();
   
   while(1){}
}
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 05, 2012 - 04:14 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62221
Location: (using avr-gcc in) Finchingfield, Essex, England

Did you get the CPOL/CPHA selection right? (as I asked above already).

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
topgun00
PostPosted: Mar 05, 2012 - 04:30 PM
Rookie


Joined: Oct 06, 2011
Posts: 37


Yes,(sorry)...but doesn't work Sad
 
 View user's profile Send private message  
Reply with quote Back to top
DocJC
PostPosted: Mar 05, 2012 - 05:19 PM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6841
Location: Cleveland, OH

First, I don't read C, so I can't help with your code.

But, This Thread , about the oscillator source for an AD DDS chip, includes a C based project link by Zauberer for his AD9834 project.

You may wish to look at his project, and code, to see how he did it, successfully.

Additionally, in This Thread, also about the AD9833 DDS chip, I included a a couple of lines of Bascom code and COMMENTS, which I used to talk to this chip.

The key point here was that I had to select and de-select the AD chip around the Entire Block of 6 bytes sent to the chip, NOT around 2-Byte words.

Once I figured this out things worked better. This may or may not have anything to do with your difficulties.

Also, I did not read your AD chip's data sheet to see its interface, but one might "assume" that it is similar to the AD9833's interface.

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
topgun00
PostPosted: Mar 06, 2012 - 01:39 PM
Rookie


Joined: Oct 06, 2011
Posts: 37


Hi...this is my code and my schematic...
But in output of DDS IC I can read a constant voltage value on oscilloscope and not a sinusoidal waveform...
I don't know what is my error and if I wrong something about circuit or software configuration
I followed the sequence of Analog device web site to initialize my DDS

voltage supply is 3V
MCLK is generate of MCU by CTC using Fclk 8MHz prescaler = 1 and OCR1A = 0

Code:

#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/interrupt.h>

//-----------------------------clock generation----------------------------//

/* si vuole una frequenza di 4 MHz */

   

//-----------------------------------SPI-----------------------------------//
#define DDR_SPI   DDRB
#define DD_MOSI   DDB5
#define DD_SCK   DDB7
#define DD_CS   DDB4



void SPI_MasterInit(void)
{
   DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS);
   
   SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
}   

void SPI_MasterTransmit(char cData)
{
   SPDR = cData;
   
   while (!(SPSR &(1<<SPIF)));

// return  SPDR;
}

void SPI_Begin(void)
{
   PORTB &= ~( 1 << DD_CS);
}

void SPI_End(void)
{
   PORTB |= 1 << DD_CS;
}

void SPI_Set(void)
{
   PORTB |= 1 << DD_CS;
}
//-----------------------------------END SPI--------------------------------------------//

void spi_ad_16(unsigned short data) {
   
   unsigned char MSdata = ((data >>8) & 0x00FF); // bit più significativi
   unsigned char LSdata = (data & 0x00FF); // bit meno significativo

   
   SPDR = MSdata; // invia i primi 8 bit più significativi

   while(!(SPSR & (1<<SPIF))); //ciclo attesa

   SPDR = LSdata; // invia gli ultimi 8 bit meno significativi

   while(!(SPSR & (1<<SPIF))); //ciclo attesa

   
}



int main(void) {
   
   
   DDRD = 0x60;   // si usa la porta OC1A come uscita pin 19
   
   TCCR1B |= (1 << WGM12);    // si imposta il Timer1 in modo CTC (su OCR1A)

   OCR1A = 0;   

   TCCR1A |= (1 << COM1A0);   // si imposta il toggle su OC1A
      
   TCCR1B |=(1 << CS10);   // prescaler di 1

   /* da questo momento in poi il timer è avviato */
SPI_Set();
   
      SPI_MasterInit();

      SPI_Begin();

             spi_ad_16(0x2000);    // parola di controllo, uscita settata a mezza tensione

   
         spi_ad_16(0x5EB8);   // MSB del registro FREQ0
   
   
         spi_ad_16(0x4014);  // LSB del registro FREQ0
   
         // va impostato anche il registro FREQ1 che è a 0 Hz
   
   
         spi_ad_16(0x8000);  // MSB del registro FREQ1
   

         spi_ad_16(0x8000);  // LSB del registro FREQ1
   

         spi_ad_16(0xC000); // phase offset di 0°   phase register0
   
      
         spi_ad_16(0xE800);  // phase offset di 180° phase register1


         spi_ad_16(0x2200);

      SPI_End();
        _delay_ms(200);

        SPI_Begin();
         
           SPI_MasterTransmit(0x11);
           SPI_MasterTransmit(0xA3);
           
        SPI_End();
        _delay_ms(200);
        
         
       
    while(1){}
}
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits