ADT7320 temperature sensor.

Dependents:   sscm

Fork of T_adt7320 by wimbeaumont Project

Committer:
NickRyder
Date:
Tue Oct 07 21:31:07 2014 +0000
Revision:
2:186e3c0afcad
Parent:
1:1b9f706b8abc
Tidying up.

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 0:e252ae2774e8 4 #define CS_ACTIVE 1
wbeaumont 0:e252ae2774e8 5 #define CS_DEACTIVE 0
wbeaumont 0:e252ae2774e8 6
wbeaumont 1:1b9f706b8abc 7 #define ADT7320_SRC_VER "1.13"
wbeaumont 1:1b9f706b8abc 8
wbeaumont 1:1b9f706b8abc 9
wbeaumont 1:1b9f706b8abc 10
wbeaumont 1:1b9f706b8abc 11
NickRyder 2:186e3c0afcad 12 adt7320::adt7320(SWSPI * spiinterface, DigitalOut * chipselect):
NickRyder 2:186e3c0afcad 13 getVersion(ADT7320_HDR_VER, ADT7320_SRC_VER, __TIME__, __DATE__)
wbeaumont 1:1b9f706b8abc 14 {
wbeaumont 0:e252ae2774e8 15 spi=spiinterface;
wbeaumont 0:e252ae2774e8 16 cs=chipselect;
NickRyder 2:186e3c0afcad 17 }
wbeaumont 0:e252ae2774e8 18
wbeaumont 0:e252ae2774e8 19 void adt7320::set_spi_mode(u8 nrbyte){
wbeaumont 0:e252ae2774e8 20 spi->format(nrbyte,1);
wbeaumont 0:e252ae2774e8 21 spi->frequency(10000000);
wbeaumont 0:e252ae2774e8 22 }
wbeaumont 0:e252ae2774e8 23
NickRyder 2:186e3c0afcad 24 u8 adt7320::format_cmd(u8 reg, bool rw){
NickRyder 2:186e3c0afcad 25 u8 cmd = 0;
NickRyder 2:186e3c0afcad 26 reg = reg & 0x7;
wbeaumont 0:e252ae2774e8 27 cmd = reg << 3;
NickRyder 2:186e3c0afcad 28 if (rw) cmd |= 0x40; // write bit 6
wbeaumont 0:e252ae2774e8 29 return cmd;
wbeaumont 0:e252ae2774e8 30 }
wbeaumont 0:e252ae2774e8 31
NickRyder 2:186e3c0afcad 32 u8 adt7320::getR08(u8 reg) {
wbeaumont 0:e252ae2774e8 33 set_spi_mode(16);
NickRyder 2:186e3c0afcad 34 u16 data = 0x00FF;
NickRyder 2:186e3c0afcad 35 u16 cmd = (u16) format_cmd(reg, true);
NickRyder 2:186e3c0afcad 36 cmd = cmd << 8;
NickRyder 2:186e3c0afcad 37 data = data | cmd;
NickRyder 2:186e3c0afcad 38 cs->write(CS_ACTIVE);
NickRyder 2:186e3c0afcad 39 data= spi->write(data);
NickRyder 2:186e3c0afcad 40 cs->write(CS_DEACTIVE);
NickRyder 2:186e3c0afcad 41 return (u8) (0x00FF & data);
wbeaumont 0:e252ae2774e8 42 }
wbeaumont 0:e252ae2774e8 43
NickRyder 2:186e3c0afcad 44 void adt7320::setR08(u8 reg, u8 datain) {
wbeaumont 1:1b9f706b8abc 45 set_spi_mode(16);
NickRyder 2:186e3c0afcad 46 u16 data = 0x0000;
NickRyder 2:186e3c0afcad 47 u16 cmd = (u16) format_cmd(reg, false);
NickRyder 2:186e3c0afcad 48 cmd = cmd << 8;
NickRyder 2:186e3c0afcad 49 data = datain | cmd;
wbeaumont 1:1b9f706b8abc 50 cs->write(CS_ACTIVE);
NickRyder 2:186e3c0afcad 51 data = spi->write(data);
wbeaumont 1:1b9f706b8abc 52 cs->write(CS_DEACTIVE);
wbeaumont 1:1b9f706b8abc 53 }
wbeaumont 1:1b9f706b8abc 54
NickRyder 2:186e3c0afcad 55 u16 adt7320::getR16(u8 reg) {
wbeaumont 0:e252ae2774e8 56 set_spi_mode(24);
NickRyder 2:186e3c0afcad 57 u32 data = 0x0000FFFF;
NickRyder 2:186e3c0afcad 58 u32 cmd = (u32) format_cmd(reg, true);
NickRyder 2:186e3c0afcad 59 cmd = cmd << 16;
NickRyder 2:186e3c0afcad 60 data = data | cmd;
NickRyder 2:186e3c0afcad 61 cs->write(CS_ACTIVE);
NickRyder 2:186e3c0afcad 62 data= spi->write(data);
NickRyder 2:186e3c0afcad 63 cs->write(CS_DEACTIVE);
NickRyder 2:186e3c0afcad 64 return (u16) (0x0000FFFF & data);
wbeaumont 0:e252ae2774e8 65 }
wbeaumont 0:e252ae2774e8 66
NickRyder 2:186e3c0afcad 67 void adt7320::setR16(u8 reg, u16 datain) {
NickRyder 2:186e3c0afcad 68 set_spi_mode(24);
NickRyder 2:186e3c0afcad 69 u32 data = 0x00000000;
NickRyder 2:186e3c0afcad 70 u32 cmd = (u32) format_cmd(reg, false);
NickRyder 2:186e3c0afcad 71 cmd = cmd << 16;
NickRyder 2:186e3c0afcad 72 data = datain | cmd;
NickRyder 2:186e3c0afcad 73 cs->write(CS_ACTIVE);
NickRyder 2:186e3c0afcad 74 data = spi->write(data);
NickRyder 2:186e3c0afcad 75 cs->write(CS_DEACTIVE);
wbeaumont 1:1b9f706b8abc 76 }
wbeaumont 1:1b9f706b8abc 77
NickRyder 2:186e3c0afcad 78 u8 adt7320::getId(){
wbeaumont 0:e252ae2774e8 79 return getR08(0x03);
wbeaumont 0:e252ae2774e8 80 }
wbeaumont 0:e252ae2774e8 81
wbeaumont 0:e252ae2774e8 82 u16 adt7320::get_TcritSP(){
wbeaumont 0:e252ae2774e8 83 return getR16(0x04);
wbeaumont 0:e252ae2774e8 84 }
wbeaumont 0:e252ae2774e8 85
wbeaumont 1:1b9f706b8abc 86 void adt7320::set_TcritSP(u16 tcrit){
NickRyder 2:186e3c0afcad 87 setR16(0x04, tcrit);
wbeaumont 1:1b9f706b8abc 88 }
wbeaumont 1:1b9f706b8abc 89
wbeaumont 0:e252ae2774e8 90 u16 adt7320::get_T(){
wbeaumont 0:e252ae2774e8 91 return getR16(0x02);
wbeaumont 0:e252ae2774e8 92 }
wbeaumont 0:e252ae2774e8 93
wbeaumont 1:1b9f706b8abc 94
NickRyder 2:186e3c0afcad 95 void adt7320::init1() {
NickRyder 2:186e3c0afcad 96 }
wbeaumont 1:1b9f706b8abc 97
NickRyder 2:186e3c0afcad 98 void adt7320::init2() {
NickRyder 2:186e3c0afcad 99 }