Menu

Problem with LONG Variables

Help
MBB
2012-11-03
2013-05-30
  • MBB

    MBB - 2012-11-03

    I was having problems using LONG variables in a complex program so I wrote this simple one to try to understand what was happening.

    I'm using a PIC 18F252.

    ;Variables
    Dim Aa As word
    Dim Bb As word
    Dim Cc As long
    '
    Aa = 823
    Bb = 346

    locate 0, 0
    print Aa

    locate 1, 0
    print Bb

    '
    Cc = Aa * Bb

    locate 2, 0
    print Cc

    At location 0, 0 on the LCD, I see 823 which was expected.

    At location 1, 0 on the LCD, I see 346 which was expected.

    But at location 2, 0 on the LCD,  I see 86.  I expect3ed to see 284758 ( 823 * 346 = 284758).

    I've tried DIMing Aa and Bb as LONG but still don't get the correct answer

    What am I doing wrong?

     
  • YT2095

    YT2095 - 2012-11-04

    LONG will treat the variable as a Word (16 bits) as opposed to a Byte of 8 bits. so the Most you can have is 65535, you`re over 4 times that value, no idea why you get 86 though, unless it`s some sort of wrap-around?

     
  • Frank

    Frank - 2012-11-04

    I don't think the LCD library has a routine yet for printing longs so its just printing the low byte of the long Cc.

    You could try printing out the four bytes of the long individually in hex -

    LCDHex(Cc_E)
    LCDHex(Cc_U)
    LCDHex(Cc_H)
    LCDHex(Cc)

    just to check that the calculation is working.

     
  • Hugh Considine

    Hugh Considine - 2012-11-04

    There was no routine in the LCD library to print Long variables, but adding one seemed like a good idea. So, please try the update from http://gcbasic.sourceforge.net/update.html - if the new LCD Print routine works, I will adapt it for RS232 as well.

     
  • MBB

    MBB - 2012-11-04

    The Update worked.

    Thanks!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.