2025-02-15 11:05:28 -05:00

95 lines
1.6 KiB
C

//#include <proc/p32mx440f256h.h>
#include "TC77.h"
#include "BoardCfg.h"
#include "timer.h"
short mDeviceID;
int TC77Configure()
{
TEMP_SENSOR_CS_PIN = 0;
//Execute a 16 bits read first.
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
//Now, write 0xFFFF to config register.
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
//Read Device ID
SPI3BUF = 0x00;
while(!SPI3STATbits.SPIRBF);
mDeviceID = SPI3BUF;
mDeviceID <<= 8;
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
mDeviceID += SPI3BUF;
TEMP_SENSOR_CS_PIN = 1;
mDeviceID &= 0xFFFC;
if(mDeviceID != 0x5400)
{
return RET_ERROR;
}
Sleep(100);
//Device detected. Now, put the device in continuous read mode.
TEMP_SENSOR_CS_PIN = 0;
//Execute a 16 bits read first.
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
//Now, write 0x0000 to config register.
SPI3BUF = 0x00;
while(!SPI3STATbits.SPIRBF);
SPI3BUF = 0x00;
while(!SPI3STATbits.SPIRBF);
TEMP_SENSOR_CS_PIN = 1;
return RET_OK;
}
float TC77GetActualTemp()
{
short RawTemp = 0;
float Temp;
TEMP_SENSOR_CS_PIN = 0;
//Read 16 bits.
SPI3BUF = 0x00;
while(!SPI3STATbits.SPIRBF);
RawTemp = SPI3BUF;
RawTemp <<= 8;
SPI3BUF = 0xFF;
while(!SPI3STATbits.SPIRBF);
RawTemp += SPI3BUF;
TEMP_SENSOR_CS_PIN = 1;
RawTemp &= 0xFFF8; //Get rid of useless 3 LSB
//RawTemp >>= 3;
Temp = ((float)0.0625 * (float)RawTemp);
Temp /= 8;
return Temp;
}
short TC77GetDeviceID()
{
return mDeviceID;
}