简体   繁体   English

向SPI发送一个8字节的数组

[英]Sending an 8-byte array to SPI

I am trying to set my SPI communication with a PIC18F452 from Microchip in order to send an 8-byte array to the bus (= transfer of all the bytes at once). 我正在尝试使用Microchip的PIC18F452进行SPI通信,以便向总线发送一个8字节的数组(=一次传输所有字节)。 Here is an example of what my array looks like : 以下是我的数组的示例:

 array   0x0080 byte[8]
array[0] 0x0080  0x01 
array[1] 0x0081  0x01 
array[2] 0x0082  '\0'
array[3] 0x0083  0x01 
array[4] 0x0084  0x01 
array[5] 0x0085  0x01 
array[6] 0x0086  0x01 
array[7] 0x0087  '\0'

The values are coming from an input signal (audio file, clock, etc.) and received on PORTDbits.RD0 to fulfill the array. 这些值来自输入信号(音频文件,时钟等),并在PORTDbits.RD0上接收以完成数组。 The values are always 1 or 0 and nothing else. 值始终为1或0,而不是其他值。

First, I have started by sending one byte at the time with putcSPI() . 首先,我已经开始使用putcSPI()发送一个字节。 As I fulfill the array, I send one byte to the SPI and the results are a match. 当我完成数组时,我向SPI发送一个字节,结果是匹配的。

Then, I tried to send all the bytes at once, using putsSPI() like follow : 然后,我尝试一次发送所有字节,使用putsSPI() ,如下所示:

/// SEND DATA TO SPI
SPI_INT = 0;     // Enable SS 
putsSPI(array);
SPI_INT = 1;     // Disable SS
delay_ms();      // Delay

But the transfer of the frame is stopped when I encounter a 0 (considered as end of array, so that's normal). 但是当我遇到0(被认为是数组的结尾,这是正常的)时,帧的传输就会停止。 And, my frame is divided into chunks. 而且,我的框架分为几个块。

eg, for the array displayed above, I have on the SPI bus "1 1" and then, the next values are from the frames that follow 例如,对于上面显示的数组,我在SPI总线上“1 1”,然后,下一个值来自后面的帧

That being said, I thought of converting the data from binary to hexadecimal (or integer) and then send it to SPI. 话虽这么说,我想到将数据从二进制转换为十六进制(或整数),然后将其发送到SPI。 I have tried several methods of conversion found here, but until now none has been successful. 我在这里尝试了几种转换方法,但直到现在还没有成功。

Is there a solution to send directly the full array to the bus, or does anyone have an idea on how to perform the conversion in this particular case ? 是否有解决方案直接将完整阵列发送到总线,或者有人知道如何在这种特殊情况下执行转换?

Many thanks in advance ! 提前谢谢了 !

Eventually and in order to achieve my goal, I have completely changed my method to solve this problem. 最终,为了实现我的目标,我完全改变了我的方法来解决这个问题。

I still use SPI for the communication. 我仍然使用SPI进行通信。 But instead of an array, I save the value of my input signal (after all the operations I apply to it) in a value. 但是,我将输入信号的值(在我应用于它的所有操作之后)保存在一个值中,而不是数组。 This value is later separate in two parts that will be send one byte one to the bus. 该值后来分成两部分,一部分将一个字节发送到总线。

// Definition
unsigned int value = 0;
unsigned char value_byte1 = 0; 
unsigned char value_byte2 = 0; 
unsigned int onTheBus_byte1 = 0; 
unsigned int onTheBus_byte2 = 0;

// In main function, the value is modified using a counter (value++;)

// Transfer to SPI
value_byte1 = value >> 8;
value_byte2 = value & 0xFF;

SPI_INT = 0;  // Enable SS 
onTheBus_byte1 = WriteSPI(value_byte1);
onTheBus_byte2 = WriteSPI(value_byte2);
SPI_INT = 1;  // Disable SS

So, if I get : 所以,如果我得到:

value = 1505 (= 0x05E1)

in the main loop, then 然后在主循环中

value_byte1 = 0x05
value_byte2 = 0xE1

(This code has been tested on Proteus, and I do see both bytes on the SPI debugger when the value is written on the bus) (这段代码已经在Proteus上测试过了,当在总线上写入值时,我确实在SPI调试器上看到了两个字节)

In case I need to use the value, say on another pic, I assemble the two parts after I have read them : 如果我需要使用该值,比如在另一张图片中,我在阅读完之后将这两个部分组合在一起:

value = (value_byte1 << 8) | value_byte2; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM