Author Topic: Unit I2CLPT1  (Read 28339 times)

Stefke35

  • Member
  • ***
  • Posts: 10
Unit I2CLPT1
« on: May 02, 2013, 07:30:41 am »
Hi everyone,

I would rewritten this unit that used the functions of your inpout32.dll.

Code: [Select]

SUB i2c_init()
     [b][color=red]OpenCom("LPT1:") [/color][/b]                                     (1)
     [color=limegreen]'OutPort(&h378+2,0)[/color]
     Out32 (&h37A2, 0)
END SUB

SUB i2c_Start()
      [color=limegreen]'OutPort(&h378+2,1)
      'OutPort(&h378+2,3)[/color]
      Out32(&h37A, &01)
      Out32(&h37A, &03)
END SUB

SUB i2c_Stop()
     [color=limegreen] 'OutPort(&h378+2,3)
      'OutPort(&h378+2,1)
      'OutPort(&h378+2,0)[/color]
      Out32(&h37A, &03)
      Out32(&h37A, &01)
      Out32(&h37A, &00)
END SUB

SUB i2c_Ack()
     [color=limegreen] 'OutPort(&h378+2,3)
      'OutPort(&h378+2,1)[/color]
      Delayus(10)
     [color=limegreen] 'OutPort(&h378+2,3)[/color]
      Out32(&h37A, &03)
      Out32(&h37A, &01)
      Delayus(10)
      Out32(&h37A, &03)
END SUB

SUB i2c_NAck()
      [color=limegreen]'OutPort(&h378+2,2)
      'OutPort(&h378+2,0)[/color]
      Delayus(10)
     [color=limegreen] 'OutPort(&h378+2,2)[/color]
      Out32(&h37A, &02)
      Out32(&h37A, &00)
      Delayus(10)
      Out32(&h37A, &02)
END SUB

FUNCTION i2c_Write(BYVAL bWord AS BYTE) AS LONG
       DIM bit_word, port_word, n AS BYTE
      
       FUNCTION = TRUE
       bit_word = &h80

       FOR n = 1 TO 8
           IF(bWord AND bit_word) = bit_word THEN port_word = 2 ELSE port_word = 3
          [color=limegreen] 'OutPort(&h378+2,port_word)
           'OutPort(&h378+2,port_word-2)[/color]
           Delayus(10)
         [color=limegreen]  'OutPort(&h378+2,port_word)[/color]
           'bit_word = bit_word \ 2
           Out32(&h37A, port_word)
           Out32(&h37A, port_word-2)
           Delayus(10)
           Out32(&h37A, port_word)
           bit_word = bit_word \ 2
      NEXT
      
       [color=limegreen] 'OutPort(&h378+2,2)
      'OutPort(&h378+2,0) [/color]
      Out32(&h37A, &02)
      Out32(&h37A, &00)
      Delayus(10)
      
    [color=limegreen]  'IF(Inport(BA+2) AND 1)) = 0 THEN FUNCTION = FALSE[/color]
      IF(Inp32(&h37A) AND 1)) = 0 THEN FUNCTION = FALSE
     [color=limegreen] 'OutPort(&h378+2,2)[/color]
      Out32(&h37A,&h02)        
END FUNCTION

FUNCTION i2c_read() AS BYTE


END FUNCTION


How can I rewritten this OpenCOM("LPT1:") into Out32 and Inp32 functions with the inpout32.dll library?
« Last Edit: May 02, 2013, 07:32:39 am by Stefke35 »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Unit I2CLPT1
« Reply #1 on: May 02, 2013, 11:40:30 am »
Comment out the OpenCom line and use the Outp function in place of Out32.

Stefke35

  • Member
  • ***
  • Posts: 10
Re: Unit I2CLPT1
« Reply #2 on: May 02, 2013, 04:58:17 pm »
Hi,

I use WinXP and Win7

Is that correct:

OpenCom("LP1:") --> Out32(&h378,&00) Is that correct

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Unit I2CLPT1
« Reply #3 on: May 02, 2013, 08:43:58 pm »
You don't have to open the port at all.

Stefke35

  • Member
  • ***
  • Posts: 10
Re: Unit I2CLPT1
« Reply #4 on: May 03, 2013, 02:31:25 am »
You don't have to open the port at all.

I rewritten the code using your inpout32 library. The code is written in PowerBasic.

Code: [Select]

