Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Strange arithmetic results on AVR

2012-11-10
2013-05-30
  • Hi!

    I tried the the new print command for long integers,but it doesn't work for me on ATtiny2313 + LCD. After some investigation i found out, that arithmetics give strange results with long and integer-variables. Tried with another AVR (Mega48) and serial communication and got the same issue.

    This code works proper on PIC (with differences for the first lines of course) but with different results on AVR:

    'ATmega48:
    #chip mega48, 1
    'Configuring UART:
    #define USART_BAUD_RATE 1200
    #define USART_BLOCKING
    Dir PORTD.1 Out
    Dim lVar as Long
    Dim iVar as Integer
    'Testing LONG arithmetics:
    HSerSend 13
    lVar = 22222 / 2
    HSerPrint [word]lVar  'should give 11111, o.k.
    HSerSend 13
    lVar = lVar * 2
    HSerPrint [word]lVar  'should give 22222, o.k.
    HSerSend 13
    lVar = lVar / 2
    HSerPrint [word]lVar  'should give 11111, but gives 0
    HSerSend 13
    lVar = 12345
    lVar = lVar % 1000
    HSerPrint [word]lVar  'should give 345, but gives 255
    HSerSend 13
    'Testing INTEGER arithmetics:
    HSerSend 13
    iVar = 22222 / 2
    HSerPrint iVar        'should give 11111, o.k.
    HSerSend 13
    iVar = iVar * 2
    HSerPrint iVar        'should give 22222, but gives -23601
    HSerSend 13
    iVar = 22222
    iVar = iVar / 2
    HSerPrint iVar        'should give 11111, but gives 1
    HSerSend 13
    iVar = 12345
    iVar = iVar % 1000
    HSerPrint iVar        'should give 345, but gives 12345
    HSerSend 13
    wait 200 ms
    End
    

    Regards
    _________________________
    Steini