Example Program Arduino Code
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include “Arduino.h”
#include “XGZP6859D.h”
//The initialiation of IIC BUS
void XGZP6859D::IIC_Init(void)
{
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(void)
{
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(void)
{
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(void)
{
byte ucErrTime=0;
pinMode(IIC_SDA, INPUT);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
while(digitalRead(IIC_SDA)==HIGH)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
digitalWrite(IIC_SCL, LOW);
return 0;
}
//Generate ACK signal
void XGZP6859D::IIC_Ack(void)
{
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(void)
{
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);
for(t=0;t<8;t++)
{
if(txd & 0x80)
digitalWrite(IIC_SDA, HIGH);
else
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);
for(i=0;i<8;i++ )
{
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
receive<<=1;
if(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(address);
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(address);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 1));
IIC_Wait_Ack();
mydata = IIC_Read_Byte(0);
IIC_Stop();
return 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(0x30, 0x0A); //Indicate a combined conversion
while ((Read_One_Byte(0x30) & 0x08) == 0x80) //Judge whether Data collection is over
{
Serial.println(“XGZP6859D is not ready”);; //Wait while the data is not ready
delay(1000);
}
for(i=0;i<5;i++) //Read ADC output Data of Pressure and temperature
buff[i] = Read_One_Byte(0x06 + i);
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);
if (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
}
else
{
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]));
if (temperature_ad > 32768)
{
temperature = (float)(((long)(temperature_ad – 65536)) / 256);
//The unit of temperature is Centigrade
}
else
{
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);
}