原件:stm32f103c8t6,RC522,MG90S舵机
首先,配置LED以及MG90S初始化
所有的代码放在这里了:
https://gitee.com/armgpio/access-control-system
MG90S的OUT口接的是PB6,也就是stm32f103c8t6定时器TIM4的通道1。通过TIM4产生周期为20ms的PWM波,当高电平所占时间为1ms时,舵机转到中点位置。
mg90s.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| void TIM4_PWM_Init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = PWM_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(MOTO_GPIO_PORT, &GPIO_InitStructure); GPIO_SetBits(MOTO_GPIO_PORT,PWM_GPIO_PIN);
TIM_TimeBaseStructure.TIM_Period = arr; TIM_TimeBaseStructure.TIM_Prescaler =psc; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); TIM_Cmd(TIM4, ENABLE); }
|
RC522
对于RC522来说,使用的是单片机的硬件SPI。在SPI通信中,需要用到微秒的延时,所以这里还要用到嘀嗒定时器。配置好嘀嗒定时器后,就是RC522的初始化。
RC522的初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| void RC522_Init ( void ) { RC522_SPI_Config (); RC522_Reset_Disable(); RC522_CS_Disable(); }
/** * @brief R522 SPI配置 * @param 无 * @retval 无 */ static void RC522_SPI_Config ( void ) { /* SPI_InitTypeDef SPI_InitStructure */ GPIO_InitTypeDef GPIO_InitStructure; /*!< Configure SPI_RC522_SPI pins: CS */ RC522_GPIO_CS_CLK_FUN ( RC522_GPIO_CS_CLK, ENABLE ); GPIO_InitStructure.GPIO_Pin = RC522_GPIO_CS_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = RC522_GPIO_CS_Mode; GPIO_Init(RC522_GPIO_CS_PORT, &GPIO_InitStructure); /*!< Configure SPI_RC522_SPI pins: SCK */ RC522_GPIO_SCK_CLK_FUN ( RC522_GPIO_SCK_CLK, ENABLE ); GPIO_InitStructure.GPIO_Pin = RC522_GPIO_SCK_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = RC522_GPIO_SCK_Mode; GPIO_Init(RC522_GPIO_SCK_PORT, &GPIO_InitStructure); /*!< Configure SPI_RC522_SPI pins: MOSI */ RC522_GPIO_MOSI_CLK_FUN ( RC522_GPIO_MOSI_CLK, ENABLE ); GPIO_InitStructure.GPIO_Pin = RC522_GPIO_MOSI_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = RC522_GPIO_MOSI_Mode; GPIO_Init(RC522_GPIO_MOSI_PORT, &GPIO_InitStructure);
/*!< Configure SPI_RC522_SPI pins: MISO */ RC522_GPIO_MISO_CLK_FUN ( RC522_GPIO_MISO_CLK, ENABLE ); GPIO_InitStructure.GPIO_Pin = RC522_GPIO_MISO_PIN; GPIO_InitStructure.GPIO_Mode = RC522_GPIO_MISO_Mode; GPIO_Init(RC522_GPIO_MISO_PORT, &GPIO_InitStructure); /*!< Configure SPI_RC522_SPI pins: RST */ RC522_GPIO_RST_CLK_FUN ( RC522_GPIO_RST_CLK, ENABLE ); GPIO_InitStructure.GPIO_Pin = RC522_GPIO_RST_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = RC522_GPIO_RST_Mode; GPIO_Init(RC522_GPIO_RST_PORT, &GPIO_InitStructure); }
|
接下来就是主函数
在main.c里面包含两个函数,一个是寻卡函数,一个是main函数。首先定义的是卡A的密匙,基本上都是6个0xff。在寻卡函数里面,讲预先设定好的IC卡序列号存储在数组中,再将读取到的序列号和数组进行比对,这样就能正确识别到已经保存的卡片信息。
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| uint8_t KeyValue[]={0xFF ,0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
void IC_test ( void ) { uint8_t ucArray_ID [ 4 ]; uint8_t ucStatusReturn; u8 ID1[]={0xA9,0x37,0x71,0xe4}; while ( 1 ) { if ( ( ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ) ) != MI_OK ) ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ); if ( ucStatusReturn == MI_OK ) { if ( PcdAnticoll ( ucArray_ID ) == MI_OK ) { PcdSelect(ucArray_ID); PcdAuthState( PICC_AUTHENT1A, 0x11, KeyValue, ucArray_ID ); GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN); if(ID1[0]==ucArray_ID[0]) { TIM_SetCompare1(TIM4,75); Delay_ms(1000); TIM_SetCompare1(TIM4,50); GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); } else { TIM_SetCompare1(TIM4,50); GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); Delay_ms(1000); GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN); Delay_ms(1000); GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); Delay_ms(1000); GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN); Delay_ms(1000); GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); Delay_ms(1000); GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN); Delay_ms(1000); GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); Delay_ms(1000); GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN); Delay_ms(1000); } PcdHalt(); } } } }
int main(void) { SysTick_Init (); RC522_Init (); TIM4_PWM_Init(999,1439); TIM_SetCompare1(TIM4,50); LED_GPIO_Config(); GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN); PcdReset(); M500PcdConfigISOType ( 'A' ); while(1) { IC_test(); } }
|