gpt4 book ai didi

c - C语言数组计算时如何打印带符号的结果

转载 作者:行者123 更新时间:2023-11-30 19:38:35 25 4
gpt4 key购买 nike

我正在尝试在树莓派中进行 SPI 传输。 Raspberry pi 配有 SPI 接口(interface),可通过 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;
}

我只需要在计算数据后带有正确符号的输出。

最佳答案

你真的认为数据如何进入printf()很重要吗?事实并非如此,printf() 当然不知道您正在使用 SPI。

如果您只想显示加号/减号(而不仅仅是负值的减号),请阅读 the manual page for printf()告诉我们:

+ A sign (+ or -) should always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers. A + overrides a space if both are used.

所以,你应该使用:

printf("%+d\t%+d\n", x[0], x[1]);

关于c - C语言数组计算时如何打印带符号的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37877547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com