Example Program Arduino Code

#incluír <stdio.h>
#incluír <string.h>
#incluír <inttypes.h>
#incluír “Arduino.h
#incluír “XGZP6859D.h
//The initialiation of IIC BUS
void XGZP6859D::IIC_Init(vacío)
{
pinMode(IIC_SCL, OUTPUT); //Set the pin to output mode
pinMode(IIC_SDA, OUTPUT);
digitalWrite(IIC_SCL, HIGH); //Putout high level
digitalWrite(IIC_SDA, HIGH);
}
//Generate start signal
void XGZP6859D::IIC_Start(vacío)
{
pinMode(IIC_SDA, OUTPUT);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME); //Delay several microseconds
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SDA, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
}
//Generate stop signal
void XGZP6859D::IIC_Stop(vacío)
{
pinMode(IIC_SDA, OUTPUT);
digitalWrite(IIC_SDA, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
}
//Wait for the ACK signal
//return 1 means failure
//return 0 means success
byte XGZP6859D::IIC_Wait_Ack(vacío)
{
byte ucErrTime=0;
pinMode(IIC_SDA, INPUT);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
ka' jo'op(digitalRead(IIC_SDA)==HIGH)
{
ucErrTime++;
Wa(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
digitalWrite(IIC_SCL, LOW);
return 0;
}
//Generate ACK signal
void XGZP6859D::IIC_Ack(vacío)
{
digitalWrite(IIC_SCL, LOW);
pinMode(IIC_SDA, OUTPUT);
digitalWrite(IIC_SDA, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
}
//Not generate ACK signal
void XGZP6859D::IIC_NAck(vacío)
{
digitalWrite(IIC_SCL, LOW);
pinMode(IIC_SDA, OUTPUT);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
}
//Send one byte across IIC bus
//The return value means answer from the slave
//1 means receiving answer
//0 means not receiving answer
void XGZP6859D::IIC_Send_Byte(byte txd)
{
byte t;
pinMode(IIC_SDA, OUTPUT);
digitalWrite(IIC_SCL, LOW);
Utia'al(t=0;t<8;t++)
{
Wa(txd & 0x80)
digitalWrite(IIC_SDA, HIGH);
asab
digitalWrite(IIC_SDA, LOW);
txd<<=1;
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
}
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
}
//Read one byte, send ACK while ack = 1, send nACK while ack = 0
byte XGZP6859D::IIC_Read_Byte(byte ack)
{
byte i,receive=0;
pinMode(IIC_SDA, INPUT);
Utia'al(i=0;Teene'<8;I++ )
{
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
receive<<=1;
Wa(digitalRead(IIC_SDA)==HIGH)
receive++;
delayMicroseconds(IIC_DELAY_TIME);
}
digitalWrite(IIC_SDA, HIGH);
return receive;
}
//Write one byte to a specified address
void XGZP6859D::Write_One_Byte(u8 address ,u8 data)
{
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 0));
Serial.print(“Device_Address is “);
Serial.println(Device_Address + 0, HEX);
IIC_Wait_Ack();
IIC_Send_Byte(dirección);
IIC_Wait_Ack();
IIC_Send_Byte(data);
IIC_Wait_Ack();
IIC_Stop();
}
//Read one byte from a specified address
byte XGZP6859D::Read_One_Byte(u8 address)
{
byte mydata;
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 0));
IIC_Wait_Ack();
IIC_Send_Byte(dirección);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 1));
IIC_Wait_Ack();
mydata = IIC_Read_Byte(0);
IIC_Stop();
Suuto'ob MyData;
}
void XGZP6859D::XGZP6859D_get_cal() //The measuring action of XGZP6859D
{
byte i=0;
byte buff[5]; //Five byte variables, used to save the bytes read from XGZP6859D
unsigned long pressure_ad,temperature_ad; //Variables used to save the calibarted pressure and temperature
Write_One_Byte(0x 30, 0x0A); //Indicate a combined conversion
ka' jo'op ((Read_One_Byte(0x 30) & 0x08) == 0x80) //Juzgar wa recopilación datos ts'o'ok u Beetik
{
Serial.println(“XGZP6859D is not ready”);; //Wait while the data is not ready
delay(1000);
}
Utia'al(i=0;Teene'<5;I++) //Read ADC output Data of Pressure and temperature
buff[Teene'] = Read_One_Byte(0x06 + Teene');
Serial.print(“buff[0…4] is 0x”);
Serial.print(buff[0], HEX);
Serial.print(“, 0x”);
Serial.print(buff[1], HEX);
Serial.print(“, 0x”);
Serial.print(buff[2], HEX);
Serial.print(“, 0x”);
Serial.print(buff[3], HEX);
Serial.print(“, 0x”);
Serial.println(buff[4], HEX);
//Compute the value of pressure converted by ADC
pressure_ad = ((unsigned long)((unsigned long)(buff[0] * 65536)) + ((unsigned int)(buff[1] * 256)) + ((byte)buff[2]));
Serial.print(“pressure_ad is “);
Serial.println(pressure_ad);
Wa (pressure_ad > 8388608) //
{
pressure = (long)(((long)(pressure_ad – 16777216)) / 8192);
//Because the span is -500Pa~+500Pa, so k is 8192;k should be changed according to the actuan condition; the unit of pressure is Pa
}
asab
{
pressure = (long)(pressure_ad / 8192);
}
//Compute the value of temperature converted by ADC
temperature_ad = ((unsigned int)((unsigned int)(buff[3]<<8)) + ((byte)buff[4]));
Wa (temperature_ad > 32768)
{
temperature = (float)(((long)(temperature_ad – 65536)) / 256);
//The unit of temperature is Centigrade
}
asab
{
temperature = (float)(temperature_ad / 256);
}
}
XGZP6859D My_XGZP6859D; //Define a object named My_XGZP6859D of the class XGZP6859D
void setup()
{
Serial.begin(9600); //The initialization of Serial port
My_XGZP6859D.IIC_Init(); //Initialize the pins of IIC bus of My_XGZP6859D
}
void loop()
{
My_XGZP6859D.XGZP6859D_get_cal(); //Call the function XGZP6859D_get_cal() of object My_XGZP6859D to calculate the pressure and temperature
Serial.print(My_XGZP6859D.pressure);
Serial.println(” Pa”);
Serial.print(My_XGZP6859D.temperature);
Serial.println(” Centigrade”);
Serial.println(“”);
delay(1000);
}