April 19, 2024, 05:38:48 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


I2C for EBasic & IWBasic (part 2)

Started by Techno, March 14, 2011, 03:41:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Hi all,

This code may be used freely.
I would like if you Offer tips, tricks and how to make improvements.
This code can be compiled as a DLL.
Its I2C datacommunication via the serialport.

I do not have to write test application because I can not test it here at my work.
Someone a sample, send it certainly

i2c_serial.eba


$USE "inpout32.lib"
$INCLUDE "i2c_rs232.inc"

'12.2 Aansluiting op de serial RS232 poort
'-----------------------------------------
'
' - Het is iets ingewikkelder om de I2C bus aan de RS232 te koppelen
' - De PC heeft meerdere serial ports
' - Geen enkele aansluiting van de serial port is bidirectioneel dus moeten we de ingangen en uitgangen op een
'   of andere manieren te zien combineren
' - In rust zijn de DTR (SDA) en de RTS (SCL) hoog.
' - We hebben de gebruikelijke open collector uitgangen niet nodig wanneer we de uitgangen in plaats van
'   logisch '0' te maken, gewoonweg uitschakelen.
' - Teruglezen van de logische toestand is alleen voor de DTR (SDA) lijn nodig (zolang er geen tweede master
'   in het systeem aanwezig is)
' - We kunnen bestaande programma's zonder veel problemen zowel met de LPT als met de COM poort worden
'   gebruikt

' ImpOut32.dll '
DECLARE IMPORT, Inp32(PortAddr : WORD), WORD          '
DECLARE IMPORT, Out32(PortAddr : WORD, PortData : WORD) '
' MSVC70.dll                                                                       '
DECLARE EXTERN _sscanf(STRING buf, STRING format,...),INT '
DECLARE EXTERN sprintf(POINTER p, POINTER p, ...), INT '
DECLARE EXTERN ReadKey alias __getch(),INT '
DECLARE EXTERN KeyPressed alias __kbhit(),INT '
' kernel32.dll '
DECLARE IMPORT, Sleep(int time)

EXPORT i2c_Init
EXPORT i2c_Start
EXPORT i2c_Stop
EXPORT i2c_Ack
EXPORT i2c_NoAck
EXPORT i2c_Write
EXPORT i2c_Read

SUB i2c_Init()
    Out32 ((COM1 + 0x04), 0x03)                             'SCL = 1, SDA = 1   (0000 0011)
RETURN
END SUB

SUB i2c_Start()
    Out32   ((COM1 + 0x04), 0x02)                           'SDA = 0, SCL = 1   (0000 0010)
    Out32   ((COM1 + 0x04), 0x00)                           'SDA = 0, SCL = 0   (0000 0000)
RETURN
END SUB

SUB i2c_Stop()
    Out32   ((COM1 + 0x04), 0x00)                           'SCL = 0, SDA = 0   (0000 0000)
    Out32   ((COM1 + 0x04), 0x02)                           'SCL = 1, SDA = 0   (0000 0010)
    Out32   ((COM1 + 0x04), 0x03)                           'SDA = 1, SDA = 1   (0000 0011)
RETURN
END SUB

SUB i2c_Ack()
    DEF m : CHAR

    Out32   ((COM1 + 0x04), 0x00)                           'SCL = 0, SDA = 0   (0000 0000)
    Out32   ((COM1 + 0x04), 0x02)                           'SCL = 1, SDA = 0   (0000 0010)

    FOR m = 1 TO 5 : NEXT m
    Out32   ((COM1 + 0x04), 0x00)                           'SCL = 0, SDA = 0   (0000 0000)
RETURN
END SUB

SUB i2c_NoAck()
    DEF m : CHAR

    Out32   ((COM1 + 0x04), 0x01)                           'SCL = 0, SDA = 1   (0000 0001)
    Out32   ((COM1 + 0x04), 0x03)                           'SCL = 1, SDA = 1   (0000 0011)

    FOR m = 1 TO 5 : NEXT m
    Out32   ((COM1 + 0x04), 0x01)                           'SCL = 0, SDA = 1   (0000 0001)
RETURN
END SUB

SUB i2c_Write(DataWoord AS CHAR)
    DEF BitWoord : CHAR
