Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 uint8_t temp_offset = 0xFB; 00004 //uint8_t temp_offset = 0x00; 00005 uint8_t hum_offset = 0x00 ; 00006 00007 Serial serial ( PA_9 , NC ); 00008 00009 // IES LEDs 00010 DigitalOut led1 ( PB_5 ); 00011 DigitalOut led2 ( PB_2 ); 00012 DigitalOut led3 ( PH_1 ); 00013 00014 DigitalIn pulse ( PA_2 ); 00015 00016 // I2C conf ( SDA , SCK ) 00017 I2C i2c ( PB_9 , PB_8 ); 00018 00019 char i2c_data[1] = { 0 }; 00020 char i2c_data2[2] = { 0 , 0 }; 00021 char i2c_data4[4] = { 0 , 0 , 0 , 0 }; 00022 00023 const int i2c_addr_read = 129; 00024 const int i2c_addr_write = 128; 00025 00026 char conf_reg[1] = { 0x0E }; 00027 char meas_conf_reg[1] = { 0x0F }; 00028 char manf_id_low_reg[1] = { 0xFC }; 00029 char temp_max_reg[1] = { 0x05 }; 00030 char temp_low_reg[1] = { 0x00 }; 00031 00032 char conf_reg_init[2] = { 0x0F , 0x00 }; 00033 char meas_conf_reg_init[2] = { 0x0F , 0x00 }; 00034 char meas_conf_reg_reset[2] = { 0x0E , 0x80 }; 00035 char meas_conf_reg_heat_on[2] = { 0x0E , 0x08 }; 00036 char temp_offset_reg[2] = { 0x08 , temp_offset }; 00037 00038 char meas_conf_reg_meas[2] = { 0x0F , 0x01 }; // start_meas: 14bit, t&h , meas 00039 //char meas_conf_reg_meas[2] = { 0x0F , 0x51 }; // start_meas: 11bit, t&h , meas 00040 //char meas_conf_reg_meas[2] = { 0x0F , 0xA1 }; // start_meas: t_8bit, h_8bit , t&h , meas 00041 00042 // HDC init 00043 void hdc_init () 00044 { 00045 serial .printf ( "\n\r" ); 00046 00047 //Konfiguracja startowa rejestrów 00048 i2c.write ( i2c_addr_write , conf_reg_init , 2 , false ); 00049 i2c.write ( i2c_addr_write , meas_conf_reg_init , 2 , false ); 00050 00051 // Wdrożenie offsetu 00052 i2c.write ( i2c_addr_write , temp_offset_reg , 2 , false ); 00053 00054 //Weryfikacja konfiguracji startowej rejestrów 00055 i2c.write ( i2c_addr_write , conf_reg , 1 , true ); 00056 i2c.read ( i2c_addr_read , i2c_data , 1 , false ); 00057 serial .printf ( "0x0E Conf. reg.: %X\n\r" , i2c_data[0] ); 00058 i2c.write ( i2c_addr_write , meas_conf_reg , 1 , true ); 00059 i2c.read ( i2c_addr_read , i2c_data , 1 , false ); 00060 serial .printf ( "0x0F Meas conf. reg.: %X\n\r" , i2c_data[0] ); 00061 00062 // Weryfikacja adresu Manf. Id Low 00063 i2c.write ( i2c_addr_write , manf_id_low_reg , 1 , true ); 00064 i2c.read ( i2c_addr_read , i2c_data , 1 , false ); 00065 serial .printf ( "HDC manf_id_low_reg: %X initiated\n\r" , i2c_data[0] ); 00066 } 00067 00068 void hdc_soft_reset () 00069 { 00070 serial .printf ( "\n\r" ); 00071 // Soft reset 00072 i2c.write ( i2c_addr_write , meas_conf_reg_reset , 2 , false ); 00073 // Wait to perform reset 00074 wait ( 1 ); 00075 serial .printf ( "Soft reset perfomed\n\r" ); 00076 } 00077 00078 void hdc_heater ( uint8_t t ) 00079 { 00080 serial .printf ( "\n\r" ); 00081 // Soft reset 00082 i2c.write ( i2c_addr_write , meas_conf_reg_reset , 2 , false ); 00083 // Wait to perform reset 00084 wait ( t ); 00085 serial .printf ( "Heater switched on for 5s\n\r" ); 00086 } 00087 00088 void hdc_single_acqusition () 00089 { 00090 i2c.write ( i2c_addr_write , meas_conf_reg_meas , 2 , false ); 00091 wait ( 0.2 ); 00092 i2c.write ( i2c_addr_write , temp_low_reg , 1 , true ); 00093 i2c.read ( i2c_addr_read , i2c_data4 , 4 , false ); 00094 //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] ); 00095 double temp = ( i2c_data4 [1] << 8 ) + i2c_data4 [0]; 00096 temp = ( ( temp * 165 ) / 65536 ) - 40 ; 00097 serial .printf ( "temp: %f\n\r" , temp ); 00098 } 00099 00100 void hdc_temp_max () 00101 { 00102 i2c.write ( i2c_addr_write , temp_max_reg , 1 , true ); 00103 i2c.read ( i2c_addr_read , i2c_data , 1 , false ); 00104 //serial .printf ( "HDC Max temp. raw[hex]: %X\n\r" , i2c_data [0] ); 00105 double temp = i2c_data [0]; 00106 temp = ( ( temp * 165 ) / 256 ) - 40 ; 00107 serial .printf ( "HDC Max temp. = %f\n\r" , temp ); 00108 } 00109 00110 // Toggle LED 00111 void toggle_led () 00112 { 00113 serial .printf ( "led toggle\n\r" ); 00114 led1 = !led1; 00115 led3 = !led2; 00116 led3 = !led3; 00117 } 00118 00119 int main() { 00120 00121 led1 = 0; 00122 led2 = 0; 00123 led3 = 0; 00124 00125 i2c.frequency ( 1000000 ); 00126 00127 hdc_init () ; 00128 //hdc_soft_reset () ; 00129 //hdc_heater ( 5 ); 00130 //hdc_init () ; 00131 00132 while ( 1 ) 00133 { 00134 for ( int i = 0 ; i < 10 ; i++ ) 00135 { 00136 hdc_single_acqusition () ; 00137 wait ( 10 ) ; 00138 } 00139 hdc_temp_max () ; 00140 } 00141 }
Generated on Sat Jul 16 2022 14:12:43 by
1.7.2