更新日期 : 20181119
研究了一下 Up Board 的 GPIO 到底要怎麼使用
一般都教怎麼在Linux 環境下使用
這邊我就介紹一下在Windows 環境下怎麼使用
首先要先去下載 Hi-Safe
目前我手上有兩個版次
Hi-Save V2_20170314
Hi-Save V2_20171108 (上面路徑提供的版次)
一開始安裝的時候還怕怕的
從網路上莫名其妙的共享空間下載下來的安裝檔,是不是病毒阿
後來安裝完,軟體開起來後,看到了熟悉的AAEON 才安心
當初我跟研揚要GPIO on win10相關資料,竟然要我上網去爬文(!@#$%^...)想必是個純業務
找SDK時,上網去看,有些是丟私人的dropbox,去抓的時候早就失效了,
從範例中找到了關鍵字 aaeonEAPI.dll 進而找到 Hi-Safe 也是私人空間,
繞了一圈還是回到研揚...
壓縮檔中有幾項重要的東西
1. API (x64, x86)
2. FrameWork (安裝 msi 或 exe 其中一個都可以)
3. 研揚軟體(不含範例, 含微軟更新KB3033929,放在安裝路徑下)
一開始解壓縮還失敗,可能壓縮軟體版次太舊,所以我改用7zip就成功解壓縮了。
GPIO
接線
一開始我先接5V , GND
確認電路是正常,能讓LED亮,
確認正常後,在將原本5V接腳改接到PIN27
出廠設定 :
Output Pin27
Input Pin12 26 28 29 31 35 38 40
所以就直接拿27來測
開啟研揚的軟體 Hi-Safe
到DIO頁面,選到有Pin27那頁
直接設定為On,LED就亮了
設定為Off, LED就滅了
設定可以成功,程式應該也可以
打開API的資料夾
看到只有三個檔案 DLL, H, Lib
標準的C/C++語言
但這邊我是使用C#,還好網路上有人轉好的,不然就要自己轉了
開頭記得加 using System.Runtime.InteropServices;//for DllImport
加入DLL程式段:
[DllImport("aaeonEAPI.dll", EntryPoint = "EApiGPIOGetDirection")]
public static extern UInt32 EApiGPIOGetDirection(UInt32 Id, UInt32 Bitmask, ref UInt32 pDirection);
[DllImport("aaeonEAPI.dll", EntryPoint = "EApiGPIOSetDirection")]
public static extern UInt32 EApiGPIOSetDirection(UInt32 Id, UInt32 Bitmask, UInt32 Direction);
[DllImport("aaeonEAPI.dll", EntryPoint = "EApiGPIOGetLevel")]
public static extern UInt32 EApiGPIOGetLevel(UInt32 Id, UInt32 Bitmask, ref UInt32 pLevel);
[DllImport("aaeonEAPI.dll", EntryPoint = "EApiGPIOSetLevel")]
public static extern UInt32 EApiGPIOSetLevel(UInt32 Id, UInt32 Bitmask, UInt32 Level);
[DllImport("aaeonEAPI.dll", EntryPoint = "EApiGPIOGetCaps")]
public static extern UInt32 EApiGPIOGetCaps(UInt32 Id, ref UInt32 PinCount, ref UInt32 pDioDisable);
[DllImport("aaeonEAPI.dll", EntryPoint = "EApiLibInitialize")]
public static extern UInt32 EApiLibInitialize();
[DllImport("aaeonEAPI.dll", EntryPoint = "EApiLibUnInitialize")]
public static extern UInt32 EApiLibUnInitialize();
定義:
public const UInt32 EAPI_STATUS_INITIALIZED = 0xFFFFFFFE;
public const UInt32 EAPI_GPIO_LOW = 0;
public const UInt32 EAPI_GPIO_HIGH = 1;
public const UInt32 EAPI_GPIO_INPUT = 1;
public const UInt32 EAPI_GPIO_OUTPUT = 0;
再包過一層:
private int Create()
{
//使用API前需要先初始化
uint err = EApiLibInitialize();
if (err != EAPI_STATUS_INITIALIZED)
{
Status = "Init Library Fail.";
return (int)err;
}
return (int)err;
}
public void Dispose()
{
//程式結束時需要釋放
EApiLibUnInitialize();
}
/// <summary>
/// 將指定腳位設定為輸入
/// </summary>
/// <param name="pin"></param>
/// <returns></returns>
public int SetInputPin(int pin)//1~40
{
uint err;
pin = pin - 1;
err = EApiGPIOSetDirection((uint)pin, 0xFFFFFFFF, EAPI_GPIO_INPUT);
if (err > 0)
{
Status = "Error #" + err;
}
return (int)err;
}
/// <summary>
/// 將指定腳位設定為輸入
/// </summary>
/// <param name="pin"></param>
/// <returns></returns>
public int SetOutputPin(int pin)//1~40
{
uint err;
pin = pin - 1;
err = EApiGPIOSetDirection((uint)pin, 0xFFFFFFFF, EAPI_GPIO_OUTPUT);
if (err > 0)
{
Status = "Error #" + err;
}
return (int)err;
}
/// <summary>
/// 確認此腳位是否為輸入腳位
/// </summary>
/// <param name="pin">腳位號碼:1~40.</param>
public bool IsInputPin(int pin)
{
uint err;
pin = pin - 1;
uint direction = 0;
err = EApiGPIOGetDirection((uint)pin, 0xFFFFFFFF, ref direction);
if (err > 0)
{
Status = "Error #" + err;
return false;
}
return direction == EAPI_GPIO_INPUT;
}
/// <summary>
/// 確認此腳位是否為輸出腳位
/// </summary>
/// <param name="pin">腳位號碼:1~40.</param>
public bool IsOutputPin(int pin)
{
uint err;
pin = pin - 1;
uint direction = 0;
err = EApiGPIOGetDirection((uint)pin, 0xFFFFFFFF, ref direction);
if (err > 0)
{
Status = "Error #" + err;
return false;
}
return direction == EAPI_GPIO_OUTPUT;
}
/// <summary>
/// 設定此腳位輸出為ON(高電位)
/// </summary>
/// <param name="pin">腳位號碼:1~40.</param>
public void SetPinOn(int pin)
{
uint err;
pin = pin - 1;
err = EApiGPIOSetLevel((uint)pin, 0xFFFFFFFF, EAPI_GPIO_HIGH);
if (err > 0)
{
Status = "Error #" + err;
}
}
/// <summary>
/// 設定此腳位輸出為OFF(低電位)
/// </summary>
/// <param name="pin">腳位號碼:1~40.</param>
public void SetPinOff(int pin)
{
uint err;
pin = pin - 1;
err = EApiGPIOSetLevel((uint)pin, 0xFFFFFFFF, EAPI_GPIO_LOW);
if (err > 0)
{
Status = "Error #" + err;
}
}
/// <summary>
/// 判斷此腳位是否為ON(高電位)
/// </summary>
/// <param name="pin">腳位號碼:1~40.</param>
public bool IsPinOn(int pin)
{
uint err;
pin = pin - 1;
uint state = 0;
err = EApiGPIOGetLevel((uint)pin, 0xFFFFFFFF, ref state);
if (err > 0)
{
Status = "Error #" + err;
return false;
}
return state == EAPI_GPIO_HIGH;
}
/// <summary>
/// 判斷此腳位是否為OFF(低電位)
/// </summary>
/// <param name="pin">腳位號碼:1~40.</param>
public bool IsPinOff(int pin)
{
uint err;
pin = pin - 1;
uint state = 0;
err = EApiGPIOGetLevel((uint)pin, 0xFFFFFFFF, ref state);
if (err > 0)
{
Status = "Error #" + err;
return false;
}
return state == EAPI_GPIO_LOW;
}
最後最後,要注意的是,要將DLL丟到執行檔的路徑下
雖然我的環境是64bit,但我習慣編譯成Any CPU
因此DLL我選擇的是x86的
留言列表