如何用C#读写DI/DO?
各位大佬,萌新手头只有这个DIO.C文件代码,怎么搞?
//***************************************************************//
//DIO sample code for KEEX-1750 //
//Please compile with Turbo C 3.0 to utilized the program //
// //
//DIO GPIO pin define from BayTrail chipset //
// DIO_0 : SOC S5_GPIO4 DIO_4 : SOC S5_GPIO22 //
// DIO_1 : SOC S5_GPIO5 DIO_5 : SOC S5_GPIO23 //
// DIO_2 : SOC S5_GPIO6 DIO_6 : SOC S5_GPIO24 //
// DIO_3 : SOC S5_GPIO7 DIO_7 : SOC S5_GPIO25 //
// //
//***************************************************************//
#include <stdio.h>
#include <conio.h>
#include <dos.h>
typedef unsigned long DWORD;
#define GPIO_BASE 0x500
#define GPIO1_SEL 0x84
#define GPIO1_LVL 0x88
#define SET_IO32(IoAddr, dSet) outpd(IoAddr, inpd(IoAddr) | (dSet))
#define RESET_IO32(IoAddr, Rst) outpd(IoAddr, inpd(IoAddr) &~ (Rst))
#define READ_IO32(IoAddr) inpd(IoAddr)
DWORD inpd(int portid)
{
DWORD dwRet;
asm mov dx, portid;
asm lea bx, dwRet;
__emit__(
0x66, 0x50, // push EAX
0x66, 0xED, // in EAX,DX
0x66, 0x89, 0x07, // mov [BX],EAX
0x66, 0x58
); // pop EAX
return dwRet;
}
void outpd(int portid, DWORD dwVal)
{
asm mov dx, portid;
asm lea bx, dwVal;
__emit__(
0x66, 0x50, // push EAX
0x66, 0x8B, 0x07, // mov EAX,[BX]
0x66, 0xEF, // out DX,EAX
0x66, 0x58
); // pop EAX
}
int main()
{
int gDioGpio[] = { 4, 5, 6, 7, 22, 23, 24, 25 };
DWORD gGpioLvValue = 0;
int i = 0;
//Set DIO_0~7 as Input
//1:input 0:output
for (i = 0; i<8; ++i)
SET_IO32(GPIO_BASE + GPIO1_SEL, 0x01 << gDioGpio[i]);
//Read DIO_0~7 value
printf("Read DIO_0~7 value\n");
for (i = 0; i< 8; i++)
{
gGpioLvValue = READ_IO32(GPIO_BASE + GPIO1_LVL);
gGpioLvValue &= (0x01 << gDioGpio[i]);
printf("DIO_%d = %d\n", i, gGpioLvValue > 0);
}
system("pause");
//Set DIO_0~7 as Output
//0:input 1:output
for (i = 0; i<8; ++i)
{
RESET_IO32(GPIO_BASE + GPIO1_SEL, 0x01 << gDioGpio[i]);
}
//set DIO_0~7 to High
printf("Set DIO_0~7 to High\n");
outpd(GPIO_BASE + GPIO1_LVL, 0xFFFFFFFF);
system("pause");
//set DIO_0~7 to Low
printf("Set DIO_0~7 to Low\n");
outpd(GPIO_BASE + GPIO1_LVL, 0x0);
system("pause");
return 0;
}