www.dfi .com
75
Chapter 5 Digital I/O Programming Guide
Chapter 5
Function Description
I2CWriteByte(SlaveAddr, SubAddr, Data):
Write a Byte data to a specified I2C Device.
I2CReadByte(SlaveAddr, SubAddr, *Data):
Read a Byte data from a specified I2C Device.
SetBit(*Data, Bit) :
Set Data bit n as “1”.
ClrBit(*Data, Bit) :
Set Data bit n as “0”.
GetBit(Data, Bit) :
Return the value of data bit n.
Sample Code
GPIO Configuration
#defi ne SLAVE_ADDR 0x42
#defi ne INPUT_PORT 0x00
#defi ne OUTPUT_PORT 0x01
#defi ne INVERSION_PORT 0x02
#defi ne COMFIG_PORT 0x03
GpioConfi g(int PinNum, int Mode)
{
BYTE
Data;
BYTE TempPinNum = PinNum%8;
//Pin0-7 Input/Output Confi guration
I2C_ReadByte(SLAVE_ADDR, CONFIG_PORT, &Data);
if(Mode == 1){SetBit(&Data, TempPinNum);} //Input
else {ClrBit(&Data, TempPinNum);} //Output
I2C_WriteByte(SLAVE_ADDR, CONFIG_PORT, Data);
return 1;
GPIO Output Process
#defi ne SLAVE_ADDR 0x42
#defi ne INPUT_PORT 0x00
#defi ne OUTPUT_PORT 0x01
#defi ne INVERSION_PORT 0x02
#defi ne COMFIG_PORT 0x03
GpioOut(int PinNum, int Level)
{
BYTE
Data;
BYTE TempPinNum = PinNum%8;
//Pin0-7
I2C_ReadByte(SLAVE_ADDR, OUTPUT_PORT, &Data);
if(Level == 0){ClrBit(&Data, TempPinNum);}
else {SetBit(&Data, TempPinNum);}
I2C_WriteByte(SLAVE_ADDR, OUTPUT_PORT, Data);
return 1;
GPIO Iutput Process
#defi ne SLAVE_ADDR 0x42
#defi ne INPUT_PORT 0x00
#defi ne OUTPUT_PORT 0x01
#defi ne INVERSION_PORT 0x02
#defi ne COMFIG_PORT 0x03
GpioIn(int PinNum, int *Status)
{
BYTE
Data;
BYTE Group = PinNum/8;
BYTE TempPinNum = PinNum%8;
//Pin0-7
I2C_ReadByte(SLAVE_ADDR, INPUT_PORT, &Data);
*Status = GetBit(Data, TempPinNum);
return 1;