📄 win_iic.cpp
字号:
#include "stdafx.h"
#include "win_iic.h"
#define SCL_OUT 0x01
#define SDA_OUT 0x02
#define SDA_IN 0x20
#define LPTBASE 0x378
UINT8 PortData = 0;
/* pin drivers */
void _SetSDAHigh2wb(void)
{
PortData |= SDA_OUT;
SetPortVal(LPTBASE , PortData, 1);
}
void _SetSDALow2wb(void)
{
PortData &= ~SDA_OUT;
SetPortVal(LPTBASE , PortData, 1);
}
void _SetSCLHigh2wb(void)
{
PortData |= SCL_OUT;
SetPortVal(LPTBASE , PortData, 1);
}
void _SetSCLLow2wb(void)
{
PortData &= ~SCL_OUT;
SetPortVal(LPTBASE , PortData, 1);
}
/* read the SDA pin */
bool _GetSDA2wb(void)
{
DWORD dwPortVal;
unsigned int temp_aa;
GetPortVal(LPTBASE+1, &dwPortVal, 1);
temp_aa = (unsigned int)dwPortVal;
if(temp_aa & SDA_IN)
return true;
else
return false;
}
void Delay(void)
{
UINT32 i;
for(i=0;i<5000;i++);
}
bool _GetAck2wb(void)
{
bool status;
_SetSDAHigh2wb();
_SetSCLHigh2wb();
Delay();
/*Acknowledge is SDA pulled low by slave*/
status = _GetSDA2wb();
_SetSCLLow2wb();
if(status)
return FALSE;
else
return TRUE;
}
void _Free2wb(void)
{
_SetSCLLow2wb(); /* set the clock low first */
_SetSDALow2wb(); /* then the data, if not already */
_SetSCLHigh2wb(); /* now put the clock high */
Delay();
_SetSDAHigh2wb(); /* and now the data high, which is the stop condition */
}
void _SendStar2wb(void)
{
_SetSDAHigh2wb();
_SetSCLHigh2wb();
Delay();
_SetSDALow2wb();
Delay();
_SetSCLLow2wb();
}
void _SendStop2wb(void)
{
_SetSDALow2wb();
_SetSCLHigh2wb();
Delay();
_SetSDAHigh2wb();
Delay();
}
void _WriteByte2wb(UINT8 opByte)
{
short bitNo;
//printf("W: 0x%x ",opByte);
for (bitNo=7;bitNo>=0;bitNo--)
{
if (opByte & 0x80)
_SetSDAHigh2wb();
else
_SetSDALow2wb();
opByte<<=1;
_SetSCLHigh2wb();
Delay();
_SetSCLLow2wb();
Delay();
}
}
void _ReadByte2wb(UINT8 *ipByte)
{
short bitNo;
register UINT8 byte;
byte=0;
_SetSDAHigh2wb();
for (bitNo=7; bitNo>=0; bitNo--)
{
byte<<=1;
_SetSCLHigh2wb();
Delay();
if (_GetSDA2wb()) byte++;
_SetSCLLow2wb();
}
//printf("R: 0x%x ",byte);
*ipByte=byte;
}
UINT _Read2wb(UINT8 deviceId, UINT8 *subAddr, UINT32 addrLen, UINT8 *buffer, UINT32 noOfBytes)
{
UINT32 i;
printf("\n_Read2wb2!deviceId= 0x%x, addrLen= %d, noOfBytes= %d ", deviceId, addrLen, noOfBytes);
if(addrLen > 0)
{
_SendStar2wb();
/* Write the device ID */
_WriteByte2wb((UINT8)(deviceId & 0xFE));
/* get slave acknowledgement*/
if (_GetAck2wb() != true)
{
printf("\n_Read2wb():Write ID Ack Err!");
//return 0xff;
}
/*Write the sub address*/
for(i = 0; i<addrLen; i++)
{
_WriteByte2wb(subAddr[i]);
if (_GetAck2wb()!= true)
{
printf("\n_Read2wb():Write SubAddr Ack Err!");
//return 0xff;
}
}
_SendStop2wb();
}
/* now read the data bytes */
_SendStar2wb();
/* Write the device ID */
_WriteByte2wb((UINT8)(deviceId | 0x01));/* set LSB to 1 for read*/
/* get slave acknowledgement*/
if (_GetAck2wb() != true)
{
printf("\nR2 ID Ack Err!");
//return 0xff;
}
for (i = 0; i < noOfBytes; i++)
{
_ReadByte2wb(&(buffer[i]));
//printf("\_ReadByte2wb: 0x%x ", buffer[i]);
if (i < (noOfBytes - 1))
{
/* Send ACK after a byte read*/
_SetSDALow2wb();
_SetSCLHigh2wb();
Delay();
_SetSCLLow2wb();
_SetSDAHigh2wb();
}
else
{
/* Send NAK after the last byte read */
_SetSDAHigh2wb();
_SetSCLHigh2wb();
Delay();
_SetSCLLow2wb();
}
}
_SendStop2wb();
return SUCCESS;
}
UINT _Write2wb(UINT8 deviceId, UINT8 *subAddr, UINT32 addrLen, UINT8 *buffer, UINT32 noOfBytes)
{
UINT32 i;
printf("\n_Write2wb!deviceId= 0x%x, addrLen= %d, noOfBytes= %d ",
deviceId, addrLen, noOfBytes);
if(addrLen > 0)
{
_SendStar2wb();
/* Write the device ID */
_WriteByte2wb((UINT8) (deviceId & 0xFE));/* set b0 to 0 for writes*/
if (_GetAck2wb() != true)
{
printf("\n_Write2wb():write ID Ack Err!");
//return 0xff;
}
for (i = 0; i < addrLen; i++)
{
_WriteByte2wb(subAddr[i]);
if (_GetAck2wb() != true)
{
printf("\n_Write2wb():write SubAddr Ack Err!");
//return 0xff;
}
}
_SendStop2wb();
}
/* now write the data bytes */
_SendStar2wb();
/* Write the device ID */
_WriteByte2wb((UINT8) (deviceId & 0xFE));/* set b0 to 0 for writes*/
if (_GetAck2wb() != true)
{
printf("\n_Write2wb():Write ID Ack Err!");
//return 0xff;
}
for (i = 0; i < noOfBytes; i++)
{
_WriteByte2wb(buffer[i]);
if (_GetAck2wb() != true)
{
printf("\n_Write2wb():write Data Ack Err!");
//return 0xff;
}
}
_SendStop2wb();
return SUCCESS;
}
void IIC_Test()
{
#if 1
_SetSDALow2wb();
printf("\nSDA = %d ",_GetSDA2wb());
printf("\nSDA = %d ",_GetSDA2wb());
Sleep(100);
_SetSDAHigh2wb();
printf("\nSDA = %d ",_GetSDA2wb());
printf("\nSDA = %d ",_GetSDA2wb());
Sleep(100);
_SetSDALow2wb();
printf("\nSDA = %d ",_GetSDA2wb());
printf("\nSDA = %d ",_GetSDA2wb());
Sleep(100);
#else
_Write2wb(0xc9,0,0,0,0);
#endif
}
void AT153_Test()
{
UINT8 str[16];
UINT8 ch = 0;
UINT i;
_Read2wb(0xbd,&ch,1,str,16);
for(i=0;i<16;i++)
printf("\nstr[%d] = 0x%x ", i, str[i]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -