Voorbeeld Programma Arduino Code
#erbij betrekken <stdio.h>
#erbij betrekken <string.h>
#erbij betrekken <inttypes.h>
#erbij betrekken “Arduino.h”
#erbij betrekken “XGZP6859D.h”
//The initialiation of IIC BUS
void XGZP6859D::IIC_Init(leegte)
{
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(leegte)
{
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(leegte)
{
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
//opbrengst 1 means failure
//opbrengst 0 means success
byte XGZP6859D::IIC_Wacht_Ack(leegte)
{
byte ucErrTime=0;
pinMode(IIC_SDA, INPUT);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
terwijl(digitalRead(IIC_SDA)==HIGH)
{
ucErrTime++;
indien(ucErrTime>250)
{
IIC_Stop();
opbrengst 1;
}
}
digitalWrite(IIC_SCL, LOW);
opbrengst 0;
}
//Generate ACK signal
void XGZP6859D::IIC_Ack(leegte)
{
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(leegte)
{
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);
voor(t=0;T<8;t++)
{
indien(txd & 0x80)
digitalWrite(IIC_SDA, HIGH);
anders
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);
voor(i=0;i<8;ik++ )
{
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
receive<<=1;
indien(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));
Serieel.afdrukken(“Device_Address is “);
Serial.println(Device_Address + 0, HEX);
IIC_Wacht_Ack();
IIC_Send_Byte(adres);
IIC_Wacht_Ack();
IIC_Send_Byte(data);
IIC_Wacht_Ack();
IIC_Stop();
}
//Read one byte from a specified address
byte XGZP6859D::Lees_Eén_Byte(u8 address)
{
byte mydata;
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 0));
IIC_Wacht_Ack();
IIC_Send_Byte(adres);
IIC_Wacht_Ack();
IIC_Start();
IIC_Send_Byte((byte)(Device_Address + 1));
IIC_Wacht_Ack();
mijndata = IIC_Read_Byte(0);
IIC_Stop();
mijngegevens retourneren;
}
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
terwijl ((Lees_Eén_Byte(0x30) & 0x08) == 0x80) //Beoordeel of het verzamelen van gegevens voltooid is
{
Serial.println(“XGZP6859D is not ready”);; //Wait while the data is not ready
vertraging(1000);
}
voor(i=0;i<5;ik++) //Read ADC output Data of Pressure and temperature
buff[i] = Read_One_Byte(0x06 + i);
Serieel.afdrukken(“buff[0…4] is 0x”);
Serieel.afdrukken(buff[0], HEX);
Serieel.afdrukken(“, 0X”);
Serieel.afdrukken(buff[1], HEX);
Serieel.afdrukken(“, 0X”);
Serieel.afdrukken(buff[2], HEX);
Serieel.afdrukken(“, 0X”);
Serieel.afdrukken(buff[3], HEX);
Serieel.afdrukken(“, 0X”);
Serial.println(buff[4], HEX);
//Compute the value of pressure converted by ADC
pressure_ad = ((lang niet ondertekend)((lang niet ondertekend)(buff[0] * 65536)) + ((niet ondertekend int)(buff[1] * 256)) + ((byte)buff[2]));
Serieel.afdrukken(“pressure_ad is “);
Serial.println(pressure_ad);
indien (pressure_ad > 8388608) //
{
druk = (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
}
anders
{
druk = (long)(pressure_ad / 8192);
}
//Compute the value of temperature converted by ADC
temperature_ad = ((niet ondertekend int)((niet ondertekend int)(buff[3]<<8)) + ((byte)buff[4]));
indien (temperature_ad > 32768)
{
temperature = (vlot)(((long)(temperature_ad – 65536)) / 256);
//The unit of temperature is Centigrade
}
anders
{
temperature = (vlot)(temperature_ad / 256);
}
}
XGZP6859D My_XGZP6859D; //Define a object named My_XGZP6859D of the class XGZP6859D
ongeldige opstelling()
{
Serieel.begin(9600); //The initialization of Serial port
My_XGZP6859D.IIC_Init(); //Initialize the pins of IIC bus of My_XGZP6859D
}
lege lus()
{
My_XGZP6859D.XGZP6859D_get_cal(); //Call the function XGZP6859D_get_cal() of object My_XGZP6859D to calculate the pressure and temperature
Serieel.afdrukken(My_XGZP6859D.pressure);
Serial.println(” vader”);
Serieel.afdrukken(My_XGZP6859D.temperature);
Serial.println(” Centigrade”);
Serial.println(“”);
vertraging(1000);
}