Jump to content
Sign in to follow this  
Blinky

SPI Hardware Interface

Recommended Posts

Blinky

Hi everyone,

This is my special pet project.

It is an example of how u can use autoit to control external devices.

this script is made to comunicate with the MAX335 chip  using the SPI protocol via the LPT(printer) port

the beauty of the MAX335 chip is that the Clock, Data_In and the Chip_Select pins can be directly connected to the LPT port without any external components, and the 12V and 5V directly from an ATX PC power suply.

aditionaly i made a custom GUI with CommandFusion instaled on an Android Tablet that sends TCP commands to an Autoit TCP server that controls three dasy chained MAX335 chips that totals 24 independent NO switches

this script works perfectly for me

i just finished this project and i think i will make an UDF including more SPI devices

$DLLFileAndPath = @ScriptDir & "/inpout32.dll"

Global $335_device_number=3 ;number of daisy chained chips
Global $335_clock_bit=0 ;bit number for LPT pin 1 in the control Register of the LPT port(where i connected the CLK pin on te MAX335)
Global $335_cs_bit=4;bit number for LPT pin 6 in the data Register of the LPT port(where i connected the CS pin on te MAX335)
Global $335_data_in_bit=7;bit number for LPT pin 9 in the data Register of the LPT port(where i connected the DI pin on te MAX335)
Global $clock_delay=1;this limits the clock speed but it works fine with 0


;the ini file will be created and will keep the MAX335 pins statuses
For $i=0 To $335_device_number*8-1 Step 1
IniWrite("355_buffer.ini","present_data",$i,"0")
Next

set_max335_output(2,3,1) ; this will activate switch 2 on device 3 on the daisy chain

Func set_max335_output($output_no,$device_no,$status)
    $bit_no=($device_no-1)*8+$output_no-1 ; get the exact bit number of the switch
    IniWrite("355_buffer.ini","present_data",$bit_no,$status); whrite it to the buffer
;this part is where the SPI protocol begins
            set_control_bit($335_clock_bit,1); drop CLK
            set_data_bit($335_cs_bit,0) ;activate CS
            Sleep($clock_delay)
        For $i=$335_device_number*8-1 To 0 Step -1; start writing from buffer MostSignificantByte first
            $data=IniRead("355_buffer.ini","present_data",$i,"0")
            set_data_bit($335_data_in_bit,$data) ; set data bit value
            set_control_bit($335_clock_bit,0) ;raise CLK
            Sleep($clock_delay)
            set_control_bit($335_clock_bit,1); drop CLK
            Sleep($clock_delay)
        Next
            set_data_bit($335_cs_bit,1);deactivate CS
EndFunc



Func set_data_bit($bit,$stat=-1) ; it will write the value of the bit in the data reg of the LPT port
    $y= DllCall($DLLFileAndPath, "int", "Inp32", "int", "0x378")
    $bits=dec_to_bin($y[0])
    If $stat = -1 Then 
        If $bits[$bit]=0 Then
            $bits[$bit]=1
        Else
            $bits[$bit]=0
        EndIf
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x378", "int", $bcd)
            Return 1
    Else
        If $bits[$bit]<>$stat  Then
            $bits[$bit]=$stat
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x378", "int", $bcd)
            Return 1
        Else
            Return 2
        EndIf
    EndIf
EndFunc

Func set_control_bit($bit,$stat=-1); it will write the value of the bit in the control reg of the LPT port
    $y= DllCall($DLLFileAndPath, "int", "Inp32", "int", "0x37a")
    $bits=dec_to_bin($y[0])
    If $stat = -1 Then
        If $bits[$bit]=0 Then
            $bits[$bit]=1
        Else
            $bits[$bit]=0
        EndIf
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x37a", "int", $bcd)
            Return 1
    Else
        If $bits[$bit]<>$stat  Then
            $bits[$bit]=$stat
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x37a", "int", $bcd)
            Return 1
        Else
            Return 2
        EndIf
    EndIf
EndFunc

Func dec_to_bin($dec)
    Local $bit_array[8]
    If $dec > 255 Then SetError(1,1,-1)
    If $dec < 0 Then SetError(2,1,-1)
    For $i=7 To 0 Step -1
        If $dec >= 2^$i Then
            $bit_array[$i] = 1
            $dec=$dec-2^$i
        Else
            $bit_array[$i] = 0
        EndIf
    Next
    Return $bit_array
EndFunc

Func bin_to_dec($bit_array)
    If IsArray($bit_array) Then
        If UBound($bit_array) = 8 Then
            $dec=0
            For $i=7 To 0 Step -1
                $dec = $bit_array[$i]*(2^$i)+$dec
            Next
        Else
            SetError(2,1,-1)
        EndIf
    Else
        SetError(1,1,-1)
    EndIf
    Return $dec
EndFunc
Edited by Blinky
  • Like 1

Share this post


Link to post
Share on other sites
D00M4N

very interesting , thx for this code.

Share this post


Link to post
Share on other sites
nWLSA

thanks for this! looks like the most can be run in my program. using send bits is the important things in the real works.so we can build more auto machines!

but still hope autoit have its port api function groups may be can run it quickly.just like the LABVIEW. :zorro: 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • Miliardsto
      By Miliardsto
      Hello Im wondering if using this https://ohtejera.github.io/ImperiusAutoIt/#started
      UDF can i make that I can control my windows application with phone?
      like for example click button Start on android phone and then something would be done in my windows app?
    • mmoalem
      By mmoalem
      Hi all - was wondering if the following is possible:
      I have a motion detecting ccrv software on PC (icam or similar) - there is a bottom that turn surveillance (motion detection) on and off. I can get autoit to automate the clicking on the button. 
       
      I want my android phone to send a trigger every time it disconnect from the home network (i.e when I leave home) to tell autoit to turn on surveillance and when reconnecting to home network to trigger it to click off
       
      does any one have a suggestion how to go about achieving this?
    • francoiste
      By francoiste
      in AutoIt we have:
      _WinAPI_IsProcessorFeaturePresent($iFeature) this internally just calls what is provided by microsoft: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724482(v=vs.85).aspx
      unfortunately this is completely outdated as we can only check for features like MMX and up to a maximum of SSE2 or SSE3 - which have been introduced back in 2001 / 2004.
      but nowadays there is stuff like AVX, AVX2, AVX-512 ...
      for example see: https://en.wikipedia.org/wiki/Template:Multimedia_extensions
      well, for CPP we have the FeatureDetector (by Mysticial): https://github.com/Mysticial/FeatureDetector
      it calls __cpuid / __cpuidex from <intrin.h>.
      for additional details see: https://msdn.microsoft.com/en-us/library/hskdteyh.aspx
       
      has anyone tried to determine such SIMD features and related CPU capabilites with AutoIt?
    • ripdad
      By ripdad
      I have had several people ask for this, so I decided to work the algorithm for it and this is the result.
      What is it?
      A Gateway Proxy Sends and Receives Data Unmodified.
      https://en.wikipedia.org/wiki/Proxy_server
      What is it used for?
      You can use it as a gateway, relay or router between two known static IP addresses.
      More information is in the header of the script.
      Download: LocalGatewayProxy.au3
      You will need WSA_NBTCP.au3 from here:
      https://www.autoitscript.com/forum/topic/191954-wsa_nbtcp-v100-udf/
      If you have any questions or problems, let me know.
       
    • ripdad
      By ripdad

      This script is based on algorithm code from EnrMa.
      Updated: January 22, 2018
      Made improvements. Changes are in the script header.
      Known Issues: AutoIt x64 does not work properly with this script.
      Download: LocalProxyServer_v1.00.zip
       
×