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.

Long Variable problem

Status
Not open for further replies.

btminzon

Full Member level 2
Joined
Jun 12, 2006
Messages
122
Helped
9
Reputation
18
Reaction score
1
Trophy points
1,298
Location
Brazil
Activity points
2,140
Hi friends,
I´m with a simple (but painfull) problem using long variable. I need to transmit serially a 7-digit long variable (signed). I´m transmitting in ASCII mode. So, i pick digit by digit and add 0x30 and send. But the most significant digits are not correct, when the number becomes negative. Here my code. Any suggestion? tanks a lot


unsigned long charMod1Milhao;
unsigned long charMod1MilhaoMod100Mil;
unsigned long charMod1MilhaoMod100MilMod10Mil;
unsigned long charMod1MilhaoMod100MilMod10MilModMil;
unsigned long charMod1MilhaoMod100MilMod10MilModMilMod100;

charMod1Milhao = (unsigned long)(CotaEixoZ % 1000000);
charMod1MilhaoMod100Mil = charMod1Milhao % 100000;
charMod1MilhaoMod100MilMod10Mil = charMod1MilhaoMod100Mil % 10000;
charMod1MilhaoMod100MilMod10MilModMil = charMod1MilhaoMod100MilMod10Mil % 1000;
charMod1MilhaoMod100MilMod10MilModMilMod100 = charMod1MilhaoMod100MilMod10MilModMil % 100;


PutCh((unsigned char)(CotaEixoZ / 1000000) + 0x30);
PutCh((unsigned char)(charMod1Milhao / 100000) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100Mil / 10000) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10Mil / 1000) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10MilModMil / 100) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10MilModMilMod100 / 10) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10MilModMilMod100 % 10) + 0x30);

The code is compiling normally, but only the result is incorrect. The serial is ok also. The long variable is 8 bytes unsigned

Breno
 

Dear Breno,

As far as i know there seems to be a logical error which is rectified in the below code.(I have not checked the below code using a C ). check if it works for u

unsigned long charMod1Milhao;
unsigned long charMod1MilhaoMod100Mil;
unsigned long charMod1MilhaoMod100MilMod10Mil;
unsigned long charMod1MilhaoMod100MilMod10MilModMil;
unsigned long charMod1MilhaoMod100MilMod10MilModMilMod100;
unsigned long charMod1MilhaoMod100MilMod10MilModMilMod100Mil10; //new

charMod1Milhao = (unsigned long)(CotaEixoZ % 10000000); // An extra Zero added
charMod1MilhaoMod100Mil = charMod1Milhao % 1000000; // An extra Zero added
charMod1MilhaoMod100MilMod10Mil = charMod1MilhaoMod100Mil % 100000; // An extra Zero added
charMod1MilhaoMod100MilMod10MilModMil = charMod1MilhaoMod100MilMod10Mil % 10000; // An Extra Zero added
charMod1MilhaoMod100MilMod10MilModMilMod100 = charMod1MilhaoMod100MilMod10MilModMil % 1000; // An extra Zero Added
charMod1MilhaoMod100MilMod10MilModMilMod100Mil10 = charMod1MilhaoMod100MilMod10MilModMilMod100 % 100 //New



PutCh((unsigned char)(CotaEixoZ / 1000000) + 0x30);
PutCh((unsigned char)(charMod1Milhao / 100000) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100Mil / 10000) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10Mil / 1000) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10MilModMil / 100) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10MilModMilMod100 / 10) + 0x30);
PutCh((unsigned char)(charMod1MilhaoMod100MilMod10MilModMilMod100Mil10 % 10) + 0x30); // New variable % 10 for LSB


please let me know if same prob persists or if a new problem crops up.

With Regards
S.Rajesh Kumar
 

Dear Rajesh ,

Thanks for your suggestion. It made me to see another way to do that, but the problem still remains. The wrong value occurs when the number is negative. When positive is OK. Instead of going +0000.000 to -0000.001, the value goes to a value around -0097.xxx, where 'x' is a random number. I cheked the spell many times. What else do you think can be? I also think must be a sign problem...I don´t know, i´ll try something here. Thanks again

best regards

Breno
 

What type of variable is CotaEixoZ? Is that the one you mean by "the long variable is 8 bytes unsigned"?
If it's unsigned, how can it ever be negative?
A 64-bit (8 byte) variable seems very big for storing only a 7-digit value.

If you have sprintf() available (it's a large function), you could use it to easily convert the value into an ASCII string.
 

Dear,


check the above problem by declaring the unsigned long variable and check the output.

Let me know the bugs.
 

Maybe you are getting overflow when you add 0x30?
-1 as a long variable is:
0xFFFF FFFF FFFF FFFF

Added after 34 minutes:

Try sending a nibble at a time like this:

Code:
int main(void)
{
long s, received = 0, bigValue = -1234567;
unsigned char ix, c;

for(ix = 0; ix < 8; ix++)
  {
  c = bigValue >> (ix * 4);
  c &= 0x0f;
  c += 0x30;        /* Send assci */

  s = c;            /* Receive assci */
  s -= 0x30;
  received |= s << (ix * 4);
  }
  return 0;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top