BLE Mesh Light Sensor output to FRDMK64f
Fork of Hexi_Blinky_Example by
main.cpp@20:7466373c9234, 2017-12-06 (annotated)
- Committer:
- roborags
- Date:
- Wed Dec 06 22:54:33 2017 +0000
- Revision:
- 20:7466373c9234
- Parent:
- 19:ffd78d964d9f
BLE Mesh Light Sensor to FRDMK64F
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| roborags | 14:02ddfa711646 | 1 | |
| dan | 0:7dec7e9ac085 | 2 | #include "mbed.h" |
| roborags | 14:02ddfa711646 | 3 | #include "math.h" |
| roborags | 14:02ddfa711646 | 4 | #include <string> |
| roborags | 14:02ddfa711646 | 5 | |
| roborags | 15:67a7cca7ae06 | 6 | Serial FRDM_UART_Debug(USBTX,USBRX); |
| roborags | 20:7466373c9234 | 7 | Serial FRDM_Data_Tx(PTC17,PTC16); |
| roborags | 15:67a7cca7ae06 | 8 | I2C I2C_Bus(PTE25,PTE24); |
| roborags | 15:67a7cca7ae06 | 9 | |
| roborags | 18:293c2885af81 | 10 | |
| roborags | 15:67a7cca7ae06 | 11 | const int Light_I2C_Addr = 0x88; |
| roborags | 20:7466373c9234 | 12 | float Light_Out = 0; |
| roborags | 14:02ddfa711646 | 13 | |
| roborags | 14:02ddfa711646 | 14 | |
| roborags | 14:02ddfa711646 | 15 | int main() |
| roborags | 14:02ddfa711646 | 16 | { |
| roborags | 14:02ddfa711646 | 17 | |
| roborags | 15:67a7cca7ae06 | 18 | FRDM_UART_Debug.baud(115200); // Baud rate used for communicating with Tera-term on PC |
| roborags | 20:7466373c9234 | 19 | FRDM_Data_Tx.baud(115200); |
| roborags | 15:67a7cca7ae06 | 20 | |
| roborags | 15:67a7cca7ae06 | 21 | I2C_Bus.frequency(100000); // set required i2c frequency |
| roborags | 15:67a7cca7ae06 | 22 | |
| roborags | 14:02ddfa711646 | 23 | while (1) |
| roborags | 14:02ddfa711646 | 24 | { |
| roborags | 20:7466373c9234 | 25 | |
| roborags | 20:7466373c9234 | 26 | uint16_t Light = 0; |
| roborags | 14:02ddfa711646 | 27 | |
| roborags | 20:7466373c9234 | 28 | char I2C_Cmd[3]; |
| roborags | 20:7466373c9234 | 29 | int Temp_i_1 = 0; |
| roborags | 20:7466373c9234 | 30 | int Temp_i_2 = 0; |
| roborags | 20:7466373c9234 | 31 | int Temp_i_3 = 0; |
| roborags | 20:7466373c9234 | 32 | int loop_count = 0; |
| roborags | 15:67a7cca7ae06 | 33 | |
| roborags | 15:67a7cca7ae06 | 34 | I2C_Cmd[0] = 0x01; //configuration register |
| roborags | 15:67a7cca7ae06 | 35 | I2C_Cmd[1]= 0xCC; //configuration data |
| roborags | 15:67a7cca7ae06 | 36 | I2C_Cmd[2]= 0x01; //configuration data |
| roborags | 15:67a7cca7ae06 | 37 | |
| roborags | 15:67a7cca7ae06 | 38 | I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 3); |
| roborags | 15:67a7cca7ae06 | 39 | |
| roborags | 15:67a7cca7ae06 | 40 | I2C_Cmd[0] = 0x00; // data register |
| roborags | 15:67a7cca7ae06 | 41 | |
| roborags | 15:67a7cca7ae06 | 42 | I2C_Bus.write(Light_I2C_Addr, I2C_Cmd, 1); |
| roborags | 15:67a7cca7ae06 | 43 | |
| roborags | 14:02ddfa711646 | 44 | wait_ms(100); |
| roborags | 14:02ddfa711646 | 45 | |
| roborags | 15:67a7cca7ae06 | 46 | I2C_Bus.read(Light_I2C_Addr, I2C_Cmd, 2); |
| roborags | 14:02ddfa711646 | 47 | |
| roborags | 15:67a7cca7ae06 | 48 | Temp_i_1= I2C_Cmd[0]>>4; |
| roborags | 15:67a7cca7ae06 | 49 | Temp_i_2= (I2C_Cmd[0]-(Temp_i_1<<4))*256+I2C_Cmd[1]; |
| roborags | 15:67a7cca7ae06 | 50 | |
| roborags | 16:39e45e59677c | 51 | for(loop_count = 0,Temp_i_3 = 1 ; loop_count < Temp_i_1 ; Temp_i_3 = Temp_i_3 * 2,loop_count++); |
| roborags | 15:67a7cca7ae06 | 52 | |
| roborags | 15:67a7cca7ae06 | 53 | Light_Out= (Temp_i_2 * Temp_i_3) / 100; |
| roborags | 15:67a7cca7ae06 | 54 | |
| roborags | 20:7466373c9234 | 55 | Light = Light_Out; |
| roborags | 18:293c2885af81 | 56 | |
| roborags | 20:7466373c9234 | 57 | FRDM_Data_Tx.printf("%d\n",Light); |
| roborags | 20:7466373c9234 | 58 | FRDM_UART_Debug.printf("Lux = %.2f\n\r", Light_Out); |
| roborags | 18:293c2885af81 | 59 | |
| roborags | 15:67a7cca7ae06 | 60 | wait(5); |
| roborags | 20:7466373c9234 | 61 | |
| roborags | 15:67a7cca7ae06 | 62 | } |
| roborags | 20:7466373c9234 | 63 | } |