DEF PortWoord : CHAR
DEF n, m : CHAR

    BitWoord = &h80

    FOR n = 1 TO 8
        IF (DataWoord & BitWoord) = BitWoord THEN
            PortWoord = 0x01    '(0000 0001)
        ELSE
            PortWoord = 0x00    '(0000 0000)
        END IF

        Out32   ((COM1 + 0x04), PortWoord)                 ' SDA = setten
        Out32   ((COM1 + 0x04), PortWoord + 0x02)          ' SCL = HOOG
        FOR m = 1 TO 5 : NEXT m                            ' delay loop

        Out32   ((COM1 + 0x04), PortWoord)                 ' SCL = reset
        Out32   ((COM1 + 0x04), BitWoord >> 0x01)          '
    NEXT n

    Out32   ((COM1 + 0x04), 0x01)                          'SCL = 0, SDA = 1    (0000 0001)
    Out32   ((COM1 + 0x04), 0x03)                          'SCL = 1, SDA = read (0000 0011)

    FOR m = 1 TO 5 : NEXT m                                ' delay loop

    IF (Inp32(COM1 + 0x06) & 0x10) = 0x10 THEN PRINT "IC antwoord niet"
    Out32 ((COM1 + 0x04), 0x01)                            'SCL = 0, SDA = 1    (0000 0001)
RETURN
END SUB

SUB i2c_Read(), CHAR
    DEF BitWoord AS CHAR
DEF DataWoord AS CHAR
DEF n, m AS CHAR

    Out32 ((COM1 + 0x04), 0x01)                            'SCL = 0, SDA = 1    (0000 0001)
    BitWoord = 0x80
    DataWoord = 0x00

    FOR n = 1 TO 8

        Out32 ((COM1 + 0x04), 0x03)                        'SCL = 1, SDA = 1    (0000 0011)

        FOR m = 1 TO 5 : NEXT m                            ' delay loop

        IF (Inp32(COM1 + 0x06) & 0x10) = 0x10 THEN
DataWoord += BitWoord
        END IF

        Out32 ((COM1 + 0x04), 0x01)                        'SCL = 0, SDA = 1    (0000 0001)
        BitWoord = BitWoord >> 1
    NEXT n
    RETURN DataWoord
ENDSUB
   




Techno



DECLARE IMPORT, i2c_Init()
DECLARE IMPORT, i2c_Start()
DECLARE IMPORT, i2c_Stop()
DECLARE IMPORT, i2c_Ack()
DECLARE IMPORT, i2c_NoAck()
DECLARE IMPORT, i2c_Write(DataWoord AS CHAR)
DECLARE IMPORT, i2c_Read(), CHAR

CONST COM1      = 0x3F8
CONST COM2      = 0x2F8
CONST COM3      = 0x3E8
CONST COM4      = 0x2E8



Techno

Fixed



$USE "inpout32.lib"
$INCLUDE "i2c_lpt.inc"

DECLARE IMPORT, Inp32(PortAddr : WORD), WORD          '
DECLARE IMPORT, Out32(PortAddr : WORD, PortData : WORD) '
'============================================================================================================'

'============================================================================================================'
' MSVC70.dll                                                                       '
DECLARE EXTERN _sscanf(STRING buf, STRING format,...),INT '
DECLARE EXTERN sprintf(POINTER p, POINTER p, ...), INT '
DECLARE EXTERN ReadKey alias __getch(),INT '
DECLARE EXTERN KeyPressed alias __kbhit(),INT '
'============================================================================================================'

'============================================================================================================'
' kernel32.dll '
DECLARE IMPORT, Sleep(int time) '
'============================================================================================================'

EXPORT i2c_Init
EXPORT i2c_Start
EXPORT i2c_Stop
EXPORT i2c_Ack
EXPORT i2c_NoAck
EXPORT i2c_Write
EXPORT i2c_Read

SUB i2c_Init()
    Out32 ((LPT1 + 0x02), 0x00)                            'SCL = 1, SDA = 1   (0000 0000)
RETURN
ENDSUB

SUB i2c_Start()
    Out32 ((LPT1 + 0x02), 0x01)                         'SCL = 1, SDA = 0   (0000 0001)
    Out32 ((LPT1 + 0x02), 0x03)                            'SCL = 0, SDA = 0   (0000 0011)
RETURN
ENDSUB

SUB i2c_Stop()
    Out32 ((LPT1 + 0x02), 0x03)                             'SCL = 0, SDA = 0   (0000 0011)
    Out32 ((LPT1 + 0x02), 0x01)                             'SCL = 1, SDA = 0   (0000 0001)
    Out32 ((LPT1 + 0x02), 0x00)                             'SDA = 1, SDA = 1   (0000 0000)
RETURN
ENDSUB

SUB i2c_Ack()
   
Out32 ((LPT1 + 0x02), 0x03)                             'SCL = 0, SDA = 0   (0000 0011)
    Out32 ((LPT1 + 0x02), 0x01)                             'SCL = 1, SDA = 0   (0000 0001)

    'FOR m = 1 TO 5 : NEXT m
    Sleep(5)
