DSP

MCP2515的使用(一)

2019-07-13 16:47发布

MCP2515的中文资料网上有很多,此,仅讨论具体的一些使用。先看下,在用ARM(LPC21XX)做控制器的情况下,用GPIO口模拟SPI总线的代码。

1.先看下SPI总线的时序图SPI总线有四根线,CSSCKMISOMOSI,是一种环形总线结构,如下图。 CS是片选。 SCK是串行时钟。 MISO是主输入从输出。 MOSI是主输出从输入。

时序图如下:(参照MCP2515的数据手册) SPI的输入时序

SPI的输出时序

2.分析下: Commands and data are sent to the device via the SI pin, with data being clocked in on the rising edge of SCK. Data is driven out by the MCP2515 (on the SO line) on the falling edge of SCK. 命令和数据通过SI脚发到设备上,数据在SCK的上升沿写入。数据在SCK的下降沿SO输出3.相应代码如下:(代码经使用是可行的,给出仅做参考) /****************************************************************************** *名称: WriteSPI()    SPI一个字节 *功能SPI一个字节        P0.11            CS        P0.12            SO        P0.13            SI        P1.22            SCK ******************************************************************************/ void  WriteSPI  (uint8  data) {     uint8 i;     IO1CLR = IO1CLR | (1 << 22);                            //SK=0     for ( i="0"; i<8 ;i++ )     {            i = i;                                                                //延时         if ( (data & 0x80)==0 )         {             IO0CLR = IO0CLR | (1 << 13);                    //SI=0         }         else         {             IO0SET = IO0SET | (1 << 13);                    //SI=1         }         i = i;               IO1SET = IO1SET | (1 << 22);                        //SK=1         i = i;         data = data << 1;         IO1CLR = IO1CLR | (1 << 22);                        //SK=0     } }   /****************************************************************************** *名称: ReadSPI()    SPI一个字节 *功能SPI一个字节        P0.11            CS        P0.12            SO        P0.13            SI        P1.22            SCK ******************************************************************************/ uint8  ReadSPI  ( void ) {     uint8 i;     uint32 bdata1,bdata2;     uint8 cdata = 0;     IO1CLR = IO1CLR | (1 << 22);                             //SK=0     for ( i="0"; i<8 ;i++ )     {         i = i;                                                                 //延时         i = i;         IO1SET = IO1SET | (1 << 22);                         //SK=1         cdata  = cdata << 1;         bdata1  = IO0PIN & 0x00001000;                  //SO                  i = i;         bdata2  = IO0PIN & 0x00001000;         if ( bdata1 != bdata2) return 0x00;               //验证下         if ( bdata1)         {             cdata = cdata | 0x01;         }         else         {             cdata = cdata & (~0x01);         }                 IO1CLR = IO1CLR | (1 << 22);                      //SK=0     }     return cdata; }