IIC Sample Code

#include “IIC.h”

//————-delay time_us——————————-
void DELAY(u16 t)
{
while (t != 0)
t–;
}

/*void IIC_Start(void) */
{
SDA = 1;
SCL = 1;
DELAY(DELAY_TIME);
SDA = 0;
DELAY(DELAY_TIME);
SCL = 0;
}

/* void IIC_Stop(void) */
{
SCL = 0;
SDA = 0;
DELAY(DELAY_TIME);
SCL = 1;
DELAY(DELAY_TIME);
SDA = 1;
}

/*
unsigned char IIC_Wait_Ack(void) */
{
u8 rec = 0;

DELAY(DELAY_TIME); //
SDA = 1; //
SCL = 1;
DELAY(DELAY_TIME);
rec = SDA;
SCL = 0; //
DELAY(DELAY_TIME);

return rec;
}

/* void IIC_Ack(void) */
{
SCL = 0;
SDA = 0;
DELAY(DELAY_TIME);
SCL = 1;
DELAY(DELAY_TIME);
SCL = 0;
DELAY(DELAY_TIME);
}

/* void IIC_NAck(void) */
{
SCL = 0;
SDA = 1;
DELAY(DELAY_TIME);
SCL = 1;
DELAY(DELAY_TIME);
SCL = 0;
DELAY(DELAY_TIME);
}


/*
void IIC_Send_Byte(u8 txd) */
{
u8 i;

for(i = 0; i < 8; i++)
{
SDA = (txd & 0x80) >> 7; //
txd <<= 1;
DELAY(DELAY_TIME);
SCL = 1;
DELAY(DELAY_TIME);
SCL = 0;
DELAY(DELAY_TIME);
}
}

/*
u8 IIC_Read_Byte(void)*/
{
u8 i, rec = 0;

SDA = 1; //
for(i = 0; i < 8; i++)
{
SCL = 0;
DELAY(DELAY_TIME);
SCL = 1;
rec <<= 1;
if(SDA) rec |= 1; //
DELAY(DELAY_TIME);
}

return rec;
}
#include “XGZP6897D.h”

//u8 temp_a5;

//————-write One Byte of Data,Data from MASTER to the SLAVER ——————————

//————-SLAVER address bit:01101101——————————-
void Write_One_Byte(u8 addr, u8 thedata) //Write “thedata” to the SLAVER’s address of “addr”

{
IIC_Start(); //IIC START
IIC_Send_Byte(0xDA); //IIC WRITE operation,SLAVER address

IIC_Wait_Ack();
IIC_Send_Byte(addr); /*address*/
IIC_Wait_Ack();
IIC_Send_Byte(thedata); /*thedata*/
IIC_Wait_Ack();
IIC_Stop(); //IIC STOP
}

 


//————-Reaed One Byte of Data,Data from SLAVER to the MASTER —————————-
u8 Read_One_Byte(u8 addr)
{
u8 mydata;
IIC_Start();
IIC_Send_Byte(0xDA);
IIC_Wait_Ack();
IIC_Send_Byte(addr);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0xDB); //IIC READ operation
IIC_Wait_Ack();
mydata = IIC_Read_Byte();
IIC_NAck(); //
IIC_Stop();
return mydata;
}

 

 


//
void XGZP_Start_Conversion()
{

Write_One_Byte(0x30, 0x0A); //indicate a combined conversion
while ((Read_One_Byte(0x30) & 0x08) > 0);

}

 

//————-Write n Bytes of Data—————————-
u8 software_i2c_Write_nByte(u8 SlaveAddress, u8 REG_Address, u8 len, u8 *buf)
{
int i;

//
IIC_Start();

//
IIC_Send_Byte(SlaveAddress);
IIC_Wait_Ack();

//
IIC_Send_Byte(REG_Address);
IIC_Wait_Ack();

//
for (i = 0; i < len; i++) {
IIC_Send_Byte(buf[i]);
if (IIC_Wait_Ack() == FAILED) {
IIC_Stop();
return FAILED;
}
}

//
IIC_Stop();
return SUCCESS;
}

//————-Read n Bytes of Data—————————-
u8 software_i2c_Read_nByte(u8 SlaveAddress, u8 REG_Address, u8 len, u8 *buf)
{
//
IIC_Start();

//
IIC_Send_Byte(SlaveAddress);
IIC_Wait_Ack();

//
IIC_Send_Byte(REG_Address);
IIC_Wait_Ack();

//
IIC_Start();

//
IIC_Send_Byte(SlaveAddress + 1);
IIC_Wait_Ack();

//
while (len) {
//
*buf = IIC_Read_Byte();
//
if (len == 1)
IIC_NAck();
//
else
IIC_Ack();
//
buf++;
//
len–;
}

//
IIC_Stop();

return SUCCESS;
}

//————-Read Three Bytes of Pressure Data—————————-
u8 Read_Pressure_ThreeBytes(u8 *pressure_H, u8 *pressure_M, u8 *pressure_L)
{
u8 buffer[3];
u8 result;

result = software_i2c_Read_nByte(0xDA, 0x06, 3, buffer); //

if(result == SUCCESS) {
*pressure_H = buffer[0]; //
*pressure_M = buffer[1]; //
*pressure_L = buffer[2]; //
return SUCCESS;
} else {
return FAILED;
}
}

void main()
{
SensorData *p_sensor_data = &sensor_data; //
p_sensor_data->pressure_k = 8192;

LCD1602_Init(); //
LCD1602_show_string(3, 0, “Pressure”); //

while(1)
{
Write_One_Byte(0x30, 0x0A); //indicate a combined conversion
// while ((Read_One_Byte(0x30) & 0x08) > 0);
Delay_ms(10);

//
if(Read_Pressure_ThreeBytes(&p_sensor_data->pressure_H,
&p_sensor_data->pressure_M,
&p_sensor_data->pressure_L) == SUCCESS)
{
//
p_sensor_data->pressure_AD = (unsigned long)((((unsigned long)p_sensor_data->pressure_H) << 16) |
(((unsigned int)p_sensor_data->pressure_M) << 8) |
((unsigned char)p_sensor_data->pressure_L));

//
p_sensor_data->pressure = (p_sensor_data->pressure_AD > 8388608) ?
(double)((p_sensor_data->pressure_AD – 16777216) / (double)(p_sensor_data->pressure_k)) :
(double)(p_sensor_data->pressure_AD / (double)(p_sensor_data->pressure_k));

//
if (is_zero_point_set)
{
zero_point_pressure = p_sensor_data->pressure; //
is_zero_point_set = 0; //
}

p_sensor_data->pressure -= zero_point_pressure;

//
if (p_sensor_data->pressure > 1000.0) {
//
p_sensor_data->pressure /= 1000.0;
float_to_str(p_sensor_data->P_buffer, p_sensor_data->pressure, 2, ” kPa”);
} else {
float_to_str(p_sensor_data->P_buffer, p_sensor_data->pressure, 2, ” Pa”);
}

LCD1602_show_string(3, 1, p_sensor_data->P_buffer);
}
else
{
//
LCD1602_show_string(3, 1, “READ ERROR”);
}

Delay_ms(200); //
}
}