June 26, 2024, 06:40:53 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


i2c function library + PCF8574 console Application

Started by Techno, September 27, 2008, 10:27:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Dear support

I have written i2c functions for I2C parallel port.
I have been compiled into i2c.dll: result it compiled en links fine en generate the i2c_lpt.dll
Then imports the library into i2c_Lpt.lib + inpout32.lib
The include files : have in the ..\include library : i2c_lpt.inc and inpout32.inc
The library for call the I/O ports mains : inpout32.dll


/* i2c_Lpt.inc */

DECLARE EXTERN i2c_Init()
DECLARE EXTERN i2c_Start()
DECLARE EXTERN i2c_Stop()
DECLARE EXTERN i2c_Ack()
DECLARE EXTERN i2c_NoAck()
DECLARE EXTERN i2c_Write(DataWoord AS CHAR)
DECLARE EXTERN 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





/* i2c_lpt.eba */
/* generated : i2c_lpt.dll */
/* generated : i2c_lpt.exp */
/* generated : i2c_lpt.lib */

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


'============================================================================================================'
' 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 ((LPT1 + 0x02), 0x00)                            'SCL = 1, SDA = 1   (0000 0000)
ENDSUB

SUB i2c_Start()
    Out32 ((LPT1 + 0x02), 0x01)                         'SCL = 1, SDA = 0   (0000 0001)
    Out32 ((LPT1 + 0x02), 0x03)                            'SCL = 0, SDA = 0   (0000 0011)
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)
ENDSUB

SUB i2c_Ack()
    DEF m AS CHAR

    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
    Out32 ((LPT1 + 0x02), 0x03)                             'SCL = 0, SDA = 0   (0000 0011)

ENDSUB

SUB i2c_NoAck()
    DEF m AS CHAR

    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
    Out32 ((LPT1 + 0x02), 0x02)                             'SCL = 0, SDA = 1   (0000 0010)

END SUB

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

    BitWoord = 0x80

    FOR n = 1 TO 8
        IF (DataWoord AND 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
        FOR m = 1 TO 5 : NEXT m                             ' delay loop

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

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

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

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

END SUB

SUB i2c_Read(), CHAR
    DEF BitWoord : CHAR
DEF DataWoord : CHAR
DEF n, m : 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)

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

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

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

    RETURN DataWoord
ENDSUB





/* PCF8574_Direct.eba */

$USE "Inpout32.inc"
$USE "i2c_lpt.lib"
$INCLUDE "E:\Program Files\EDev\Include\i2c_lpt.inc"

$MAIN

OPENCONSOLE
DEF s AS STRING
    DEF Ausgabe AS CHAR

    DO
        INPUT "Schrijven: ",s : Ausgabe = VAL(s)
        Parallel_Output(Ausgabe)
        PRINT "Lezen = " ,STR$(Parallel_Input())
    UNTIL Ausgabe = 0

CLOSECONSOLE
END

SUB Parallel_Output(DataWoord AS CHAR)
    i2c_Start()
    i2c_Write(32 * 2 + 0)
    i2c_Write(DataWoord)
    i2c_Stop()
ENDSUB

SUB Parallel_Input(), CHAR
DEF ret : CHAR
    i2c_Start()
    i2c_Write(32 * 2 + 1)
    ret = i2c_Read()
RETURN ret
    i2c_Ack()
    i2c_Stop()

This give my errors, but why?

Compiling...
PCF8574D.eba
Unable to Open - E:\Program Files\EDev\Include\i2c_lpt.inc
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (38) Warning: undeclared function 'i2c_Start' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (39) Warning: undeclared function 'i2c_Write' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (40) Warning: undeclared function 'i2c_Write' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (41) Warning: undeclared function 'i2c_Stop' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (46) Warning: undeclared function 'i2c_Start' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (47) Warning: undeclared function 'i2c_Write' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (48) Warning: undeclared function 'i2c_Read'
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (50) Warning: undeclared function 'i2c_Ack' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (51) Warning: undeclared function 'i2c_Stop' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (52) Warning: RETURN value expected. - ENDSUB
Error(s) in compiling "E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba"

ENDSUB

1e Question
=======

Does my i2c_lpt.dll or i2c_lpt.lib is right written en works correctly?. Can someone test it because I will know if it works.

2e Question
========

How can I this rewritten in ebasic ?

[code]

bitword = bitword div 2 ?



Sapero can you please help me, but my main program doesn't work for driving the I2C  IC PCF8575 ?

Kind regards
Stephane                                             





[/code]

Techno

Quote from: Techno on September 27, 2008, 10:27:31 AM
Dear support

I have written i2c functions for I2C parallel port.
I have been compiled into i2c.dll: result it compiled en links fine en generate the i2c_lpt.dll
Then imports the library into i2c_Lpt.lib + inpout32.lib
The include files : have in the ..\include library : i2c_lpt.inc and inpout32.inc
The library for call the I/O ports mains : inpout32.dll


/* i2c_Lpt.inc */

DECLARE EXTERN i2c_Init()
DECLARE EXTERN i2c_Start()
DECLARE EXTERN i2c_Stop()
DECLARE EXTERN i2c_Ack()
DECLARE EXTERN i2c_NoAck()
DECLARE EXTERN i2c_Write(DataWoord AS CHAR)
DECLARE EXTERN 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





