TMP102
TMP102.cpp@0:fea9b4d9a373, 2016-02-10 (annotated)
- Committer:
- Nikollao
- Date:
- Wed Feb 10 16:10:42 2016 +0000
- Revision:
- 0:fea9b4d9a373
now the library is ready to use;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nikollao | 0:fea9b4d9a373 | 1 | #include "TMP102" |
Nikollao | 0:fea9b4d9a373 | 2 | |
Nikollao | 0:fea9b4d9a373 | 3 | TMP102::TMP102(PinName sda, PinName scl) |
Nikollao | 0:fea9b4d9a373 | 4 | { |
Nikollao | 0:fea9b4d9a373 | 5 | is2_ = new I2C(sda,scl); |
Nikollao | 0:fea9b4d9a373 | 6 | led_ = new DigitalOut(LED_RED); |
Nikollao | 0:fea9b4d9a373 | 7 | } |
Nikollao | 0:fea9b4d9a373 | 8 | |
Nikollao | 0:fea9b4d9a373 | 9 | void TMP102::init() { |
Nikollao | 0:fea9b4d9a373 | 10 | |
Nikollao | 0:fea9b4d9a373 | 11 | i2c_->frequency(400000); |
Nikollao | 0:fea9b4d9a373 | 12 | int ack; |
Nikollao | 0:fea9b4d9a373 | 13 | char config_data[2]; |
Nikollao | 0:fea9b4d9a373 | 14 | char reg CONFIG_REG; |
Nikollao | 0:fea9b4d9a373 | 15 | |
Nikollao | 0:fea9b4d9a373 | 16 | ack = i2c_->write(TMP102_W_ADD,®,1); |
Nikollao | 0:fea9b4d9a373 | 17 | |
Nikollao | 0:fea9b4d9a373 | 18 | if (ack) |
Nikollao | 0:fea9b4d9a373 | 19 | |
Nikollao | 0:fea9b4d9a373 | 20 | error(); |
Nikollao | 0:fea9b4d9a373 | 21 | |
Nikollao | 0:fea9b4d9a373 | 22 | i2c_->read(TMP102_R_ADD,&config_data,1); |
Nikollao | 0:fea9b4d9a373 | 23 | |
Nikollao | 0:fea9b4d9a373 | 24 | if (ack) |
Nikollao | 0:fea9b4d9a373 | 25 | |
Nikollao | 0:fea9b4d9a373 | 26 | error(); |
Nikollao | 0:fea9b4d9a373 | 27 | |
Nikollao | 0:fea9b4d9a373 | 28 | //set conversion rate to 1 Hz |
Nikollao | 0:fea9b4d9a373 | 29 | config_data[1] |= (1<<6); //set bit 6 |
Nikollao | 0:fea9b4d9a373 | 30 | config_data[1] &= ~(1<<7); //clear bit 7 |
Nikollao | 0:fea9b4d9a373 | 31 | |
Nikollao | 0:fea9b4d9a373 | 32 | char data_packet[3] = {reg, config_data[0], config_data[1]}; |
Nikollao | 0:fea9b4d9a373 | 33 | |
Nikollao | 0:fea9b4d9a373 | 34 | ack = i2c_->write(TMP102_W_ADD,data_packet,3); |
Nikollao | 0:fea9b4d9a373 | 35 | |
Nikollao | 0:fea9b4d9a373 | 36 | if (ack) |
Nikollao | 0:fea9b4d9a373 | 37 | error(); |
Nikollao | 0:fea9b4d9a373 | 38 | |
Nikollao | 0:fea9b4d9a373 | 39 | } |
Nikollao | 0:fea9b4d9a373 | 40 | |
Nikollao | 0:fea9b4d9a373 | 41 | void TMP102::read_temperature() { |
Nikollao | 0:fea9b4d9a373 | 42 | |
Nikollao | 0:fea9b4d9a373 | 43 | int ack; |
Nikollao | 0:fea9b4d9a373 | 44 | char data[2]; |
Nikollao | 0:fea9b4d9a373 | 45 | char reg TEMP_REG; |
Nikollao | 0:fea9b4d9a373 | 46 | |
Nikollao | 0:fea9b4d9a373 | 47 | ack = i2c_->write (TMP102_W_ADD,®,1); |
Nikollao | 0:fea9b4d9a373 | 48 | |
Nikollao | 0:fea9b4d9a373 | 49 | if (ack) |
Nikollao | 0:fea9b4d9a373 | 50 | error(); |
Nikollao | 0:fea9b4d9a373 | 51 | |
Nikollao | 0:fea9b4d9a373 | 52 | ack = i2c_->read(TMP102_R_ADD,data,2); |
Nikollao | 0:fea9b4d9a373 | 53 | |
Nikollao | 0:fea9b4d9a373 | 54 | if (ack) |
Nikollao | 0:fea9b4d9a373 | 55 | error(); //if we don't receive acknowledgment, flash error message |
Nikollao | 0:fea9b4d9a373 | 56 | |
Nikollao | 0:fea9b4d9a373 | 57 | int temperature = (data[0] << 4) | (data[1] >> 4); |
Nikollao | 0:fea9b4d9a373 | 58 | |
Nikollao | 0:fea9b4d9a373 | 59 | temperature_ = temperature*0.0625F; |
Nikollao | 0:fea9b4d9a373 | 60 | |
Nikollao | 0:fea9b4d9a373 | 61 | } |
Nikollao | 0:fea9b4d9a373 | 62 | |
Nikollao | 0:fea9b4d9a373 | 63 | float TMP102::get_temperature() { |
Nikollao | 0:fea9b4d9a373 | 64 | |
Nikollao | 0:fea9b4d9a373 | 65 | read_temperature(); |
Nikollao | 0:fea9b4d9a373 | 66 | return temperature_; |
Nikollao | 0:fea9b4d9a373 | 67 | } |
Nikollao | 0:fea9b4d9a373 | 68 | |
Nikollao | 0:fea9b4d9a373 | 69 | void TMP102::error() { |
Nikollao | 0:fea9b4d9a373 | 70 | |
Nikollao | 0:fea9b4d9a373 | 71 | while (1) { |
Nikollao | 0:fea9b4d9a373 | 72 | |
Nikollao | 0:fea9b4d9a373 | 73 | led_-> write(0); |
Nikollao | 0:fea9b4d9a373 | 74 | wait(0.2); |
Nikollao | 0:fea9b4d9a373 | 75 | led_> write(1); |
Nikollao | 0:fea9b4d9a373 | 76 | wait(0.2); |
Nikollao | 0:fea9b4d9a373 | 77 | } |
Nikollao | 0:fea9b4d9a373 | 78 | } |
Nikollao | 0:fea9b4d9a373 | 79 | |
Nikollao | 0:fea9b4d9a373 | 80 | |
Nikollao | 0:fea9b4d9a373 | 81 | |
Nikollao | 0:fea9b4d9a373 | 82 |