使用 NETMF 的 SPI 主设备到 PIC18F4550 从设备同步(C18)

发布于 2024-09-28 18:31:45 字数 815 浏览 0 评论 0原文

.NET Micro Framework 设备(本例中为 ChipworkX)通过 SPI 接口将字节发送到PIC18F。启用 PIE1bits.SSPIE 后,会在中断时执行以下代码:

void high_isr (void)
{
     PIE1bits.SSPIE = 0;
     PIR1bits.SSPIF = 0; //Clear interrupt flag.
     LATDbits.LATD5 = 1; //Enables LED for high interrupt activity.
     while ( !SSPSTATbits.BF ); //Wait until cycle complete
     red_byte_array[1] = SSPBUF;
     SSPBUF = 0x00;
     LATDbits.LATD5 = 0;
     PIE1bits.SSPIE = 1;
}

多次发送同一字节时,数据读取似乎不一致。主设备和从设备都设置为时钟空闲低电平,数据时钟在上升沿。我不使用片选线,因为它是直接通信。 最后,主设备以 100kHz 发送数据,而 PIC 以 8MHz 运行。

如何改进和/或修复此代码?

A .NET Micro Framework device (ChipworkX in this case) sends a byte through the SPI interface to a PIC18F. Having PIE1bits.SSPIE enabled, the following code is executed on interrrupt:

void high_isr (void)
{
     PIE1bits.SSPIE = 0;
     PIR1bits.SSPIF = 0; //Clear interrupt flag.
     LATDbits.LATD5 = 1; //Enables LED for high interrupt activity.
     while ( !SSPSTATbits.BF ); //Wait until cycle complete
     red_byte_array[1] = SSPBUF;
     SSPBUF = 0x00;
     LATDbits.LATD5 = 0;
     PIE1bits.SSPIE = 1;
}

When sending the same byte a few times, the data does not seem to be read consistently. Both master and slave are setup for clock idle low level, and data clocking on rising edge. I don't use the chip select line, because it's direct communictation.
Finally, the master sends data at 100 kHz, while the PIC is operating at 8 MHz.

How do I improve and/or fix this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌无敌 2024-10-05 18:31:45

PIC16F886/7 上:

如果不使用 /SS,则数据在上升沿发生变化并在下降沿采样,对于 SCK 空闲在 0:CKE = 0CKP = 0(或 1)、SMP = 0代码>.

从移位寄存器移动到缓冲寄存器的字节会导致 BF 位和 SSPIF 中断,因此通常不会在等待 BF 的中断中循环

不需要禁用 SSP 中断 (SSPIE = 0),但您可能需要在从中断返回之前清除 SSPIF

我猜你应该在 SSP 中断 (SSPIF = 1) 上:

red_byte_array[x] = SSPBUF
SSPIF = 0

您可能需要检查 WCOLSSPOV 是否有错误。

On the PIC16F886/7:

If you are not using the /SS, then the data changes on the rising edge and is sampled on the falling edge, for a SCK idling at 0: CKE = 0, CKP = 0 (or 1), SMP = 0.

The byte moving from the shift register to the buffer register causes BF bit and SSPIF the interrupt, so you don't normally loop about in the interrupt waiting for BF.

There should not be any need to disable SSP interrupts (SSPIE = 0), but you probably need to clear the SSPIF before returning from interrupt.

I would guess you should, on SSP interrupt (SSPIF = 1):

red_byte_array[x] = SSPBUF
SSPIF = 0

You may need to check WCOL and SSPOV for errors.

铃予 2024-10-05 18:31:45

鉴于您的 PIC 只有 (8 MHz / 100 kHz) 80 个周期来响应,Delay1KTCYx() 看起来相当长。

Given that your PIC only has ( 8 MHz / 100 kHz ) 80 cycles to respond, that Delay1KTCYx() seems rather long.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文