#include "../../include.h"
#if EXTERNAL_MODULE_SD2405_MODE != 0
void IIC_Init(void)
{
SETBIT(IIC_DDR, IIC_SCL); // Set SCL to output
SETBIT(IIC_DDR, IIC_SDA); // Set SDA to output
SETBIT(IIC_PORT, IIC_SCL); // Set SCL high
SETBIT(IIC_PORT, IIC_SDA); // Set SDA high
}
void IIC_Delay(void)
{
TARGET_Delayus(1, 0);
}
void IIC_Start( void )
{
SETBIT(IIC_PORT, IIC_SCL); // Set SCL High
SETBIT(IIC_PORT, IIC_SDA); // Set SDA High
SETBIT(IIC_DDR, IIC_SDA); // Set SDA to output
CLRBIT(IIC_PORT, IIC_SDA); // Clear SDA
}
void IIC_Stop( void )
{
SETBIT(IIC_DDR, IIC_SDA); // Set SDA to output
CLRBIT(IIC_PORT, IIC_SDA); // Clear SDA
CLRBIT(IIC_PORT, IIC_SCL); // Clear SCL
SETBIT(IIC_PORT, IIC_SCL); // Set SCL High
IIC_Delay();
SETBIT(IIC_PORT, IIC_SDA); // Set SDA High
}
void IIC_Clock( void )
{
SETBIT(IIC_PORT, IIC_SCL); // Set SCL high
IIC_Delay();
CLRBIT(IIC_PORT, IIC_SCL); // Set SCL low
}
uint8 IIC_Ackn( void )
{
uint8 Ackn = 0; // Temp RAM for Ackn flag
CLRBIT(IIC_PORT, IIC_SCL);
CLRBIT(IIC_DDR, IIC_SDA); // Set SDA to input
SETBIT(IIC_PORT, IIC_SCL); // Clock the ACK bit
if (CHKBIT(IIC_PIN, IIC_SDA)) Ackn = 1; // Check the ACK bit on SDA
CLRBIT(IIC_PORT, IIC_SCL); // Clear the clock
return Ackn; // Return our ACK bit
}
void IIC_WriteControl( uint8 u8_HardwareAddress, uint8 u8_ReadOrWrite )
{
// *** Send the I2C device Control code *** //
CLRBIT(IIC_PORT, IIC_SCL); // Clear SCL clock
if( (u8_HardwareAddress & 0x80) == 0x80) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
if( (u8_HardwareAddress & 0x40) == 0x40) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
if( (u8_HardwareAddress & 0x20) == 0x20) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
if( (u8_HardwareAddress & 0x10) == 0x10) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
// *** Send the I2C Control byte chip selects bits *** //
if( (u8_HardwareAddress & 0x08) == 0x08) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
if( (u8_HardwareAddress & 0x04) == 0x04) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
if( (u8_HardwareAddress & 0x02) == 0x02) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
// *** Set or Clear the read / write bit for I2C control *** //
if(u8_ReadOrWrite) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
IIC_Clock(); // Clock I2C bit
IIC_Ackn(); // Check for acknowledge from I2C device
}
void IIC_WriteByte( uint8 u8_Data )
{
uint8 i;
SETBIT(IIC_DDR, IIC_SDA); // Set SDA to output
CLRBIT(IIC_PORT, IIC_SCL);
for(i = 0; i < 8; i++) // Loop for our 8 bits
{
// Set or Clear SDA pin
if((u8_Data & 0x80) == 0x80) SETBIT(IIC_PORT, IIC_SDA); // Set I2C SDA PIN
else CLRBIT(IIC_PORT, IIC_SDA); // Clear I2C SDA PIN
SETBIT(IIC_PORT, IIC_SCL); // Set SCL High, Clock data
u8_Data = u8_Data << 1; // Shift data in buffer right one
CLRBIT(IIC_PORT, IIC_SCL); // Clear SCL
}
IIC_Ackn(); // Check for acknowledge from I2C device
}
uint8 IIC_ReadByte(void)
{
uint8 i,buff = 0;
for (i = 0; i < 8; i++)
{
buff = buff << 1;
CLRBIT(IIC_DDR, IIC_SDA); // Set SDA to input
SETBIT(IIC_PORT, IIC_SCL); // Set SCL High,Clock bit out
if (CHKBIT(IIC_PIN, IIC_SDA)) buff = buff | 0x01; // Read data on SDA pin
CLRBIT(IIC_PORT, IIC_SCL); // Clear SCL
}
return buff; // Return our I2C byte
}
void IIC_WriteOneByteAddressDevice(uint8 u8_HardwareAddress, uint8 u8_MemoryAddress, uint8 u8_Data)
{
uint16 c; // Temp Ram used for write delay
IIC_Start(); // Set I2C start condition
IIC_WriteControl(u8_HardwareAddress, 0); // Send the EEPROM control Byte
IIC_WriteByte(u8_MemoryAddress); // Send the EEPROM internal Address
IIC_WriteByte(u8_Data); // Send the EEPROM Data
IIC_Stop(); // Set I2C Stop condition
for(c = 0; c < 1000; c++); // Delay for EEPROM Write
}
uint8 IIC_ReadOneByteAddressDevice(uint8 u8_HardwareAddress, uint8 u8_MemoryAddress)
{
uint8 Temp; // Temp RAM for EEPROM Read
IIC_Start(); // Set I2C start condition
IIC_WriteControl(u8_HardwareAddress, 0); // Send the EEPROM control Byte
IIC_WriteByte(u8_MemoryAddress); // Send the EEPROM internal Address
IIC_Start(); // Set I2C start condition
IIC_WriteControl(u8_HardwareAddress, 1); // Send the EEPROM control Byte
Temp = IIC_ReadByte(); // Read data from EEPROM
IIC_Stop(); // Set I2C Stop condition
return Temp; // Return data from EEPROM
}
void IIC_WriteTwoOneByteAddressDevice(uint8 u8_HardwareAddress, uint16 u16_MemoryAddress, uint8 u8_Data)
{
uint16 c;
// Temp Ram used for write delay
uint8 u8_HighAddress;
uint8 u8_LowAddress;
u8_HighAddress = u16_MemoryAddress / 256;
u8_LowAddress = u16_MemoryAddress % 256;
IIC_Start(); // Set I2C start condition
IIC_WriteControl(u8_HardwareAddress, 0); // Send the EEPROM control Byte
IIC_WriteByte(u8_HighAddress);
IIC_WriteByte(u8_LowAddress); // Send the EEPROM internal Address
IIC_WriteByte(u8_Data); // Send the EEPROM Data
IIC_Stop(); // Set I2C Stop condition
for(c = 0; c < 1000; c++); // Delay for EEPROM Write
}
uint8 IIC_ReadTwoOneByteAddressDevice(uint8 u8_HardwareAddress, uint16 u16_MemoryAddress)
{
uint8 Temp; // Temp RAM for EEPROM Read
uint8 u8_HighAddress;
uint8 u8_LowAddress;
u8_HighAddress = u16_MemoryAddress / 256;
u8_LowAddress = u16_MemoryAddress % 256;
IIC_Start(); // Set I2C start condition
IIC_WriteControl(u8_HardwareAddress, 0); // Send the EEPROM control Byte
IIC_WriteByte(u8_HighAddress);
IIC_WriteByte(u8_LowAddress); // Send the EEPROM internal Address
IIC_Start(); // Set I2C start condition
IIC_WriteControl(u8_HighAddress, 1); // Send the EEPROM control Byte
Temp = IIC_ReadByte(); // Read data from EEPROM
IIC_Stop(); // Set I2C Stop condition
return Temp; // Return data from EEPROM
}
#endif
|