Number formats not what we expect

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Number formats not what we expect

#1 Post by rrsquez » 09 Feb 2016 00:28

Hello Mikroe Team. I have a really weird problem. Please look at the first line of my UART output screen (also attached). You will see that the second group of numbers are all 0's (bits 0 to 15). I know sprintf/sprinti/sprintl all have trouble printing out 32 bit numbers, so I have formatted my word so it should work, but it does not.

Just to note: the format string for this bad value is missing the "#" because it's part of the previous number block. I tried adding it in too, but that didn't effect the value at all. The value prints out all 0's, but you can see from both the convert ADC value and the Filtertempsingle value that the result should not be 0 for bit 0 to 15.

This is the UART output:
Gasvalue = 0x011a 0000 = 2139062143.390621430 (this is NOT correct)
ADC = 5.514245960 (this is correct)
Filtertempsingle = 18502740.000000000 (this is 0x011a5454 = 5.514245960 volts, which is correct; 0x011a0000 is wrong)

Code: Select all

   
long Getgaslong1;
int myIntegervalue;

   myIntegervalue = 0xffff & Getgaslong1;    // MIKROE Team: This variable doesn't work. Please see output file.
   sprintf(Txt,"Gasvalue = %#.4x %.4x = %12.9f\r\n", (Getgaslong1>>16), myIntegervalue, Getgaslong1);  // See below for alternate
   UART1_Write_Text(Txt);


   sprintf(Txt,"ADC = %12.9f\r\n", (ADC_RESOLUTION * Getgaslong1));      // ADC_RESOLUTION  is 298.023223876953125e-9
   UART1_Write_Text(Txt);


also tried this, but didn't help:
   sprintf(Txt,"Gasvalue = %#.4x %.4x = %12.9f\r\n", (Getgaslong1>>16), (0xffff & Getgaslong1), Getgaslong1);  // See above for primary
I have also attached the entire project as a zip file. The problem is in this code though. Would someone please look at this 1 number conversion and tell me what I am doing wrong? The other number conversions I am doing work well. The ADC value I get is valid.

Thank you, Richard V
Attachments
truncated Bad Number formats.zip
(909 Bytes) Downloaded 126 times
Last edited by rrsquez on 10 Feb 2016 05:34, edited 1 time in total.

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Number formats not what we expect

#2 Post by aCkO » 09 Feb 2016 02:00

rrsquez wrote:I know sprintf/sprinti/sprintl all have trouble printing out 32 bit numbers, so I have formatted my word so it should work, but it does not.
I really doubt that. Can you provide a minimized example? Your project is too big for quick analysis.

Regards

rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Re: Number formats not what we expect

#3 Post by rrsquez » 10 Feb 2016 05:37

Hello aCkO. The lone C file is the simplified code with the problem. The text file is the output that generated it (HyperTerminal).

Thank you, Richard V

User avatar
uros.cvetinovic
mikroElektronika team
Posts: 803
Joined: 14 Dec 2015 09:24

Re: Number formats not what we expect

#4 Post by uros.cvetinovic » 10 Feb 2016 15:30

Hi Richard,

Can you provide me with some details:
Which MCU do you use?
How did you declare "Getgaslong1"?

Also, can you provide zipped project that you can attach here so I could test it?

Best regards,

Uros

rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Re: Number formats not what we expect

#5 Post by rrsquez » 11 Feb 2016 00:50

Hello. I had already posted the code, but aCkO said it was too large. The only code that matters is posted. It is what prints the sample screen (UART) also shown.

As you can see, the lower 16-bit word prints all 0's. The upper word (bits 16 to 31) come out okay, so it's a real mystery. The CPU is the ATmega128A.

Thank you, Richard V
UART output from Getgasvalue() function:

Manrange != Texas. Manrange = 04
Gasvalue = 0x011a 0000 = 2139062143.390621430 Note: Notice that the second word is always "0000"
ADC = 5.514245960
Filtertempsingle = 18502740.000000000

