test program for ADT7320 temperature sensor

Dependencies:   mbed

Committer:
rcele_85
Date:
Sat Dec 24 15:31:40 2016 +0000
Revision:
0:66156a1e785a
ADT7320 Library for mbed using SPI

Who changed what in which revision?

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