Reading temperature from DS1631 via I2C
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 /*********************************************************************** 00004 * Read temperature by using DS1631 00005 * 00006 * Normal I2C-protocol (start,write,..,stop) does NOT work 00007 * It has to be used 00008 * i2c.write(address,data,length,repeat) 00009 * which includes start and stop and a special addressing function 00010 ************************************************************************/ 00011 DigitalOut myled(LED2); 00012 DigitalIn button(USER_BUTTON); 00013 I2C i2c (PB_9,PB_8); // sda, scl 00014 00015 int main() { 00016 int data; 00017 uint16_t cr1; 00018 int ack; 00019 char data_write[2]; 00020 char data_read[2]; 00021 //cr1 = I2C1->CR1; 00022 //I2C1->CR2 = 0x0002; 00023 00024 data_write[0] = 0xEE; 00025 ack = i2c.write(0x90, data_write, 1, 0); 00026 data_write[0] = 0xAC; // writes the 1-byte configuration register 00027 data_write[1] = 0x0F; // 1shot;12bit;pol=1 00028 ack = i2c.write(0x90, data_write, 2, 0); 00029 00030 while(1) { 00031 while (button); // start measurement by user_button 00032 00033 data_write[0] = 0x51; // Initiates temperature conversions 00034 ack = i2c.write(0x90, data_write, 1, 0); 00035 //printf("%d\n",ack); 00036 wait_ms(1000); 00037 00038 data_write[0] = 0xAA; // Reads last converted temperature value from the 2-byte temperature register 00039 ack = i2c.write(0x90, data_write, 1, 1); 00040 i2c.read(0x90, data_read, 2, 0); 00041 00042 data=data_read[0]*100; 00043 data=data+(((data_read[1] >> 4 )* 50)/8); 00044 printf("%d\n",data); 00045 //wait_ms(1000); 00046 } 00047 }
Generated on Thu Jul 28 2022 01:34:55 by
1.7.2