Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Initializing signal with 125Khz.

Status
Not open for further replies.

veerubiji

Full Member level 2
Full Member level 2
Joined
Feb 24, 2011
Messages
122
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Visit site
Activity points
2,301
Hi,
I have small problem with generating sine wave initially with 125 kHz. This seems very simple but it took lot of time from me but i couldn't figure it out what is the problem. I am using CodeVisionAVR compiler.

I am using ATmega32-A micro controller, AD9833 programmable wave form generator to generate sine wave. I have written the fallowing code.
Code:
#include <MEGA32.h>
#include <main.h>
#include <interface.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delay.h>
#include <sleep.h>

 // GLOBAL VARIABLES

                    unsigned int i;

                    //Status
                    unsigned char state = 0;
                    unsigned char debug = 0;
                    unsigned int status = 0x0010;

                    //External variables
                    unsigned char Command;
                    unsigned int  Param;
                    extern unsigned char rx_counter0;

  //Initialize registers
                    void init(void){

                    #asm("cli");        //Disable global interrupt

                    // Input/Output Ports initialization
                    // Port B initialization 
                    PORTB=0x00;
                    DDRB=0xBF;

                    // Port C initialization
                    PORTC=0x00;
                    DDRC=0xC3;


                                               
                    // Port D initialization
                    PORTD=0x00;
                    DDRD=0xFC;

                    // USART initialization
                    // Communication Parameters: 8 Data, 1 Stop, No Parity
                    // USART Receiver: On
                    // USART Transmitter: On
                    // USART0 Mode: Asynchronous
                    // USART Baud Rate: 9600
                    UCSRC=0x86;
                    UBRRH=0x00;
                    UBRRL=0x67;
                    UCSRA=0x00;
                    UCSRB=0xD8;
                    //UCSRC=0x86;
 
                    // SPI initialisation
                    // SPI clock rate fck/16
                    // SPI master
                    // SPI MSB first
                    // SPI CPOL = 1, CPHA = 1
                    SPCR=0x5D;
                    //PORTB.1 = 1;    //Flash CS high
                    PORTB.3 = 1;    //ADC CS high

                    //Global enable interrupts
                    #asm("sei")
                    }

                void runCom(void){

                        switch(Command){
                                      
                            case(NO_COM):
                                Command = 0;
                                break;

                            case(WGF):
                                if(Param < 500)
                                    SetWGFreq(Param);   
                                    Command = 0;
                                break; 
                                                             
                            case(WGP):
                                if(Param < 4095)
                                    SetWGPhase(Param);
                                Command = 0;
                                break;          
                            default:
                                Command = 0;
                                break;   
                        }
                 }

             void main(void){
                        status = 0x0010;
                        init();                                     //Initialize controller

                        SetWGFreq(125);     //Initialize waveformgenerator 125 kH     
             
                       debug = 0;              //Controls output during motor running      
                        while (1){
                            
                            if(rx_counter0) getCom();                     
                            if(Command)     runCom();
                             
                                }
                                }
                            }
                            #asm("WDR");        //Reset WD timer 
                        }  // EOF "while(1)"
                    } // EOF "main(void)"


unsigned char spi(unsigned char data) 
{ 
    //Start transmision 
    SPDR = data; 
    //Wait for transmision complete 
    while (!(SPSR & 0x80)); 
    return SPDR; 
}        
  


//Sets the waveform generator output to given kHz
  void SetWGFreq(unsigned int freq)
  {
    unsigned long freg;
    char fByte0;
    char fByte1;
    char fByte2;
    char fByte3;
    
    freg = (unsigned long)freq*33554.432;   //Number based on a MCLK of 8 MHz
    fByte0 = (char)freg;
    fByte1 = (char)(freg>>8);
    fByte1 = (fByte1 & 0x3F) | 0x40;        //clears bits 15 and 14, then sets for FREQ0
    fByte2 = (char)(freg>>14);              //byte1 only has 6 bits, so move over by 8+6
    fByte3 = (char)(freg>>22);              //byte1 only has 6 bits, so move over by 8+8+6
    fByte3 = (fByte3 & 0x3F) | 0x40;        //clears bits 15 and 14, then sets for FREQ0
    
    SPCR = 0x5A;        //Set SPI to mode 2 and Fosc/64
    WG_CS = 0;
    while(WG_CS_PIN);   //Wait for chip select pin to go low
    spi(0x20);          //Load control register with B28 high
    spi(0x00);
    spi(fByte1);
    spi(fByte0);
    spi(fByte3);
    spi(fByte2);
    WG_CS = 1;
  }
I am using SPI communication between ATmega32-A and AD9833. I have written one function for setting frequency of the signal and one more function for changing the phase of the signal.

I have written commands in switch case when i enter "WGF" and frequency value it is generating signal with whatever frequency you entered, same with "WGP" i am able to change phase of the signal. So i am able to set frequency and phase of the signal successfully using commands.

Now my problem is,

I want to Initialize waveform generator 125 kHz when i run the code. even when power off/on also it should be 125KHz. if user wants to change frequency from 125 to any other he has to use "WGF" function.
For that purpose I have called " SetWGFreq" function in the main program before entering into the while loop.
Code:
 SetWGFreq(125);     //Initialize waveformgenerator 125 kH
I don't know why it was not working.

But when place this function in while loop it is working(intially signal is generating with 125KHz) but "WGF" is not working. it means after that if i want change frequency of the signal using "WGF" function, it was not working.

Please help me with this, i couldn't figure out what was the mistake i am doing.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top