I have a problem trying to set the pins of the Arduino Mega 2560 so I can program the address pins & the data pins for setting the frequency of the DDS that I'm programming. It uses a 48 bit data space to store the number. First I have to convert it to binary (hence the 48 bit binary number) so I can individually set the pins to be stored in the data bits of each Data byte. So far I have this
void setFTW1( int Frequency1) {
byte addresses [] = {0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
int FTW1 = (Frequency * power (2, 48)) / 300000000;
foreach ( address in addresses ) {
setAddress(address);
setData(data); <===== this is the problem
}
}
FTW1 is the frequency Tuning Word and it needs to be in binary format.
This is C++ code for my library when I get done.
Thanks in advance for all your help.
Mel
That's was quick, I got an error message from some one that I had to correct but could not see how to do that.
Shannon,
The pins are 23 - 33 for addresses and 24 - 38 for the data. But they are connected with a 36 pin connector to the DDS so it is a ribbon cable. Some of the other pins on the connector will be for other type of inputs and outputs.
jremington,
I like the long long suggestion as well. I sort of would like to keep it all together in the software. I have a function in excel that I've been trying to reproduce as well but it needed the long long that you suggested.
blh64 ,
Your right of course I'm 90% C# and having to do things in C++ is a challenge again.
gcjr,
Not sure how I'm going to get this into my code. This is what I'm looking for:
dec bin
83726188455589 10011000010011000000101101110001010011010100101
This code works correctly, so just use mask and shift to output 8 bits at a time.
Note: Serial.print() doesn't seem to understand long long.
void setup() {
long long x = 83726188455589LL;
Serial.begin(9600);
long y = x/65536LL; //top 32 of 48 bits.
Serial.println(y); //prints 1277560248
y = x>>16;
Serial.println(y); //ditto
}
void loop() {}
With terrible waste of memory, you can store binary constants as a character string. The lowest order bit of the string is x[strlen(x)] and so on. Put the string in PROGMEM to save RAM, of course.
OK, I'm looking at this code which does not run as expected.
long long x;
int result;
void setup() {
Serial.begin(9600);
delay(5000);
}
void loop() {
x = 83726188455589LL;
for (int n = 48; n <= 0; n--)
{
result = bitRead(x, n);
Serial.print(result);
delay(1000);
Serial.print(result);
delay(1000);
}
Serial.println(result);
}
This code only prints out the last Serial.println; repeatedly.
When I use the prints in the for loop nothing prints out, with the exception of the println outside the for loop.
long long x;
int result;
void setup() {
Serial.begin(9600);
x = 83726188455589LL;
for (int n = 0; n<48; n++)
{
result = bitRead(x, 47-n);
Serial.print(result); //010011000010011000000101101110001010011010100101
}
Serial.println();
}
void loop() {}
It is correct; but, it is not logical. The logical approach is what the OP has attempted (with a little bit of errors).
for (int n = 47; n>=0 ; n--)
{
In English Language, we read/write from left to right; so, the OP wants to read the 47th bit (in binary count sequence) first and then display it. Therefore, the for() loop should logically begin with n=47 and not n=0?
Posts don't get closed. The idea is to leave the question and answer as it stands, in case someone else can learn from it. You could edit the thread title and add "Solved".