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
Solved! Go to Solution.
Hello,
With the use of 32-bit integers, floating point arithmetic, and floating point library functions, you will need a much larger stack size than you might otherwixe need. Simply allocate a larger stack within the PRM file.
Do you really need to use floating point operations? It is often feasible to use scaled integer calculations with sufficient precision. This will also speed up the code.
Regards,
Mac
Hello,
With the use of 32-bit integers, floating point arithmetic, and floating point library functions, you will need a much larger stack size than you might otherwixe need. Simply allocate a larger stack within the PRM file.
Do you really need to use floating point operations? It is often feasible to use scaled integer calculations with sufficient precision. This will also speed up the code.
Regards,
Mac
I would prefer not to use floating point but the numbers used in the frequency calculations for the DDS exceed the 32 bit limit for integer clculation ( See reply to my posting on the 12th ) so as 64 bit integers are not a capability it down to floating point.
Your comment on larger stack requirement was along the lines I was suspecting but could not see how to do it in the prm file ( very newbie with little documentation available) , however I was about to ask how this is done when I looked at the project.prm file on my desktop and noticed the "STACKSIZE" entry after the "END" ( On the little netbook I program on, the screen size is just enough to include the blank line after the "END" statement and I'd missed the last few lines )
I now realise that the "END" in the file is not the end OF the file :smileyhappy:
Increasing the STACKSIZE has done the job , Many thanks for taking the time to reply and my apologies for not RTFM , or not Reading The Whole Of The File (RTWOTF ?? )
Regards art
Hello Art,
I have attached some code, for special arithmetic functions, that might be an alternative to using floating point. The code includes a function for unsigned 64-bit x 32-bit division. The 64-bit quantity is handled as two 32-bit variables, and the divisor is 32-bits. The functions directly utilize assembly code for HCS08, as an attempt to achieve maximum efficiency. Even so, the division function uses 37 bytes of stack, and takes approximately 7800 cycles to complete. I do not know how this compares with your floating point operations.
Of course, if your DDS clock rate happened to be a power of 2, the calculation process could potentially be greatly simplified.
Regards,
Mac