TC77传感器的C编程错误

标签 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;
}

最佳答案

您还没有分配对的评价

(((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)。它不支持 float 。”

关于TC77传感器的C编程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29096241/

相关文章:

将矩阵的每一行复制到一个临时数组

c - 如何替换双向链表中的元素

c - 使用具有多个客户端的管道

C代码: Calling function in main

c - 我如何获得此readdir代码示例以搜索其他目录

c - 什么范围的机器的二进制兼容性?

c - 如何在给定到插值点的距离的情况下插值线上点的位置

c++ - 从 .c 文件中定义的 .cpp 文件调用的函数

c - C 中缺少访问说明符 & 不会导致编译错误

c - 字符串比较函数