server240 发表于 2012-11-27 10:37:51

求助RC522调试

主芯片LPC1114与RC522通过SPI进行通信,用IO口模拟SPI。发现读取RC522任意寄存器,返回值都为0xff。麻烦知道的人帮我解答一下,谢谢了。代码如下:

void WriteRawRC(unsigned char Address, unsigned char value)
{
    unsigned char i, ucAddr;

    GPIOSetValue( PORT0, 10, 0 ); //sck = 0;
    GPIOSetValue( PORT1, 22, 0 ); //NSS = 0;
    ucAddr =((Address<<1)&0x7E);                                //地址字节的第一位为MSB,MSB=1为读,MSB=0为写

    for(i=8;i>0;i--)
    {                                          
          if(ucAddr&0x80) GPIOSetValue( PORT0, 9, 1 );
                else GPIOSetValue( PORT0, 9, 0 );
                GPIOSetValue( PORT0, 10, 1 );
                __nop();__nop();__nop();__nop();__nop();                  
                ucAddr <<= 1;
                GPIOSetValue( PORT0, 10, 0 );
          __nop();__nop();__nop();__nop();__nop();
    }
       
    for(i=8;i>0;i--)
    {
          if(value&0x80)GPIOSetValue( PORT0, 9, 1 );
                else GPIOSetValue( PORT0, 9, 0 );
                GPIOSetValue( PORT0, 10, 1 );
                __nop();__nop();__nop();__nop();__nop();
                value <<= 1;
                GPIOSetValue( PORT0, 10, 0 );
                __nop();__nop();__nop();__nop();__nop();
    }
    GPIOSetValue( PORT0, 10, 1 ); //SCK = 1;
    GPIOSetValue( PORT1, 22, 1 ); //NSS = 1;

}


unsigned char ReadRawRC(unsigned char Address)
{
   unsigned char i, ucAddr;
   unsigned char ucResult=0;

    GPIOSetValue( PORT0, 10, 0 ); //SCK = 0;
    GPIOSetValue( PORT1, 22, 0 ); //NSS = 0;
    ucAddr = ((Address<<1)&0x7E)|0x80;           //地址字节的第一位为MSB,MSB=1为读,MSB=0为写

   for(i=8;i>0;i--)
   {
          if(ucAddr&0x80) GPIOSetValue( PORT0, 9, 1 );
                else GPIOSetValue( PORT0, 9, 0 );
                GPIOSetValue( PORT0, 10, 1 );
                __nop();__nop();__nop();__nop();__nop();                  
                ucAddr <<= 1;
                GPIOSetValue( PORT0, 10, 0 );
          __nop();__nop();__nop();__nop();__nop();
   }

   for(i=8;i>0;i--)
   {
         GPIOSetValue( PORT0, 9, 1 ); //SCK = 1;//MF522_SCK = 1;
               __nop();__nop();__nop();__nop();__nop();
         ucResult <<= 1;
               if(0==RC523_MISO) {ucResult|=0x0;}
               else {ucResult|=0x1;}
         GPIOSetValue( PORT0, 10, 0 );//MF522_SCK = 0;
               __nop();__nop();__nop();__nop();__nop();
   }
    GPIOSetValue( PORT0, 10, 1 ); //SCK = 1;
    GPIOSetValue( PORT1, 22, 1 ); //NSS = 1;
    return ucResult;
}
int main(void)                        //主函数
{
unsigned char tt1;
unsigned char status1;
unsigned char sn;
       //SystemInit(); 可直接配置
        GPIOInit();       //GPIO初始化gpio.c

       /*模拟SPI I/O初始化 */
        GPIOSetDir( PORT0, 8, 0 ); //MISO       输入
    GPIOSetDir( PORT0, 9, 1 ); //MOSI       输出
    GPIOSetDir( PORT0, 10, 1 ); //SCK       时钟
    GPIOSetDir( PORT0, 11, 1 ); //RST       RC522复位控制
    GPIOSetDir( PORT0, 2, 1 ); //CS0       选通

    GPIOSetDir( PORT1, 31, 1 ); //LED        控制为输出口

    GPIOSetValue( PORT0, 8, 0 );
    GPIOSetValue( PORT0, 9, 1 );
    GPIOSetValue( PORT0, 10, 0 );
    GPIOSetValue( PORT0, 11, 0 );
    GPIOSetValue( PORT0, 2, 1 );

    GPIOSetValue( PORT1, 31, 0 );
GPIOSetValue( PORT1, 31, 1 );//MF522_RST=1;       //软件将RST置一段低电平,复位
    __nop();__nop();__nop();__nop();__nop();
    GPIOSetValue( PORT1, 31, 0 );//MF522_RST=0;
    __nop(); __nop();__nop();__nop();__nop();
    GPIOSetValue( PORT1, 31, 1 );//MF522_RST=1;
    __nop();__nop();__nop();__nop();__nop();

    WriteRawRC(CommandReg,PCD_RESETPHASE);       //设置commandreg复位指令

    while(1)
{
      for (i = 0; i < 0x40000; i++)   // DELAY
      {
      }

                j++;
                if(j==10)
                {
                        j=0;
                        shellPrint("version is:%x \n",ReadRawRC(0x37));}   //串口调试输出值


}
}

server240 发表于 2013-1-10 17:08:06

问题解决,如有遇到这种问题的朋友,建议看下引脚波形,时序是否正确。
一点一点查找,会有收获的!
我这里是有个变量定义错误导致的。
页: [1]
查看完整版本: 求助RC522调试