Örnek Program Arduino Kodu

#katmak <stdio.h>
#katmak <string.h>
#katmak <inttypes.h>
#katmak “Arduino.h”
#katmak “XGZP6859D.h
//The initialiation of IIC BUS
void XGZP6859D::IIC_Init(geçersiz)
{
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_Başlangıç(geçersiz)
{
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_Durdur(geçersiz)
{
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
//geri dönmek 1 means failure
//geri dönmek 0 means success
byte XGZP6859D::IIC_Wait_Ack(geçersiz)
{
byte ucErrTime=0;
pinMode(IIC_SDA, INPUT);
digitalWrite(IIC_SDA, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
sırasında(digitalRead(IIC_SDA)==HIGH)
{
ucErrTime++;
eğer(ucErrTime>250)
{
IIC_Durdur();
geri dönmek 1;
}
}
digitalWrite(IIC_SCL, LOW);
geri dönmek 0;
}
//Generate ACK signal
void XGZP6859D::IIC_Ack(geçersiz)
{
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(geçersiz)
{
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);
için(t=0;T<8;t++)
{
eğer(teşekkürler & 0x80)
digitalWrite(IIC_SDA, HIGH);
başka
digitalWrite(IIC_SDA, LOW);
teşekkürler<<=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çin(i=0;Ben<8;i++ )
{
digitalWrite(IIC_SCL, LOW);
delayMicroseconds(IIC_DELAY_TIME);
digitalWrite(IIC_SCL, HIGH);
delayMicroseconds(IIC_DELAY_TIME);
receive<<=1;
eğer(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_Başlangıç();
IIC_Send_Byte((bayt)(Device_Address + 0));
Seri.baskı(“Device_Address is “);
Seri.println(Device_Address + 0, HEX);
IIC_Wait_Ack();
IIC_Send_Byte(adres);
IIC_Wait_Ack();
IIC_Send_Byte(data);
IIC_Wait_Ack();
IIC_Durdur();
}
//Read one byte from a specified address
byte XGZP6859D::Read_One_Byte(u8 address)
{
byte mydata;
IIC_Başlangıç();
IIC_Send_Byte((bayt)(Device_Address + 0));
IIC_Wait_Ack();
IIC_Send_Byte(adres);
IIC_Wait_Ack();
IIC_Başlangıç();
IIC_Send_Byte((bayt)(Device_Address + 1));
IIC_Wait_Ack();
verilerim = IIC_Read_Byte(0);
IIC_Durdur();
verilerimi döndür;
}
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
sırasında ((Read_One_Byte(0x30) & 0x08) == 0x80) //Veri toplamanın bitip bitmediğine karar verin
{
Seri.println(“XGZP6859D is not ready”);; //Wait while the data is not ready
gecikme(1000);
}
için(i=0;Ben<5;i++) //Read ADC output Data of Pressure and temperature
buff[Ben] = Read_One_Byte(0x06 + Ben);
Seri.baskı(“buff[0…4] is 0x”);
Seri.baskı(buff[0], HEX);
Seri.baskı(“, 0X”);
Seri.baskı(buff[1], HEX);
Seri.baskı(“, 0X”);
Seri.baskı(buff[2], HEX);
Seri.baskı(“, 0X”);
Seri.baskı(buff[3], HEX);
Seri.baskı(“, 0X”);
Seri.println(buff[4], HEX);
//Compute the value of pressure converted by ADC
pressure_ad = ((imzasız uzun)((imzasız uzun)(buff[0] * 65536)) + ((imzasız int)(buff[1] * 256)) + ((bayt)buff[2]));
Seri.baskı(“pressure_ad is “);
Seri.println(pressure_ad);
eğer (pressure_ad > 8388608) //
{
basınç = (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
}
başka
{
basınç = (long)(pressure_ad / 8192);
}
//Compute the value of temperature converted by ADC
temperature_ad = ((imzasız int)((imzasız int)(buff[3]<<8)) + ((bayt)buff[4]));
eğer (temperature_ad > 32768)
{
temperature = (batmadan yüzmek)(((long)(temperature_ad – 65536)) / 256);
//The unit of temperature is Centigrade
}
başka
{
temperature = (batmadan yüzmek)(temperature_ad / 256);
}
}
XGZP6859D My_XGZP6859D; //Define a object named My_XGZP6859D of the class XGZP6859D
geçersiz kurulum()
{
Seri.başlangıç(9600); //The initialization of Serial port
My_XGZP6859D.IIC_Init(); //Initialize the pins of IIC bus of My_XGZP6859D
}
geçersiz döngü()
{
My_XGZP6859D.XGZP6859D_get_cal(); //Call the function XGZP6859D_get_cal() of object My_XGZP6859D to calculate the pressure and temperature
Seri.baskı(My_XGZP6859D.pressure);
Seri.println(” Pa”);
Seri.baskı(My_XGZP6859D.temperature);
Seri.println(” Centigrade”);
Seri.println(“”);
gecikme(1000);
}