|
主芯片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[2];
unsigned char status1;
unsigned char sn[4];
//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));} //串口调试输出值
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|