[D6F-PH] Application Note No.MDMK-14-0386 Application Note 01 Usage of MEMS Differential Pressure Sensor (D6F-PH) Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 1 [D6F-PH] Application Note No.MDMK-14-0386 Contents Outline ........................................................................................................................................3 Structure .....................................................................................................................................3 Dimensions.................................................................................................................................3 Principle of Pressure detection ..................................................................................................4 Features of Product ....................................................................................................................4 Usage .........................................................................................................................................5 6-1. Recommended tube connection method of D6F-PH......................................................... 5 6-2. Electrical connection method of D6F-PH........................................................................... 5 7 Specification of Communication .................................................................................................6 7-1. Outline of I2C Interface ...................................................................................................... 6 7-2. Interface Configuration Registers ...................................................................................... 7 7-2-1. Access Address Registers (00h – 01h) .......................................................................... 7 7-2-2. Serial Control Register (02h).......................................................................................... 7 7-2-3. Write Buffer Registers (03h – 06h)................................................................................. 8 7-2-4. Read Buffer Registers (07h – 0Ah) ................................................................................ 8 7-2-5. Example of I2C Access Commands............................................................................... 9 7-3. Description of Registers ...........................................................................................................10 7-3-1. Sensor Control (D040h) ............................................................................................... 10 7-3-2. Flags (D046h)............................................................................................................... 10 7-3-3. CRC Calculation Control ( D049h ) .............................................................................. 11 7-3-4. Data Registers (D051h-D068h).................................................................................... 12 8. Explanation of output data .......................................................................................................13 8-1. Data alignment ................................................................................................................. 13 8-2. Register content ............................................................................................................... 13 8-3. Example of Sensing data ................................................................................................. 14 9. Sensor Operation flow chart ....................................................................................................15 10. I2C Instruction for Sensor Operation .......................................................................................16 11. Sample Source Code ...............................................................................................................18 11-1. D6F_PH_Sample.h ...................................................................................................... 18 11-2. D6F_PH_Sample.c....................................................................................................... 19 12. WARRANTY AND LIMITED LIABILITY....................................................................................26 13. CONTACT ................................................................................................................................28 14. History ......................................................................................................................................28 1. 2. 3. 4. 5. 6. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 2 [D6F-PH] Application Note No.MDMK-14-0386 1. Outline This application note is intended to demonstrate how to use and interface with Omron’s MEMS differential pressure sensor(D6F-PH). It should be noted that this document is intended to supplement the datasheet, which should be referenced when using the sensor. 2. Structure Fig.1 shows the internal cross-section view of the MEMS differential pressure sensor (D6F-PH). Air will flow from one inlet and out the other passing over the MEMS flow chip surface. The MEMS chip is able to measure the airflow as air passes over the chip. For more details on Omron’s MEMS Flow sensor chip, please see the application note(MDMK-13-0153). Flow Sensor chip フローセンサチップ 基板 Substrate Fig.1 the internal cross-section view of MEMS differential pressure sensor (D6F-PH) 3. Dimensions High-pressure side Low-pressure side Fig.2 Outline dimensions of D6F-PH Please refer to Section 6: Usage about bypass tube connection and electrical connection. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 3 [D6F-PH] Application Note No.MDMK-14-0386 4. Principle of Pressure detection By using a thermal mass flow sensor, Omron’s MEMS differential pressure sensor can detect fine changes in differential pressure. For more detailed information, please refer to Application Note(MDMK-13-0153). 5. Features of Product By using a thermal mass flow method, Omron’s MEMS differential pressure sensor is more sensitive compared with that of a conventional(capacitive) differential pressure sensor in the low-pressure range. Vout vchip Thermal mass flow method The output of Omron’s sensor is proportional to the square root of the gas flow rate through the sensor chip surface. 2 p vmain Orange:thermal mass flow method Blue:conventional method Conventional method The output of a conventional sensor is proportional to the square of the gas flow velocity through the main channel. Fig. 3 Comparison with conventional method and thermal mass flow method Item Range of Differential Pressure Resolution Zero point accuracy (Note) Span accuracy (Note) Span shift by Temperature Table1. Specifications of D6F-PH□□□□ Description Min Typ Max Unit -50 50 Pa 0 250 Pa -500 500 Pa 12 bit -0.2 +0.2 Pa -3 +3 %R.D. -0.5 +0.5 %R.D. Response Time Ambient Operating Temp -20 33 - 50 80 msec degC Ambient Storage Temp -40 - 80 degC Ambient Operating Humidity 35 - 85 %RH Ambient Storage Humidity 35 - 85 %RH Note D6F-PH0505AD3 D6F-PH0025AD1 D6F-PH5050AD3 With respect to a change of 10 degC 12bit Resolution without freezing and condensation without freezing and condensation without freezing and condensation without freezing and condensation Supply Voltage 2.3 3.3 3.6 VDC Current Consumption 6 mA Vcc=3.3V、25degC Frequency of SCL 400 kHz FAST Mode (Note)Span accuracy and zero point accuracy are the independence errors, and are not satisfied at the same time. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 4 [D6F-PH] Application Note No.MDMK-14-0386 6. Usage 6-1. Recommended tube connection method of D6F-PH When connecting the D6F-PH sensor in a bypass configuration, the sensor is able to detect fine pressure changes. This is achieved by providing an orifice in the main, which generates a small pressure change before and after the orifice. The D6F-PH will be connected to the bypass flow path from the pressure port which is provided before and after the orifice. High Pressure side Bypass flow path length is 800[mm] or less Lower Pressure side Main Chanel Flow direction of main channel Orifice Fig. 4 Recommended tube connection method of D6F-PH Here, the inner diameter of the bypass tube which is connected to the D6F-PH is 4[mm] and its length is 800[mm] or less. 6-2. Electrical connection method of D6F-PH For the I2C output, the D6F-PH will require a pull-up resistor to each clock line(SCL) and data line (SDA). A pull-up resistor of 2.2[kΩ] (recommended value) should be implemented between the Vcc as shown in Fig.5. In addition, please adjust the pull-up resistor’s value depending on the transfer rate of SCL and the I2C wire length. Sensor Fig. 5 Electrical connection method of D6F-PH Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 5 [D6F-PH] 7 Application Note No.MDMK-14-0386 Specification of Communication 7-1. Outline of I2C Interface Table2. Basic functions of I2C communication Item Communication method SCL Frequency Output format Slave Address Descriptions I2C Max 400kHz (Fast Mode) Binary data (Upper byte, Lower byte) 1101100b or 0x6C * Please note that the slave address is 7bits. Interface Configuration Register The memory and register access are controlled by writing to the interface configuration registers. Various internal registers In case access to internal registers are needed, the target register’s address needs to be set to the Interface Configuration Register (address:00h and 01h). Table3. Interface Configuration Register Signal Conditioning Digital Processing I2C 1. 2. 3. 4. SDA GND Vcc SCL Register A/D converter Address D040h D046h D049h D051h D052h D061h D062h D065h D066h D067h D068h Configuration Address 00h 01h 02h 03h 04h 05h 06h 07h 08h 09h 0Ah 0Bh 0Dh Function Access Address 1 Access Address 2 Serial Control Write Buffer 0 Write Buffer 1 Write Buffer 2 Write Buffer 3 Read Buffer 0 Read Buffer 1 Read Buffer 2 Read Buffer 3 Initialize Power sequence Table4. Internal Register Map Register name Descriptions SENS_CTRL Sensor Control Register FLAGS Flag Register INT_CTRL CRC Calculation Control COMP_DATA1_H Compensated Flow rate Register COMP_DATA1_L TMP_H Internal Temperature Register TMP_L REF_FLOW1_H Sensor Reference Flow Register REF_FLOW1_L THRESH_FLOW1_H Sensor Threshold Flow Register THRESH_FLOW1_L Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 6 [D6F-PH] 7-2. Application Note No.MDMK-14-0386 Interface Configuration Registers The memory and registers access are controlled by writing to the interface configuration registers. Table5. Interface Configuration Register Map Configuration Function Note Address 00h Access Address 1 (Upper byte) Upper byte of first Access Address 01h Access Address 2 (Lower byte) Lower byte of first Access Address 02h Serial Control Write / Read Access Control 03h Write Buffer 0 Data to be written at Address 04h Write Buffer 1 Data to be written at Address + 1 05h Write Buffer 2 Data to be written at Address + 2 06h Write Buffer 3 Data to be written at Address + 3 07h Read Buffer 0 Data read from Address 08h Read Buffer 1 Data read from Address + 1 09h Read Buffer 2 Data read from Address + 2 0Ah Read Buffer 3 Data read from Address + 3 0Bh Initialize 0Dh Power Sequence Hardware reset control Upper byte:bit[15:8] of 16bit data, Lower byte:bit[7:0] of 16bit data 7-2-1. Access Address Registers (00h – 01h) The access address registers are used to access internal register blocks including sensor register map, ADC register map, and internal memory. It specifies the data transfer start address with auto increment for multiple byte data transfer. Table6. Access Address Register Address MSB D7 A15 A7 00h 01h D6 A14 A6 D5 A13 A5 D4 A12 A4 D3 A11 A3 D2 A10 A2 D1 A9 A1 LSB D0 A8 A0 7-2-2. Serial Control Register (02h) Table7. Serial Control Register (02h) The serial control register contains various bits to modify the behavior of the serial access. Address 02h • MSB D7 D_byte_ cnt[3] D6 D_byte_ cnt[2] D5 D_byte_ cnt[1] D4 D_byte_ cnt[0] D3 Req D2 R_WZ D1 Acc_ctl2 [1] LSB D0 Acc_ctl2 [0] Acc_ctl2 [1:0] – Access Control bits 0 0 = 16bits address (A15-A0) access ( internal ROM and registers) Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 7 [D6F-PH] Application Note No.MDMK-14-0386 0 1 = 8bits address (A7-A0) access and used to access MCU internal 256 byte dual port RAM. 1 0 = reserved 1 1 = reserved 7-2-3. • R_WZ – Read or Write access select bit 0 = Write Access 1 = Read Access • Req- Request bit 0 = the previous request is done 1 = new request. After the serial bus bridge controller finishes a request, it will clear Req to 0. For write requests the bridge controller moves the data in write data buffers to the location pointed by access address. For read requests the bridge controller stores the read data into data buffer. • D_byte_cnt3 [3:0] Transfer data byte count. It only supports 1, 2, 3, 4 data byte transfer. Write Buffer Registers (03h – 06h) There are four write data buffer registers at address: 03h – 06h. To perform a write, the host can either use a single command or perform separate writes to the following addresses. The host can write to sensor register map in single byte transfer: The host can burst write data start from address = 00h with following data byte, A[15:8]、A[7:0]、18h、data[0]. Or the host can do four serial bus writes and write one data byte into serial bus register with the following steps. ・Write A[15:8] to address = 00h of interface configuration registers. ・Write A[7:0] to address = 01h of interface configuration registers. ・Write data[0] to address = 03h of interface configuration registers. ・Write 18h to address = 18h of interface configuration registers.(1byte, new request, write) [Note] Read Serial Control register(02h). If Req = 0 (02h[3]), controller is finished with write. 7-2-4. Read Buffer Registers (07h – 0Ah) There are four read data buffer registers at address: 07h – 0Ah. To perform a read, the host can either use a single streaming command or perform separate commands to the following addresses. After the read request is done by the internal serial bus bridge controller, the Req bit is cleared to 0 and read data is stored in rd_buf1 – rd_buf4 (address = 07h – 0Ah). For single byte read request the host can burst write A[15:8], A[7:0], 1Ch at start address = 00h. The host needs to read the command register until the Req bit is cleared to 0, then read “read data buffer” for read data at address = 07h. The host can perform a single byte read by individually programming the following registers. ・Write A[15:8] to address = 00h of interface configuration registers. ・Write A[7:0] to address = 01h of interface configuration registers. ・Write 1Ch to address = 02h of interface configuration registers.(1byte, new request, read) [Note] Read address = 02h. If Req = 0 (02h[3]), controller is finished with read data[0] from address = 07h. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 8 [D6F-PH] Application Note No.MDMK-14-0386 7-2-5. Example of I2C Access Commands I2C Command Examples Start address of serial configuration registers. ・I2C command:I2C write Address to be set to 00h/01h of serial configuration registers. START Slave Address ACK Access Address ACK Reg Address H ACK Reg Address L ACK S D8h (6Ch (7b)+ 0) A 00h A D0h A 40h A Data to be set to 02h/03h of serial configuration registers. ・I2C command:I2C read Serial Ctrl ACK Write Data ACK STOP 18h A 06h A P Start address of serial configuration registers (Read Buffer 0) START Slave Address ACK Access Address ACK S D8h (6Ch (7b)+ 0) A 07h A This data will be stored in serial configuration register “07h” & “08h” .(Read Buffer 0 / Read Buffer 1) Re-Start Slave Address ACK Read Data H ACK Read Data L ACK STOP RS D9h (6Ch (7b)+ 1) A xxh A xxh NA P Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 9 [D6F-PH] Application Note No.MDMK-14-0386 7-3. Description of Registers The internal memory and registers of the sensor module can be accessed via the interface configuration registers. 7-3-1. Sensor Control (D040h) Table8. SENS_CTRL Address MSB D7 LSB D6 D5 D4 D3 D040h D2 D1 D0 MS DV_PWR[ 1] DV_PWR[ 0] Write Access None None None None None Host & MCU Host & MCU Host & MCU Default 0 0 0 0 0 0 0 0 • DV_PWR[1:0] – Main Device power mode setting 0 0 = Standby – All blocks are powered down. 1 0 = MCU on – Used when only MCU is required. Basic analog and memories are powered on and MCU clock is running. Note :This register should not be changed during a measurement. • MS – MCU start – Begin execution of measurement or MCU mode based on the state of DV_PWR. • • 7-3-2. 0 = Stop Sequences are stopped and MCU clock is turned off. 1 = Start The MCU clock is started and the MCU mode is executed. Flags (D046h) Table9. FLAGS Address MSB D7 LSB D6 D5 D4 D046h Write Access None None None D0 HV1 SV None Host & MCU None Host & MCU Host & MCU 0 0 0 0 0 0 = Supply voltage is within specification. 1 = Supply voltage is outside of specification. HV1 – Heater Voltage Flag • • • D1 SV – Supply Voltage (VDD) Flag • • • D2 OS1 Default • D3 0 = Heater voltage is within specification. 1 = Heater voltage is outside of specification. OS1 – Open Sensor Flag • • 0 = Sensor is connected. 1 = Sensor is not connected. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 10 [D6F-PH] • Application Note No.MDMK-14-0386 HV2 and OS2 are reserved bits. In case of write access, you should set to “0”. * If you want to read flag register, it is recommended to read twice in order to avoid conflict with the MCU update. 7-3-3. CRC Calculation Control ( D049h ) Table 10. INT_CTRL MSB ADDR D7 LSB D6 D5 D4 D3 D2 D049h D1 D0 CRC_EN Write Access NONE NONE NONE NONE NONE NONE Host& MCU NONE Default 0 0 0 0 0 0 1 0 • ・ CRC_EN – CRC check calculation enable (See below for more information about the CRC) • 0 = CRC check calculation disable • 1 = CRC check calculation enable Description of the CRC calculation CRC Overview The CRC is used as an error detection method in a data communication. Our flow sensor use the CRC8 polynomial x^8 + x^5 + x^4 + 1. The following is an example of I2C access 2 byte read using CRC function. Fig 9. Example of 2byte read with CRC ・ 1. 2. 3. 4. ・ Fig.6 Example of 2byte read with CRC Bit unit CRC-8 calculation method The data bit sequence will be aligned in a line. The polynomial bit string will be aligned under the line of the data bit sequence. If the data bit above the leftmost the polynomial bit sequence is 0, the polynomial bit sequence is shifted one bit to the right. If the data bit above the leftmost polynomial bit sequence is 1, the data bit and the polynomial bit are calculated by XOR. Then the polynomial bit sequence are shifted one bit to the right. 1-3steps are repeated until the polynomial bit sequence reaches the right end of the data bit sequence. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 11 [D6F-PH] Application Note No.MDMK-14-0386 The following example shows how to calculate the CRC byte based on XOR calculation. hex 1st Byte of data 04h 2nd Byte of data 02h Polynomial (x^8+ x^5+ x^4+ 1) 131h CRC-byte checksum 225h bin 00000100 00000010 100110001 11100001 1st Byte of data 2nd Byte of data 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 1 1 0 0 1 1 0 0 0 1 0 1 1 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 Data bit sequence 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 0 0 1 1 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 Polynomial bit bit sequence sequence Polynomial Fig.7 XOR operation example of CRC-8 7-3-4. Data Registers (D051h-D068h) Table11. 16bit Data Register Map MSB LSB Address Registers Name D051h COMP_DATA1_H DATA<15:8> D052h COMP_DATA1_L DATA<7:0> D061h TMP_H DATA<15:8> D062h TMP_L DATA<7:0> D7 D6 D5 D4 D3 D2 D1 Description D0 Compensated Flow rate Register Internal Temperature Register For additional information, please refer to Section 8: Explanation of output data. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 12 [D6F-PH] Application Note No.MDMK-14-0386 8. Explanation of output data The measured data is sent to the dedicated registers. These registers contain, respectively, upper byte and lower byte of the sensing signals of the sensor. For example, the compensated flow data is given by the concatenation COMP_DATA1_H & COMP_DATA1_L in unsigned 16bit number. And the raw flow data is given by the concatenation UCFM1_H & UCFM1_L and it is expressed as a 2’s complement of signed 16bit number. 8-1. Data alignment Sensing data is represented as 16bit numbers. The data is split and sent in two consecutive bytes to Flow Registers in “Big Endian” format. “Big Endian” means that the upper byte of the number is stored in a register at the lowest address, and the lower byte at the highest address. 8-2. Register content ・COMP_DATA1_H & COMP_DATA1_L [D051h – D052h] : Compensated Data (unsigned) These registers contain compensated flow rate data. ○ If pressure range is ±50[Pa] or ±500[Pa] Dp[Pa] = (Pv - 1024)/60000*RANGE – RANGE/2 (RANGE = 100 or 1000) Where、Pv is Register content stored in the Compensated Flow Data registers [D051h – D052h]. ○ If pressure range is 0-250[Pa] Dp[Pa] = (Pv - 1024)/60000*RANGE ( RANGE = 250 ) Where、Pv is Register content stored in the Compensated Flow Data registers [D051h – D052h]. ・TMP_H & TMP_L [D061h – D062h] : Temperature data (signed) The values stored in these registers represent the temperature data measured by the internal temperature of the ASIC. The following formula can be applied to convert register data into temperature value. Tv [℃] = (Rv – 10214) / 37.39 Where, Tv is Converted temperature value in the degC format, and Rv is Register content stored in the Temperature Data register. Note: Temperature data is for reference ONLY. Its accuracy is not specified in the device specifications. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 13 [D6F-PH] 8-3. Application Note No.MDMK-14-0386 Example of Sensing data The below tables provide a few basic examples of the data that is read in Flow Sensor Registers when the device is subject to given flow and temperature. The values listed in the tables are given under the hypothesis of perfect device calibration (i.e. no offset, no gain error, etc). Table12. Temperature Data registers content vs. Temperature value Address of Registers TMP_H TMP_L D061h D062h 2Bh 8Dh 2Eh FFh 26h BBh Value of Registers HEX 2B8Dh 2EFFh 26BBh DEC 11149 12031 9915 Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. Converted Temperature values 25.0 degC 48.6 degC -8.0 degC 14 [D6F-PH] Application Note No.MDMK-14-0386 9. Sensor Operation flow chart By calling the Initialize function of the provided sample code, No.1 routine can be executed. 1. Initialization Enable CRC Function*1 2. Execute MCU mode after desired configurations are set 3. Reading the MSB and LSB of the COMP_DATA Register (D051h and D052h) By calling the Press_meas function of provided sample code, No.3 routine can be executed. In case of temperature measurement, please call Temp_meas function. Hardware Reset Enable *1 If you use CRC function, please send some command refer to Page.17. Fig.8 Flowchart of Sensor operation ・Communication time Item Response time Sampling interval Master Sign α β Remarks α ≧33 ms β>α In Press_meas, 4.Send 5.Send In Press_meas, 4.Send In Press_meas, Command of data acquisition Data acquisition Command of data acquisition β α Time Slave Start of data acquisition COMP_DATA Start of data acquisition Fig.9 Time axis image view of the differential pressure measurement Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 15 [D6F-PH] Application Note No.MDMK-14-0386 10. I2C Instruction for Sensor Operation 1. Initialization after power up [Must be done] I2C command: The device must be unlocked then write 0x00 to the EEPROM Control Register(0xB) to load NVM trim values, but keep the MCU in non-reset state. START Slave Address ACK Access Address ACK Write Data ACK STOP S D8h (6Ch (7b)+ 0) A 0Bh A 00h A P 2. Execute MCU mode after desired configuration registers Writing 06h to the Sensor Control Register (D040h) will execute the MCU mode outlined in Section 6 with the configured setting for the ADC Resolution and Gain, Compensation. Reading the Sensor control register after writing a 06h will show the MUX selection chosen by the MCU. After running the process, MS bit will be set to “0”. [Caution]: Do not read or write to the Device while the MCU is executing. It would be safe to read/write only after 33ms. I2C command: Write 06h to the Sensor Control Register (D040h) (MS=1&MCU_on) START Slave Address ACK Access Address ACK Reg Address H ACK Reg Address L ACK S D8h (6Ch (7b)+ 0) A 00h A D0h A 40h A Serial Ctrl 18h ACK A Write Data 06h ACK A STOP P 3. Reading the Upper and Lower byte of Compensated Flow Data Registers(D051h & D052h) I2C command: To read Compensated flow data register, it needs to set 2Ch (it means 2byte read) to interface configuration register (address:2h). START Slave Address ACK Access Address ACK Reg Address H ACK Reg Address L ACK S D8h (6Ch (7b)+ 0) A 00h A D0h A 51h A Serial Ctrl ACK STOP 2Ch A P I2C command: Through Read Buffer 0(address:07h) and Read Buffer 1(address:08h), you can read the 2byte of Compensated flow data. START Slave Address ACK Access Address ACK S D8h (6Ch (7b)+ 0) A 07h A Re-Start Slave Address ACK Read Data H ACK Read Data L ACK STOP RS D9h (6Ch (7b)+ 1) A xxh A xxh NA P Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 16 [D6F-PH] Application Note No.MDMK-14-0386 Enable CRC Function 1. Set to "1" to bit[1] of the CRC control register. I2C command: Write 02h to the CRC Control Register (D049h). START Slave Address ACK Access Address ACK Reg Address H ACK Reg Address L ACK S D8h (6Ch (7b)+ 0) A 00h A D0h A 49h A Serial Ctrl ACK Write Data ACK STOP 18h A 02h A P Execute Hardware Reset 1. Set to “1” to bit[7] of the Power Sequence register. I2C command: Write 80h to the Power Sequence Register (0Dh). START Slave Address ACK Access Address ACK Write Data ACK STOP S D8h (6Ch (7b)+ 0) A 0Dh A 80h A P The hardware reset after the execution, bit 7 is cleared to "0" automatically. Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 17 [D6F-PH] Application Note No.MDMK-14-0386 11. Sample Source Code The following is a sample source code of the D6F-PH control in the case of using STM32microcontroller. The I2C control unit will need to be adjusted to whatever microcontroller is used. 11-1. D6F_PH_Sample.h /*=================================================*/ /* D6F-PH Digital Flow Sensor Header File (using STM32) * :Copyright: (C) OMRON Corporation, Microdevice H.Q. * :Auther : * :Revision: $Rev$ * :Id: $Id$ * :Date: $Date$ * * All Rights Reserved * OMRON Proprietary Right *=================================================*/ /*=======================*/ /* for General */ /*=======================*/ #define SA_7 0x6C // for 7bit Slave Address //#define RANGE_MODE 100 // Full Range +/-50[Pa] Please change the RANGE_MODE #define RANGE_MODE 250 // Full Range 0-250[Pa] define for your target Product //#define RANGE_MODE 1000 // Full Range +/-500[Pa] Pressure range. /*=======================*/ /* for Measure Mode */ /*=======================*/ #define P 1 // Pressure mode #define T 2 // Temperature mode /* Function prototypes -------------------------------------------------------*/ void Initialize( void ); short Press_meas( void ); short Temp_meas( void ); /* Private Functions --------------------------------------------------------*/ int I2C_WR(unsigned char add, char *dbuf, unsigned char n); uint8_t I2C_RD_8(unsigned char add, char *dbuf, unsigned char n); short I2C_RD_16(unsigned char add, char *dbuf, unsigned char n); unsigned short I2C_RD_u16(unsigned char add, char *dbuf, unsigned char n); void I2C1_Init(void); void I2C1_Start(void); void I2C1_MastrSel(uint8_t address, uint8_t rw); void I2C1_AckEn(void); void I2C1_AckDis(void); void I2C1_Stop(void); void I2C1_senddata(uint8_t data); uint8_t I2C1_rcvdata(void); Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 18 [D6F-PH] 11-2. Application Note No.MDMK-14-0386 D6F_PH_Sample.c /*=================================================*/ /* D6F-PH Digital Flow Sensor Sample Code (using STM32) * :Copyright: (C) OMRON Corporation, Microdevice H.Q. * :Auther : * :Revision: $Rev$ * :Id: $Id$ * :Date: $Date$ * * All Rights Reserved * OMRON Proprietary Right *=================================================*/ #include "stm32f10x_i2c.h" #include "D6F_PH_Sample.h" #define #define #define #define I2C1_SCL_PIN I2C1_SDA_PIN I2C2_SCL_PIN I2C2_SDA_PIN GPIO_Pin_6 GPIO_Pin_7 GPIO_Pin_10 GPIO_Pin_11 typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned long uint32; short RD_FIFO; /* 16bit data width */ unsigned short uRD_FIFO; /* 16bit data width */ uint8_t RD_REG; /* 8bit data width */ char setting_done_flag = 0; // Dummy wait routine void adc_wait(volatile unsigned long delay) { while(delay) delay--; } /*=================================================*/ /* Initialize Function */ /* Usage : Initialize( void ) */ /* Argument : Null */ /* Return value : T.B.D */ /*=================================================*/ void Initialize( void ) { /* EEPROM Control <= 00h */ char send1[] = {0x0B, 0x00}; Copyright 2013 - 2015 OMRON I2C_WR(SA_7, send1, 2); Corporation. All Rights Reserved. /* [D042] <= 0C35h 50msec wait@4MHz */ char send2[] = {0x00, 0xD0, 0x42, 0x28, 0x0C, 0x35}; I2C_WR(SA_7, send2, 6); 19 [D6F-PH] Application Note No.MDMK-14-0386 char send1[] = {0x0B, 0x00}; I2C_WR(SA_7, send1, 2); } /*=======================================================*/ /* Pressure measure Function */ /* Usage : Press_meas( void ) */ /* Argument : NULL */ /* Return value : Compensated Pressure value(unsigned) */ /*=======================================================*/ short Press_meas(void) { short rd_fifo; short rd_flow; unsigned long wait_time; /* [D040] <= 06h */ char send2[] = {0x00, 0xD0, 0x40, 0x18, 0x06}; I2C_WR(SA_7, send2, 5); wait_time = 33; /*33msec wait */ /* wait time depend on resolution mode */ adc_wait(wait_time); /* [D051/D052] => Read Compensated Flow value */ char send3[] = {0x00, 0xD0, 0x51, 0x2C, 0x07}; uRD_FIFO = I2C_RD_u16(SA_7, send3, 5); // Press Mode : [Pa] = (xx[count] - 1024) * Full Range [Pa]/ 60000 - Full Range [Pa] at other if (RANGE_MODE == 250) { rd_flow = ((rd_fifo - 1024) * RANGE_MODE *10/ 60000); /* convert to [Pa] */ } else { rd_flow = ((rd_fifo - 1024) * RANGE_MODE *10/ 60000) - RANGE_MODE*10/2; /* convert to [Pa] */ } return rd_flow; } /*=======================================================*/ /* Temperature measure Function */ /* Usage : Temp_meas() */ /* Argument : NULL */ /* Return value : x10 Temperature */ /*=======================================================*/ short Temp_meas(void) Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. { short rd_temp; unsigned long wait_time; /* [D040] <= 06h */ 20 [D6F-PH] Application Note No.MDMK-14-0386 short Temp_meas(void) { short rd_temp; unsigned long wait_time; /* [D040] <= 06h */ char send2[] = {0x00, 0xD0, 0x40, 0x18, 0x06}; I2C_WR(SA_7, send2, 5); /* wait time depend on resolution mode */ wait_time = 33; /* 33msec wait */ adc_wait(wait_time); /* [D061/D062] => Read TMP_H/TMP_L value */ char send3[] = {0x00, 0xD0, 0x61, 0x2C, 0x07}; RD_FIFO = I2C_RD_16 (SA_7, send3, 5); rd_temp = ((RD_FIFO -10214)*1000 / 3739); // convert to degree-C(x10) return rd_temp; } /* Public Basic Functions ----------------------------------------------------------*/ /*=================================================*/ /* I2C Write command */ /* Usage : I2C1_WR() */ /* Argument : 7bit Slave Address(char) */ /* /* Return value : 8bit Read result */ /*=================================================*/ int I2C_WR(unsigned char add, char *dbuf, unsigned char n) { int i = 0; I2C1_Start(); I2C1_MastrSel(add, 0); while (n--) { I2C1_senddata(dbuf[i]); i++; } I2C1_Stop(); return 0; /* Start condition */ /* Slave Address */ /* Send Data */ /* Stop condition */ } /*=================================================*/ /* I2C Read command */ /* Usage : I2C_RD() */ /* Argument : char add (7bit Slave Address) */ /* char *dbuf (Write data) */ /* unsigned char n (Number of bytes)*/ /* Return 2013 value- 2015 : 8bit Read Corporation. result */ Copyright OMRON All Rights Reserved. /*=================================================*/ uint8_t I2C_RD_8 (unsigned char add, char *dbuf, unsigned char n) { int i= 0; char n_w; 21 [D6F-PH] Application Note No.MDMK-14-0386 /* unsigned char n (Number of bytes)*/ /* Return value : 8bit Read result */ /*=================================================*/ uint8_t I2C_RD_8 (unsigned char add, char *dbuf, unsigned char n) { int i= 0; char n_w; n_w = n - 1; /* I2C Pre-WR Access */ I2C1_Start(); I2C1_MastrSel(add, 0); while (n_w--) { I2C1_senddata(dbuf[i]); i++; } I2C1_Stop(); /* I2C RD Access */ I2C1_Start(); I2C1_MastrSel(add, 0); I2C1_senddata(dbuf[n-1]); I2C1_Start(); I2C1_MastrSel(add, 1); I2C1_AckDis(); I2C1_Stop(); RD_REG = I2C1_rcvdata(); return RD_REG; /* Start condition */ /* Slave Address 7bit => 8bit */ /* Send Data */ /* Stop condition */ /* /* /* /* /* /* /* /* Start condition */ Slave Address 7bit => 8bit */ Word Address */ Re-Start condition */ Slave 7bit => 8bit for RD */ ack diable for 1 byte */ Stop condition send */ Read Data */ } /*=================================================*/ /* I2C Read command */ /* Usage : I2C_RD_16() */ /* Argument : char add (7bit Slave Address) */ /* char *dbuf (Write data) */ /* unsigned char n (Number of bytes)*/ /* Return value : 16bit Read result */ /*=================================================*/ short I2C_RD_16 (unsigned char add, char *dbuf, unsigned char n) { int i= 0; char n_w; uint8_t rd_fifo[2] = {0, 0}; n_w = n - 1; /* I2C Pre-WR Access */ I2C1_Start(); /* Start condition */ I2C1_MastrSel(add, 0); /* Slave Address 7bit => 8bit */ while (n_w--) { I2C1_senddata(dbuf[i]); /* Send Data */ i++; 2013 - 2015 OMRON Corporation. All Rights Reserved. Copyright } I2C1_Stop(); /* Stop condition */ adc_wait(5); /* 5msec wait */ 22 [D6F-PH] Application Note No.MDMK-14-0386 i++; } I2C1_Stop(); /* Stop condition */ adc_wait(5); /* 5msec wait */ I2C1_Start(); I2C1_MastrSel(add, 0); I2C1_senddata(dbuf[n-1]); I2C1_Start(); I2C1_MastrSel(add, 1); I2C1_AckEn(); rd_fifo[0] = I2C1_rcvdata(); I2C1_AckDis(); I2C1_Stop(); rd_fifo[1] = I2C1_rcvdata(); RD_FIFO = ((rd_fifo[0] << 8) return RD_FIFO; /* Start condition */ /* Slave Address 7bit => 8bit */ /* Word Address */ /* Re-Start condition */ /* Slave 7bit => 8bit for RD */ /* ack enable send after MSB 1 byte read */ /* Read Data */ /* ack diable send after LSB 1 byte read */ /* Stop condition send */ /* Read Data */ | rd_fifo[1]); } /*=================================================*/ /* I2C Read command */ /* Usage : I2C_RD_u16() */ /* Argument : char add (7bit Slave Address) */ /* char *dbuf (Write data) */ /* unsigned char n (Number of bytes)*/ /* Return value : 16bit Read result */ /*=================================================*/ unsigned short I2C_RD_u16 (unsigned char add, char *dbuf, unsigned char n) { int i= 0; char n_w; uint8_t rd_fifo[2] = {0, 0}; n_w = n - 1; /* I2C Pre-WR Access */ I2C1_Start(); I2C1_MastrSel(add, 0); while (n_w--) { I2C1_senddata(dbuf[i]); i++; } I2C1_Stop(); /* Start condition */ /* Slave Address 7bit => 8bit */ /* Send Data */ /* Stop condition */ adc_wait(5); /* 5msec wait */ I2C1_Start(); /* Start condition */ I2C1_MastrSel(add, 0); /* Slave Address 7bit => 8bit */ I2C1_senddata(dbuf[n-1]); /* Word Address */ I2C1_Start(); /* Re-Start condition */ Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. I2C1_MastrSel(add, 1); /* Slave 7bit => 8bit for RD */ I2C1_AckEn(); /* ack enable send after MSB 1 byte read */ rd_fifo[0] = I2C1_rcvdata(); /* Read Data */ I2C1_AckDis(); /* ack diable send after LSB 1 byte read */ I2C1_Stop(); /* Stop condition send */ 23 [D6F-PH] I2C1_Start(); /* I2C1_MastrSel(add, 1); /* I2C1_AckEn(); /* rd_fifo[0] = I2C1_rcvdata(); /* I2C1_AckDis(); /* I2C1_Stop(); /* rd_fifo[1] = I2C1_rcvdata(); /* uRD_FIFO = ((rd_fifo[0] << 8) | return uRD_FIFO; Application Note No.MDMK-14-0386 Re-Start condition */ Slave 7bit => 8bit for RD */ ack enable send after MSB 1 byte read */ Read Data */ ack diable send after LSB 1 byte read */ Stop condition send */ Read Data */ rd_fifo[1]); } void I2C1_Init(){ I2C_InitTypeDef I2C1_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // start clock of I2C I2C1_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C1_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C1_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C1_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C1_InitStructure.I2C_ClockSpeed = 400000; GPIO_InitTypeDef GPIO_InitStructure; // make instance of InitStructure RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // start clock of GPIO pins GPIO_InitStructure.GPIO_Pin =( I2C1_SCL_PIN | I2C1_SDA_PIN ); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_DeInit(I2C1); I2C_Init(I2C1, &I2C1_InitStructure); // Initialize with above parameters I2C_Cmd(I2C1, ENABLE); } void I2C1_Start(){ I2C_GenerateSTART(I2C1,ENABLE); // issue start condition while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); } void I2C1_MastrSel( uint8_t address, uint8_t RW){ uint8_t direct; uint32_t event; direct =(RW == 0)?I2C_Direction_Transmitter : I2C_Direction_Receiver; event =(RW == 0)?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED; I2C_Send7bitAddress(I2C1,(address <<All 1),direct ); //write to Slave Copyright 2013 - 2015 OMRON Corporation. Rights Reserved. while(!I2C_CheckEvent(I2C1, event)); // wait ACK } void I2C1_senddata(uint8_t data){ I2C_SendData(I2C1, data); : 24 //受信コマンドを発信す [D6F-PH] Application Note No.MDMK-14-0386 I2C_Send7bitAddress(I2C1,(address << 1),direct ); //write to Slave while(!I2C_CheckEvent(I2C1, event)); // wait ACK } void I2C1_senddata(uint8_t data){ I2C_SendData(I2C1, data); //transmit the received command while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // wait ACK } uint8_t I2C1_rcvdata(void){ while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)); // wait ACK return I2C_ReceiveData(I2C1); // receive 4th 8bit data } void I2C1_Stop(){ I2C_GenerateSTOP(I2C1, ENABLE); } // put stop condition void I2C1_AckEn(){ I2C_AcknowledgeConfig(I2C1, ENABLE); } // ack enable void I2C1_AckDis(){ I2C_AcknowledgeConfig(I2C1, DISABLE); // ack disable } Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 25 [D6F-PH] Application Note No.MDMK-14-0386 12. WARRANTY AND LIMITED LIABILITY Thank you for your usage of products of Omron Corporation (“Omron”). Without any special agreements, this Terms and Conditions shall apply to all transactions regardless of who sells. Place an order, accepting this Terms and Conditions. 1. DEFINITIONS The following terms used herein have following meaning. (1) Omron Products; Electronic components sold by Omron (2) Catalogues; Any and all catalogues (including the Components Catalogue), specifications, instructions and manuals relating to Omron Products, including electronically provided data. (3) Conditions; Use conditions, rating, performance, operating environment, handling procedure, precautions and/or prohibited use of Omron Products described in the Catalogues. (4) User Application(s); Application of Omron Products by a customer, including but not limited to embedding Omron Products into customer’s components, electronic circuit boards, devices, equipments or systems (5) Fitness; (a)performance, (b) no infringement of intellectual property of third party, (c) compliance with laws and regulations and (d)conformity to various standards by Omron Products in User Applications. 2. NOTE ABOUT DESCRIPTIONS Please understand following as to contents of the Catalogues. (1) Rating and performance is tested separately. Combined conditions are not warranted. (2) Reference data is intended to be used just for reference. Omron does NOT warrant that the Omron Product can work properly in the range of reference data. (3) Examples are intended for reference. Omron does not warrant the Fitness in usage of the examples. (4) Omron may discontinue Omron Products or change specifications of them because of improvements or other reasons. 3. NOTE ABOUT USE Please understand followings as to your adoption and use of Omron Products (1) Please use the product in conformance to the Conditions, including rating and performance. (2) Please confirm the Fitness and decide whether or not Omron Products are able to be adopted in the User Application. (3) Omron will not warrant any items in 1.(5) (a) to (d) of User Application nor the Fitness. (4) If you use Omron Products in the application below, please ensure followings; (i) allowance in aspect of rating and performance, (ii) safety design which can minimize danger of the Application when the product does not work properly and (iii) periodical maintenance of the product and the Application. (a) Applications requiring safety, including, without limitation, nuclear control facilities, combustion facilities, aerospace and aviation facilities, railroad facilities, elevating facilities, amusement facilities, medical facilities, safety devices or other applications which has possibility to influence lives or bodies (b) Applications requiring high reliability, including, without limitation, supplying systems of gas, water and electric power and applications handling right, title, ownership or property, such as payment systems (c) Applications in a harsh condition or environment, including, without limitation, outdoor facilities, facilities with potential of chemical contamination or electromagnetic interference, facilities with vibration or impact and facilities on continual operation for a long period (d) Applications under conditions or environment which are not described in this specification (5) Omron Products shown in this catalogue are not intended to be used in automotive applications (including two wheel vehicles). Please DO NOT use the Omron Products in the automotive application. (6)THE PRODUCTS CONTAINED IN THIS CATALOG ARE NOT SAFETY RATED. THEY Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 26 [D6F-PH] Application Note No.MDMK-14-0386 ARE NOT DESIGNED OR RATED FOR ENSURING SAFETY OF PERSONS, AND SHOULD NOT BE RELIED UPON AS A SAFETY COMPONENT OR PROTECTIVE DEVICE FOR SUCH PURPOSES. Please refer to separate catalogs for OMRON's safety rated products. 4. WARRANTY Warranty of Omron Products is subject to followings. (1) Warranty Period; One year after your purchase (2) Warranty; Omron will provide, free of charge, replacements of the same number of malfunctioning products (3) Exceptions; This warranty does not cover malfunctions caused by any of the following. (a) Usage in the manner other than its original purpose (b) Usage out of the Conditions (c) Cause which could not be foreseen by the level of science and technology at the time of shipment of the product (d) Cause outside Omron or Omron Products, including force majeure such as disasters 5. LIMITATION ON LIABILITY THE WARRANTY DESCRIBED IN THIS “TERMS AND CONDITIONS” IS A WHOLE AND SOLE LIABILITY FOR OMRON PRODUCTS. THERE ARE NO OTHER WARRANTIES, EXPRESSED OR IMPLIED. OMRON AND DISTRIBUTORS ARE NOT LIABLE FOR ANY DAMAGES ARISEN FROM OR RELATING TO OMRON PRODUCTS. 6. PROGRAMMABLE PRODUCTS OMRON shall not be responsible for the user's programming of a programmable product, or any consequence thereof. 7. EXPORT CONTROLS Buyer shall comply with all applicable laws and regulations of Japan and/or other related countries at the time of export or provision to non-citizens of Omron Products or their technical information. EC200E Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 27 [D6F-PH] Application Note No.MDMK-14-0386 13. CONTACT OMRON Electronic Components Web http://www.omron.com/ecb/index.html Contact Us For further inquiry such as delivery, price, sample and/or specification, please contact your local agency or Omron sales representative. Global Sales Office http://www.omron.com/ecb/service/network.html Mail Contact http://www.omron.com/ecb/contact/index.html Phone Micro Devices H.Q. Tel: (81) 77-588-9200 686-1 Ichimiyake, Yasu, Shiga, 520-2362 JAPAN Place an order, accepting this Terms and Conditions. http://www.omron.com/ecb/products/order/index.html 14. History Revision Rev 1.0 Rev 2.0 DATE Oct 1,2013 Mar 27, 2014 Rev 3.0 Aug 07, 2015 Note New Released At table3 & table5 of section7, add expression of the initialize register. At I2C command example of section10, add the ACK to Initialization after power up. Add CRC Calculation and Hardware reset. Section7(7.3.3),9(Fig.8),10(P17) Copyright 2013 - 2015 OMRON Corporation. All Rights Reserved. 28