【GD32L233C-START 评测】-六、SPI驱动RC522刷卡
[复制链接]
刚好要做个刷卡的测试,就利用GD开始了
RC522是买的网上通用的那种廉价的,RST接到PB0,SDA接PB12,SCK接PB13,MISO接PB14,MOSI接PB15.
GD32的MCU手册中可以查到,使用spi需要重映射为AF6
于是,发现一个大坑
PB12这里一直出问题,后来查了其他测友的文章,只映射PB13-PB15,就OK了,不知道其他测友有没有遇到类似的问题。
#define SPI_CS_PORT GPIOB
#define SPI_CS_PIN GPIO_PIN_12
#define RST_522_PORT GPIOB
#define RST_522_PIN GPIO_PIN_0
#define RCU_SPI_PORT RCU_GPIOB
#define SPI_CS_HIGH gpio_bit_set(SPI_CS_PORT, SPI_CS_PIN)
#define SPI_CS_LOW gpio_bit_reset(SPI_CS_PORT, SPI_CS_PIN)
#define RST_522_HIGH gpio_bit_set(RST_522_PORT, RST_522_PIN)
#define RST_522_LOW gpio_bit_reset(RST_522_PORT, RST_522_PIN)
宏定义之后的初始化代码如下
void SpiInit(void)
{
spi_parameter_struct spi_init_struct;
rcu_periph_clock_enable(RCU_SPI_PORT);
rcu_periph_clock_enable(RCU_SPI1);
gpio_mode_set(RST_522_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RST_522_PIN );
gpio_output_options_set(RST_522_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RST_522_PIN);
/* SPI1 GPIO configuration: NSS/PB12 */
gpio_mode_set(SPI_CS_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, SPI_CS_PIN );
gpio_output_options_set(SPI_CS_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SPI_CS_PIN);
/* SPI1 GPIO configuration: SCK/PB13, MISO/PB14, MOSI/PB15 */
gpio_af_set(GPIOB, GPIO_AF_6, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);
/* deinitilize SPI and the parameters */
spi_i2s_deinit(SPI1);
spi_struct_para_init(&spi_init_struct);
/* SPI1 parameter configuration */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_8;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI1, &spi_init_struct);
spi_crc_polynomial_set(SPI1, 7);
spi_enable(SPI1);
}
Rc522的代码是以前stm32的项目中翻过来的
uint8_t Scan_Card(void)
{
uint8_t status, i, snr;
static u8 Key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
//static u8 Tempbuf[16] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C};;
u8 CardID[5];
status = MI_NOTAGERR;
for(i = 0; i < 4; i++)//将存放卡号的数组清空
CardID[i] = 0;
status = PcdRequest(PICC_REQALL, CardID);//寻卡,CardID存放卡类型
if(status == MI_OK)
{
status = PcdAnticoll(CardID);//防冲撞
if(status == MI_OK)
{
//printf ( "\r\n防冲撞OK" );
status = PcdSelect(CardID);//选定卡片--参数为卡的序列号
if(status == MI_OK)
{
printf ( "\r\n选定卡片OK" );
// snr = 8;
// status = PcdAuthState(PICC_AUTHENT1A, (snr * 4 - 1), Key, CardID);
// //验证密码
// if(status == MI_OK)
// printf ( "\r\n验证密码成功" );
// else
// printf ( "\r\n验证密码失败" );
//
//Display_CARD( CardID [ 0 ], CardID [ 1 ], CardID [ 2 ], CardID [ 3 ] );
printf("\r\n Scan_Card No.: %d-%d-%d-%d",CardID [ 0 ], CardID [ 1 ], CardID [ 2 ], CardID [ 3 ]);
return MI_OK;
}
}
}
else
status = MI_NOTAGERR;
return status;
}
初始化后,在main主循环中调用Scan_Card就能看到效果了
|