我正在尝试在树莓派中进行SPI传输。 Raspberry pi带有一个SPI接口,通过GPIO(通用输入输出引脚)发送和接收数据。 spi驱动程序信息为here。我正在做环回(连接MOSI和MISO引脚)。我在我的代码中描述了我发送的数据,由于环回,我将收到这些数据。我想要对收到的数据进行计算。这些数据以数组的形式存储。在计算之后,我需要用正确的符号打印结果,但我没有得到它。这是我的完整代码。
/************************All header should come here************************************************************************ ********************************/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
/***************************All declaration should come here***************************************************************************************************/
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static const char *device = "/dev/spidev0.0";//selecting spi device in the raspberry pi
static uint8_t mode;//mode of spi transfer
static uint8_t bits =8;//no. of bits to be transfer, Pi supports 8 bit transfer
static uint32_t speed = 32000000;//spi clock speed,32 MHz is the maximum supported clock frequency
static uint16_t delay;
/*****************************Function definition should come here**********************************************************************************************/
/*function for transfer*/
static void transfer(int fd)//declaring a function for the transfer, this should be call in main program for transfer
{
int ret;// a return variable
uint8_t tx[]={4,5,6,5,4,3,5,4};// Array initialization
uint8_t rx[ARRAY_SIZE(tx)]={0,};//recieving array should be same as transferred array
struct spi_ioc_transfer tr= { // standard structure for spi driver usage
.tx_buf=(unsigned long)tx,//transmitting buffer
.rx_buf= (unsigned long)rx,//recieving buffer
.len = ARRAY_SIZE(tx),//length of trasnmitting buffer
.delay_usecs=delay,
.speed_hz = speed,
.bits_per_word= bits,
};
ret = ioctl (fd, SPI_IOC_MESSAGE(1), &tr);//comm. is done by ioctl command, This initializes the transfer
for (ret=0;ret< ARRAY_SIZE(tx);ret++){//recieving the data in rx buffer and rx array size is same as tx array size
if (!(ret%4))//meant for printing process
puts("");
printf("%d\t %X\n",ret, rx[ret]);//formatting
}//herer return is used as an index for the recieving array
printf("\n");
float x=((rx[1]+rx[3])-(rx[0]+rx[2]))/(rx[0]+rx[1]+rx[2]+rx[3]);
//x[1]=((rx[5]+rx[7])-(rx[4])+rx[5])/(rx[4]+rx[5]+rx[6]+rx[7]);
printf("%f\n",x);
}//end of function for the transfer
/************************Main Program****************************************************************************************************************************/
int main(int argc, char *argv[]){// start of main
int ret=0;//initializing return
int fd;// a file handle to handle the device as a file
fd=open (device,O_RDWR);//fd handles this device with read and write permission
ret=ioctl(fd, SPI_IOC_WR_MODE,&mode);
ret=ioctl(fd, SPI_IOC_RD_MODE,&mode);
ret=ioctl(fd, SPI_IOC_WR_BITS_PER_WORD,&bits);
ret=ioctl(fd, SPI_IOC_RD_BITS_PER_WORD,&bits);
ret=ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ,&speed);
ret=ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ,&speed);
transfer(fd);//transfer of data begins
close(fd);//close the file handle, it has opened earlier
return ret;
}
在计算数据后,我只需要输出正确的符号。
答案 0 :(得分:0)
你真的认为数据如何进入printf()
很重要吗?它确实没有,printf()
当然不知道你正在使用SPI。
如果您只想要显示加号/减号(而不是负值的减号),请阅读the manual page for printf()
告诉我们:
+ 应始终在符号转换生成的数字之前放置一个符号(+或 - )。默认情况下,符号仅用于负数。如果使用两者,A +将覆盖空格。
所以,你应该使用:
printf("%+d\t%+d\n", x[0], x[1]);