📄 iic.h
字号:
#include "common.h"
#include <winioctl.h>
int i;
CHAR op1=1;
// CHAR op;
ULONG addr;
ULONG size=1;
ULONG data;
HANDLE hPortIo;
// CHAR buffer[128];
BOOL status;
ULONG nBytesReturned;
ULONG PortHandle;
PORTOPENPARAMS OpenPort;
PORTREADPARAMS ReadPort;
PORTWRITEPARAMS WritePort;
PORTCLOSEPARAMS ClosePort;
// -------------------------------------------------------------------------
//
// Main
//
//void test(int Band,int StartCode);
void _Outport (unsigned short nAddr , int nData);
int _Inport (int nAddr);
void _Openport (int addr)
{
OpenPort.open_Address = addr;
OpenPort.open_PortSize = 1;
OpenPort.open_PortCount = 1;
status = DeviceIoControl(
hPortIo,
IOCTL_PORTIO_OPENPORT,
&OpenPort,
sizeof(OpenPort),
&PortHandle,
sizeof(PortHandle),
&nBytesReturned,
NULL
);
if ( !status || (PortHandle == NULL) )
{ ::MessageBox(NULL,"不能打开端口","",MB_OK);
exit(0);
}
}
void _Outport (unsigned short nAddr, int nData)
{
addr = nAddr;
data = nData;
OpenPort.open_Address = addr;
OpenPort.open_PortSize = 1;
OpenPort.open_PortCount = 1;
status = DeviceIoControl(
hPortIo,
IOCTL_PORTIO_OPENPORT,
&OpenPort,
sizeof(OpenPort),
&PortHandle,
sizeof(PortHandle),
&nBytesReturned,
NULL
);
if ( !status || (PortHandle == NULL) )
{ ::MessageBox(NULL,"不能打开端口","",MB_OK);
exit(0);
}
WritePort.wr_Handle = PortHandle;
WritePort.wr_Index = 0;
WritePort.wr_Data = data;
status = DeviceIoControl(
hPortIo,
IOCTL_PORTIO_WRITEPORT,
&WritePort,
sizeof(WritePort),
NULL,
0,
&nBytesReturned,
NULL
);
if ( !status )
{
::MessageBox(NULL,"写端口失败","",MB_OK);
exit(0);
}
}
int _Inport (int nAddr)
{
addr = nAddr;
OpenPort.open_Address = addr;
OpenPort.open_PortSize = 1;
OpenPort.open_PortCount = 1;
status = DeviceIoControl(
hPortIo,
IOCTL_PORTIO_OPENPORT,
&OpenPort,
sizeof(OpenPort),
&PortHandle,
sizeof(PortHandle),
&nBytesReturned,
NULL
);
if ( !status || (PortHandle == NULL) )
{ ::MessageBox(NULL,"不能打开端口","",MB_OK);
exit(0);
}
ReadPort.rd_Handle = PortHandle;
ReadPort.rd_Index = 0;
status = DeviceIoControl(
hPortIo,
IOCTL_PORTIO_READPORT,
&ReadPort,
sizeof(ReadPort),
&data,
sizeof(data),
&nBytesReturned,
NULL
);
if ( !status || (PortHandle == NULL) )
{ ::MessageBox(NULL,"读端口失败","",MB_OK);
exit(0);
}
return (data);
}
//#include <conio.h>
//#define Port_DO0 0x378
//#define Port_DO1 0x301
//#define Port_DI0 0x302
//#define Port_DI1 0x303
__int8 wPortState;
bool bDelay;
//bool FALSE;
//bool TRUE;
double Volt_Set[7],Volt_Read[7];
double Current_Read[7];
void __cdecl usTime_Delay(unsigned long usTime);
void __cdecl Out_Bit();
void __cdecl Set_Bit(int DBIT,int D01);
void __cdecl IIC_Start(void);
void __cdecl IIC_Stop(void);
void __cdecl IIC_Clock(void);
void __cdecl IIC_Write(int DATA);
int __cdecl IIC_Read(int InBit);
void __cdecl _Openport (int addr);
void __cdecl usTime_Delay(unsigned long usTime)
{
unsigned long II=0;
while(II<usTime*2)
II++;
//Sleep(msTime);
}
void __cdecl Out_Bit()
{
_Outport(0x37a,wPortState);
/* if(bDelay==FALSE)
usTime_Delay(1);
else
usTime_Delay(3);*/
}
void __cdecl Set_Bit(int DBIT,int D01)
{
int LLL;
wPortState=(wPortState & ((1<<DBIT)^0xff)) | (D01*(1<<DBIT));
if(D01==0)
bDelay=FALSE;//usTime_Delay(10);
else
bDelay=TRUE;//usTime_Delay(50);
LLL=~wPortState;
}
void __cdecl IIC_Start(void)
{
Set_Bit(0,1);
Out_Bit();
Set_Bit(1,1);
Out_Bit();
Set_Bit(0,0);
Out_Bit();
Set_Bit(1,0);
Out_Bit();
}
void __cdecl IIC_Stop(void)
{
Set_Bit(0,0);
Out_Bit();
Set_Bit(1,0);
Out_Bit();
Set_Bit(1,1);
Out_Bit();
Set_Bit(0,1);
Out_Bit();
Set_Bit(1,0);
Out_Bit();
// IIC_Clock();
}
void __cdecl IIC_Clock(void)
{
Set_Bit(1,0);
Out_Bit();
Set_Bit(1,1);
Out_Bit();
Set_Bit(1,0);
Out_Bit();
}
void __cdecl IIC_Write(int DATA)
{
int Bit=0,DBit=0,j;
for(int i=7;i>-1;i--)
{
Bit=(DATA>>i) & 0x1;
Set_Bit(0,Bit);
Out_Bit();
IIC_Clock();
DBit=DBit+(Bit<<i);
j=1;
}
Set_Bit(0,1);
Out_Bit();
IIC_Clock();
if(DBit==DATA)
;
else
j=0;
// Sleep(10);
}
int __cdecl IIC_Read(void)
{
int Bit=0;
__int16 InPort=0;
for(int i=7;i>-1;i--)
{
InPort=_Inport(0x379);
switch(i)
{
case 0:
Bit=Bit+(((InPort >> 5) & 0x1) << 0);
break;
case 1:
Bit=Bit+(((InPort >> 5) & 0x1) << 1);
break;
case 2:
Bit=Bit+(((InPort >> 5) & 0x1) << 2);
break;
case 3:
Bit=Bit+(((InPort >> 5) & 0x1) << 3);
break;
case 4:
Bit=Bit+(((InPort >> 5) & 0x1) << 4);
break;
case 5:
Bit=Bit+(((InPort >> 5) & 0x1) << 5);
break;
case 6:
Bit=Bit+(((InPort >> 5) & 0x1) << 6);
break;
case 7:
Bit=Bit+(((InPort >> 5) & 0x1) << 7);
break;
}
IIC_Clock();
usTime_Delay(1000);
}
Set_Bit(0,0);
Out_Bit();
IIC_Clock();
Set_Bit(0,1);
Out_Bit();
return Bit;
}
int IIC_Scan()
{ int Bit2=0;
for (int Bit1=0;Bit1<10;)
{
Bit2=_Inport(0x379);
Bit2=Bit2 & 0x20 ;
IIC_Stop();
if (Bit2==0)
{Bit1++;}
else
{Bit1=0;break;}
}
return Bit1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -