使用 NETMF 的 SPI 主设备到 PIC18F4550 从设备同步(C18)
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 PIC16F886/7 上:
如果不使用
/SS
,则数据在上升沿发生变化并在下降沿采样,对于SCK
空闲在 0:CKE = 0
、CKP = 0
(或1
)、SMP = 0
代码>.从移位寄存器移动到缓冲寄存器的字节会导致
BF
位和SSPIF
中断,因此通常不会在等待BF 的中断中循环
。不需要禁用
SSP
中断 (SSPIE = 0
),但您可能需要在从中断返回之前清除SSPIF
。我猜你应该在
SSP
中断 (SSPIF = 1
) 上:red_byte_array[x] = SSPBUF
SSPIF = 0
您可能需要检查
WCOL
和SSPOV
是否有错误。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 aSCK
idling at 0:CKE = 0
,CKP = 0
(or1
),SMP = 0
.The byte moving from the shift register to the buffer register causes
BF
bit andSSPIF
the interrupt, so you don't normally loop about in the interrupt waiting forBF
.There should not be any need to disable
SSP
interrupts (SSPIE = 0
), but you probably need to clear theSSPIF
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
andSSPOV
for errors.鉴于您的 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.