完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
在PIC16LF1824单片机上,使用MPLAB MCC生成SPI从属码。然而,目前还不清楚如何使用这些API。最重要的是,当一个中断被SPI接口触发时,数据将如何被应用程序读取,它在哪里被存储或被访问?其目的是当主机启动SPI传输时,从MCU被中断,它应该通过SPI读取数据并将其存储在一个数组中,这将被应用程序用于其他操作,例如确定设备ID和数据包的内容。不幸的是,我找不到。任何MCC生成的API的代码片段的任何文档。如果提供了如何使用ISR通过SPI接收数据包的代码片段,我将非常感激。上面的任何代码截断或示例都将非常感谢。
以上来自于百度翻译 以下为原文 I have generated the SPI Slave code using MPLAB MCC for PIC16LF1824 MCU. However, what is unclear is as to how to use these APIs. Most importantly, when an interrupt is triggered by the SPI interface, how the data will be read by the application and where is it stored or can be accessed? The aim is when the master starts the SPI transfer, slave MCU is interrupted, and it should read the data via SPI and store it in an array, which will be then used by my application for other operations such as determining the device ID and the contents of the packet. if(PIE1bits.SSP1IE == 1 && PIR1bits.SSP1IF == 1) { SPI_ISR(); PIR1bits.SSP1IF=0; // clear SSP interrupt bit } Unfortunately,I could not find any documentation of code snippet of this APIs generated by the MCC. I would be really grateful if any resources or code snippet as to how to use the ISR to received the data packet via the SPI is provided. uint8_t (*SPI_xchgHandler)(uint8_t); void SPI_Initialize(void) { SSP1STAT = 0x00; SSP1CON1 = 0x34; SSP1CON3 = 0x00; SSP1BUF = DUMMY_DATA; SPI_setExchangeHandler(SPI_DefaultExchangeHandler); PIE1bits.SSP1IE = 1; } void SPI_ISR(void) { SSP1BUF = SPI_xchgHandler(SSP1BUF); } void SPI_setExchangeHandler(uint8_t (* InterruptHandler)(uint8_t)){ SPI_xchgHandler = InterruptHandler; } uint8_t SPI_DefaultExchangeHandler(uint8_t byte){ // add your SPI interrupt custom code // or set custom function using SPI_setExchangeHandler() return byte; } Any code snipped or examples on above will be really grateful. |
|
相关推荐
8个回答
|
|
当该代码调用SpIsIsR()时,数据将在SSPBUF寄存器中可用。在PATH中为您的PIC读取SPI章节是很有启发性的。使用代码生成器并不意味着您可以跳过学习外围设备的工作方式。
以上来自于百度翻译 以下为原文 When that code calls SPI_ISR(); the data will be available in the SSPBUF register. It really would be instructive to read the SPI chapter in the datasheet for your PIC. Using a code generator does not mean you can skip learning how the peripheral works. |
|
|
|
谢谢Qub。简单地说,在SPS1BUF或SSPBUF中,我能通过SPI接收的字节数是多少?所以我刚刚读了SSP1BUF/SSPBUF,比如:RXYBuff= SSP1BUF;如果我错了请更正我。谢谢。
以上来自于百度翻译 以下为原文 Thanks qub. To put it simply, whatever number of bytes I receive via the SPI will be available in the SSP1BUF or SSPBUF? So then I just read the SSP1BUF/ SSPBUF something like: rx_buff = SSP1BUF; Correct me if I am wrong. Thanks |
|
|
|
也许你可以看看生成的项目,头文件,MCC生成的文件,SPI.H.这里有使用SPI的例子。
以上来自于百度翻译 以下为原文 Maybe you could have a look in the generated project, Header Files, MCC Generated Files, spi.h There in are examples of using the spi |
|
|
|
是的,虽然你发布的代码被论坛损坏了,因为你没有把代码标签放在它周围。我确信你想要每个字节都会得到一个中断。这是由你来决定如何告诉奴隶,它已经收到了所有的字节,如果你发送一个以上。N.B.请不要问问题的PM。这不是论坛的运作方式。
以上来自于百度翻译 以下为原文 Yes, although the code you posted was corrupted by the forum because you did not put code tags around it. I'm sure you intended rx_buff = SSP1BUF; but the was interpreted as "start italic font" by the forum You will get one interrupt per byte. It's up to you to work out how to tell the slave it has received all the bytes if you are sending more than one. n.b. please do not ask questions by PM. That is not how forums work. |
|
|
|
在读取数据表和浏览一些论坛之后,我创建了以下的ISR,用于处理/接收SPI上的多个字节数据。然而,我没有正确接收到3个字节。我从主服务器发送以下信息:当我从ISR()接收到以下内容时:我是不是漏掉了什么东西或者处理了SPI错误?
以上来自于百度翻译 以下为原文 After reading the datasheet and going over some forums I created the following ISR for handling/ receiving multiple bytes of data over SPI. void interrupt __high_priority my_isr_high(void) { if (PIR1bits.SSP1IF) { // Interrupt from SPI? rx[buffer_pointer] = SSP1BUF; // Get data from MSSP and store in RX buffer buffer_pointer++; // Next data if (buffer_pointer < FRAME_SIZE) // Ended? SSP1BUF = tx[buffer_pointer]; // Send next byte to SPI else buffer_pointer = FRAME_SIZE; PIR1bits.SSP1IF = 0; // Clear interrupt flag } } However, I am not receiving the 3 bytes correctly. I am sending the following from the master: dataPacket[0] = 0x43; // Decimal 67 dataPacket[1] = 0x42; //66 dataPacket[2] = 0x41; //65 While I am receiving as follows from the ISR(): rx[0]: 67 rx[1]: 65 rx[2]: 67 Am I missing something or handling the SPI incorrectly? |
|
|
|
我在这里分享我的代码,这样有助于快速找到解决方案。还包括一个zip文件,用于编译.HTTPS://www. DROPBOX.COM/SHI/4SOIKMRVCMYBN0/AAAVX6HJ5ELVNEWPPHGBKQ66A?DL= 0到目前为止,这段代码对我来说并没有起作用:因此,经过一点点的网上挖掘和其他论坛,我找到了读取多个字节的方法:尽管上面的代码给了我所期望的(即,正确的数据包按有序的方式),但是,它忽略了两个SPI中断。然后每次显示/捕获正确的数据。因此,两组数据总是丢失,然后第三个数据被正确接收。是否有错误配置或丢失?我真的在寻找解决办法,到目前为止还没有成功:
以上来自于百度翻译 以下为原文 I am sharing my codes here so that it helps to find the solution quickly. Also included is a .zip file for compiling. https://www.dropbox.com/sh/4soikmrvcmoibn0/AAAvx6hj5eLVnwEpPHgBKQi6a?dl=0 So far this code did not work for me properly: void interrupt __high_priority my_isr_high(void) { if (PIR1bits.SSP1IF) { // Interrupt from SPI? rx[buffer_pointer] = SSP1BUF; // Get data from MSSP and store in RX buffer buffer_pointer++; // Next data if (buffer_pointer < FRAME_SIZE) // Ended? SSP1BUF = tx[buffer_pointer]; // Send next byte to SPI else buffer_pointer = FRAME_SIZE; PIR1bits.SSP1IF = 0; // Clear interrupt flag } } Therefore, after a little bit of digging online and other forums I found the following way to read multiple bytes: uint8_t SPI_ExchangeHandler(uint8_t byte){ static uint8_t i = 0; for(i=0; i<3; i++){ SSP1BUF =0x00; while(!SSP1STATbits.BF); rx_buff=SSP1BUF; } State = SEND; return byte; } Although the above codes give me what expected (i.e, correct data packets in the ordered manner), however, it misses two SPI interrupts every time and then displays/captures the correct data. Hence, two sets of data are always lost and then the third one is received correctly. Is something wrongly configured or missing? I am really looking for a solution and so far has been unsuccessful :( |
|
|
|
你发布的第二个代码是一个主,而不是一个奴隶。一个奴隶应该等到数据到达,读取它,然后才把任何虚拟数据写入SSPBUF。
以上来自于百度翻译 以下为原文 The second code you posted is for a Master, not a slave. A slave should wait until data arrives, read it, and only then write any dummy data back to SSPBUF. |
|
|
|
最后,我成功地接收了所有的3个字节。共享下面的代码:当主SPI有数据发送时,我的中断服务例程触发MCU。SpIsIISR代码是由MCC GUI自动生成的。然后,我通过SPIX StExchange EngHeall()的定制函数处理SPI:我打印出如下的调试值:HOVEV呃,我敢肯定这不是最好的/优化的方法来处理SPI中的多字节,因此,如果有一个备选方案或建议,我很高兴听到这一点,并感谢如果你可以分享。
以上来自于百度翻译 以下为原文 Finally, I managed to receive all the 3 bytes correctly. Sharing the codes below: My interrupt service routine that triggers the MCU when master SPI has data to send. void interrupt INTERRUPT_InterruptManager (void){ if(PIE1bits.SSP1IE == 1 && PIR1bits.SSP1IF == 1) { SPI_ISR(); } } The SPI_ISR code was autogenerated by the MCC GUI. void SPI_ISR(void) { SSP1BUF = SPI_xchgHandler(SSP1BUF); } void SPI_setExchangeHandler(uint8_t (* InterruptHandler)(uint8_t)){ SPI_xchgHandler = InterruptHandler; } Then I handle the SPI via a custom function using SPI_setExchangeHandler() as follows: #define FRAME_SIZE 10 // Frame fixed size volatile static uint8_t rx_buff[FRAME_SIZE]; //RX buffer uint8_t SPI_ExchangeHandler(uint8_t byte){ static uint8_t i = 0; rx_buff=SSP1BUF; i++; if(i <= 2){ rx_buff=SSP1BUF; SSP1BUF = 0x00; while(!SSP1STATbits.BF){}; i++; } else{ i = 2; } PIR1bits.SSP1IF = 0; // Clear the SPI interrupt flag State = SEND; return byte; } And I print out the values as follows for debugging: printf("CMD:%d n", rx_buff[0]); printf("BUF1: %d n", rx_buff[1]); printf("BUF2: %d nn", rx_buff[2]); However, I am pretty sure this is not the best/optimized way to handle multiple bytes from SPI, therefore, if there is an alternative or suggestion, I am glad to hear that and appreciate if you could share. |
|
|
|
只有小组成员才能发言,加入小组>>
5183 浏览 9 评论
2005 浏览 8 评论
1932 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3178 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2229 浏览 5 评论
739浏览 1评论
626浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
511浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
637浏览 0评论
535浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 01:54 , Processed in 1.499476 second(s), Total 94, Slave 75 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号