Out32 ((LPT1 + 0x02), 0x03)                             'SCL = 0, SDA = 0   (0000 0011)
RETURN
ENDSUB

SUB i2c_NoAck()

Out32 ((LPT1 + 0x02), 0x02)                            'SCL = 0, SDA = 1   (0000 0010)
    Out32 ((LPT1 + 0x02), 0x00)                          'SCL = 1, SDA = 1   (0000 0000)

    'FOR m = 1 TO 5 : NEXT m
    Sleep(5)
Out32 ((LPT1 + 0x02), 0x02)                             'SCL = 0, SDA = 1   (0000 0010)
RETURN
END SUB

SUB i2c_Write(DataWoord AS CHAR)
    DEF BitWoord : CHAR
DEF PortWoord : CHAR
DEF n    : CHAR

    BitWoord = 0x80

    FOR n = 1 TO 8
        IF (DataWoord & BitWoord) = BitWoord THEN
            PortWoord = 0x02    '(0000 0010)
        ELSE
            PortWoord = 0x03    '(0000 0011)
        END IF

        Out32 ((LPT1 + 0x02), PortWoord)                  ' SDA = setten
        Out32 ((LPT1 + 0x02), PortWoord - 0x02)          ' SCL = HOOG
        Sleep(5)
'FOR m = 1 TO 5 : NEXT m                             ' delay loop

        Out32 ((LPT1 + 0x02), PortWoord)                    ' SCL = reset
        Out32 ((LPT1 + 0x02), BitWoord >> 0x01)             '
    NEXT n

    Out32 ((LPT1 + &H02), 0x02)                             'SCL = 0, SDA = 1    (0000 0010)
    Out32 ((LPT1 + &H02), 0x00)                             'SCL = 1, SDA = read (0000 0000)

Sleep(5)
    'FOR m = 1 TO 5 : NEXT m                                 ' delay loop

    IF (Inp32(LPT1 + 0x02) & 0x01) = 0 THEN PRINT "IC Antwoord niet"
    Out32 ((LPT1 + &H02), 0x02)                             'SCL = 0, SDA = 1    (0000 0010)
RETURN
END SUB

SUB i2c_Read(), CHAR
    DEF BitWoord : CHAR
DEF DataWoord : CHAR
DEF n    : CHAR

    Out32 ((LPT1 + 0x02), 0x02)                             'SCL = 0, SDA = 1    (0000 0010)
    BitWoord = 0x80
    DataWoord = 0x00

    FOR n = 1 TO 8

        Out32 ((LPT1 + 0x02), 0x00)                         'SCL = 1, SDA = 1    (0000 0000)

Sleep(5)
        'FOR m = 1 TO 5 : NEXT m                             ' delay loop

        IF (Inp32(LPT1 + 0x02) & 0x01) = 0 THEN
            DataWoord += BitWoord
        END IF

        Out32 ((LPT1 + 0x02), 0x02)                         'SCL = 0, SDA = 1    (0000 0010)
        BitWoord = BitWoord >> 0x01
    NEXT n
    RETURN DataWoord
ENDSUB

DECLARE i2c_Init()
DECLARE i2c_Start()
DECLARE i2c_Stop()
DECLARE i2c_Ack()
DECLARE i2c_NoAck()
DECLARE i2c_Write(DataWoord AS CHAR)
DECLARE i2c_Read(), CHAR

CONST LPT1      = 0x378
CONST LPT2      = 0x278
CONST LPT3      = 0x3BC

CONST STROBE    = 0x01
CONST AUTO_FEED = 0x02
'CONST INIT      = 0x04
CONST SLCTIN    = 0x08

CONST ERROR     = 0x08
CONST SELECTS   = 0x10
CONST PE        = 0x20
CONST ACK       = 0x40
CONST BUSY      = 0x80





$USE "inpout32.lib"
$INCLUDE "i2c_rs232.inc"

DECLARE IMPORT, Inp32(PortAddr : WORD), WORD          '
DECLARE IMPORT, Out32(PortAddr : WORD, PortData : WORD) '
' MSVC70.dll                                                                       '
DECLARE EXTERN _sscanf(STRING buf, STRING format,...),INT '
DECLARE EXTERN sprintf(POINTER p, POINTER p, ...), INT '
DECLARE EXTERN ReadKey alias __getch(),INT '
DECLARE EXTERN KeyPressed alias __kbhit(),INT '
' kernel32.dll '
DECLARE IMPORT, Sleep(int time)

EXPORT i2c_Init
EXPORT i2c_Start
EXPORT i2c_Stop
EXPORT i2c_Ack
EXPORT i2c_NoAck
EXPORT i2c_Write
EXPORT i2c_Read

