I want to use MSP430 to control TMP126, single conversion, single reading and writing. The program is as follows. It doesn't work normally. The read temperature conversion result is 0xff
#ifndef _TMP126_H
#define _TMP126_H
// TMP126的端口配置
#define TMP126_SCLK_OUT P1DIR|=BIT3
#define TMP126_SCLK_0 P1OUT&=~BIT3
#define TMP126_SCLK_1 P1OUT|=BIT3
#define TMP126_CS_OUT P3DIR|=BIT1
#define TMP126_CS_0 P3OUT&=~BIT1
#define TMP126_CS_1 P3OUT|=BIT1
#define TMP126_SIO_OUT P3DIR|=BIT0
#define TMP126_SIO_IN P3DIR&=~BIT0
#define TMP126_SO_0 P3OUT&=~BIT0
#define TMP126_SO_1 P3OUT|=BIT0
#define TMP126_SI_VAL ((P3IN&BIT0)==BIT0)
#define TMP126_DATAREG 0x00
#define TMP126_STATUS 0x02
#define TMP126_CONFIGREG 0x03
#define START_TMP126 0x01
#define WAIT_TMP126_CONVERT 0x02
#define READ_TMP126_RESULT 0x03
#define IDEL_TMP126 0x04
#define SET_TMP126_CONVERT 0x012 //one-slot,62.5ms/convter
unsigned char ReadTMP126Step;
unsigned char TMP126SleepFlag;
void TMP126_port_init(void);
void spi_write_int_to_TMP126(unsigned int x);
unsigned int spi_read_int_from_TMP126(void);
void write_TMP126_reg16(unsigned char reg,unsigned int data);
unsigned int read_TMP126_reg16(unsigned char reg);
void start_TMP126(void);
float read_temperature(void);
#endif
#include "all.h"
//******************************************************************************
// @brief :
// TMP126_port_init ioport direction & int
// @note:
// none
// @para:
// none
//******************************************************************************
void TMP126_port_init(void)
{
TMP126_SCLK_OUT;
TMP126_CS_OUT;
TMP126_SIO_OUT;
}
//******************************************************************************
// @brief :
// write a int data to TMP126
// @note:
// none
// @para:
// uint_16 x data
//******************************************************************************
void spi_write_int_to_TMP126(unsigned int x)
{
for(unsigned char i=0;i<16;i++)
{
TMP126_SCLK_0;
if((x&0x8000)==0x8000)
{
TMP126_SO_1;
}
else
{
TMP126_SO_0;
}
x<<=1;
TMP126_SCLK_1;
}
}
//******************************************************************************
// @brief :
// read a int data from TMP126
// @note:
// none
// @para:
// uint_16 x data
//******************************************************************************
unsigned int spi_read_int_from_TMP126(void)
{
unsigned char x;
TMP126_SIO_IN;
TMP126_SCLK_1;
for(unsigned char i=0;i<16;i++)
{
TMP126_SCLK_0;
x<<=1;
if(TMP126_SI_VAL)
{
x|=0x01;
}
else
{
x&=~0x01;
}
TMP126_SCLK_1;
}
return x;
}
//******************************************************************************
// @brief :
// write TMP126 reg
// @note:
// none
// @para:
// uint_16 x data
//******************************************************************************
void write_TMP126_reg16(unsigned char reg,unsigned int data)
{
unsigned int temp=0;
TMP126_SIO_OUT;
temp|=reg;
spi_write_int_to_TMP126(temp);
spi_write_int_to_TMP126(data);
TMP126_SO_1;
TMP126_SIO_OUT;
}
//******************************************************************************
// @brief :
// read TMP126 reg
// @note:
// none
// @para:
// uint_16 x data
//******************************************************************************
unsigned int read_TMP126_reg16(unsigned char reg)
{
unsigned int temp=0x100;
TMP126_SIO_OUT;
temp|=reg;
spi_write_int_to_TMP126(temp);
TMP126_SIO_IN;
temp=spi_read_int_from_TMP126();
return temp;
}
//******************************************************************************
// @brief :
// start TMP126 one-shot converter
// @note:
// none
// @para:
// uint_16 x data
//******************************************************************************
void start_TMP126(void)
{
TMP126_CS_0;
write_TMP126_reg16(TMP126_CONFIGREG,SET_TMP126_CONVERT);
TMP126_CS_1;
}
//******************************************************************************
// @brief :
// read TMP126
// @note:
// none
// @para:
// uint_16 x data
//******************************************************************************
float read_temperature(void)
{
float f=-100;
unsigned int status;
INT16U x;
TMP126_CS_0;
status=read_TMP126_reg16(TMP126_STATUS);
TMP126_CS_1;
TMP126_CS_0;
x=read_TMP126_reg16(TMP126_DATAREG);
TMP126_CS_1;
if((status&BIT0)==BIT0)
{
f=(x)*0.0078125;// 0.0078125=0.03125/4
}
return f;
}
float temperatureVal;
void main (void)
{
TMP126_port_init();
while(1)
{
start_TMP126();
delay_ms(200);
temperatureVal=read_temperature();
}
}