Manrange != Texas. Manrange = 04
Gasvalue = 0x0005 0000 = -144494736.000000000 Note: Notice that the second word is always "0000"
ADC = 0.113299190
Filtertempsingle = 380169.000000000

Manrange != Texas. Manrange = 04
Gasvalue = 0x0005 0000 = -3068.327880840 Note: Notice that the second word is always "0000"
ADC = 0.112704930
Filtertempsingle = 378175.000000000

Code: Select all

void Getgasvalue()
{
   float tempvar;  // used for swap
   float myIntval; // used for modf
   int myIntegervalue;

   Getgaslong1 = LTC2400();                // Read ADC here. This is where all O2 values originate. This works well.
   Gasvararray[Adloop] = Getgaslong1;      // Just always do this.

   myIntegervalue = 0xffff & Getgaslong1;    // MIKROE Team: This variable doesn't work. Please see output file.
   Delay_ms(2);
   sprintf(Txt,"Gasvalue = %#.4x %.4x = %12.9f\r\n", (Getgaslong1>>16), myIntegervalue, Getgaslong1);
   UART1_Write_Text(Txt);
   Delay_ms(2);

   sprintf(Txt,"ADC = %12.9f\r\n", (ADC_RESOLUTION * Getgaslong1));      // ADC_RESOLUTION  is 298.023223876953125e-9
   UART1_Write_Text(Txt);
   Delay_ms(2);

   Filtertempsingle = Getgaslong1;

   sprintf(Txt,"Filtertempsingle = %12.9f\r\n\r\n", Filtertempsingle);
   UART1_Write_Text(Txt);
   Delay_ms(2);


// ignore the remaining code
}

rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Re: Number formats not what we expect

#6 Post by rrsquez » 11 Feb 2016 06:14

Hello Mikroe Team. Looking at the code, I see the following information is missing:

Code: Select all

long LTC2400();  //  prototype for ADC return value. This can be negative.

float Gasvararray[MAXGASVAR];   //  definition for this array
Thank you, Richard V

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Number formats not what we expect

#7 Post by aCkO » 11 Feb 2016 06:58

Hi Richard,

When you think there is a problem with built-in libraries don't expect to get help if you just copy-paste some part of a big project. Just create a minimized example that can be compiled and describe (preferably in comments) what values you expect to get.

As I suspected, there's nothing wrong with sprintf library. Expression (Getgaslong1>>16) returns long where sprintf expects an int. That's why you get "0000" for the other argument. You're also using some unnecessary variables.

Here's how a properly minimized example should look like (also the solution to your problem):

Code: Select all

#include "built_in.h"

void main() {
   char txt[50];
   long Getgaslong1;

   Getgaslong1 = 0x12345678;
   sprintf(txt, "Gasvalue = %#04x %04x = %12.9f\r\n", HiWord(Getgaslong1), LoWord(Getgaslong1), Getgaslong1);
}
Regards

rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Re: Number formats not what we expect

#8 Post by rrsquez » 11 Feb 2016 17:26

Hello aCkO. Yes, I'll try to create a "minimal" hardware version of my project in the future. I don't like posting the whole thing for everyone to take anyhow.

Ah! That conversion problem interferes with the second argument. That makes sense now! I didn't realize that the upper 16 bits would still be there (as 0's now) after a shift. I'll try to keep that in mid in future problems with type conversions.

I like the your usage of the commands shown. The code looks cleaner too. Thank you for your help. I'm sure that's going to fix it now.

Regards, Richard V

rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Re: Number formats not what we expect

#9 Post by rrsquez » 12 Feb 2016 18:08

Hello aCk0, That HiWord() command is not a legitimate command.

This will not compile:

Code: Select all

int myint;
myint = HiWord(0x12345678);
Thank you, Richard V

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Number formats not what we expect

#10 Post by aCkO » 12 Feb 2016 18:10

You forgot:

Code: Select all

#include "built_in.h"
Regards

Post Reply

Return to “mikroC PRO for AVR General”