• AVR Freaks

Hot!Unsigned int cannot hold value greater than 32767

Author
Sds
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2019/03/12 20:03:34
  • Location: 0
  • Status: offline
2020/09/19 23:21:56 (permalink)
0

Unsigned int cannot hold value greater than 32767

Im using pic 16f877a for counter
I read numbers from keypad here upto 32767 value get's display on LCD when value greater than 32767 it changes to negative or any other value.i think it's problem with unsigned int
Here's my code:

char key;
char lcd[20];
void main()
{
lcd_init();
keypad_init();
unsigned int data=0;
while (1)
{
key=read_switches();
if(key>='0'&&key<='9')
{
if(data==0){data=key&0x0f);
else {data=(data*10+(key &0x0f));}
Sprintf (lcd,"%5d",data);
lcd_string(2,1,lcd);
}
}
}
post edited by Sds - 2020/09/19 23:34:23
#1

13 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Unsigned int cannot hold value greater than 32767 2020/09/19 23:26:38 (permalink)
    +1 (1)
    You didn't show all your code.
    Where (and how) is the "key" variable defined.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Unsigned int cannot hold value greater than 32767 2020/09/19 23:32:16 (permalink)
    +1 (1)
    try
    else {data=(data*10U+(key &0x0f));}

    and if that doesn't help, try:
    else {data=(data*10U+(unsigned int)(key &0x0f));}

     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Unsigned int cannot hold value greater than 32767 2020/09/19 23:43:43 (permalink)
    +1 (1)
    Note, these sort of problems are always the fault of the calculation, not the type of variable it is stored in.
    Calculations in C do NOT pay any attention to what sort of variable the result is going to be stored in. They follow strict rules that only observe the types of the operands in the calculatiion, not the destination variable.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    Sds
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/03/12 20:03:34
    • Location: 0
    • Status: offline
    Re: Unsigned int cannot hold value greater than 32767 2020/09/19 23:46:58 (permalink)
    0
    It's doesn't work
    I want to enter value of 50000 counting
    Eg:
    When I give value one by one upto 5000 it shows correctly after 5000 i have pressed "0" key to change value from 5000 to 50000 it shows -15536 on LCD
    #5
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Unsigned int cannot hold value greater than 32767 2020/09/19 23:49:52 (permalink)
    +1 (1)
    Change
    Sprintf (lcd,"%5d",data);
    to
    Sprintf (lcd,"%5ud",data);

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    Sds
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/03/12 20:03:34
    • Location: 0
    • Status: offline
    Re: Unsigned int cannot hold value greater than 32767 2020/09/20 00:04:49 (permalink)
    0
    Sprintf (lcd,"%5ud",data); it displays d on LCD
    #7
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Unsigned int cannot hold value greater than 32767 2020/09/20 00:10:28 (permalink)
    +1 (1)
    Sorry my mistake, it should just be
    sprintf (lcd,"%5u",data);
     
    Note, it should not be a capital "S" at the start. Is that just a typo when you post the code into the forum?
     
    post edited by ric - 2020/09/20 00:14:49

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    Sds
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/03/12 20:03:34
    • Location: 0
    • Status: offline
    Re: Unsigned int cannot hold value greater than 32767 2020/09/20 00:16:01 (permalink)
    -1 (1)
    I had found error
    Unsigned int is correct
    Error is in sprintf function
    When i divided data by 10,100,1000,10000 and display individual digit on LCD it works correctly
    And get values upto 65535 and for 65536 it flows to 0
    #9
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Unsigned int cannot hold value greater than 32767 2020/09/20 00:53:31 (permalink)
    +1 (1)
    Sds
    Error is in sprintf function

    It certainly will be if you use "%d" (which is "signed int")
    rather than "%u" (which is unsigned int).
    Did you try it with "%u" ?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    Sds
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/03/12 20:03:34
    • Location: 0
    • Status: offline
    Re: Unsigned int cannot hold value greater than 32767 2020/09/20 00:56:56 (permalink)
    0
    Yes same problem exists
    #11
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Unsigned int cannot hold value greater than 32767 2020/09/20 01:11:49 (permalink)
    +1 (1)
    I don't see how that is possible.
    A "%u" format string will never output a negative number.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    paulfjujo
    paulfjujo
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2011/03/08 05:33:46
    • Location: France 01700
    • Status: offline
    Re: Unsigned int cannot hold value greater than 32767 2020/09/20 08:06:05 (permalink)
    0
    hello
     
    this could be a problem :

     
     
     
    if(data==0){data=key&0x0f);
     
     
     

    wrong  closing brace ..

     
     
     
    if(data==0){data=key&0x0f);}
     
     
     

    are you sure sprintf exist for PIC16F
    not available in MikroC compiler, because use to much ressources
    (only spinti, sprintl)
    data is a reserved word for MikroC
     
     
    short test with a 16F887, using sprinti
    i don't have a keypad...
      

     

    key='0';
    dat=0;
    while (1)
    {
    UART1_Write(key);
    if(key>='0'&& key<='9')
    {
    if(dat==0)
    {
    dat=key&0x0f;
    }
    else
    {
    dat=(dat*10+(key &0x0f));
    }
    sprinti (lcd,"%5u",dat);         // sprinti  mikroC library
    Print(lcd);
    CRLF1();
     
    }
    if (key>='9') break;
    key++;
    dat++;
    }
     

     
     
    gives

    0     0
    1    11
    2   122
    3  1233
    4  12344
    5  57919
    6  54918
    7  24909
    8  52500
    9    731    Overflow the int capability ... else use a long int !

     
     
     
     
     
     
     
     
     
    post edited by paulfjujo - 2020/09/20 08:24:18
    #13
    mlp
    boots too small
    • Total Posts : 967
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Unsigned int cannot hold value greater than 32767 2020/09/24 17:28:48 (permalink)
    +1 (1)
    paulfjujo
    this could be a problem :

    if(data==0){data=key&0x0f);

    wrong  closing brace ..

     
    It could be a problem, if the complaint was that it wouldn't compile.
    The complaint is actually that it produces the wrong output, so obviously this syntactically-incorrect code is not the code actually being used.
    This is why we repeatedly ask questioners to post the actual code they're using.
     
    are you sure sprintf exist for PIC16F
    not available in MikroC compiler
    ...
    data is a reserved word for MikroC

     
    The original poster did not mention MikroC.

    Mark (this opinion available for hire)
    #14
    Jump to:
    © 2020 APG vNext Commercial Version 4.5