AnsweredAssumed Answered

protecting variables

Question asked by chop on Apr 18, 2012
Latest reply on Apr 19, 2012 by bigmac

Still stuggling with my DDS control system :-

I declare  two frequencies , startfreq and endfreq,   to setup a scan  ,( they are stored at 0113h and 0117h)  , reading them and displaying them on the LCD screen shows no problem , when I then bring in the routine to output the start frequency , the calculation trashes the data ! , How do I protect the values of the variables ?  ( they can't be set as constants as they need to be changed)

I'm assuming that the MCU has sufficient RAM to handle the maths without trashing the variables amd I'm  doing something glaringly obviously wrong  but  can't see it for the life of me ,

 

Any help ( even if it's just to point out how bad I'm doing ) would be appreciated 

 

 

The MCU is MC9S08DN32 and the code is :-

  **************************

Relevant ( I think!) secion of main code

****************************************

char *fstring;
unsigned char c,r,j;
unsigned long startfreq,endfreq ;
extern long freqint;          // from DDS routine
void MCU_init(void);          /* Device initialization function declaration */
 
void main(void) {
  MCU_init(); /* call Device Initialization */
   
   initlcd();
   
   clearram();
   
   startfreq =   10151400;
    endfreq = 10151600;      
                 c=1;
                 r=1;
     
             cursorxy(r,c);         //  cursor c limits 1-14, r limits 1-6
              
             fstring = "Start Freq";
             putstr (fstring);             
                
                   c=1;
                   r=2;
             
             cursorxy(r,c);         //  cursor c limits 1-14, r limits 1-6
                        
             writebignumber(startfreq);           //  THIS DISPLAYS THE CORRECT FREQUENCY
                         
                   c=1;
                   r=3;     
                  cursorxy(r,c);      
                            
                 fstring = "DDS factor";                                ///  INCLUDING THIS TRASHES THE TWO VARIABLES
                 putstr (fstring);     
               
               
                 sendFrequency(startfreq);
 
            // initlcd();  
               
                   c=1;
                   r=4;
                    
                   cursorxy(r,c);      
                                               
                   writebignumber (freqint);           //   THE RETURNED NUMBER IS FORRECTLY CALCULATED AND DISPLAYED
                   
                   writebignumber(endfreq);        //   BOTH startfreq AND endfreq HAVE BEEN TRASHED IN MEMORY

 

 

*******************************************

 

The "sendFrequency"  routine referred to above is this :..................

 

   
//**********************************************************
void sendFrequency(double frequency) {
 
 
  tuning_word = (frequency *(pow(2,32)))/ DDS_CLOCK;
 
  freqint = (long)tuning_word;
 
  DDS_LOAD = 0;                            // take load pin low
 
  for ( i = 0; i<32; i++)              // count for 32 bits
 
  {
     if ((freqint & 1) == 1)
            outOne();
     else
            outZero();
     
     freqint = freqint >> 1;
     
  }
   byte_out(0x09);                           // x6 multiplier?
   
   DDS_LOAD  = 1;                                // take load pin high
   
   
   freqint = tuning_word;
   
   
}

******************************************************************8

 

 

            

Outcomes