【AT-START-F425测评】 七、SPI驱动RC522读卡器评测
[复制链接]
雅特力AT-START-F425提供了很多的demo例程,spi方面的有很多
但都是内部评测的,刚好手头有RC522的刷卡器,顺便评测以下刷卡的应用
代码是网上的,不过都是应用于STM32的,和AT-START-F425比较,就是硬件操作函数有些不同
评测用的主函数如下
int main(void)
{
system_clock_config();
at32_board_init();
uart_print_init(115200);
InitRc522();
printf("Rc522 Test \r\n");
while(1)
{
Scan_Card();
delay_ms(200);
}
}
刷卡程序是通用的
u8 Scan_Card(void)
{
u8 status, i, snr;
static u8 Key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
u8 CardID[5];
char StrCardID [ 30 ];
status = MI_NOTAGERR;
for(i = 0; i < 4; i++)//将存放卡号的数组清空
CardID[i] = 0;
status = PcdRequest(PICC_REQALL, CardID);//寻卡,CardID存放卡类型
if(status == MI_OK)
{
if(status == MI_OK)
{
status = PcdAnticoll(CardID);//防冲撞
if(status == MI_OK)
{
printf ( "防冲撞OK\n" );
status = PcdSelect(CardID);//选定卡片--参数为卡的序列号
if(status == MI_OK)
{
printf ( "选定卡片OK\n" );
snr = 8;
status = PcdAuthState(PICC_AUTHENT1A, (snr * 4 - 1), Key, CardID);
//验证密码
if(status == MI_OK)
{
printf ( "验证密码成功\n" );
at32_led_on(LED2);//LED1亮 刷卡了就亮灯,等到处理完数据才能重新刷
delay_ms(10);
at32_led_off(LED2);//灯灭
}
else
printf ( "验证密码失败\n" );
sprintf ( StrCardID, "The Card ID is: %02X%02X%02X%02X", CardID [ 0 ], CardID [ 1 ], CardID [ 2 ], CardID [ 3 ] );
printf ( "%s\r\n",StrCardID );
//else
return status;
}
else
return status;
}
else
return status;
}
else
return status;
}
else
return MI_NOTAGERR;
}
SPI初始化相关的部分
void RC522_SPI_Init(void)
{
gpio_init_type gpio_initstructure;
spi_init_type spi_init_struct;
//开启GPIO端口的时钟
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
//使用 SPI1
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
/* software cs, pa4 as a general io to control flash cs */
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_pins = GPIO_PINS_4;
gpio_init(GPIOA, &gpio_initstructure);
/* sck */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = GPIO_PINS_5;
gpio_init(GPIOA, &gpio_initstructure);
/* miso */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = GPIO_PINS_6;
gpio_init(GPIOA, &gpio_initstructure);
/* mosi */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = GPIO_PINS_7;
gpio_init(GPIOA, &gpio_initstructure);
//配置RST引脚的输出模式
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = RC522_SPI_RST_PIN;
gpio_init(GPIOA, &gpio_initstructure);
//拉高CS引脚,不选中模块,避免初始化影响
RC522_SPI_CS_HIGH();
//初始化SPIx
crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
spi_default_para_init(&spi_init_struct);
spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_8;
spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;
spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
spi_init(SPI1, &spi_init_struct);
spi_enable(SPI1, TRUE);
}
读写部分,就是用的寄存器不同,也没有去查手册,直接用的IDE中的提示,选择了看起来差不多的寄存器,评测结果直接通过了。
u8 RC522_SPI_ReadWriteByte(u8 TxData)
{
u16 retry = 0;
while((SPI1->sts & 1 << 1) == 0) //等待发送区空
{
retry++;
if(retry > 0XFFFE)return 0;
}
SPI1->dt = TxData; //发送一个byte
retry = 0;
while((SPI1->sts & 1 << 0) == 0) //等待接收完一个byte
{
retry++;
if(retry > 0XFFFE)return 0;
}
return SPI1->dt; //返回收到的数据
}
整体接线图
评测刷卡结果
移植很顺利。
|