SUB i2c_Init()
    Out32 ((COM1 + 0x04), 0x03)                             'SCL = 1, SDA = 1   (0000 0011)
RETURN
END SUB

SUB i2c_Start()
    Out32   ((COM1 + 0x04), 0x02)                           'SDA = 0, SCL = 1   (0000 0010)
    Out32   ((COM1 + 0x04), 0x00)                           'SDA = 0, SCL = 0   (0000 0000)
RETURN
END SUB

SUB i2c_Stop()
    Out32   ((COM1 + 0x04), 0x00)                           'SCL = 0, SDA = 0   (0000 0000)
    Out32   ((COM1 + 0x04), 0x02)                           'SCL = 1, SDA = 0   (0000 0010)
    Out32   ((COM1 + 0x04), 0x03)                           'SDA = 1, SDA = 1   (0000 0011)
RETURN
END SUB

SUB i2c_Ack()
    DEF m : CHAR

    Out32   ((COM1 + 0x04), 0x00)                           'SCL = 0, SDA = 0   (0000 0000)
    Out32   ((COM1 + 0x04), 0x02)                           'SCL = 1, SDA = 0   (0000 0010)

    FOR m = 1 TO 5 : NEXT m
    Out32   ((COM1 + 0x04), 0x00)                           'SCL = 0, SDA = 0   (0000 0000)
RETURN
END SUB

SUB i2c_NoAck()
    DEF m : CHAR

    Out32   ((COM1 + 0x04), 0x01)                           'SCL = 0, SDA = 1   (0000 0001)
    Out32   ((COM1 + 0x04), 0x03)                           'SCL = 1, SDA = 1   (0000 0011)

    FOR m = 1 TO 5 : NEXT m
    Out32   ((COM1 + 0x04), 0x01)                           'SCL = 0, SDA = 1   (0000 0001)
RETURN
END SUB

SUB i2c_Write(DataWoord AS CHAR)
    DEF BitWoord : CHAR
DEF PortWoord : CHAR
DEF n, m : CHAR

    BitWoord = &h80

    FOR n = 1 TO 8
        IF (DataWoord & BitWoord) = BitWoord THEN
            PortWoord = 0x01    '(0000 0001)
        ELSE
            PortWoord = 0x00    '(0000 0000)
        END IF

        Out32   ((COM1 + 0x04), PortWoord)                 ' SDA = setten
        Out32   ((COM1 + 0x04), PortWoord + 0x02)          ' SCL = HOOG
        FOR m = 1 TO 5 : NEXT m                            ' delay loop

        Out32   ((COM1 + 0x04), PortWoord)                 ' SCL = reset
        Out32   ((COM1 + 0x04), BitWoord >> 0x01)          '
    NEXT n

    Out32   ((COM1 + 0x04), 0x01)                          'SCL = 0, SDA = 1    (0000 0001)
    Out32   ((COM1 + 0x04), 0x03)                          'SCL = 1, SDA = read (0000 0011)

    FOR m = 1 TO 5 : NEXT m                                ' delay loop

    IF (Inp32(COM1 + 0x06) & 0x10) = 0x10 THEN PRINT "IC antwoord niet"
    Out32 ((COM1 + 0x04), 0x01)                            'SCL = 0, SDA = 1    (0000 0001)
RETURN
END SUB

SUB i2c_Read(), CHAR
    DEF BitWoord AS CHAR
DEF DataWoord AS CHAR
DEF n, m AS CHAR

    Out32 ((COM1 + 0x04), 0x01)                            'SCL = 0, SDA = 1    (0000 0001)
    BitWoord = 0x80
    DataWoord = 0x00

    FOR n = 1 TO 8

        Out32 ((COM1 + 0x04), 0x03)                        'SCL = 1, SDA = 1    (0000 0011)

        FOR m = 1 TO 5 : NEXT m                            ' delay loop

        IF (Inp32(COM1 + 0x06) & 0x10) = 0x10 THEN
DataWoord += BitWoord
        END IF

        Out32 ((COM1 + 0x04), 0x01)                        'SCL = 0, SDA = 1    (0000 0001)
        BitWoord = BitWoord >> 1
    NEXT n
    RETURN DataWoord
ENDSUB

DECLARE i2c_Init()
DECLARE i2c_Start()
DECLARE i2c_Stop()
DECLARE i2c_Ack()
DECLARE i2c_NoAck()
DECLARE i2c_Write(DataWoord AS CHAR)
DECLARE i2c_Read(), CHAR

CONST COM1      = 0x3F8
CONST COM2      = 0x2F8
CONST COM3      = 0x3E8
CONST COM4      = 0x2E8