• AVR Freaks

Hot!PIC16f18345 loop from positive to negative value

Author
ababa
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/11/16 16:43:03
  • Location: 0
  • Status: offline
2020/10/19 12:16:10 (permalink)
0

PIC16f18345 loop from positive to negative value

Hi.
 
I am trying to do a simple loop that counts from 4 to -4.  When counter reaches zero and next number should be -1 it gives 0xFFFF instead. Please, what I am missing? Thanks.
PIC16f18345, compiler XC8. Test is signed int. 
        
test++;
   if(test > 4)
  {
    do
    {
     test--;
    }while(test > -4);
}

#1

9 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28941
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f18345 loop from positive to negative value 2020/10/19 14:34:13 (permalink)
    +1 (1)
    How do you know what the value is?
    Depending upon how the number is displayed, -1 and 0xFFFF are the same value.
     

    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
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 12032
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: PIC16f18345 loop from positive to negative value 2020/10/19 14:51:32 (permalink)
    +3 (3)
    Test is signed int.

     
    Show the complete code.
     
    I'm guessing you're seeing it in the debugger and have it configured as unsigned.
    #3
    dan1138
    Super Member
    • Total Posts : 3990
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC16f18345 loop from positive to negative value 2020/10/19 15:30:54 (permalink)
    +1 (1)
    ababa
    Hi.
     
    I am trying to do a simple loop that counts from 4 to -4.  When counter reaches zero and next number should be -1 it gives 0xFFFF instead. Please, what I am missing? Thanks.
    PIC16f18345, compiler XC8. Test is signed int. 
          test++;
       if(test > 4)
      {
        do
        {
         test--;
        }while(test > -4);
    }

    This is how your code works for me:

    Attached Image(s)

    #4
    ababa
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/11/16 16:43:03
    • Location: 0
    • Status: offline
    Re: PIC16f18345 loop from positive to negative value 2020/10/20 10:29:54 (permalink)
    +2 (2)
    Thank you for replies. I changed the debugger view to decimal and it works. 
    I feel stupid.  grin: grin
    #5
    dan1138
    Super Member
    • Total Posts : 3990
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC16f18345 loop from positive to negative value 2020/10/20 13:19:06 (permalink)
    -1 (1)
    ababa
    I feel stupid.  grin: grin



    Don't call yourself stupid, if Olin was still around that would have been his job. ;)
    #6
    ric
    Super Member
    • Total Posts : 28941
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f18345 loop from positive to negative value 2020/10/20 13:43:35 (permalink)
    +2 (2)
    ababa
    Thank you for replies. I changed the debugger view to decimal and it works. 

    Hopefully lesson learned.
    That's why I asked "how do you know?".
    It's important to mention HOW you are determining something is wrong, as often it is the testing method that is at fault...
     

    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!
    #7
    1and0
    Access is Denied
    • Total Posts : 11501
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16f18345 loop from positive to negative value 2020/10/21 06:43:51 (permalink)
    0
    vexorg
    The debugger view shouldn't have stopped the code running as it should, it should still have run the same number of loops.

    I don't think OP said it has stopped the code from running. ;)
    #8
    Murton Pike Systems
    Senior Member
    • Total Posts : 72
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC16f18345 loop from positive to negative value 2020/10/21 08:41:21 (permalink)
    -1 (1)
    for (test=4; test>=-4;test--)
    {
    }
     
    #9
    Jump to:
    © 2020 APG vNext Commercial Version 4.5