TEL:0086 15161516705

Documents

Your current location:Home > Documents > Tech-Support
022021.02
Example Program Arduino Code
//Libraries#include <Arduino.h>#include <Wire.h>//constants & variablesbool error=0; //reading error variable (not used in this sketch)float pressureKPA=0.0; //declare pressure KPa variablefloat pressureCMH2O=0.0; //declare pressure cmH2O variablefloat temperatureC=0.0; //declare temperature Celsius variablefloat temperatureF=0.0; //declare temperature Fahrenheitlong startMillis=0; //start sampling milliseconds timeconst long period=2000; //time between samples in milliseconds//hardware settings//functionsbool readCFSensor(float &temp, float &press,byte sensorAddress);void setup() { Serial.begin(9600); //begin serial port Wire.begin(); //begin i2c bus delay(200); //wait for electronics}void loop() { Serial.println(""); // Serial.println(F("-------------------------------------------------------------------------")); //samples division Serial.println(""); // startMillis=millis(); //save the starting time error = readCFSensor(temperatureC,pressureKPA,0x6D); //start conversion and read on pressure sensor ad 0x6D address Serial.println("Temperature *C: " + String(temperatureC,1)); //print *C temperature temperatureF= temperatureC * 1.8 + 32; //*C to *F conversion Serial.println("Temperature *F: " + String(temperatureF,1)); //print *F temperature Serial.println("Pressure KPa: " + String(pressureKPA,3)); //print KPa pressure pressureCMH2O = pressureKPA * 10.1972; //KPa to cmH2O conversion Serial.println("Pressure cmH2O: " + String(pressureCMH2O,2)); //print cmH2O pressure while((millis()-startMillis) < period); //waits until period done}bool readCFSensor(float &temp, float &press,byte sensorAddress) { byte reg0xA5=0; Wire.beginTransmission(sensorAddress); //send Start and sensor address Wire.write(0xA5); //send 0xA5 register address Wire.endTransmission(); //send Stop Wire.requestFrom(sensorAddress,byte(1)); //send Start and read 1 byte command from sensor address if(Wire.available()){ //check if data is available on i2c buffer reg0xA5 = Wire.read(); //read 0xA5 register value } Wire.endTransmission(); //send Stop Serial.println("Register 0xA5 read: " + String(reg0xA5,HEX)); //for debugging purposes reg0xA5 = reg0xA5 & 0xFD; //mask 0xA5 register AND 0xFD to set ADC output calibrated data Wire.beginTransmission(sensorAddress); //send Start and sensor address Wire.write(0xA5); //send 0xA5 register address Wire.write(reg0xA5); //send 0xA5 regiter new value Wire.endTransmission(); //send Stop Serial.println("Write 0xA5 register: " + String(reg0xA5,HEX)); //for debugging purposes Wire.beginTransmission(sensorAddress); //send Start and sensor address Wire.write(0x30); //send 0x30 register address Wire.write(0x0A); //set and start combined conversion Wire.endTransmission(); //send Stop Serial.println("Write 0x0A on 0x30 register and start conversion"); //for debugging purposes byte reg0x30 = 0x08; //declare byte variable for 0x30 register copy (0x08 initializing for while enter) while((reg0x30 & 0x08) > 0) { //loop while bit 3 of 0x30 register copy is 1 delay(1); //1mS delay Wire.beginTransmission(sensorAddress); //send Start and sensor address Wire.write(0x30); //send 0x30 register address Wire.endTransmission(); //send Stop Wire.requestFrom(sensorAddress,byte(1));//send Start and read 1 byte command from sensor address if(Wire.available()){ //check if data is available on i2c buffer reg0x30 = Wire.read(); //read 0x30 register value } Wire.endTransmission(); //send Stop Serial.println("Register 0x30 read: " + String(reg0x30,HEX)); //for debugging purposes } unsigned long pressure24bit; //declare 32bit variable for pressure ADC 24bit value byte pressHigh=0; //declare byte temporal pressure high byte variable byte pressMid=0; //declare byte temporal pressure middle byte variable byte pressLow=0; //declare byte temporal pressure low byte variable Wire.beginTransmission(sensorAddress); //send Start and sensor address Wire.write(0x06); //send pressure high byte register address Wire.endTransmission(); //send Stop Wire.requestFrom(sensorAddress,byte(3)); //send Start and read 1 byte command from sensor address while(Wire.available() < 3); //wait for 3 byte on buffer pressHigh = Wire.read(); //read pressure high byte pressMid = Wire.read(); //read pressure middle byte pressLow = Wire.read(); //read pressure low byte Wire.endTransmission(); //send Stop Serial.print(String(pressHigh)); //for debugging purposes Serial.print("," + String(pressMid)); //for debugging purposes Serial.print("," + String(pressLow)); //for debugging purposes pressure24bit = pressure24bit | pressHigh; pressure24bit = pressure24bit & 0x000000FF; pressure24bit = pressure24bit << 8; pressure24bit = pressure24bit | pressMid; pressure24bit = pressure24bit & 0x0000FFFF; pressure24bit = pressure24bit << 8; pressure24bit = pressure24bit | pressLow; pressure24bit = pressure24bit & 0x00FFFFFF; Serial.print(":" + String(pressure24bit)); //for debugging purposes int temp16bit=0; //declare 16bit variable for temperature ADC value byte tempHigh=0; //declare temperature high byte variable byte tempLow=0; //declare temperature low byte variable Wire.beginTransmission(sensorAddress); //send Start and sensor address Wire.write(0x09); //send temperature high byte register address Wire.endTransmission(); //send Stop Wire.requestFrom(sensorAddress,byte(2)); //send Start and read 1 byte command from sensor address while(Wire.available() < 2); //wait for 2 byte on buffer tempHigh = Wire.read(); //read temperature high byte tempLow = Wire.read(); //read temperature low byte Wire.endTransmission(); //send Stop Serial.print(" - " + String(tempHigh)); //for debugging purposes Serial.print("," + String(tempLow)); //for debugging purposes temp16bit = tempHigh * 256 + tempLow; //merge of 16bit temperature ADC value Serial.println(":" + String(temp16bit)); //for debugging purposes temp = float(temp16bit)/256; //real Celsius temperature calculation if(pressure24bit > 8388608) { //check sign bit for twos complement press = (float(pressure24bit) - float(16777216)) * 0.0000078125; //KPa negative pressure calculation } else { //no sign press = float(pressure24bit) * 0.0000078125; //KPa positive pressure calculation } return 0;}Thanks Marcelo J. Pino from Telecomunicaciones
022021.02
IIC Sample Code
#include <reg52.h>#include <math.h>#define DELAY_TIME 600#define TRUE 1#define FALSE 0#define uchar unsigned char#define uint unsigned int //--------------define IIC SCL,SDA port------------------------------ sbit SCL = P1 ^ 7; sbit SDA = P1 ^ 6; //-----------------define Max7219 port---------------------------sbit Max7219_pinCLK = P2 ^ 2; sbit Max7219_pinCS = P2 ^ 1;sbit Max7219_pinDIN = P2 ^ 0;//-------------delay time_us-------------------------------void DELAY(uint t) { while (t != 0) t--;}//-------------IIC START CONDITION-------------------------------void I2C_Start(void) { SDA = 1; //SDA output high DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); //SCL output high SDA = 0; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME);} //-------------IIC STOP CONDITION-------------------------------void I2C_Stop(void) { SDA = 0; //SDA OUTPUT LOW DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SDA = 1; DELAY(DELAY_TIME); SCL = 0; //SCL OUTPUT LOW DELAY(DELAY_TIME);}//-------------IIC SEND DATA "0"-------------------------------void SEND_0(void) { SDA = 0; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME);}//-------------IIC SEND DATA "1"-------------------------------void SEND_1(void) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME);}//-------------Check SLAVEs Acknowledge -------------------------------bit Check_Acknowledge(void) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME / 2); F0 = SDA; DELAY(DELAY_TIME / 2); SCL = 0; DELAY(DELAY_TIME); if (F0 == 1) return FALSE; return TRUE;}//-------------Write One Byte of Data -------------------------------void WriteI2CByte(uchar b) reentrant { char i; for (i = 0; i < 8; i++) if ((b << i) & 0x80) SEND_1(); else SEND_0();}//-------------Read One Byte of Data -------------------------------uchar ReadI2CByte(void) reentrant { char b = 0, i; for (i = 0; i < 8; i++) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); //DELAY(10); F0 = SDA; DELAY(DELAY_TIME); //DELAY(10); SCL = 0; if (F0 == 1) { b = b << 1; b = b | 0x01; } else b = b << 1; } return b;}//-------------write One Byte of Data,Data from MASTER to the SLAVER -------------------------------//-------------SLAVER address bit:01101101-------------------------------void Write_One_Byte(uchar addr, uchar thedata) //Write "thedata" to the SLAVERs address of "addr"{ bit acktemp = 1; I2C_Start(); //IIC START WriteI2CByte(0xDA); //IIC WRITE operation,SLAVER address bit:01101010 acktemp = Check_Acknowledge(); //check the SLAVER WriteI2CByte(addr); /*address*/ acktemp = Check_Acknowledge(); WriteI2CByte(thedata); /*thedata*/ acktemp = Check_Acknowledge(); I2C_Stop(); //IIC STOP }//-------------Reaed One Byte of Data,Data from SLAVER to the MASTER -------------------------------uchar Read_One_Byte(uchar addr) { bit acktemp = 1; uchar mydata; I2C_Start(); WriteI2CByte(0xDA); acktemp = Check_Acknowledge(); WriteI2CByte(addr); acktemp = Check_Acknowledge(); I2C_Start(); WriteI2CByte(0xDB); //IIC READ operation acktemp = Check_Acknowledge(); mydata = ReadI2CByte(); acktemp = Check_Acknowledge(); I2C_Stop(); return mydata;}//-------------Delay_ms -------------------------------void Delay_xms(uint x) { uint i, j; for (i = 0; i < x; i++) for (j = 0; j < 112; j++) ;}//-------------Write One Byte to the Max7219-------------void Write_Max7219_byte(uchar DATA) { uchar i; Max7219_pinCS = 0; //CS low effect for (i = 8; i >= 1; i--) { Max7219_pinCLK = 0; Max7219_pinDIN = DATA & 0x80; DATA = DATA << 1; Max7219_pinCLK = 1; //when pinCLK is high send the Data }}//-------------decide which address shows the Data-------------void Write_Max7219(uchar address,uchar dat) { Max7219_pinCS = 0; Write_Max7219_byte(address); Write_Max7219_byte(dat); Max7219_pinCS = 1;}//-------------MAX_7219 Initialization-------------void Init_MAX7219(void) { Write_Max7219(0x09, 0xff); //Decoded mode:BCD code Write_Max7219(0x0a, 0x03); //Brightness Write_Max7219(0x0b, 0x07); //Scan limit:8 digital display Write_Max7219(0x0c, 0x01); //Power-Down modes :0,Normal code:1 Write_Max7219(0x0f, 0x01); //Display test:1;Test end,normal display:0}void main(void){ uchar yali1, yali2, yali3,wendu1,wendu2; uchar temp_a5; long int ad,temp; long float pas; uchar dis[8]; Init_MAX7219(); Delay_xms(1000); Write_Max7219(0x0f, 0x00); while (1) { temp_a5 = Read_One_Byte(0xA5);//Read ASIC Sys_config temp_a5 = temp_a5 & 0xFD; //(Raw_data_on: 0: output calibrated data,namly the value in 0x06-0x0a) Write_One_Byte(0xA5, temp_a5); //Set ADC output calibrated Data Write_One_Byte(0x30, 0x0A); //indicate a combined conversion (once temperature conversion immediately followed by once sensor signal conversion)(wire measure order in 0x30,000:single temp. measure;001:single pressure. measure;010:combination:single pressure and temp. measurement;011:sleep mode(combined mode measurement at certain time intervals)) while ((Read_One_Byte(0x30) & 0x08) > 0); //Judge whether Data collection is over // -------------READ ADC output Data of Pressure ------------- yali1 = Read_One_Byte(0x06); yali2 = Read_One_Byte(0x07); yali3 = Read_One_Byte(0x08); ad = yali1 * 65536 + yali2 * 256 + yali3; // -------------READ ADC output Data of Temperature ------------- wendu1= Read_One_Byte(0x09); wendu2= Read_One_Byte(0x0a); temp=wendu1*256+wendu2; /*Conversion, the following is the conversion formula of 100*/ if (ad > 8388608) // >8388606 as negative value, handle at display end { pas = (ad - 16777216)/64/1000; //unit is kpa } else { pas = ad/64/1000; //unit is kpa } if (pas < 0) pas = fabs(pas);/*Display program with Max7219*/ dis[0] = (long int)pas / 10000000; dis[1] = (long int)pas % 10000000 / 1000000; dis[2] = (long int)pas % 1000000 / 100000; dis[3] = (long int)pas % 100000 / 10000; dis[4] = (long int)pas % 10000 / 1000; dis[5] = (long int)pas % 1000 / 100; dis[6] = (long int)pas % 100 / 10; dis[7] = (long int)pas % 10; Write_Max7219(8, dis[0]); Write_Max7219(7, dis[1]); Write_Max7219(6, dis[2]); Write_Max7219(5, dis[3]); Write_Max7219(4, dis[4]); Write_Max7219(3, dis[5]); Write_Max7219(2, dis[6]); Write_Max7219(1, dis[7]); Delay_xms(100); //delay 100ms }}

Contact

Add: 14-15F,Bldg 4, Hi-Tech Park, Yijiang District, Wuhu 241000 PRC.

Email: info@CFSensor.com

Tel No: 0086 553 2167689

Mobile/Whatsapp/Wechat:

0086 15161516705

Follow us: 22.png33.gif11.jpg66.gif55.png

Scan wechat
Online
WhatsApp Skype QQ 15161516705
WeChat