#define macRC522_CS_Enable() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define macRC522_CS_Disable() GPIO_SetBits(GPIOA, GPIO_Pin_4)
#define macRC522_Reset_Enable() GPIO_ResetBits(GPIOA, GPIO_Pin_6)
#define macRC522_Reset_Disable() GPIO_SetBits(GPIOA, GPIO_Pin_6)
#define macRC522_SCK_0() GPIO_ResetBits( GPIOA, GPIO_Pin_1 )
#define macRC522_SCK_1() GPIO_SetBits ( GPIOA, GPIO_Pin_1 )
#define macRC522_MOSI_0() GPIO_ResetBits( GPIOA, GPIO_Pin_0 )
#define macRC522_MOSI_1() GPIO_SetBits ( GPIOA, GPIO_Pin_0 )
#define macRC522_MISO_GET() GPIO_ReadInputDataBit ( GPIOB, GPIO_Pin_1 )
/*
* 函数名:SPI_RC522_SendByte
* 描述 :向RC522发送1 Byte 数据
* 输入 :byte,要发送的数据
* 返回 : RC522返回的数据
* 调用 :内部调用
*/
void SPI_RC522_SendByte ( u8 byte )
{
u8 counter;
for(counter=0;counter<8;counter++)//分8次发送8个位
{
if ( byte & 0x80 )//从最高位开始发送,这是高位在前模式MSB
{
macRC522_MOSI_1 ();//高电平
}
else
{
macRC522_MOSI_0 ();//低电平
}
Delay_us(5);//保持5us上个状态
macRC522_SCK_0 ();//先低
Delay_us(5);
macRC522_SCK_1();//后高 SPI_CPOL_Low CPOL=0模式
Delay_us(5);//先保持5us高
byte <<= 1;
}
}
/*
* 函数名:SPI_RC522_ReadByte
* 描述 :从RC522发送1 Byte 数据
* 输入 :无
* 返回 : RC522返回的数据
* 调用 :内部调用
*/
u8 SPI_RC522_ReadByte ( void )
{
u8 counter;
u8 SPI_Data;
for(counter=0;counter<8;counter++)
{
SPI_Data <<= 1; //先左移一位,counter=0时的第一次值没变化
macRC522_SCK_0 ();//sck拉低
Delay_us(5);//延时
if ( macRC522_MISO_GET() == 1)//如果输入为高,最低位置1
{
SPI_Data |= 0x01;
}
Delay_us(5);//延时5us
macRC522_SCK_1 ();//拉高
Delay_us(5);//拉高5us
}
return SPI_Data;
}