/* i2c_lpt.eba */
/* generated : i2c_lpt.dll */
/* generated : i2c_lpt.exp */
/* generated : i2c_lpt.lib */

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


'============================================================================================================'
' 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 ((LPT1 + 0x02), 0x00)                            'SCL = 1, SDA = 1   (0000 0000)
ENDSUB

SUB i2c_Start()
    Out32 ((LPT1 + 0x02), 0x01)                         'SCL = 1, SDA = 0   (0000 0001)
    Out32 ((LPT1 + 0x02), 0x03)                            'SCL = 0, SDA = 0   (0000 0011)
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)
ENDSUB

SUB i2c_Ack()
    DEF m AS CHAR

    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
    Out32 ((LPT1 + 0x02), 0x03)                             'SCL = 0, SDA = 0   (0000 0011)

ENDSUB

SUB i2c_NoAck()
    DEF m AS CHAR

    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
    Out32 ((LPT1 + 0x02), 0x02)                             'SCL = 0, SDA = 1   (0000 0010)

END SUB

SUB i2c_Write(DataWoord AS CHAR)
    DEF BitWoord : CHAR
DEF PortWoord : CHAR
DEF n, m : 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
        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)

    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)

END SUB

SUB i2c_Read(), CHAR
    DEF BitWoord : CHAR
DEF DataWoord : CHAR
DEF n, m : 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)

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

        IF (Inp32(LPT1 + 0x02) AND 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





/* PCF8574_Direct.eba */

$USE "Inpout32.inc"
$USE "i2c_lpt.lib"
$INCLUDE "E:\Program Files\EDev\Include\i2c_lpt.inc"

$MAIN

OPENCONSOLE
DEF s AS STRING
    DEF Ausgabe AS CHAR

    DO
        INPUT "Schrijven: ",s : Ausgabe = VAL(s)
        Parallel_Output(Ausgabe)
        PRINT "Lezen = " ,STR$(Parallel_Input())
    UNTIL Ausgabe = 0

CLOSECONSOLE
END

SUB Parallel_Output(DataWoord AS CHAR)
    i2c_Start()
    i2c_Write(32 * 2 + 0)
    i2c_Write(DataWoord)
    i2c_Stop()
ENDSUB

SUB Parallel_Input(), CHAR
DEF ret : CHAR
    i2c_Start()
    i2c_Write(32 * 2 + 1)
    ret = i2c_Read()
RETURN ret
    i2c_Ack()
    i2c_Stop()

This give my errors, but why?

Compiling...
PCF8574D.eba
Unable to Open - E:\Program Files\EDev\Include\i2c_lpt.inc
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (38) Warning: undeclared function 'i2c_Start' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (39) Warning: undeclared function 'i2c_Write' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (40) Warning: undeclared function 'i2c_Write' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (41) Warning: undeclared function 'i2c_Stop' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (46) Warning: undeclared function 'i2c_Start' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (47) Warning: undeclared function 'i2c_Write' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (48) Warning: undeclared function 'i2c_Read'
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (50) Warning: undeclared function 'i2c_Ack' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (51) Warning: undeclared function 'i2c_Stop' - )
File: E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba (52) Warning: RETURN value expected. - ENDSUB
Error(s) in compiling "E:\Program Files\EBDev\projects\Electronics\PCF8574D.eba"

ENDSUB

1e Question
=======

Does my i2c_lpt.dll or i2c_lpt.lib is right written en works correctly?. Can someone test it because I will know if it works.

2e Question
========

How can I this rewritten in ebasic ?

[code]

bitword = bitword div 2 ?



Sapero can you please help me, but my main program doesn't work for driving the I2C  IC PCF8575 ?

Kind regards
Stephane                                             





[/code]

pistol350

Hi Stephane!
First thing i got to say if that you're unfortunately in the wrong forum as your code is Ebasic code.  ;)
Then, my advice to you when you are coding in Ebasic is to always put the "RETURN" statement (before the ENDSUB statement) even though your routines do not return anything.

I managed to get your code compile but you have to change a few things.
First, in the  /* i2c_lpt.eba */ sample

Just put every "RETURN" statement required then compile the DLL.
After compiling the DLL, go to the IDE menu and do "Tool", then "create import library" use the browser to go to the folder where your just compiled "i2c_lpt.dll"
is located. After creating that library, let's continue to your "PCF8574_Direct.eba" sample

In that sample, you will also have to add every "RETURN" statement required.
Then change that line

$USE "Inpout32.inc"
to
$USE "Inpout32.lib"

Also, i'd rather advise you to add every sample files of your project in the same folder to prevent indicating the "INCLUDE" folder as you do there
$INCLUDE "E:\Program Files\EDev\Include\i2c_lpt.inc"

IF the "i2c_lpt.inc" file is in the same folders as the other files, you'll just have to write this instead.
$INCLUDE "i2c_lpt.inc"

Then, You may compile the sample code.
Assuming that the " inpout32.dll " is in the same folder as your executable, the program should work properly.

hope that helps.




Regards,

Peter B.

Techno