![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Final tidy of code following installation of new sensor, more comments added prior to submission
Dependencies: mbed
TMP102/TMP102.cpp@18:fc63b51a0302, 2022-02-04 (annotated)
- Committer:
- legstar85
- Date:
- Fri Feb 04 09:20:18 2022 +0000
- Revision:
- 18:fc63b51a0302
- Parent:
- 13:5ad65a688f3f
Final tidy of code following installation of new sensor, more comments added prior to submission
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
legstar85 | 13:5ad65a688f3f | 1 | /* * Print String |
legstar85 | 13:5ad65a688f3f | 2 | * |
legstar85 | 13:5ad65a688f3f | 3 | * Prints a string of characters to the screen buffer, string is cut off after the 83rd pixel. |
legstar85 | 13:5ad65a688f3f | 4 | * @param x - the column number (0 to 83) |
legstar85 | 13:5ad65a688f3f | 5 | * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row |
legstar85 | 13:5ad65a688f3f | 6 | * @author - David Leaming - 25574043 |
legstar85 | 13:5ad65a688f3f | 7 | * @ Date - December 2021 |
legstar85 | 13:5ad65a688f3f | 8 | * |
legstar85 | 13:5ad65a688f3f | 9 | * Acknowledgements |
legstar85 | 13:5ad65a688f3f | 10 | * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016 |
legstar85 | 13:5ad65a688f3f | 11 | * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries |
legstar85 | 13:5ad65a688f3f | 12 | * |
legstar85 | 13:5ad65a688f3f | 13 | */ |
legstar85 | 13:5ad65a688f3f | 14 | |
legstar85 | 13:5ad65a688f3f | 15 | // library implemtation file |
legstar85 | 13:5ad65a688f3f | 16 | |
legstar85 | 13:5ad65a688f3f | 17 | // obviously need to include the header |
legstar85 | 13:5ad65a688f3f | 18 | #include "TMP102.h" |
legstar85 | 13:5ad65a688f3f | 19 | |
legstar85 | 13:5ad65a688f3f | 20 | // we now implement each of the methods listed in the header |
legstar85 | 13:5ad65a688f3f | 21 | |
legstar85 | 13:5ad65a688f3f | 22 | // note the TMP102:: at the beginning of the method name |
legstar85 | 13:5ad65a688f3f | 23 | TMP102::TMP102(PinName sda, PinName scl) |
legstar85 | 13:5ad65a688f3f | 24 | { |
legstar85 | 13:5ad65a688f3f | 25 | // in the constructor, we create the mbed API objects using 'new' |
legstar85 | 13:5ad65a688f3f | 26 | i2c_ = new I2C(sda,scl); |
legstar85 | 13:5ad65a688f3f | 27 | led_ = new DigitalOut(LED_RED); |
legstar85 | 13:5ad65a688f3f | 28 | } |
legstar85 | 13:5ad65a688f3f | 29 | |
legstar85 | 13:5ad65a688f3f | 30 | void TMP102::init() |
legstar85 | 13:5ad65a688f3f | 31 | { |
legstar85 | 13:5ad65a688f3f | 32 | i2c_->frequency(400000); // set bus speed to 400 kHz |
legstar85 | 13:5ad65a688f3f | 33 | |
legstar85 | 13:5ad65a688f3f | 34 | int ack; // used to store acknowledgement bit |
legstar85 | 13:5ad65a688f3f | 35 | char config_data[2]; // array for data |
legstar85 | 13:5ad65a688f3f | 36 | char reg = CONFIG_REG; // register address |
legstar85 | 13:5ad65a688f3f | 37 | |
legstar85 | 13:5ad65a688f3f | 38 | //////// Read current status of configuration register /////// |
legstar85 | 13:5ad65a688f3f | 39 | |
legstar85 | 13:5ad65a688f3f | 40 | ack = i2c_->write(TMP102_W_ADD,®,1); // send the slave write address and the configuration register address |
legstar85 | 13:5ad65a688f3f | 41 | if (ack) |
legstar85 | 13:5ad65a688f3f | 42 | error(); // if we don't receive acknowledgement, flash error message |
legstar85 | 13:5ad65a688f3f | 43 | |
legstar85 | 13:5ad65a688f3f | 44 | ack = i2c_->read(TMP102_R_ADD,config_data,2); // read default 2 bytes from configuration register and store in array |
legstar85 | 13:5ad65a688f3f | 45 | if (ack) |
legstar85 | 13:5ad65a688f3f | 46 | error(); // if we don't receive acknowledgement, flash error message |
legstar85 | 13:5ad65a688f3f | 47 | |
legstar85 | 13:5ad65a688f3f | 48 | ///////// Configure the register ////////// |
legstar85 | 13:5ad65a688f3f | 49 | |
legstar85 | 13:5ad65a688f3f | 50 | // set conversion rate to 1 Hz - CR1 and CR2 are in the second data byte |
legstar85 | 13:5ad65a688f3f | 51 | config_data[1] |= (1 << 6); // set bit 6 |
legstar85 | 13:5ad65a688f3f | 52 | config_data[1] &= ~(1 << 7); // clear bit 7 |
legstar85 | 13:5ad65a688f3f | 53 | |
legstar85 | 13:5ad65a688f3f | 54 | //////// Send the configured register value to the slave config register //////////// |
legstar85 | 13:5ad65a688f3f | 55 | |
legstar85 | 13:5ad65a688f3f | 56 | // create data packet |
legstar85 | 13:5ad65a688f3f | 57 | char data_packet[3] = {reg,config_data[0],config_data[1]}; |
legstar85 | 13:5ad65a688f3f | 58 | |
legstar85 | 13:5ad65a688f3f | 59 | ack = i2c_->write(TMP102_W_ADD,data_packet,3); // send the data packet to the slave write address |
legstar85 | 13:5ad65a688f3f | 60 | if (ack) |
legstar85 | 13:5ad65a688f3f | 61 | error(); // if we don't receive acknowledgement, flash error message |
legstar85 | 13:5ad65a688f3f | 62 | |
legstar85 | 13:5ad65a688f3f | 63 | } |
legstar85 | 13:5ad65a688f3f | 64 | |
legstar85 | 13:5ad65a688f3f | 65 | void TMP102::read_temperature() |
legstar85 | 13:5ad65a688f3f | 66 | { |
legstar85 | 13:5ad65a688f3f | 67 | int ack; // used to store acknowledgement bit |
legstar85 | 13:5ad65a688f3f | 68 | char data[2]; // array for data |
legstar85 | 13:5ad65a688f3f | 69 | char reg = TEMP_REG; // temperature register address |
legstar85 | 13:5ad65a688f3f | 70 | |
legstar85 | 13:5ad65a688f3f | 71 | ack = i2c_->write(TMP102_W_ADD,®,1); // send temperature register address |
legstar85 | 13:5ad65a688f3f | 72 | if (ack) |
legstar85 | 13:5ad65a688f3f | 73 | error(); // if we don't receive acknowledgement, flash error message |
legstar85 | 13:5ad65a688f3f | 74 | |
legstar85 | 13:5ad65a688f3f | 75 | ack = i2c_->read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array |
legstar85 | 13:5ad65a688f3f | 76 | if (ack) |
legstar85 | 13:5ad65a688f3f | 77 | error(); // if we don't receive acknowledgement, flash error message |
legstar85 | 13:5ad65a688f3f | 78 | |
legstar85 | 13:5ad65a688f3f | 79 | int temperature = (data[0] << 4) | (data[1] >> 4); |
legstar85 | 13:5ad65a688f3f | 80 | |
legstar85 | 13:5ad65a688f3f | 81 | temperature_ = temperature*0.0625F; |
legstar85 | 13:5ad65a688f3f | 82 | } |
legstar85 | 13:5ad65a688f3f | 83 | |
legstar85 | 13:5ad65a688f3f | 84 | // temperature accessor method |
legstar85 | 13:5ad65a688f3f | 85 | float TMP102::get_temperature() |
legstar85 | 13:5ad65a688f3f | 86 | { |
legstar85 | 13:5ad65a688f3f | 87 | read_temperature(); |
legstar85 | 13:5ad65a688f3f | 88 | return temperature_; |
legstar85 | 13:5ad65a688f3f | 89 | } |
legstar85 | 13:5ad65a688f3f | 90 | |
legstar85 | 13:5ad65a688f3f | 91 | void TMP102::error() |
legstar85 | 13:5ad65a688f3f | 92 | { |
legstar85 | 13:5ad65a688f3f | 93 | while(1) { // if error, hang while flashing error message |
legstar85 | 13:5ad65a688f3f | 94 | led_->write(0); |
legstar85 | 13:5ad65a688f3f | 95 | wait(0.2); |
legstar85 | 13:5ad65a688f3f | 96 | led_->write(1); |
legstar85 | 13:5ad65a688f3f | 97 | wait(0.2); |
legstar85 | 13:5ad65a688f3f | 98 | } |
legstar85 | 13:5ad65a688f3f | 99 | } |