interface for the temperature sensor chip adt7320, with sw SPI interface
Diff: adt7320.cpp
- Revision:
- 0:e252ae2774e8
- Child:
- 1:1b9f706b8abc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/adt7320.cpp Thu Oct 02 19:25:55 2014 +0000 @@ -0,0 +1,65 @@ +#include "adt7320.h" + + +#define CS_ACTIVE 1 +#define CS_DEACTIVE 0 + +adt7320::adt7320(SWSPI *spiinterface ,DigitalOut* chipselect ){ + spi=spiinterface; + cs=chipselect; + } + +void adt7320::set_spi_mode(u8 nrbyte){ + spi->format(nrbyte,1); + spi->frequency(10000000); +} + + +u8 adt7320::format_cmd( u8 reg, bool rw){ + u8 cmd=0; ; + reg= reg & 0x7; + cmd = reg << 3; + if ( rw) cmd |= 0x40; // write bit 6 + return cmd; +} + + + +u8 adt7320::getR08( u8 reg){ + set_spi_mode(16); + u16 data=0x00FF; + u16 cmd = (u16) format_cmd(reg,true); + cmd = cmd <<8; + data= data | cmd; + cs->write(CS_ACTIVE); + data= spi->write(data); + cs->write(CS_DEACTIVE); + return (u8)(0x00FF &data); + +} + +u16 adt7320::getR16( u8 reg){ + set_spi_mode(24); + u32 data=0x0000FFFF; + u32 cmd =(u32) format_cmd(reg,true); + cmd = cmd <<16; + data= data | cmd; + cs->write(CS_ACTIVE); + data= spi->write(data); + cs->write(CS_DEACTIVE); + return (u16)(0x0000FFFF &data); +} + + +u8 adt7320::getId(){ + return getR08(0x03); +} + +u16 adt7320::get_TcritSP(){ + return getR16(0x04); +} + +u16 adt7320::get_T(){ + return getR16(0x02); +} +