Maciej Zemlo
/
IES-R1
default version
main.cpp@0:52c8f009b307, 2019-05-14 (annotated)
- Committer:
- yabool2001
- Date:
- Tue May 14 09:28:55 2019 +0000
- Revision:
- 0:52c8f009b307
default version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yabool2001 | 0:52c8f009b307 | 1 | #include "mbed.h" |
yabool2001 | 0:52c8f009b307 | 2 | |
yabool2001 | 0:52c8f009b307 | 3 | uint8_t temp_offset = 0xFB; |
yabool2001 | 0:52c8f009b307 | 4 | //uint8_t temp_offset = 0x00; |
yabool2001 | 0:52c8f009b307 | 5 | uint8_t hum_offset = 0x00 ; |
yabool2001 | 0:52c8f009b307 | 6 | |
yabool2001 | 0:52c8f009b307 | 7 | Serial serial ( PA_9 , NC ); |
yabool2001 | 0:52c8f009b307 | 8 | |
yabool2001 | 0:52c8f009b307 | 9 | // IES LEDs |
yabool2001 | 0:52c8f009b307 | 10 | DigitalOut led1 ( PB_5 ); |
yabool2001 | 0:52c8f009b307 | 11 | DigitalOut led2 ( PB_2 ); |
yabool2001 | 0:52c8f009b307 | 12 | DigitalOut led3 ( PH_1 ); |
yabool2001 | 0:52c8f009b307 | 13 | |
yabool2001 | 0:52c8f009b307 | 14 | DigitalIn pulse ( PA_2 ); |
yabool2001 | 0:52c8f009b307 | 15 | |
yabool2001 | 0:52c8f009b307 | 16 | // I2C conf ( SDA , SCK ) |
yabool2001 | 0:52c8f009b307 | 17 | I2C i2c ( PB_9 , PB_8 ); |
yabool2001 | 0:52c8f009b307 | 18 | |
yabool2001 | 0:52c8f009b307 | 19 | char i2c_data[1] = { 0 }; |
yabool2001 | 0:52c8f009b307 | 20 | char i2c_data2[2] = { 0 , 0 }; |
yabool2001 | 0:52c8f009b307 | 21 | char i2c_data4[4] = { 0 , 0 , 0 , 0 }; |
yabool2001 | 0:52c8f009b307 | 22 | |
yabool2001 | 0:52c8f009b307 | 23 | const int i2c_addr_read = 129; |
yabool2001 | 0:52c8f009b307 | 24 | const int i2c_addr_write = 128; |
yabool2001 | 0:52c8f009b307 | 25 | |
yabool2001 | 0:52c8f009b307 | 26 | char conf_reg[1] = { 0x0E }; |
yabool2001 | 0:52c8f009b307 | 27 | char meas_conf_reg[1] = { 0x0F }; |
yabool2001 | 0:52c8f009b307 | 28 | char manf_id_low_reg[1] = { 0xFC }; |
yabool2001 | 0:52c8f009b307 | 29 | char temp_max_reg[1] = { 0x05 }; |
yabool2001 | 0:52c8f009b307 | 30 | char temp_low_reg[1] = { 0x00 }; |
yabool2001 | 0:52c8f009b307 | 31 | |
yabool2001 | 0:52c8f009b307 | 32 | char conf_reg_init[2] = { 0x0F , 0x00 }; |
yabool2001 | 0:52c8f009b307 | 33 | char meas_conf_reg_init[2] = { 0x0F , 0x00 }; |
yabool2001 | 0:52c8f009b307 | 34 | char meas_conf_reg_reset[2] = { 0x0E , 0x80 }; |
yabool2001 | 0:52c8f009b307 | 35 | char meas_conf_reg_heat_on[2] = { 0x0E , 0x08 }; |
yabool2001 | 0:52c8f009b307 | 36 | char temp_offset_reg[2] = { 0x08 , temp_offset }; |
yabool2001 | 0:52c8f009b307 | 37 | |
yabool2001 | 0:52c8f009b307 | 38 | char meas_conf_reg_meas[2] = { 0x0F , 0x01 }; // start_meas: 14bit, t&h , meas |
yabool2001 | 0:52c8f009b307 | 39 | //char meas_conf_reg_meas[2] = { 0x0F , 0x51 }; // start_meas: 11bit, t&h , meas |
yabool2001 | 0:52c8f009b307 | 40 | //char meas_conf_reg_meas[2] = { 0x0F , 0xA1 }; // start_meas: t_8bit, h_8bit , t&h , meas |
yabool2001 | 0:52c8f009b307 | 41 | |
yabool2001 | 0:52c8f009b307 | 42 | // HDC init |
yabool2001 | 0:52c8f009b307 | 43 | void hdc_init () |
yabool2001 | 0:52c8f009b307 | 44 | { |
yabool2001 | 0:52c8f009b307 | 45 | serial .printf ( "\n\r" ); |
yabool2001 | 0:52c8f009b307 | 46 | |
yabool2001 | 0:52c8f009b307 | 47 | //Konfiguracja startowa rejestrów |
yabool2001 | 0:52c8f009b307 | 48 | i2c.write ( i2c_addr_write , conf_reg_init , 2 , false ); |
yabool2001 | 0:52c8f009b307 | 49 | i2c.write ( i2c_addr_write , meas_conf_reg_init , 2 , false ); |
yabool2001 | 0:52c8f009b307 | 50 | |
yabool2001 | 0:52c8f009b307 | 51 | // Wdrożenie offsetu |
yabool2001 | 0:52c8f009b307 | 52 | i2c.write ( i2c_addr_write , temp_offset_reg , 2 , false ); |
yabool2001 | 0:52c8f009b307 | 53 | |
yabool2001 | 0:52c8f009b307 | 54 | //Weryfikacja konfiguracji startowej rejestrów |
yabool2001 | 0:52c8f009b307 | 55 | i2c.write ( i2c_addr_write , conf_reg , 1 , true ); |
yabool2001 | 0:52c8f009b307 | 56 | i2c.read ( i2c_addr_read , i2c_data , 1 , false ); |
yabool2001 | 0:52c8f009b307 | 57 | serial .printf ( "0x0E Conf. reg.: %X\n\r" , i2c_data[0] ); |
yabool2001 | 0:52c8f009b307 | 58 | i2c.write ( i2c_addr_write , meas_conf_reg , 1 , true ); |
yabool2001 | 0:52c8f009b307 | 59 | i2c.read ( i2c_addr_read , i2c_data , 1 , false ); |
yabool2001 | 0:52c8f009b307 | 60 | serial .printf ( "0x0F Meas conf. reg.: %X\n\r" , i2c_data[0] ); |
yabool2001 | 0:52c8f009b307 | 61 | |
yabool2001 | 0:52c8f009b307 | 62 | // Weryfikacja adresu Manf. Id Low |
yabool2001 | 0:52c8f009b307 | 63 | i2c.write ( i2c_addr_write , manf_id_low_reg , 1 , true ); |
yabool2001 | 0:52c8f009b307 | 64 | i2c.read ( i2c_addr_read , i2c_data , 1 , false ); |
yabool2001 | 0:52c8f009b307 | 65 | serial .printf ( "HDC manf_id_low_reg: %X initiated\n\r" , i2c_data[0] ); |
yabool2001 | 0:52c8f009b307 | 66 | } |
yabool2001 | 0:52c8f009b307 | 67 | |
yabool2001 | 0:52c8f009b307 | 68 | void hdc_soft_reset () |
yabool2001 | 0:52c8f009b307 | 69 | { |
yabool2001 | 0:52c8f009b307 | 70 | serial .printf ( "\n\r" ); |
yabool2001 | 0:52c8f009b307 | 71 | // Soft reset |
yabool2001 | 0:52c8f009b307 | 72 | i2c.write ( i2c_addr_write , meas_conf_reg_reset , 2 , false ); |
yabool2001 | 0:52c8f009b307 | 73 | // Wait to perform reset |
yabool2001 | 0:52c8f009b307 | 74 | wait ( 1 ); |
yabool2001 | 0:52c8f009b307 | 75 | serial .printf ( "Soft reset perfomed\n\r" ); |
yabool2001 | 0:52c8f009b307 | 76 | } |
yabool2001 | 0:52c8f009b307 | 77 | |
yabool2001 | 0:52c8f009b307 | 78 | void hdc_heater ( uint8_t t ) |
yabool2001 | 0:52c8f009b307 | 79 | { |
yabool2001 | 0:52c8f009b307 | 80 | serial .printf ( "\n\r" ); |
yabool2001 | 0:52c8f009b307 | 81 | // Soft reset |
yabool2001 | 0:52c8f009b307 | 82 | i2c.write ( i2c_addr_write , meas_conf_reg_reset , 2 , false ); |
yabool2001 | 0:52c8f009b307 | 83 | // Wait to perform reset |
yabool2001 | 0:52c8f009b307 | 84 | wait ( t ); |
yabool2001 | 0:52c8f009b307 | 85 | serial .printf ( "Heater switched on for 5s\n\r" ); |
yabool2001 | 0:52c8f009b307 | 86 | } |
yabool2001 | 0:52c8f009b307 | 87 | |
yabool2001 | 0:52c8f009b307 | 88 | void hdc_single_acqusition () |
yabool2001 | 0:52c8f009b307 | 89 | { |
yabool2001 | 0:52c8f009b307 | 90 | i2c.write ( i2c_addr_write , meas_conf_reg_meas , 2 , false ); |
yabool2001 | 0:52c8f009b307 | 91 | wait ( 0.2 ); |
yabool2001 | 0:52c8f009b307 | 92 | i2c.write ( i2c_addr_write , temp_low_reg , 1 , true ); |
yabool2001 | 0:52c8f009b307 | 93 | i2c.read ( i2c_addr_read , i2c_data4 , 4 , false ); |
yabool2001 | 0:52c8f009b307 | 94 | //serial .printf ( "HDC Single acquisition raw[hex] 3-0: %X , %X , %X , %X \n\r" , i2c_data4[3] , i2c_data4[2] , i2c_data4[1] , i2c_data4[0] ); |
yabool2001 | 0:52c8f009b307 | 95 | double temp = ( i2c_data4 [1] << 8 ) + i2c_data4 [0]; |
yabool2001 | 0:52c8f009b307 | 96 | temp = ( ( temp * 165 ) / 65536 ) - 40 ; |
yabool2001 | 0:52c8f009b307 | 97 | serial .printf ( "temp: %f\n\r" , temp ); |
yabool2001 | 0:52c8f009b307 | 98 | } |
yabool2001 | 0:52c8f009b307 | 99 | |
yabool2001 | 0:52c8f009b307 | 100 | void hdc_temp_max () |
yabool2001 | 0:52c8f009b307 | 101 | { |
yabool2001 | 0:52c8f009b307 | 102 | i2c.write ( i2c_addr_write , temp_max_reg , 1 , true ); |
yabool2001 | 0:52c8f009b307 | 103 | i2c.read ( i2c_addr_read , i2c_data , 1 , false ); |
yabool2001 | 0:52c8f009b307 | 104 | //serial .printf ( "HDC Max temp. raw[hex]: %X\n\r" , i2c_data [0] ); |
yabool2001 | 0:52c8f009b307 | 105 | double temp = i2c_data [0]; |
yabool2001 | 0:52c8f009b307 | 106 | temp = ( ( temp * 165 ) / 256 ) - 40 ; |
yabool2001 | 0:52c8f009b307 | 107 | serial .printf ( "HDC Max temp. = %f\n\r" , temp ); |
yabool2001 | 0:52c8f009b307 | 108 | } |
yabool2001 | 0:52c8f009b307 | 109 | |
yabool2001 | 0:52c8f009b307 | 110 | // Toggle LED |
yabool2001 | 0:52c8f009b307 | 111 | void toggle_led () |
yabool2001 | 0:52c8f009b307 | 112 | { |
yabool2001 | 0:52c8f009b307 | 113 | serial .printf ( "led toggle\n\r" ); |
yabool2001 | 0:52c8f009b307 | 114 | led1 = !led1; |
yabool2001 | 0:52c8f009b307 | 115 | led3 = !led2; |
yabool2001 | 0:52c8f009b307 | 116 | led3 = !led3; |
yabool2001 | 0:52c8f009b307 | 117 | } |
yabool2001 | 0:52c8f009b307 | 118 | |
yabool2001 | 0:52c8f009b307 | 119 | int main() { |
yabool2001 | 0:52c8f009b307 | 120 | |
yabool2001 | 0:52c8f009b307 | 121 | led1 = 0; |
yabool2001 | 0:52c8f009b307 | 122 | led2 = 0; |
yabool2001 | 0:52c8f009b307 | 123 | led3 = 0; |
yabool2001 | 0:52c8f009b307 | 124 | |
yabool2001 | 0:52c8f009b307 | 125 | i2c.frequency ( 1000000 ); |
yabool2001 | 0:52c8f009b307 | 126 | |
yabool2001 | 0:52c8f009b307 | 127 | hdc_init () ; |
yabool2001 | 0:52c8f009b307 | 128 | //hdc_soft_reset () ; |
yabool2001 | 0:52c8f009b307 | 129 | //hdc_heater ( 5 ); |
yabool2001 | 0:52c8f009b307 | 130 | //hdc_init () ; |
yabool2001 | 0:52c8f009b307 | 131 | |
yabool2001 | 0:52c8f009b307 | 132 | while ( 1 ) |
yabool2001 | 0:52c8f009b307 | 133 | { |
yabool2001 | 0:52c8f009b307 | 134 | for ( int i = 0 ; i < 10 ; i++ ) |
yabool2001 | 0:52c8f009b307 | 135 | { |
yabool2001 | 0:52c8f009b307 | 136 | hdc_single_acqusition () ; |
yabool2001 | 0:52c8f009b307 | 137 | wait ( 10 ) ; |
yabool2001 | 0:52c8f009b307 | 138 | } |
yabool2001 | 0:52c8f009b307 | 139 | hdc_temp_max () ; |
yabool2001 | 0:52c8f009b307 | 140 | } |
yabool2001 | 0:52c8f009b307 | 141 | } |