PIC SPI初学者问题(XC8 MCC)



我最近得到了I2C工作在没有时间与一些MCC生成的功能,这些功能在.h文件中有很好的记录,但是SPI没有给我任何东西,并且正在引起我的挫折,没有帮助的事实,我是新的这些串行协议。

我只是试图读取MCP23S17上的寄存器,然后写入它,然后再次读取它以验证它已更改。

我甚至不确定我是在正确的方式,但我已经包括我的代码下面的注释。出于某种原因,我似乎需要添加一些虚拟写以使第一次读取工作,但只在第二次循环中发生。

#include "mcc_generated_files/mcc.h"
uint8_t receiveData; /* Data that will be received */
uint8_t OpCodeW = 0x40;
uint8_t OpCodeR = 0x41;
void main(void)
{
SYSTEM_Initialize();
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
Reset1_GPIO_SetLow();
__delay_ms(200);
Reset1_GPIO_SetHigh();
__delay_ms(200);

printf("Initalised rn");

while (1)
{
SPI1_Open(SPI1_DEFAULT);
CS1_GPIO_SetLow();

// Read IODIRA Register 0x00
SPI1_ExchangeByte(OpCodeR);             // Address + Read
SPI1_ExchangeByte(0x00);                // ??? -- When I add this in it works 2nd loop -- ???
receiveData = SPI1_ExchangeByte(0x00);  // Returns 0x00 1st loop, then 0xFF after ... 
// ... but only when duplicate sending of byte above ???
printf("Read IODIRA: 0x%02x rn", receiveData);


// Try writing to IODIRA Register 
// Not sure what SPI1_WriteByte actually does!
// I thought it might be the same as ExchangeByte but without anything returned
// No idea!
SPI1_WriteByte(OpCodeW);                // Address + Write
SPI1_WriteByte(0x00);                   // Register Addres IODIRA (Port A)
SPI1_WriteByte(0xF0);                   // Data to be written


// Read back changed IODIRA Register again - Same routine as above
SPI1_ExchangeByte(OpCodeR);             // Address + Read
SPI1_ExchangeByte(0x00);                // Same routine as above  ...
// ... but always prints 0x00
receiveData = SPI1_ExchangeByte(0x00);  // Register Address, IODIRA (Port A)
printf("Wrote to IODIRA and read back: 0x%02x rn", receiveData);    

printf(" ----- rnn");
CS1_GPIO_SetHigh();               
SPI1_Close();
__delay_ms(5000);
}
}

实际打印的输出如下所示:

Initalised
Read IODIRA: 0x00    // Should be 0xFF
Wrote to IODIRA and read back: 0x00 // Should be 0xF0
-----
Read IODIRA: 0xff   // This is right now!
Wrote to IODIRA and read back: 0x00  // but this hasn't changed
-----
Read IODIRA: 0xff
Wrote to IODIRA and read back: 0x00
Read IODIRA: 0xff
Wrote to IODIRA and read back: 0x00
  1. 我的主要问题是我的方法是正确的吗?
  2. 为什么我需要一些虚拟写来让它在第二次循环中工作?-明显错误
  3. SPI1_ExchangeByte和SPI1_WriteByte有什么区别
  4. 是否有一些文档或指南使用这些功能,我错过了?

任何帮助都非常感谢。

结果是答案是芯片选择需要设置(低),每个SPI写入告诉设备消息何时启动/完成。我一直在使用这更像是启用(保持设备CS低),因为我只想与一个设备交谈,但这是不正确的。

相关内容

  • 没有找到相关文章