透過電腦的Parallel Port與I2C裝置通訊時,需使用到Data0(SCL)來負責產生與I2C所需時脈、Data1(SDA)來負責資料的傳送、STATUS3(SDA)為資料的接收及Control2(WP)作為防寫開關的啟閉。
由於電腦輸出端的訊號電壓較低,因此需透過外部電路把Logic準位拉至標準電壓(5V或3.3V),可以參考下面這張圖:
(另外可參考7405的使用 http://www.maximintegrated.com/app-notes/index.mvp/id/3230)
要特別注意的是,從Parallel Port送出訊號經過轉換電路後,裝置接收到的訊號為反向,因此需將發送的資料反向,裝置才會得到正確訊號。
利用C#來撰寫程式
//ParallelPort.cs
using System; using System.Runtime.InteropServices; namespace System { public class PortAccess {
[DllImport("inpout32.dll", EntryPoint = "Inp32")] //Reading a Pin (Status Register) public static extern int Input(int adress); [DllImport("inpout32.dll", EntryPoint = "Out32")] //Writing to a Pin (Data Register) public static extern void Output(int adress, int value); } }
I2C.cs
Output(0x378,0x00); //SCL=0;SDA=0
Output(0x378,0x01); //SCL=1
Output(0x378,0x02); //SDA=1
Input(0x379);//讀取SDA
//發送SCL及SDA需反向
Output(0x378,~0x00);
|
利用C++來撰寫程式
//ParallelPort.cpp
#include "stdafx.h" #include <iostream> #include <windows.h>
typedef UINT (CALLBACK* INP32)(DWORD); typedef UINT (CALLBACK* OUT32)(DWORD,UINT);
HINSTANCE hDLL; // Handle to DLL
static OUT32 Out32 = NULL; static INP32 Inp32 = NULL;
void ParallelPortInit(void) { hDLL=LoadLibrary(L"inpout32.dll"); if (hDLL != NULL) { Inp32 = (INP32)GetProcAddress(hDLL, "Inp32"); if (!Inp32) // handle the error { FreeLibrary(hDLL); printf("Cannot Find Function Inp32"); while(1); }
Out32 = (OUT32)GetProcAddress(hDLL, "Out32"); if (!Out32) // handle the error { FreeLibrary(hDLL); printf("Cannot Find Function Out32"); while(1); }
} }
void ParallelPortUnload(void) { FreeLibrary(hDLL); //解除DLL }
Out32(0x378,0x00); //SCL=0;SDA=0
Out32(0x378,0x01); //SCL=1
Out32(0x378,0x02); //SDA=1
Inp32(0x379);//讀取SDA
//發送SCL及SDA需反向
Out32(0x378,~0x00); |
參考工具
SoftCollection Parallel Port Viewer
留言列表