ADT7320 SPI High Resolution MBED Library
ADT7320_SPI.cpp@0:f57cf8786393, 2017-01-08 (annotated)
- Committer:
- rcele_85
- Date:
- Sun Jan 08 16:35:26 2017 +0000
- Revision:
- 0:f57cf8786393
ADT7320 Temperature Sensor MBED Lib Rev-1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rcele_85 | 0:f57cf8786393 | 1 | #include "ADT7320_SPI.h" |
rcele_85 | 0:f57cf8786393 | 2 | |
rcele_85 | 0:f57cf8786393 | 3 | //#include "mbed.h" |
rcele_85 | 0:f57cf8786393 | 4 | |
rcele_85 | 0:f57cf8786393 | 5 | ADT7320_SPI::ADT7320_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs) : spi_(mosi, miso, sclk),cs_(cs) { |
rcele_85 | 0:f57cf8786393 | 6 | // Chip must be deselected |
rcele_85 | 0:f57cf8786393 | 7 | cs_ = 1; |
rcele_85 | 0:f57cf8786393 | 8 | // Setup the spi for 8 bit data, high steady state clock, |
rcele_85 | 0:f57cf8786393 | 9 | // second edge capture, with a 1MHz clock rate |
rcele_85 | 0:f57cf8786393 | 10 | spi_.format(8,3); |
rcele_85 | 0:f57cf8786393 | 11 | spi_.frequency(1000000); |
rcele_85 | 0:f57cf8786393 | 12 | } |
rcele_85 | 0:f57cf8786393 | 13 | |
rcele_85 | 0:f57cf8786393 | 14 | //Read from register from the ADT7320: |
rcele_85 | 0:f57cf8786393 | 15 | unsigned int ADT7320_SPI::readRegister(uint8_t thisRegister, int bytesToRead ) { |
rcele_85 | 0:f57cf8786393 | 16 | uint8_t inByte = 0; // incoming byte from the SPI |
rcele_85 | 0:f57cf8786393 | 17 | unsigned int result = 0; // result to return |
rcele_85 | 0:f57cf8786393 | 18 | // take the chip select low to select the device: |
rcele_85 | 0:f57cf8786393 | 19 | cs_=0; |
rcele_85 | 0:f57cf8786393 | 20 | // send the device the register you want to read: |
rcele_85 | 0:f57cf8786393 | 21 | spi_.write(thisRegister); |
rcele_85 | 0:f57cf8786393 | 22 | // send a value of 0 to read the first byte returned: |
rcele_85 | 0:f57cf8786393 | 23 | result = spi_.write(0xFF); |
rcele_85 | 0:f57cf8786393 | 24 | // decrement the number of bytes left to read: |
rcele_85 | 0:f57cf8786393 | 25 | bytesToRead--; |
rcele_85 | 0:f57cf8786393 | 26 | // if you still have another byte to read: |
rcele_85 | 0:f57cf8786393 | 27 | if (bytesToRead > 0) { |
rcele_85 | 0:f57cf8786393 | 28 | // shift the first byte left, then get the second byte: |
rcele_85 | 0:f57cf8786393 | 29 | result = result << 8; |
rcele_85 | 0:f57cf8786393 | 30 | inByte = spi_.write(0xFF); |
rcele_85 | 0:f57cf8786393 | 31 | // combine the byte you just got with the previous one: |
rcele_85 | 0:f57cf8786393 | 32 | result = result | inByte; |
rcele_85 | 0:f57cf8786393 | 33 | // decrement the number of bytes left to read: |
rcele_85 | 0:f57cf8786393 | 34 | bytesToRead--; |
rcele_85 | 0:f57cf8786393 | 35 | } |
rcele_85 | 0:f57cf8786393 | 36 | // take the chip select high to de-select: |
rcele_85 | 0:f57cf8786393 | 37 | cs_=1; |
rcele_85 | 0:f57cf8786393 | 38 | // return the result: |
rcele_85 | 0:f57cf8786393 | 39 | return(result); |
rcele_85 | 0:f57cf8786393 | 40 | } |
rcele_85 | 0:f57cf8786393 | 41 | |
rcele_85 | 0:f57cf8786393 | 42 | float ADT7320_SPI::readTemp(void) |
rcele_85 | 0:f57cf8786393 | 43 | { |
rcele_85 | 0:f57cf8786393 | 44 | //Read the temperature data |
rcele_85 | 0:f57cf8786393 | 45 | unsigned int tempData = readRegister(0x50, 2); // 0x50 is read commad for 0x02 register |
rcele_85 | 0:f57cf8786393 | 46 | tempData = tempData/8;// MSB bit15 and LSB bit4 so received value need to be divide/8 |
rcele_85 | 0:f57cf8786393 | 47 | |
rcele_85 | 0:f57cf8786393 | 48 | // convert the temperature to celsius and display it: |
rcele_85 | 0:f57cf8786393 | 49 | float realTemp = (float)tempData * 0.0625; |
rcele_85 | 0:f57cf8786393 | 50 | return realTemp; |
rcele_85 | 0:f57cf8786393 | 51 | } |
rcele_85 | 0:f57cf8786393 | 52 |