Código ejemplo le CII

#incluír “IIC.h

//————-Retrasar time_us——————————-
nulo RETRASO(u16 t)
{
ka' jo'op (t != 0)
t–;
}

/*void IIC_Start(vacío) */
{
ASD = 1;
SCL = 1;
DEMORAR(DELAY_TIME);
ASD = 0;
DEMORAR(DELAY_TIME);
SCL = 0;
}

/* void IIC_Stop(vacío) */
{
SCL = 0;
ASD = 0;
DEMORAR(DELAY_TIME);
SCL = 1;
DEMORAR(DELAY_TIME);
ASD = 1;
}

/*
unsigned char IIC_Wait_Ack(vacío) */
{
u8 rec = 0;

DEMORAR(DELAY_TIME); //
ASD = 1; //
SCL = 1;
DEMORAR(DELAY_TIME);
rec = SDA;
SCL = 0; //
DEMORAR(DELAY_TIME);

return rec;
}

/* void IIC_Ack(vacío) */
{
SCL = 0;
ASD = 0;
DEMORAR(DELAY_TIME);
SCL = 1;
DEMORAR(DELAY_TIME);
SCL = 0;
DEMORAR(DELAY_TIME);
}

/* void IIC_NAck(vacío) */
{
SCL = 0;
ASD = 1;
DEMORAR(DELAY_TIME);
SCL = 1;
DEMORAR(DELAY_TIME);
SCL = 0;
DEMORAR(DELAY_TIME);
}


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

Utia'al(i = 0; Teene' < 8; I++)
{
ASD = (txd & 0x80) >> 7; //
txd <<= 1;
DEMORAR(DELAY_TIME);
SCL = 1;
DEMORAR(DELAY_TIME);
SCL = 0;
DEMORAR(DELAY_TIME);
}
}

/*
u8 IIC_Read_Byte(vacío)*/
{
u8 i, rec = 0;

ASD = 1; //
Utia'al(i = 0; Teene' < 8; I++)
{
SCL = 0;
DEMORAR(DELAY_TIME);
SCL = 1;
rec <<= 1;
Wa(SDA) rec |= 1; //
DEMORAR(DELAY_TIME);
}

return rec;
}
#incluír “XGZP6897D.h

//u8 temp_a5;

//————-Ts'íibtik jump'éel byte datos,Datos u MASTER ti' le SLAVER ——————————

//————-Bit de dirección SLAVER:01101101——————————-
Write_One_Byte nula(u8 addr, u8 thedata) //Tsíib “Le datos” yaan u dirección u SLAVER u “Addr”

{
IIC_Start(); //CHÚUNUL CII
IIC_Send_Byte(0xDA); //Operación IIC WRITE,SLAVER address

IIC_Wait_Ack();
IIC_Send_Byte(Addr); /*dirección * leti'
IIC_Wait_Ack();
IIC_Send_Byte(Le datos); /*Le datos * leti'
IIC_Wait_Ack();
IIC_Stop(); //PARADA CII
}


//————-Cosechado jump'éel byte datos,Datos u SLAVER ti' le 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); //Operación IIC READ
IIC_Wait_Ack();
mydata = IIC_Read_Byte();
IIC_NAck(); //
IIC_Stop();
Suuto'ob MyData;
}


//
void XGZP_Start_Conversion()
{

Write_One_Byte(0x 30, 0x0A); //Indicar jump'éel tzeltalo'obo' combinada
ka' jo'op ((Read_One_Byte(0x 30) & 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();

//
Utia'al (i = 0; Teene' < len; I++) {
IIC_Send_Byte(buf[Teene']);
Wa (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();

//
ka' jo'op (len) {
//
*buf = IIC_Read_Byte();
//
Wa (len == 1)
IIC_NAck();
//
asab
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); //

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

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

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

ka' jo'op(1)
{
Write_One_Byte(0x 30, 0x0A); //Indicar jump'éel tzeltalo'obo' combinada
// ka' jo'op ((Read_One_Byte(0x 30) & 0x08) > 0);
Delay_ms(10);

//
Wa(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));

//
Wa (is_zero_point_set)
{
zero_point_pressure = p_sensor_data->presión; //
is_zero_point_set = 0; //
}

p_sensor_data->pressure -= zero_point_pressure;

//
Wa (p_sensor_data->presión > 1000.0) {
//
p_sensor_data->pressure /= 1000.0;
float_to_str(p_sensor_data->P_buffer, p_sensor_data->presión, 2, ” kPa”);
} asab {
float_to_str(p_sensor_data->P_buffer, p_sensor_data->presión, 2, ” Pa”);
}

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

Delay_ms(200); //
}
}