我在 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/