TC77传感器的C编程错误

时间:2015-03-17 10:05:04

标签: c

我在xilinx SDK中使用SPI和温度传感器TC77,除了这个子程序之外一切正常,我需要获得适当的温度,但输出是“温度是......没有输入,什么可以是错误

#include "xparameters.h"    
#include "xspi.h"       
#include "xspi_l.h"
#include "stdio.h"
#include "xstatus.h"
#include <stdlib.h>
#include "xil_cache.h"
#include "xbasic_types.h"

#define SPI_DEVICE_ID             XPAR_SPI_0_DEVICE_ID           
#define SPI_SELECT 0x01
#define BUFFER_SIZE     8          received

XSpi SpiInstance; 

u8 SendBuffer[BUFFER_SIZE]; 
u8 RecvBuffer[BUFFER_SIZE];

int Spi_Test(XSpi *SpiInstancePtr, u16 SpiDeviceId);

int main(void)

{
int Status;

for (;;)
{

Status = Spi_Test(&SpiInstance, SPI_DEVICE_ID);
if (Status != XST_SUCCESS) {
    return XST_FAILURE;
}
}
return XST_SUCCESS;
}

int Spi_Test(XSpi *SpiInstancePtr, u16 SpiDeviceId)
{
    int Status;
    int i,j;

    XSpi_Config *ConfigPtr; /* Pointer to Configuration data */

    /*****  Initialize the SPI driver ******/

    ConfigPtr = XSpi_LookupConfig(SPI_DEVICE_ID);
    if (ConfigPtr == NULL) {
        return XST_DEVICE_NOT_FOUND;
    }

    XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
            ConfigPtr->BaseAddress);

    XSpi_SetOptions(SpiInstancePtr,
            XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION);

    XSpi_GetSlaveSelect(SpiInstancePtr);

    XSpi_SetSlaveSelect(SpiInstancePtr, SPI_SELECT);

    XSpi_Start(SpiInstancePtr);

    XSpi_IntrGlobalDisable(SpiInstancePtr);

    /***** switch to Continuous mode to get the temperature, connect MOSI pin *****/
        /*SendBuffer[0] = 0x00;
        SendBuffer[1] = 0x00;

        Status = XSpi_Transfer(SpiInstancePtr, SendBuffer, RecvBuffer, 2);
        if (Status != XST_SUCCESS) {
        return XST_FAILURE;
        }*/

    /**** Get the temperature in RecvBuffer[0]& RecvBuffer [1]****/

        XSpi_Transfer(SpiInstancePtr, SendBuffer, RecvBuffer, 2);

        xil_printf("MSB of Temperature Register is = 0x%x\r\n", RecvBuffer[0]);
        xil_printf("LSB of Temperature Register is = 0x%x\r\n", RecvBuffer[1]);

        // sign bit method

        int sign = RecvBuffer[0] & 0x80;
        int tempValue =0;
        float tempCelsius;

        if (sign == 0)
        {
            tempValue = (RecvBuffer[0] << 8 | RecvBuffer[1]) >> 3;
            tempCelsius = (float) tempValue * 0.0625;
            xil_printf("The temperature is p = %3.1f \n\r", tempCelsius);
        }
        else
        if (sign == 1)
        {
            tempValue= (((RecvBuffer[0] & 0x7f) << 8 | RecvBuffer[1]) >> 3) - 4096;
            tempCelsius = (float) tempValue * 0.0625;
            xil_printf("The temperature is p = %3.1f \n\r", tempCelsius);
        }
            return 0;
}

2 个答案:

答案 0 :(得分:2)

您尚未分配

的评估
(((RecvBuffer[0] & 0x7f) << 8 | RecvBuffer[1]) >> 3) - 4096;

您的意思是代码吗?

if (signBit == 0)
    tempValue= (RecvBuffer[0] << 8 | RecvBuffer[1]) >> 3; 
else
    tempValue= (((RecvBuffer[0] & 0x7f) << 8 | RecvBuffer[1]) >> 3) - 4096;
float tempCelsius = (float) tempValue * 0.0625;
xil_printf("The temperature is = %f \n\r", tempCelsius);

修改

我搜索了这个并被告知 xil_printf()不支持%f,它只支持%d,l,x,c,s。你必须打印两个整数,正值的情况:

int whole, thousandths;
whole = tempCelsius;
thousandths = (tempCelsius - whole) * 1000;
xil_printf("%d.%03d\n", whole, thousandths);

This文档说xil_printf()“这个函数类似于printf,但尺寸要小得多(只有1 kB)。它不支持浮点数。”< / em>的

答案 1 :(得分:0)

由于计算机语言的缘故,该主题略有偏离,但是,此仓库中的代码 https://github.com/robin48gx/TH7 在python中读取TC77。 但是,这是相关的,因为....

tc77输出是一个带符号的13位数字,位于 在16位字的高13位中。另外,您还必须写入最后16位AS ZEROS,以命令tc77保持连续读取模式。的 该仓库中的python代码可以处理所有这些问题。但是,在“ C”中 您可以将其强制转换为16位int(通常为'short int' 但请检查您的编译器/实现)。这样可以解决标志
问题。

另请参阅: http://ww1.microchip.com/downloads/en/devicedoc/20092a.pdf