ads1118内部温度传感器值读取不准
1.ads1118用3.3v供电,室温10~18摄氏度。
2. SPI_RW_Reg(0x849B);
while (1)
{
delay(0xf0000);
a=SPI_RW_Reg(0x8499);
printf("\n\r The Value is %08x \r",a);
}
3.得到的数据
The Value is 0890049a ~ 08c8049a
4.转换成温度大概是50摄氏度,跟实际不同。
See code below that I use. It's CCS compiler code, but with the comments I think it's readable.
The function adc_TempReadTrig() return a -signed- integer of 0.1 degrees units. Thus 250 = 25.0 degress, -250 = -25.0 degrees.
You can use / adapt is as you like.
void adc_Init() {
// init in single shot temperature conversion at 860 SPS
// for now init is called after Flash initialisation.
int8 tmp;
CKE=0; // MCU spi shift out on pos edge
output_low(En_Spi_ADS1118);
tmp = spi_read(0x81); //start conversion, mux000, PGA000, single shot
tmp = spi_read(0xF2); //rate 860SPS, TEMP, NoPull, valid data
tmp = spi_read(0x0);
tmp = spi_read(0x0);
output_high(En_Spi_ADS1118);
CKE=1; // MCU spi shift out on neg.edge, change back
}
void adc_TempReadTrig() {
// function presumes that previous trigger was >= 1.5ms ago
// the conversion ready bits of the ADS1118 do -not- function as status bits (-stupid!-)
int8 tmp;
int8 i80, i81;
int16 i160;
float f1;
CKE=0; // MCU spi shift out on pos edge
output_low(En_Spi_ADS1118);
i80 = spi_read(0x81);
i81 = spi_read(0xF2);
tmp = spi_read(0);
tmp = spi_read(0);
output_high(En_Spi_ADS1118);
CKE=1;
i160 = make16(i80,i81); //make a temperature word
i160>>=2; //undo the 'left justified' format
if (bit_test(i160,13)) i160|=0xC000; //test sign-bit and if set(negative), and if also set b15&14
f1 = (float)((signed int16)i160) * 0.03125; //calculate degrees
adc.iT = (signed int16)(f1*10); //convert to 0.1degree/lsb integer (250=25.0 degrees) and store it.
}
Hope the above can help you!
按照以上功能代码,写入0x81F20000后,我在室温20度左右,得到的原始数据是0x0680,乘以0.03125得到温度为摄氏52度。
为什么差这么远呢?
unsigned char cmd[4]={0x81,0xF2,0x00,0x00};
CS_0; //拉低SSN,片选信号使能
for(i=0; i<4; i++)
{
while (!(SPI1_SR & BIT1)); //等待发送缓冲区空
SPI1_DR = cmd[i];
while (!(SPI1_SR & BIT0)); // 等待接收到一个有效数据字节
GetData[i] = SPI1_DR;
}
CS_1;
为什么写81F2?手册22页,最后一位应该写1,是不是应该写成81F3?
前面的代码,如果写入0x81F381F3,得到的4字节数据是0x07 18 02 C9,其中温度数据是0x0718,计数结果已经超过50度了,而室温肯定低于20度的。
请问该问题解决了吗?我遇到了类似的问题,我读出来计算的温度为80度左右,不知道怎么回事?