#Compile SLL "ic2_lpt_1.sll"
#Dim All
Declare Function Inp32 CDecl Lib "inpout32.dll" Alias "inp32" (ByVal PortAddr As Word) As Word
Declare Sub      Out32 CDecl Lib "inpout32.dll" Alias "Out32" (ByVal PortAddr As Word, ByVal PortData As Word)


Declare Sub i2c_Init()
Declare Sub i2c_Start()
Declare Sub i2c_Stop()
Declare Sub i2c_Ack()
Declare Sub i2c_NAck()
Declare Function i2c_Write((ByVal bWord As Byte) As Long
Declare Function i2c_Read() As Byte


Sub i2c_Init()
    Out32(&h37A,&h00)                                       ' SCL = 1; SDA = 1
End Sub

Sub i2c_Start()
    Out32(&h37A,&h01)                                       'SDA = 0
    Out32(&h37A,&h03)                                       'SCL = 0
End Sub

Sub i2c_Stop()
    Out32(&h37A,&h03)                                       'SCL = 0; SDA = 0
    Out32(&h37A,&h01)                                       'SCL = 1
    Out32(&h37A,&h00)                                       'SDA = 0
End Sub

Sub i2c_Ack()
    Out32(&h37A,&h03)                                       'SCL = 0; SDA = 0
    Out32(&h37A,&h01)                                       'SCL = 1
    Delayus(10)                                             'Wait loop 10µs
    Out32(&h37A,&h03)                                       'SCL = 0
End Sub

Sub i2c_NAck()
    Out32(&h37A,&h02)                                       'SCL = 0; SDA = 1
    Out32(&h37A,&h00)                                       'SCL = 1
    Delayus(10)                                             'Wait loop 10µs
    Out32(&h37A,&h02)                                       'SCL = 0
End Sub

Function i2c_Write(ByVal bWord As Byte) As Long
    Local bit_word  As Byte
    Local port_word As Byte
    Local n         As Byte

    Function = -1
    bit_word = &h80

    For n = 1 To 8
        If((bWord And bit_word) = bit_word) Then
            port_word = &h02
        Else
            port_word = &h03
        End If

        Out32(&h37A,port_word)                              'Set SDA = 1
        Out32(&h37A,port_word - &h02)                       'SCL = 1
        Delayus(10)                                         'Wait loop 10µs
        Out32(&h37A,port_word)                              'SCL = 0
        bit_word = bit_word \ 2
    Next

    Out32(&h37A,&h02)                                       'SDA = 1; SCL = 0
    Out32(&h37A,&h00)                                       'SCL = 1, Reading SDA
    Delayus(10)                                             'Wait loop 10µs

    If(Inp32(&h37A) And &h01) = 0 Then
        Function = 0
    End If

    Out32(&h37A,&h02)                                       'SCL = 0
End Function

Function i2c_Read() As Byte
    Local bit_word  As Byte
    Local byt_word  As Byte
    Local n         As Byte

    Out32(&h37A,&h02)                                       'SDA = 1; SCL = 0
    bit_word = &h80
    byt_word = 0

    For n = 1 To 8
        Out32(&h37A,&h00)                                   'SCL = 1, Reading SDA
        Delayus(10)                                         'wait loop 10µs

        If(inp32(&h37A) And &h01) = 0 Then
            byt_word = byt_word + bit_word
        End If

        Out32(&h37A,&h02)                                   'SCL = 0
        bit_word = bit_word \ 2
    Next

    Function = byt_word

End Function           


I have a few questions. I need two delay functions for delay's of micro seconds and milliseconds. Can you please written this code for me with using Win32API functions. What kind of delay functions can I use of the Win32API. I think the multimedia timers or others. But I don't know witch

Is the code correct?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Unit I2CLPT1
« Reply #5 on: May 03, 2013, 10:05:18 am »
A discussion about delays in PowerBasic:

http://www.powerbasic.com/support/pbforums/showthread.php?t=24962

As to whether your code is correct, why don't you try it to find out?

Stefke35

  • Member
  • ***
  • Posts: 10
Re: Unit I2CLPT1
« Reply #6 on: May 04, 2013, 03:33:39 am »
A discussion about delays in PowerBasic:

http://www.powerbasic.com/support/pbforums/showthread.php?t=24962

As to whether your code is correct, why don't you try it to find out?

Jan,

Can you please read functions for control the serialport or can I use your inpout32 library to control the serialport?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research