72
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
#defi ne INPUT_PORT
#defi ne OUTPUT_PORT
#defi ne INVERSION_PORT
#defi ne COMFIG_PORT
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
#defi ne INPUT_PORT
#defi ne OUTPUT_PORT
#defi ne INVERSION_PORT
#defi ne COMFIG_PORT
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
#defi ne INPUT_PORT
#defi ne OUTPUT_PORT
#defi ne INVERSION_PORT
#defi ne COMFIG_PORT
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;
0x42
0x00
0x01
0x02
0x03
0x42
0x00
0x01
0x02
0x03
0x42
0x00
0x01
0x02
0x03