An example program for Rohm SensorShield-EVK-001 running with Mbed OS 5.
Dependencies: BM1422AGMV rohm-rpr0521 rohm-sensor-hal rohm-bh1790glc-driver BH1749NUC KX224-1053 BM1383AGLV RegisterWriter
Getting Started with Rohm SensorShield-EVK-001
This example demonstrates how to use the SensorShield-EVK-001 component with multiple sensors. those measured values, which received through a serial connection from a mbed board, are printed out in a terminal software window.
Setup
The SensorShield-EVK-001 is plugged on top of a mbed board and a serial connection should be set up between the mbed board and PC with the following parameters:
- baud rate: 115200
- data: 8 bit
- parity: none
- stop: 1bit
- flow control: none
Terminal parameters
Measured Data Output
Source/main.cpp@0:a9aeed8cd62e, 2019-02-18 (annotated)
- Committer:
- Ren Boting
- Date:
- Mon Feb 18 15:08:30 2019 +0900
- Revision:
- 0:a9aeed8cd62e
- Child:
- 2:d022d883a31f
initial commit for Rohm SensorShield-EVK-001 example.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ren Boting | 0:a9aeed8cd62e | 1 | /* Rohm SensorShield-EVK-xxx Example |
Ren Boting | 0:a9aeed8cd62e | 2 | * Copyright (c) 2019 ARM Limited |
Ren Boting | 0:a9aeed8cd62e | 3 | * |
Ren Boting | 0:a9aeed8cd62e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Ren Boting | 0:a9aeed8cd62e | 5 | * you may not use this file except in compliance with the License. |
Ren Boting | 0:a9aeed8cd62e | 6 | * You may obtain a copy of the License at |
Ren Boting | 0:a9aeed8cd62e | 7 | * |
Ren Boting | 0:a9aeed8cd62e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ren Boting | 0:a9aeed8cd62e | 9 | * |
Ren Boting | 0:a9aeed8cd62e | 10 | * Unless required by applicable law or agreed to in writing, software |
Ren Boting | 0:a9aeed8cd62e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Ren Boting | 0:a9aeed8cd62e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Ren Boting | 0:a9aeed8cd62e | 13 | * See the License for the specific language governing permissions and |
Ren Boting | 0:a9aeed8cd62e | 14 | * limitations under the License. |
Ren Boting | 0:a9aeed8cd62e | 15 | */ |
Ren Boting | 0:a9aeed8cd62e | 16 | |
Ren Boting | 0:a9aeed8cd62e | 17 | #include "BM1383AGLV.h" |
Ren Boting | 0:a9aeed8cd62e | 18 | #include "BM1422AGMV.h" |
Ren Boting | 0:a9aeed8cd62e | 19 | #include "KX224.h" |
Ren Boting | 0:a9aeed8cd62e | 20 | #include "rohm-bh1790glc-driver/bh1790glc.h" |
Ren Boting | 0:a9aeed8cd62e | 21 | #include "rohm-rpr0521/rohm-rpr0521/rpr0521.h" |
Ren Boting | 0:a9aeed8cd62e | 22 | #include "rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h" |
Ren Boting | 0:a9aeed8cd62e | 23 | #include "rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h" |
Ren Boting | 0:a9aeed8cd62e | 24 | |
Ren Boting | 0:a9aeed8cd62e | 25 | |
Ren Boting | 0:a9aeed8cd62e | 26 | |
Ren Boting | 0:a9aeed8cd62e | 27 | void clearFlo(float * data, int length); |
Ren Boting | 0:a9aeed8cd62e | 28 | void clearUint(uint16_t * data, int length); |
Ren Boting | 0:a9aeed8cd62e | 29 | BM1383AGLV _bm1383aglv(I2C_SDA, I2C_SCL); |
Ren Boting | 0:a9aeed8cd62e | 30 | BM1422AGMV _bm1422agmv(I2C_SDA, I2C_SCL); |
Ren Boting | 0:a9aeed8cd62e | 31 | KX224 _kx224_1053(I2C_SDA, I2C_SCL); |
Ren Boting | 0:a9aeed8cd62e | 32 | RegisterWriter i2c_rw_bh(I2C_SDA, I2C_SCL); |
Ren Boting | 0:a9aeed8cd62e | 33 | BH1790GLC _bh1790glc(i2c_rw_bh); |
Ren Boting | 0:a9aeed8cd62e | 34 | AnalogIn _bd1020hfv(A2); |
Ren Boting | 0:a9aeed8cd62e | 35 | |
Ren Boting | 0:a9aeed8cd62e | 36 | Serial pc(USBTX, USBRX); |
Ren Boting | 0:a9aeed8cd62e | 37 | |
Ren Boting | 0:a9aeed8cd62e | 38 | int main() { |
Ren Boting | 0:a9aeed8cd62e | 39 | float axis_ac[3], temp1_bd1020hfv, temp2_bd1020hfv ; |
Ren Boting | 0:a9aeed8cd62e | 40 | bool err_rpr0521; |
Ren Boting | 0:a9aeed8cd62e | 41 | int err_bh1790, err_KX224; |
Ren Boting | 0:a9aeed8cd62e | 42 | uint16_t _bh1790[2], _rpr0521[3]; |
Ren Boting | 0:a9aeed8cd62e | 43 | |
Ren Boting | 0:a9aeed8cd62e | 44 | pc.printf("\r\nExample program for using following Rohm sensor libraries\r\n"); |
Ren Boting | 0:a9aeed8cd62e | 45 | pc.printf("\n - BM1383AGLV: Pressure/Temperature sensor\n"); |
Ren Boting | 0:a9aeed8cd62e | 46 | pc.printf("\r - BM1422AGMV: 3 axis magnetic sensor\n"); |
Ren Boting | 0:a9aeed8cd62e | 47 | pc.printf("\r - KX224-1053: 3 axis accelerometer\n"); |
Ren Boting | 0:a9aeed8cd62e | 48 | pc.printf("\r - BH1790GLC: optical sensor for heart rate monitor\n"); |
Ren Boting | 0:a9aeed8cd62e | 49 | pc.printf("\r - RPR-0521RS: Light & Proximity Sensor\n"); |
Ren Boting | 0:a9aeed8cd62e | 50 | pc.printf("\r - BD1020HFV: Temperature Sensor, Formula values should be calibrated before use.\n"); |
Ren Boting | 0:a9aeed8cd62e | 51 | //init KX224-1053 |
Ren Boting | 0:a9aeed8cd62e | 52 | err_KX224=_kx224_1053.initialize(); |
Ren Boting | 0:a9aeed8cd62e | 53 | if (err_KX224!=0){ |
Ren Boting | 0:a9aeed8cd62e | 54 | pc.printf("\r\n_kx224_1053 : Failed initialization \r\n"); |
Ren Boting | 0:a9aeed8cd62e | 55 | } |
Ren Boting | 0:a9aeed8cd62e | 56 | |
Ren Boting | 0:a9aeed8cd62e | 57 | do{ //init BH1790GLC |
Ren Boting | 0:a9aeed8cd62e | 58 | err_bh1790 = _bh1790glc.set_default_on(); |
Ren Boting | 0:a9aeed8cd62e | 59 | wait_ms(300); |
Ren Boting | 0:a9aeed8cd62e | 60 | } while (err_bh1790); |
Ren Boting | 0:a9aeed8cd62e | 61 | |
Ren Boting | 0:a9aeed8cd62e | 62 | //init RPR-0521RS |
Ren Boting | 0:a9aeed8cd62e | 63 | I2CCommonBegin(); |
Ren Boting | 0:a9aeed8cd62e | 64 | rpr0521_wait_until_found(); |
Ren Boting | 0:a9aeed8cd62e | 65 | pc.printf("\nRPR-0521RS Sensor found.\r\n"); |
Ren Boting | 0:a9aeed8cd62e | 66 | rpr0521_initial_setup(); |
Ren Boting | 0:a9aeed8cd62e | 67 | |
Ren Boting | 0:a9aeed8cd62e | 68 | while(1) { |
Ren Boting | 0:a9aeed8cd62e | 69 | pc.printf("\r++++++++++++++++++\r"); |
Ren Boting | 0:a9aeed8cd62e | 70 | pc.printf("\r\n_bm1383aglv : pressure=%7.2f, temperature=%5.3f\r\n", _bm1383aglv.getPressure(), _bm1383aglv.getTemperature()); |
Ren Boting | 0:a9aeed8cd62e | 71 | clearFlo(&axis_ac[0],3); |
Ren Boting | 0:a9aeed8cd62e | 72 | _bm1422agmv.get_val(&axis_ac[0]); |
Ren Boting | 0:a9aeed8cd62e | 73 | pc.printf("\r\n_bm1422agmv : X=%7.2f, Y=%7.2f, Z=%7.2f\n\r", axis_ac[0], axis_ac[1], axis_ac[2]); |
Ren Boting | 0:a9aeed8cd62e | 74 | clearFlo(&axis_ac[0],3); |
Ren Boting | 0:a9aeed8cd62e | 75 | _kx224_1053.get_val(&axis_ac[0]); |
Ren Boting | 0:a9aeed8cd62e | 76 | pc.printf("\r\n_kx224_1053 : X=%7.2f, Y=%7.2f, Z=%7.2f\n\r", axis_ac[0], axis_ac[1], axis_ac[2]); |
Ren Boting | 0:a9aeed8cd62e | 77 | clearUint(&_bh1790[0], 2); |
Ren Boting | 0:a9aeed8cd62e | 78 | err_bh1790 = _bh1790glc.getresults(&_bh1790[0]); |
Ren Boting | 0:a9aeed8cd62e | 79 | if(err_bh1790) { |
Ren Boting | 0:a9aeed8cd62e | 80 | pc.printf("\r\nBH1790GLC : Failed reading value from BH1790GLC\r\n"); |
Ren Boting | 0:a9aeed8cd62e | 81 | } else |
Ren Boting | 0:a9aeed8cd62e | 82 | { |
Ren Boting | 0:a9aeed8cd62e | 83 | pc.printf("\r\nBH1790GLC : value= %d, \t%d\n\r", _bh1790[1], _bh1790[0]); |
Ren Boting | 0:a9aeed8cd62e | 84 | } |
Ren Boting | 0:a9aeed8cd62e | 85 | clearUint(&_rpr0521[0], 3); |
Ren Boting | 0:a9aeed8cd62e | 86 | err_rpr0521 = rpr0521_read_data(&_rpr0521[0]); |
Ren Boting | 0:a9aeed8cd62e | 87 | if(err_rpr0521) { |
Ren Boting | 0:a9aeed8cd62e | 88 | pc.printf("\r\nRPR-0521RS : Failed reading value from RPR-0521RS\r\n"); |
Ren Boting | 0:a9aeed8cd62e | 89 | } else |
Ren Boting | 0:a9aeed8cd62e | 90 | { |
Ren Boting | 0:a9aeed8cd62e | 91 | pc.printf("\r\nRPR-0521RS : PS= %4u, Als0= %4u, Als1= %4u\r\n", _rpr0521[0], _rpr0521[1], _rpr0521[2]); |
Ren Boting | 0:a9aeed8cd62e | 92 | } |
Ren Boting | 0:a9aeed8cd62e | 93 | //Input voltage 0-3.3V == 0.0-1.0f |
Ren Boting | 0:a9aeed8cd62e | 94 | //-40'C == 1.87V |
Ren Boting | 0:a9aeed8cd62e | 95 | // 0'C == 1.546V |
Ren Boting | 0:a9aeed8cd62e | 96 | //192'C == 0V (out of scale) |
Ren Boting | 0:a9aeed8cd62e | 97 | //1.87V / 232'C = 0.008060V/'C |
Ren Boting | 0:a9aeed8cd62e | 98 | temp1_bd1020hfv = -(1000 * (_bd1020hfv * 3.3f) - 1546) / 8.2; |
Ren Boting | 0:a9aeed8cd62e | 99 | temp2_bd1020hfv = 192 - ( _bd1020hfv * 3.3f / 0.008060 ); |
Ren Boting | 0:a9aeed8cd62e | 100 | pc.printf("\r\nBD1020HFV : formula1= %5.3f, formula2= %5.3f\r\n", temp1_bd1020hfv, temp2_bd1020hfv); |
Ren Boting | 0:a9aeed8cd62e | 101 | wait(3); |
Ren Boting | 0:a9aeed8cd62e | 102 | } |
Ren Boting | 0:a9aeed8cd62e | 103 | } |
Ren Boting | 0:a9aeed8cd62e | 104 | |
Ren Boting | 0:a9aeed8cd62e | 105 | void clearFlo(float * data, int length) |
Ren Boting | 0:a9aeed8cd62e | 106 | { |
Ren Boting | 0:a9aeed8cd62e | 107 | if(data==NULL) { |
Ren Boting | 0:a9aeed8cd62e | 108 | pc.printf("\r\nFailed in clearFlo, data ptr is NULL!!\r\n"); |
Ren Boting | 0:a9aeed8cd62e | 109 | }else |
Ren Boting | 0:a9aeed8cd62e | 110 | { |
Ren Boting | 0:a9aeed8cd62e | 111 | for(int i=0; i<length; i++) |
Ren Boting | 0:a9aeed8cd62e | 112 | *(data+i) = 0.0f; |
Ren Boting | 0:a9aeed8cd62e | 113 | } |
Ren Boting | 0:a9aeed8cd62e | 114 | } |
Ren Boting | 0:a9aeed8cd62e | 115 | |
Ren Boting | 0:a9aeed8cd62e | 116 | void clearUint(uint16_t * data, int length) |
Ren Boting | 0:a9aeed8cd62e | 117 | { |
Ren Boting | 0:a9aeed8cd62e | 118 | if(data==NULL) { |
Ren Boting | 0:a9aeed8cd62e | 119 | pc.printf("\r\nFailed in clearUint, data ptr is NULL!!\r\n"); |
Ren Boting | 0:a9aeed8cd62e | 120 | }else |
Ren Boting | 0:a9aeed8cd62e | 121 | { |
Ren Boting | 0:a9aeed8cd62e | 122 | for(int i=0; i<length; i++) |
Ren Boting | 0:a9aeed8cd62e | 123 | *(data+i) = 0; |
Ren Boting | 0:a9aeed8cd62e | 124 | } |
Ren Boting | 0:a9aeed8cd62e | 125 | } |
Ren Boting | 0:a9aeed8cd62e | 126 |