示例程序 Arduino 代码

#包括 <stdio.h>
#包括 <string.h>
#包括 <inttypes.h>
#包括 “Arduino.h”
#包括 “XGZP6859D.h”
//The initialiation of IIC BUS
void XGZP6859D::IIC_Init(空白)
{
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(空白)
{
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(空白)
{
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(空白)
{
byte ucErrTime=0;
pinMode(IIC_SDA, INPUT);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
尽管(digitalRead(IIC_SDA)==HIGH)
{
ucErrTime++;
如果(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
digitalWrite(IIC_SCL, LOW);
return 0;
}
//Generate ACK signal
void XGZP6859D::IIC_Ack(空白)
{
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(空白)
{
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);
为了(t=0;吨<8;t++)
{
如果(txd & 0x80)
digitalWrite(IIC_SDA, HIGH);
别的
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);
为了(i=0;一世<8;我++ )
{
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
receive<<=1;
如果(digitalRead(IIC_SDA)==HIGH)
receive++;
delayMicroseconds(IIC_DELAY_TIME);
}
digitalWrite(IIC_SDA, HIGH);
return receive;
}
//Write one byte to a specified address
void XGZP6859D::写一个字节(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(地址);
IIC_Wait_Ack();
IIC_Send_Byte(data);
IIC_Wait_Ack();
IIC_Stop();
}
//Read one byte from a specified address
byte XGZP6859D::读取一个字节(u8 address)
{
byte mydata;
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 0));
IIC_Wait_Ack();
IIC_Send_Byte(地址);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 1));
IIC_Wait_Ack();
mydata = IIC_Read_Byte(0);
IIC_Stop();
返回我的数据;
}
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
写一个字节(0x30, 0x0A); //Indicate a combined conversion
尽管 ((读取一个字节(0x30) & 0x08) == 0x80) //判断数据采集是否结束
{
Serial.println(“XGZP6859D is not ready”);; //Wait while the data is not ready
delay(1000);
}
为了(i=0;一世<5;我++) //Read ADC output Data of Pressure and temperature
buff[一世] = Read_One_Byte(0x06 + 一世);
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);
如果 (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
}
别的
{
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]));
如果 (temperature_ad > 32768)
{
temperature = (float)(((long)(temperature_ad – 65536)) / 256);
//The unit of temperature is Centigrade
}
别的
{
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);
}