AnsweredAssumed Answered

AD9834: how to implement DPSK Modulation

Question asked by nerbone85 on Mar 13, 2012
Latest reply on Mar 16, 2012 by nerbone85

Hi all,

I need an help to implement DPSK modulation: if I read a 0, phase changes of 180 degree, otherwise the phase is the same of the previous.

I'd like to use always bit control...so I have a question: how many clock cycles do I have to wait for seeing change of phase?

This is my code to implement DPSK modulation...what do you think about this? How can I test if it's all right?

 

#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 */

void set_adMCLK(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

}

 

//-----------------------------------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) |(1<<CPOL);

}   

 

 

void SPI_Begin(void)

{

    PORTB &= ~( 1 << DD_CS);

}

 

void SPI_End(void)

{

    PORTB |= 1 << DD_CS;

}

void SPI_Set(void)

{

    PORTB |= 1 << DD_CS;

}

 

void SPI_MasterTransmit(char cData)

{

  

   SPDR = cData;

   

   while (!(SPSR &(1<<SPIF)));

 

 

}

 

 

 

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

 

   

}

 

 

//-----------------------------------END SPI--------------------------------------------//

 

//-----------------------------------INITIALIZATION PHASE 0-----------------------------//

void phase0(void) {   

 

    SPI_Begin();

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

    SPI_End(); 

            _delay_us(300);

 

    SPI_Begin();

            spi_ad_16(0x5EB8);  // MSB del registro FREQ0       

    SPI_End(); 

            _delay_us(300);

               

    SPI_Begin();               

            spi_ad_16(0x4014);    // LSB del registro FREQ0           

    SPI_End(); 

            _delay_us(300);

 

    SPI_Begin();

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

    SPI_End(); 

        _delay_us(300);

 

    SPI_Begin();

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

    SPI_End(); 

            _delay_us(300);           

               

    SPI_Begin();

            spi_ad_16(0x2000);       

    SPI_End();    

          _delay_us(300);

}

//-----------------------------------END PHASE 0--------------------------------------------//

 

//-----------------------------------INITIALIZATION PHASE 1---------------------------------//

void phase1(void) {

   

   

    SPI_Begin();

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

    SPI_End(); 

            _delay_us(300);

 

    SPI_Begin();

            spi_ad_16(0x5EB8);  // MSB del registro FREQ0       

    SPI_End(); 

            _delay_us(300);

               

    SPI_Begin();               

            spi_ad_16(0x4014);    // LSB del registro FREQ0           

    SPI_End(); 

            _delay_us(300);

 

    SPI_Begin();

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

    SPI_End(); 

        _delay_us(300);

 

    SPI_Begin();

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

    SPI_End(); 

            _delay_us(300);           

               

    SPI_Begin();

            spi_ad_16(0x2400);       

    SPI_End();    

          _delay_us(300);

}

//-----------------------------------END PHASE 1--------------------------------------------//

 

int main(void) {

 

    DDRA=0b01010101;

   

    int dato;

   

    DDRC |= (1 << DDC0);

   

    set_adMCLK();

 

    SPI_Set();

    _delay_us(3000);

   

    SPI_MasterInit();

 

    for (int i=0; i<8; i++) {

       

        dato = DDRA;

               

        if(dato & (0b00000001) == 1) {

               

                PORTC |= (1 << PC0); // accendi il led

                _delay_us(200000);

                phase0();

   

        }

        else {

                PORTC = 0x00;

                _delay_us(200000);

                phase1();

       

        }

        DDRA = DDRA >>1;       

    }

   

 

    while(1){}

}

 

..thanks to all! :-)

Outcomes