模拟SPI通迅分析

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值