Menu

SWAP4 function

Theo
2014-10-01
2014-10-04
  • Theo

    Theo - 2014-10-01

    For a few days now i'm trying to get a 2-wire lcd-display working (with shiftreg 74LS164 connected to an Atmega8), but no result at all. Trying to understand how the 2-wire routine was implemented by looking into the lcd.h, I discovered that the function swap4 was used. So i used the example from the help-file to see what was happening. But the result was not as expected; the function does not work at all.
    Looking in the asm-file at the FN_SWAP4, it looks likes there is a mix-up between SysCalcTempX en SysCalcTempB; or am I wrong? In the past my 2-wire LCD-display did work with a 16F648A, so the hardware is OK. Could it be a typical AVR (compiler)problem?

    ----------------------------------
    #chip mega8,8
    
    ;#define LCD_IO 4
    #define LCD_DB4 PORTD.0 ; 11
    #define LCD_DB5 PORTD.1 ; 12
    #define LCD_DB6 PORTD.2 ; 13
    #define LCD_DB7 PORTD.3 ; 14
    #define LCD_RS PORTD.4  ; 4
    #define LCD_Enable PORTD.5 ; 6
    #define LCD_NO_RW
    
    cls
    wait 1 S
    
    dim ByteVariable as Byte
    
    ' Set variable to  0x12
    ByteVariable = 0x12
    
    locate 0,0: print hex(ByteVariable)
    locate 1,0: print hex(Swap4(ByteVariable))' Should return 0x21 but returns 0x12 now
    
    end
    -------------------------------------
    

    Kind regards
    Theo.

     

    Last edit: Anobium 2014-10-02
  • Anobium

    Anobium - 2014-10-02

    Thank for letting us know. We will look into the root cause of the issue.

    As a workaround please insert this is you code. This is NOT a permanent fix. Please forgive me for my poor use of variables.

    #define Swap4 mySwap4
    'Swap nibbles (4-byte blocks)
    Function MySwap4(Swap4In)
        #ifdef PIC
            swapf Swap4In, W
            movwf swap4
        #endif
        #ifdef AVR
                TempA = (Swap4In and 0xF0) / 16
                TempB = ( Swap4In and 0x0F) * 16
                MySwap4 = TempB + TempA
        #endif
    End Function
    

    Did you get the LCD working?

     

    Last edit: Anobium 2014-10-02
  • Theo

    Theo - 2014-10-02

    Thanks for your help, Anobium.

    Does your fix works? Yes and no.

    See the listing; I get the expected result (0x21) but if I use the bytetobin function my
    variable AAAA is changed (there is no relation with the name AAAA).

    Run the program as it is; the output will be like this: first the dec value, second the hex value
    18 13
    33 21
    then remove the apostrophe to print the Bytevariable with the bytetobin function and watch line 2; the variable AAAA has changed to 1.
    The wordtobin function shows no problem.

    Does my 2-wire lcd work? No, but I don't know for shure that the Swap4 function is 100% now.

    An complete other question:
    should the swap4 function work as described over here?

    http://electricimp.com/docs/squirrel/system/swap4/

    Then our expected result should be like this.

    not like this: 0x21 33 00100001
    but like this: 0x84 132 10000100

    Kind regards
    Theo.


    #chip mega8,8

    #define LCD_IO 4
    #define LCD_DB4 PORTD.0 ; 11
    #define LCD_DB5 PORTD.1 ; 12
    #define LCD_DB6 PORTD.2 ; 13
    #define LCD_DB7 PORTD.3 ; 14
    #define LCD_RS PORTD.4 ; 4
    #define LCD_Enable PORTD.5 ; 6
    #define LCD_NO_RW

    #define Swap4 mySwap4
    'Swap nibbles (4-byte blocks)
    Function MySwap4(Swap4In)
    #ifdef PIC
    swapf Swap4In, W
    movwf swap4
    #endif
    #ifdef AVR
    TempA = (Swap4In and 0xF0) / 16
    TempB = (Swap4In and 0x0F) * 16
    MySwap4 = TempB + TempA
    #endif
    End Function

    cls
    wait 1 S

    dim ByteVariable as Byte
    dim AAAA as byte

    ' Set variable to 0x12
    ByteVariable = 0x12
    AAAA = Swap4(ByteVariable)

    locate 0,0
    print ByteVariable
    print " ":print hex(ByteVariable):print " "
    'print bytetobin(ByteVariable) ; remove here the apostrophe
    'print wordtobin(ByteVariable)
    locate 1,0
    print AAAA
    print " ":print hex(AAAA): print " " '
    'print bytetobin(AAAA)
    'print wordtobin(AAAA)
    end

     

    Last edit: Theo 2014-10-02
  • Anobium

    Anobium - 2014-10-02

    Please replace your stdbasic.h with this file @ https://sourceforge.net/p/gcbasic/code/HEAD/tree/GCBASIC/trunk/include/lowlevel/stdbasic.h

    This file will swap the nibbles correctly. The GCB swap function swaps nibbles - this is the design intent GCB does not XOR the nibble swap. We need compatibility with the PIC swap function.

    This new file is intended to fix the error in the SWAP4 code for the AVR issue you reported.

    Please see if this also resolves the bytetobin issue. On my simulator here I do not get the same error.

     

    Last edit: Anobium 2014-10-02
  • Theo

    Theo - 2014-10-02

    It's a beautiful day, as Freddie Mercury sung a long time ago.

    Both problems have been fixed; or actually three problems.

    The swap4 function is working and the Bytetobin function also again.
    But more important: 2-wire lcd is working; i've tested it with a 74LS164 and a 74HC164.

    Thanks again Anobium, for solving this problem and the explanation of GCB Swap4 (and all this within 22 hours!).

     
  • Anobium

    Anobium - 2014-10-02

    I did a quick patch before breakfast. Hugh did the permanent patch before he went to bed!

    The wonders of global collaboration!!!

    Enjoy the day.

    Post you final code and please post your design for othera to share.... including me!!

    :-)

     
  • Theo

    Theo - 2014-10-04

    Thanks also to Hugh for solving the problem!

    The hardware for using a 2-wire lcd with 74LS164 is simple and based on Myke Predko interface with 74LS174.

    See:
    http://voodooz.no-ip.org/robotics/LCD_2_wire.html
    or
    http://www.electro-tech-online.com/threads/k8lh-breadboard-buddy-lcd-interface.92030/

    Attached an adapted jpg from K8LH.

    The software is even more simple:


    #chip mega8,8

    'Use LCD in 2 pin mode and define LCD pins
    #define LCD_IO 2 ' with 74HC164 / 74LS164
    #define LCD_DB PORTC.0 ; databit
    #define LCD_CB PORTC.1 ; clockbit
    #define LCD_NO_RW

    print "Hello World"

    end


    It could be idea to create in this forum a hardware part where 'proven' hardware will be published. Certainly for beginners it could be place to start.
    (Btw. The breadbord buddy lcd inferface from K8LH looks very interesting, without difficult hardware you get pushbuttons, buzzer/speaker connected to the same shiftreg)

     
  • Anobium

    Anobium - 2014-10-04

    Good work.

    I will this great work into the next release of the help file. I will document your method, we already the Myke Predko interface with 74LS174 method and I will add the 74HC595 method.

    Happy Days.

     

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.