2
\$\begingroup\$

I need a big number in my firmware, So i choosed long for the job. In the microchip's documents the long type provides 4Bytes for use: enter image description here

So i created this variable (timeCounter): enter image description here

In a interrupt of 100us of timer2 this variable is incremented: enter image description here

Problem: The variable is unsigned long but when i plot his value the max value achieved is aproximately 32725! and the variable is SIGNED!!!! image: enter image description here

The code below shows a sizeof(unsigned long) equals to 4bytes... enter image description here enter image description here

I've already disabled the compiler optimizations. Using PIC18f26k80.

=========================================================

code:

#include "main.h"

void main( void ){

char string[100];
unsigned long timeCounter;

setupHw();
initHw();
initSw();

while( true ){

    clearWDT();   


    /*============================================================================*\
     * Task3 (60s)
     * Processo...
     * Dependências:                  
    \* ===========================================================================*/
    if( f60s ){
        f60s = false;

        sprintf(string, "TimeCounter (60s): %d\n", timeCounter);
        txStringUart1(string);
       } // end da task2       
    }
}
\$\endgroup\$
3
  • 7
    \$\begingroup\$ Use text when excerpting code and console output, not screenshots. \$\endgroup\$ – Ben Voigt Jan 25 '17 at 22:43
  • 1
    \$\begingroup\$ do not put code and text output in images. Please edit the post and copy and paste the text here \$\endgroup\$ – phuclv Jan 26 '17 at 6:58
  • \$\begingroup\$ -1 because of the above comments: please remove the pictures of text and replace with the actual text, and the downvote will be reverted. \$\endgroup\$ – Adam Lawrence Feb 3 '17 at 12:40
22
\$\begingroup\$

The problem is not with your type but with the way you print it. In C %d format specifier is for int, not for long. For signed long you should use %ld instead, if your compiler supports it. For unsigned use %lu.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Please also specify to avoid many issue is better to use the c99 standard int library, with it's own formatter that makes very clear how many bit your are working with. \$\endgroup\$ – Lesto Jan 26 '17 at 8:50
  • 1
    \$\begingroup\$ @Rique It is a standard C feature. If the compiler is standard-compliant it will have it. You should consult the docs and/or just try it. \$\endgroup\$ – Eugene Sh. Jan 30 '17 at 18:38
  • \$\begingroup\$ @Rique Welcome to EE.SE. It is standard practice on this site to not immediately accept an answer, even if it is correct, to stimulate discussion and allow alternate answers a chance. Because you have already accepted an answer, the reputation bonus is gone and further discussion no longer has much incentive (the prize is gone). 24 hours is a reasonable wait time. You are free to up/downvote as you see fit with no time restrictions. \$\endgroup\$ – Adam Lawrence Feb 3 '17 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.