interface for the temperature sensor chip adt7320, with sw SPI interface

Dependents:   SPItest sscm

Committer:
wbeaumont
Date:
Fri Oct 23 11:23:08 2015 +0000
Revision:
4:ee71e7bca786
Parent:
3:49638acbc5d4
production version archiving

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:e252ae2774e8 1 #include "adt7320.h"
wbeaumont 0:e252ae2774e8 2
wbeaumont 0:e252ae2774e8 3
wbeaumont 3:49638acbc5d4 4 /* ***************************************************
wbeaumont 3:49638acbc5d4 5 * class implementation to read the adt7320 temperature sensor via SPI
wbeaumont 3:49638acbc5d4 6 *
wbeaumont 4:ee71e7bca786 7 *
wbeaumont 4:ee71e7bca786 8 * (C) Wim Beaumont Univeristeit Antwerpen 2014 , 2015
wbeaumont 3:49638acbc5d4 9
wbeaumont 3:49638acbc5d4 10 ************************************************** */
wbeaumont 3:49638acbc5d4 11
wbeaumont 3:49638acbc5d4 12
wbeaumont 3:49638acbc5d4 13
wbeaumont 0:e252ae2774e8 14 #define CS_ACTIVE 1
wbeaumont 0:e252ae2774e8 15 #define CS_DEACTIVE 0
wbeaumont 0:e252ae2774e8 16
wbeaumont 3:49638acbc5d4 17 #define ADT7320_SRC_VER "1.20"
wbeaumont 1:1b9f706b8abc 18
wbeaumont 1:1b9f706b8abc 19
wbeaumont 1:1b9f706b8abc 20
wbeaumont 1:1b9f706b8abc 21
wbeaumont 1:1b9f706b8abc 22 adt7320::adt7320(SWSPI *spiinterface ,DigitalOut* chipselect )
wbeaumont 1:1b9f706b8abc 23 :getVersion( ADT7320_HDR_VER ,ADT7320_SRC_VER , __TIME__, __DATE__)
wbeaumont 1:1b9f706b8abc 24 {
wbeaumont 0:e252ae2774e8 25 spi=spiinterface;
wbeaumont 0:e252ae2774e8 26 cs=chipselect;
wbeaumont 3:49638acbc5d4 27 Tmode=13 ; // 13 bits temperature reading
wbeaumont 0:e252ae2774e8 28 }
wbeaumont 0:e252ae2774e8 29
wbeaumont 0:e252ae2774e8 30 void adt7320::set_spi_mode(u8 nrbyte){
wbeaumont 0:e252ae2774e8 31 spi->format(nrbyte,1);
wbeaumont 0:e252ae2774e8 32 spi->frequency(10000000);
wbeaumont 0:e252ae2774e8 33 }
wbeaumont 0:e252ae2774e8 34
wbeaumont 0:e252ae2774e8 35
wbeaumont 0:e252ae2774e8 36 u8 adt7320::format_cmd( u8 reg, bool rw){
wbeaumont 0:e252ae2774e8 37 u8 cmd=0; ;
wbeaumont 0:e252ae2774e8 38 reg= reg & 0x7;
wbeaumont 0:e252ae2774e8 39 cmd = reg << 3;
wbeaumont 0:e252ae2774e8 40 if ( rw) cmd |= 0x40; // write bit 6
wbeaumont 0:e252ae2774e8 41 return cmd;
wbeaumont 0:e252ae2774e8 42 }
wbeaumont 0:e252ae2774e8 43
wbeaumont 0:e252ae2774e8 44
wbeaumont 0:e252ae2774e8 45
wbeaumont 0:e252ae2774e8 46 u8 adt7320::getR08( u8 reg){
wbeaumont 0:e252ae2774e8 47 set_spi_mode(16);
wbeaumont 0:e252ae2774e8 48 u16 data=0x00FF;
wbeaumont 0:e252ae2774e8 49 u16 cmd = (u16) format_cmd(reg,true);
wbeaumont 0:e252ae2774e8 50 cmd = cmd <<8;
wbeaumont 0:e252ae2774e8 51 data= data | cmd;
wbeaumont 0:e252ae2774e8 52 cs->write(CS_ACTIVE);
wbeaumont 0:e252ae2774e8 53 data= spi->write(data);
wbeaumont 0:e252ae2774e8 54 cs->write(CS_DEACTIVE);
wbeaumont 0:e252ae2774e8 55 return (u8)(0x00FF &data);
wbeaumont 0:e252ae2774e8 56
wbeaumont 0:e252ae2774e8 57 }
wbeaumont 0:e252ae2774e8 58
wbeaumont 1:1b9f706b8abc 59
wbeaumont 1:1b9f706b8abc 60 void adt7320::setR08( u8 reg, u8 datain){
wbeaumont 1:1b9f706b8abc 61 set_spi_mode(16);
wbeaumont 1:1b9f706b8abc 62 u16 data=0x0000;
wbeaumont 1:1b9f706b8abc 63 u16 cmd = (u16) format_cmd(reg,false);
wbeaumont 1:1b9f706b8abc 64 cmd = cmd <<8;
wbeaumont 1:1b9f706b8abc 65 data= datain | cmd;
wbeaumont 1:1b9f706b8abc 66 cs->write(CS_ACTIVE);
wbeaumont 1:1b9f706b8abc 67 data= spi->write(data);
wbeaumont 1:1b9f706b8abc 68 cs->write(CS_DEACTIVE);
wbeaumont 1:1b9f706b8abc 69 }
wbeaumont 1:1b9f706b8abc 70
wbeaumont 0:e252ae2774e8 71 u16 adt7320::getR16( u8 reg){
wbeaumont 0:e252ae2774e8 72 set_spi_mode(24);
wbeaumont 0:e252ae2774e8 73 u32 data=0x0000FFFF;
wbeaumont 0:e252ae2774e8 74 u32 cmd =(u32) format_cmd(reg,true);
wbeaumont 0:e252ae2774e8 75 cmd = cmd <<16;
wbeaumont 0:e252ae2774e8 76 data= data | cmd;
wbeaumont 0:e252ae2774e8 77 cs->write(CS_ACTIVE);
wbeaumont 0:e252ae2774e8 78 data= spi->write(data);
wbeaumont 0:e252ae2774e8 79 cs->write(CS_DEACTIVE);
wbeaumont 0:e252ae2774e8 80 return (u16)(0x0000FFFF &data);
wbeaumont 0:e252ae2774e8 81 }
wbeaumont 0:e252ae2774e8 82
wbeaumont 1:1b9f706b8abc 83 void adt7320::setR16( u8 reg,u16 datain){
wbeaumont 1:1b9f706b8abc 84 set_spi_mode(24);
wbeaumont 1:1b9f706b8abc 85 u32 data=0x00000000;
wbeaumont 1:1b9f706b8abc 86 u32 cmd =(u32) format_cmd(reg,false);
wbeaumont 1:1b9f706b8abc 87 cmd = cmd <<16;
wbeaumont 1:1b9f706b8abc 88 data= datain | cmd;
wbeaumont 1:1b9f706b8abc 89 cs->write(CS_ACTIVE);
wbeaumont 1:1b9f706b8abc 90 data= spi->write(data);
wbeaumont 1:1b9f706b8abc 91 cs->write(CS_DEACTIVE);
wbeaumont 1:1b9f706b8abc 92 }
wbeaumont 1:1b9f706b8abc 93
wbeaumont 1:1b9f706b8abc 94
wbeaumont 0:e252ae2774e8 95
wbeaumont 2:2b886cea4fcb 96 void adt7320::serial_line_rst() {
wbeaumont 2:2b886cea4fcb 97 set_spi_mode(32);
wbeaumont 2:2b886cea4fcb 98 u32 data=0xFFFFFFFF;
wbeaumont 2:2b886cea4fcb 99 cs->write(CS_ACTIVE); // needed ?
wbeaumont 2:2b886cea4fcb 100 data= spi->write(data);
wbeaumont 2:2b886cea4fcb 101 cs->write(CS_DEACTIVE);
wbeaumont 2:2b886cea4fcb 102 set_spi_mode(24);
wbeaumont 2:2b886cea4fcb 103 }
wbeaumont 2:2b886cea4fcb 104
wbeaumont 2:2b886cea4fcb 105
wbeaumont 2:2b886cea4fcb 106
wbeaumont 0:e252ae2774e8 107 u8 adt7320::getId(){
wbeaumont 0:e252ae2774e8 108 return getR08(0x03);
wbeaumont 0:e252ae2774e8 109 }
wbeaumont 0:e252ae2774e8 110
wbeaumont 0:e252ae2774e8 111 u16 adt7320::get_TcritSP(){
wbeaumont 0:e252ae2774e8 112 return getR16(0x04);
wbeaumont 0:e252ae2774e8 113 }
wbeaumont 0:e252ae2774e8 114
wbeaumont 1:1b9f706b8abc 115 void adt7320::set_TcritSP(u16 tcrit){
wbeaumont 1:1b9f706b8abc 116
wbeaumont 1:1b9f706b8abc 117 setR16(0x04,tcrit );
wbeaumont 1:1b9f706b8abc 118 }
wbeaumont 1:1b9f706b8abc 119
wbeaumont 1:1b9f706b8abc 120
wbeaumont 1:1b9f706b8abc 121
wbeaumont 0:e252ae2774e8 122 u16 adt7320::get_T(){
wbeaumont 0:e252ae2774e8 123 return getR16(0x02);
wbeaumont 0:e252ae2774e8 124 }
wbeaumont 0:e252ae2774e8 125
wbeaumont 1:1b9f706b8abc 126
wbeaumont 3:49638acbc5d4 127 float adt7320::getTemperature(){
wbeaumont 3:49638acbc5d4 128 u16 Td = get_T(); bool neg=false;
wbeaumont 3:49638acbc5d4 129 if ( Tmode== 13) Td=Td & 0xFFF8;
wbeaumont 3:49638acbc5d4 130 if ( Td & 0x8000 ) { // negative
wbeaumont 3:49638acbc5d4 131 neg =true;
wbeaumont 3:49638acbc5d4 132 Td= ~Td+1;
wbeaumont 3:49638acbc5d4 133 }
wbeaumont 3:49638acbc5d4 134 float temp = (float)Td /128 ; // same for 16 and 13 bits mode
wbeaumont 3:49638acbc5d4 135 if( neg ) temp=-temp;
wbeaumont 3:49638acbc5d4 136
wbeaumont 3:49638acbc5d4 137 return temp;
wbeaumont 3:49638acbc5d4 138 }
wbeaumont 3:49638acbc5d4 139
wbeaumont 3:49638acbc5d4 140
wbeaumont 3:49638acbc5d4 141 void adt7320::init1(){}
wbeaumont 3:49638acbc5d4 142 void adt7320::init2(){}
wbeaumont 1:1b9f706b8abc 143
wbeaumont 1:1b9f706b8abc 144
wbeaumont 1:1b9f706b8abc 145