前言:
学完了江科大的stm32的视频后,着手了第二个小项目智能门锁,起初开始做也是毫无头绪在网上找了一堆资料,看了别人的代码,好多都是单独的模块文件,自己就不断调试整合在自己main函数里面,整合期间才发现自己的代码水平是真的烂,不过磕磕碰碰还是得到现在这个版本,代码写得一坨,感觉很冗杂,还有一些小bug但基本上的功能都实现了,写个博客记录一下自己踩得坑分享给大家 ,源码在文章末尾也是无偿分享给大家,希望有什么需要改进的地方大家在评论区提出来,代码优化的空间还很大!!!
一、材料准备
1。stm32c8t6芯片
2。sg90舵机
3。矩阵按键
4。OLED显示屏 0.96寸 4针
5。RFID模块
6。HC-05蓝牙模块
7。有源蜂鸣器
烧录工具这些就不说了基本上大家都有 这些成本加起来不过50块钱 用面包板就行了 指纹模块有点小贵就没有弄 想弄的友友直接加上模块就行不是很难 基本上都是一些模块化的编程 都是移植单个模块的使用代码然后再自己整合成一个完整的项目
二、模块设计
1.OLED模块
这个直接照搬江科大的OLED代码 直接用函数就行没什么难度
2.4x4矩阵按键模块
这个最麻烦的就是接线因为有8跟线 ,至于原理这些可以去网上自行查看 接线我放在代码注释里面了直接看就是了
/*
*功能 : 已标准库的形式驱动4x4的矩阵按键,返回1~16的uint8_t的数据
*
*/
#include "stm32f10x.h" // Device header
#include "Delay.h"
# define ROW_PORT GPIOA
# define ROW1 GPIO_Pin_15 // 接线 R4
# define ROW2 GPIO_Pin_14 //R3
# define ROW3 GPIO_Pin_13 //R2
# define ROW4 GPIO_Pin_12 //R1
# define CLON1 GPIO_Pin_8 // ½ÓÏߣºC4
# define CLON2 GPIO_Pin_11 // C3
# define CLON3 GPIO_Pin_12
# define CLON4 GPIO_Pin_0
uint8_t MatrixKey_GetNum(void);
void MatrixKey_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = ROW1 | ROW2 | ROW3 | ROW4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//ÍÆÍìÊä³ö
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin =CLON1 | CLON2 | CLON3 | CLON4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//ÄÚ²¿ÉÏÀ
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*uint8_t MatrixKey_GetValue()
*¹¦ÄÜ£ºÉ¨ÃèÿÐУ¬²¢µ÷ÓÃuint8_t MatrixKey_GetNum()ɨÃè¶ÔÓ¦ÁÐ
* 扫描行 并且获取键值
*/
uint8_t MatrixKey_GetValue()
{
uint8_t Key;
GPIO_ResetBits(GPIOB,ROW1);
GPIO_SetBits(GPIOB,ROW2);
GPIO_SetBits(GPIOB,ROW3);
GPIO_SetBits(GPIOB,ROW4);
Key=MatrixKey_GetNum();
if(Key!=0)
return Key;
GPIO_SetBits(GPIOB,ROW1);
GPIO_ResetBits(GPIOB,ROW2);
GPIO_SetBits(GPIOB,ROW3);
GPIO_SetBits(GPIOB,ROW4);
Key=MatrixKey_GetNum();
if(Key!=0)
return Key+4;
GPIO_SetBits(GPIOB,ROW1);
GPIO_SetBits(GPIOB,ROW2);
GPIO_ResetBits(GPIOB,ROW3);
GPIO_SetBits(GPIOB,ROW4);
Key=MatrixKey_GetNum();
if(Key!=0)
return Key+8;
GPIO_SetBits(GPIOB,ROW1);
GPIO_SetBits(GPIOB,ROW2);
GPIO_SetBits(GPIOB,ROW3);
GPIO_ResetBits(GPIOB,ROW4);
Key=MatrixKey_GetNum();
if(Key!=0)
return Key+12;
return 0;
}
//扫描列
uint8_t MatrixKey_GetNum()
{
uint8_t Key = 0;
if(GPIO_ReadInputDataBit(GPIOA,CLON1)==0)
{
Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA,CLON1)==0);
Key=1;
}
else if(GPIO_ReadInputDataBit(GPIOA,CLON2)==0)
{
Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA,CLON2)==0);
Delay_ms(10);
Key=2;
}
else if(GPIO_ReadInputDataBit(GPIOA,CLON3)==0)
{
Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA,CLON3)==0);
Delay_ms(10);
Key= 3;
}
else if(GPIO_ReadInputDataBit(GPIOA,CLON4)==0)
{
Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA,CLON4)==0);
Delay_ms(10);
Key= 4;
}
return Key;
}
3.RFID模块
网上搜出来都是单独的例程,没法结合实际的使用 此项目需要读卡 删卡 存卡几个功能 创建4个数组来存放4张卡片的数据,并且保存在flash里面 实现掉电不丢失功能 就放一些代码在这 详细阅读还是去云盘下载完整的代码
#include "stm32f10x.h" // Device header
#include "RC522.H"
uint8_t UID[4],Temp[4];
uint8_t UI0[4]={0xFF,0xFF,0xFF,0xFF}; //¿¨0IDºÅ
uint8_t UI1[4]={0xFF,0xFF,0xFF,0xFF}; //¿¨1IDºÅ
uint8_t UI2[4]={0xFF,0xFF,0xFF,0xFF}; //¿¨2IDºÅ
uint8_t UI3[4]={0xFF,0xFF,0xFF,0xFF}; //¿¨3IDºÅ
//RC522¶Ë¿Ú¶¨Òå
void PcdInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the GPIO Clock */
RCC_APB2PeriphClockCmd(MF522_RST_CLK, ENABLE);
/* Configure the GPIO pin */
GPIO_InitStructure.GPIO_Pin = MF522_RST_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(MF522_RST_PORT, &GPIO_InitStructure);
/* Enable the GPIO Clock */
RCC_APB2PeriphClockCmd(MF522_MISO_CLK, ENABLE);
/* Configure the GPIO pin */
GPIO_InitStructure.GPIO_Pin = MF522_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(MF522_MISO_PORT, &GPIO_InitStructure);
/* Enable the GPIO Clock */
RCC_APB2PeriphClockCmd(MF522_MOSI_CLK, ENABLE);
/* Configure the GPIO pin */
GPIO_InitStructure.GPIO_Pin = MF522_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(MF522_MOSI_PORT, &GPIO_InitStructure);
/* Enable the GPIO Clock */
RCC_APB2PeriphClockCmd(MF522_SCK_CLK, ENABLE);
/* Configure the GPIO pin */
GPIO_InitStructure.GPIO_Pin = MF522_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(MF522_SCK_PORT, &GPIO_InitStructure);
/* Enable the GPIO Clock */
RCC_APB2PeriphClockCmd(MF522_NSS_CLK, ENABLE);
/* Configure the GPIO pin */
GPIO_InitStructure.GPIO_Pin = MF522_NSS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(MF522_NSS_PORT, &GPIO_InitStructure);
}
///ÒÔÏÂΪRC522Çý¶¯º¯Êý/
///ÒÔÏÂΪRC522Çý¶¯º¯Êý/
///ÒÔÏÂΪRC522Çý¶¯º¯Êý/
//¹¦ ÄÜ£ºÑ°¿¨
//²ÎÊý˵Ã÷: req_code[IN]:Ñ°¿¨·½Ê½
// 0x52 = Ñ°¸ÐÓ¦ÇøÄÚËùÓзûºÏ14443A±ê×¼µÄ¿¨
// 0x26 = Ѱδ½øÈëÐÝÃß״̬µÄ¿¨
// pTagType[OUT]£º¿¨Æ¬ÀàÐÍ´úÂë
// 0x4400 = Mifare_UltraLight
// 0x0400 = Mifare_One(S50)
// 0x0200 = Mifare_One(S70)
// 0x0800 = Mifare_Pro(X)
// 0x4403 = Mifare_DESFire
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdRequest(unsigned char req_code,unsigned char *pTagType)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
// unsigned char xTest ;
ClearBitMask(Status2Reg,0x08);
WriteRawRC(BitFramingReg,0x07);
// xTest = ReadRawRC(BitFramingReg);
// if(xTest == 0x07 )
// { LED_GREEN =0 ;}
// else {LED_GREEN =1 ;while(1){}}
SetBitMask(TxControlReg,0x03);
ucComMF522Buf[0] = req_code;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);
// if(status == MI_OK )
// { LED_GREEN =0 ;}
// else {LED_GREEN =1 ;}
if ((status == MI_OK) && (unLen == 0x10))
{
*pTagType = ucComMF522Buf[0];
*(pTagType+1) = ucComMF522Buf[1];
}
else
{ status = MI_ERR; }
return status;
}
//¹¦ ÄÜ£º·À³åײ
//²ÎÊý˵Ã÷: pSnr[OUT]:¿¨Æ¬ÐòÁкţ¬4×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdAnticoll(unsigned char *pSnr)
{
char status;
unsigned char i,snr_check=0;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg,0x08);
WriteRawRC(BitFramingReg,0x00);
ClearBitMask(CollReg,0x80);
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x20;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);
if (status == MI_OK)
{
for (i=0; i<4; i++)
{
*(pSnr+i) = ucComMF522Buf[i];
snr_check ^= ucComMF522Buf[i];
}
if (snr_check != ucComMF522Buf[i])
{ status = MI_ERR; }
}
SetBitMask(CollReg,0x80);
return status;
}
//¹¦ ÄÜ£ºÑ¡¶¨¿¨Æ¬
//²ÎÊý˵Ã÷: pSnr[IN]:¿¨Æ¬ÐòÁкţ¬4×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdSelect(unsigned char *pSnr)
{
char status;
unsigned char i;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x70;
ucComMF522Buf[6] = 0;
for (i=0; i<4; i++)
{
ucComMF522Buf[i+2] = *(pSnr+i);
ucComMF522Buf[6] ^= *(pSnr+i);
}
CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]);
ClearBitMask(Status2Reg,0x08);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x18))
{ status = MI_OK; }
else
{ status = MI_ERR; }
return status;
}
//¹¦ ÄÜ£ºÑéÖ¤¿¨Æ¬ÃÜÂë
//²ÎÊý˵Ã÷: auth_mode[IN]: ÃÜÂëÑé֤ģʽ
// 0x60 = ÑéÖ¤AÃÜÔ¿
// 0x61 = ÑéÖ¤BÃÜÔ¿
// addr[IN]£º¿éµØÖ·
// pKey[IN]£ºÃÜÂë
// pSnr[IN]£º¿¨Æ¬ÐòÁкţ¬4×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = auth_mode;
ucComMF522Buf[1] = addr;
for (i=0; i<6; i++)
{ ucComMF522Buf[i+2] = *(pKey+i); }
for (i=0; i<6; i++)
{ ucComMF522Buf[i+8] = *(pSnr+i); }
// memcpy(&ucComMF522Buf[2], pKey, 6);
// memcpy(&ucComMF522Buf[8], pSnr, 4);
status = PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (!(ReadRawRC(Status2Reg) & 0x08)))
{ status = MI_ERR; }
return status;
}
//¹¦ ÄÜ£º¶ÁÈ¡M1¿¨Ò»¿éÊý¾Ý
//²ÎÊý˵Ã÷: addr[IN]£º¿éµØÖ·
// pData[OUT]£º¶Á³öµÄÊý¾Ý£¬16×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdRead(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_READ;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x90))
// { memcpy(pData, ucComMF522Buf, 16); }
{
for (i=0; i<16; i++)
{ *(pData+i) = ucComMF522Buf[i]; }
}
else
{ status = MI_ERR; }
return status;
}
//¹¦ ÄÜ£ºÐ´Êý¾Ýµ½M1¿¨Ò»¿é
//²ÎÊý˵Ã÷: addr[IN]£º¿éµØÖ·
// pData[IN]£ºÐ´ÈëµÄÊý¾Ý£¬16×Ö½Ú
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdWrite(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_WRITE;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
if (status == MI_OK)
{
//memcpy(ucComMF522Buf, pData, 16);
for (i=0; i<16; i++)
{ ucComMF522Buf[i] = *(pData+i); }
CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
}
return status;
}
//¹¦ ÄÜ£ºÃüÁƬ½øÈëÐÝÃß״̬
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdHalt(void)
{
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_HALT;
ucComMF522Buf[1] = 0;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
return MI_OK;
}
//ÓÃMF522¼ÆËãCRC16º¯Êý
void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData)
{
unsigned char i,n;
ClearBitMask(DivIrqReg,0x04);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i<len; i++)
{ WriteRawRC(FIFODataReg, *(pIndata+i)); }
WriteRawRC(CommandReg, PCD_CALCCRC);
i = 0xFF;
do
{
n = ReadRawRC(DivIrqReg);
i--;
}
while ((i!=0) && !(n&0x04));
pOutData[0] = ReadRawRC(CRCResultRegL);
pOutData[1] = ReadRawRC(CRCResultRegM);
}
//¹¦ ÄÜ£º¸´Î»RC522
//·µ »Ø: ³É¹¦·µ»ØMI_OK
char PcdReset(void)
{
RST_H;
delay_10ms(1);
RST_L;
delay_10ms(1);
RST_H;
delay_10ms(10);
if(ReadRawRC(0x02) == 0x80)
{
}
WriteRawRC(CommandReg,PCD_RESETPHASE);
WriteRawRC(ModeReg,0x3D); //ºÍMifare¿¨Í¨Ñ¶£¬CRC³õʼֵ0x6363
WriteRawRC(TReloadRegL,30);
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
WriteRawRC(TxAutoReg,0x40);
return MI_OK;
}
//ÉèÖÃRC632µÄ¹¤×÷·½Ê½
char M500PcdConfigISOType(unsigned char type)
{
if (type == 'A') //ISO14443_A
{
ClearBitMask(Status2Reg,0x08);
/* WriteRawRC(CommandReg,0x20); //as default
WriteRawRC(ComIEnReg,0x80); //as default
WriteRawRC(DivlEnReg,0x0); //as default
WriteRawRC(ComIrqReg,0x04); //as default
WriteRawRC(DivIrqReg,0x0); //as default
WriteRawRC(Status2Reg,0x0);//80 //trun off temperature sensor
WriteRawRC(WaterLevelReg,0x08); //as default
WriteRawRC(ControlReg,0x20); //as default
WriteRawRC(CollReg,0x80); //as default
*/
WriteRawRC(ModeReg,0x3D);//3F
/* WriteRawRC(TxModeReg,0x0); //as default???
WriteRawRC(RxModeReg,0x0); //as default???
WriteRawRC(TxControlReg,0x80); //as default???
WriteRawRC(TxSelReg,0x10); //as default???
*/
WriteRawRC(RxSelReg,0x86);//84
// WriteRawRC(RxThresholdReg,0x84);//as default
// WriteRawRC(DemodReg,0x4D); //as default
// WriteRawRC(ModWidthReg,0x13);//26
WriteRawRC(RFCfgReg,0x7F); //4F
/* WriteRawRC(GsNReg,0x88); //as default???
WriteRawRC(CWGsCfgReg,0x20); //as default???
WriteRawRC(ModGsCfgReg,0x20); //as default???
*/
WriteRawRC(TReloadRegL,30);//tmoLength);// TReloadVal = 'h6a =tmoLength(dec)
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
// PcdSetTmo(106);
delay_10ms(1);
PcdAntennaOn();
}
else{ return (char)-1; }
return MI_OK;
}
//¹¦ ÄÜ£º¶ÁRC632¼Ä´æÆ÷
//²ÎÊý˵Ã÷£ºAddress[IN]:¼Ä´æÆ÷µØÖ·
//·µ »Ø£º¶Á³öµÄÖµ
unsigned char ReadRawRC(unsigned char Address)
{
unsigned char i, ucAddr;
unsigned char ucResult=0;
NSS_L;
ucAddr = ((Address<<1)&0x7E)|0x80;
for(i=8;i>0;i--)
{
SCK_L;
if(ucAddr&0x80)
MOSI_H;
else
MOSI_L;
SCK_H;
ucAddr <<= 1;
}
for(i=8;i>0;i--)
{
SCK_L;
ucResult <<= 1;
SCK_H;
if(READ_MISO == 1)
ucResult |= 1;
}
NSS_H;
SCK_H;
return ucResult;
}
//¹¦ ÄÜ£ºÐ´RC632¼Ä´æÆ÷
//²ÎÊý˵Ã÷£ºAddress[IN]:¼Ä´æÆ÷µØÖ·
// value[IN]:дÈëµÄÖµ
void WriteRawRC(unsigned char Address, unsigned char value)
{
unsigned char i, ucAddr;
SCK_L;
NSS_L;
ucAddr = ((Address<<1)&0x7E);
for(i=8;i>0;i--)
{
if(ucAddr&0x80)
MOSI_H;
else
MOSI_L;
SCK_H;
ucAddr <<= 1;
SCK_L;
}
for(i=8;i>0;i--)
{
if(value&0x80)
MOSI_H;
else
MOSI_L;
SCK_H;
value <<= 1;
SCK_L;
}
NSS_H;
SCK_H;
}
//¹¦ ÄÜ£ºÖÃRC522¼Ä´æÆ÷λ
//²ÎÊý˵Ã÷£ºreg[IN]:¼Ä´æÆ÷µØÖ·
// mask[IN]:ÖÃλֵ
void SetBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg,tmp | mask); // set bit mask
}
//¹¦ ÄÜ£ºÇåRC522¼Ä´æÆ÷λ
//²ÎÊý˵Ã÷£ºreg[IN]:¼Ä´æÆ÷µØÖ·
// mask[IN]:Çåλֵ
void ClearBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg, tmp & ~mask); // clear bit mask
}
//¹¦ ÄÜ£ºÍ¨¹ýRC522ºÍISO14443¿¨Í¨Ñ¶
//²ÎÊý˵Ã÷£ºCommand[IN]:RC522ÃüÁî×Ö
// pInData[IN]:ͨ¹ýRC522·¢Ë͵½¿¨Æ¬µÄÊý¾Ý
// InLenByte[IN]:·¢ËÍÊý¾ÝµÄ×Ö½Ú³¤¶È
// pOutData[OUT]:½ÓÊÕµ½µÄ¿¨Æ¬·µ»ØÊý¾Ý
// *pOutLenBit[OUT]:·µ»ØÊý¾ÝµÄ볤¶È
char PcdComMF522(unsigned char Command,
unsigned char *pInData,
unsigned char InLenByte,
unsigned char *pOutData,
unsigned int *pOutLenBit)
{
char status = MI_ERR;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned char lastBits;
unsigned char n;
unsigned int i;
switch (Command)
{
case PCD_AUTHENT:
irqEn = 0x12;
waitFor = 0x10;
break;
case PCD_TRANSCEIVE:
irqEn = 0x77;
waitFor = 0x30;
break;
default:
break;
}
WriteRawRC(ComIEnReg,irqEn|0x80);
ClearBitMask(ComIrqReg,0x80);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i<InLenByte; i++)
{ WriteRawRC(FIFODataReg, pInData[i]); }
WriteRawRC(CommandReg, Command);
if (Command == PCD_TRANSCEIVE)
{ SetBitMask(BitFramingReg,0x80); }
// i = 600;//¸ù¾ÝʱÖÓƵÂʵ÷Õû£¬²Ù×÷M1¿¨×î´óµÈ´ýʱ¼ä25ms
i = 2000;
do
{
n = ReadRawRC(ComIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitFor));
ClearBitMask(BitFramingReg,0x80);
if (i!=0)
{
if(!(ReadRawRC(ErrorReg)&0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{ status = MI_NOTAGERR; }
if (Command == PCD_TRANSCEIVE)
{
n = ReadRawRC(FIFOLevelReg);
lastBits = ReadRawRC(ControlReg) & 0x07;
if (lastBits)
{ *pOutLenBit = (n-1)*8 + lastBits; }
else
{ *pOutLenBit = n*8; }
if (n == 0)
{ n = 1; }
if (n > MAXRLEN)
{ n = MAXRLEN; }
for (i=0; i<n; i++)
{ pOutData[i] = ReadRawRC(FIFODataReg); }
}
}
else
{ status = MI_ERR; }
}
SetBitMask(ControlReg,0x80); // stop timer now
WriteRawRC(CommandReg,PCD_IDLE);
return status;
}
//¿ªÆôÌìÏß
//ÿ´ÎÆô¶¯»ò¹Ø±ÕÌìÏÕ·¢ÉäÖ®¼äÓ¦ÖÁÉÙÓÐ1msµÄ¼ä¸ô
void PcdAntennaOn()
{
unsigned char i;
i = ReadRawRC(TxControlReg);
if (!(i & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
//¹Ø±ÕÌìÏß
void PcdAntennaOff()
{
ClearBitMask(TxControlReg, 0x03);
}
//µÈ´ý¿¨À뿪
void WaitCardOff(void)
{
char status;
unsigned char TagType[2];
while(1)
{
status = PcdRequest(REQ_ALL, TagType);
if(status)
{
status = PcdRequest(REQ_ALL, TagType);
if(status)
{
status = PcdRequest(REQ_ALL, TagType);
if(status)
{
return;
}
}
}
delay_10ms(10);
}
}
// Delay 10ms
void delay_10ms(unsigned int _10ms)
{
unsigned int i, j;
for(i=0; i<_10ms; i++)
{
for(j=0; j<60000; j++);
}
}
///ÒÔÉÏΪRC522Çý¶¯º¯Êý/
///ÒÔÉÏΪRC522Çý¶¯º¯Êý/
///ÒÔÉÏΪRC522Çý¶¯º¯Êý/
//RFIDÄ£¿é³õʼ»¯
void RFID_Init(void)
{
PcdInit(); //RC522¶Ë¿Ú¶¨Òå
PcdReset(); //¸´Î»RC522
PcdAntennaOff(); //¹ØÌìÏß
PcdAntennaOn(); //¿ªÌìÏß
M500PcdConfigISOType('A');
}
//»ñÈ¡¿¨±àºÅº¯Êý£¬·µ»Ø¿¨±àºÅ1-3£¬·Çϵͳ¼È뿨·µ»Ø0£¬Ã»ÓÐʶ±ðµ½¿¨·µ»Ø5»ò6
uint8_t Rc522Test(void)
{
uint8_t cardno;
if (PcdRequest(REQ_ALL, Temp) == MI_OK)
{
if (PcdAnticoll(UID) == MI_OK)
{
cardno=0;
if(UID[0]==UI0[0]&&UID[1]==UI0[1]&&UID[2]==UI0[2]&&UID[3]==UI0[3])
{
cardno=1;
}
else if(UID[0]==UI1[0]&&UID[1]==UI1[1]&&UID[2]==UI1[2]&&UID[3]==UI1[3])
{
cardno=2;
}
else if(UID[0]==UI2[0]&&UID[1]==UI2[1]&&UID[2]==UI2[2]&&UID[3]==UI2[3])
{
cardno=3;
}
else if(UID[0]==UI3[0]&&UID[1]==UI3[1]&&UID[2]==UI3[2]&&UID[3]==UI3[3])
{
cardno=4;
}
else cardno = 0;
}
else cardno = 5;
}
else cardno = 6;
return cardno;
}
4.蓝牙模块和舵机模块
这两个模块比较简单就不再单独说明了 蓝牙模块完整的使用网上一大堆,蓝牙模块配合一个手机开锁app使用都会分享出来
哥们就不在这当小丑了 还是实打实的分享源码才是最重要的!!! 查了好多资料都不分享源码真的心都碎了
三、密码锁逻辑
密码锁最主要的功能就是输入密码 修改密码啦 也是保存flash掉电不丢失 然后再OLED上面与用户做交互,万事开头难 这个功能写出来了 后面就好办了 三、密码锁逻辑
//°´¼ü½âËø
KeyNum= MatrixKey_GetValue();
//»ñÈ¡¼üÖµ
if(KeyNum) //Èç¹ûÓа´¼ü°´ÏÂ
{
OLED_ShowChinese(5,0,"ÊäÈëÃÜÂ룺");
OLED_ShowChinese(0,48,"·µ»Ø");
OLED_ShowChinese(97,48,"½âËø");
OLED_Update();
if(KeyNum>=1&&KeyNum<=8) //¹æ¶¨ÃÜÂëÖ»ÄÜÊÇ1~8µÄÊý×Ö
{
Show_Num+=8; //ÏÔʾÓÒÒÆ8¸ö¸ñ×Ó£¨Êý×ÖÕ¼8¸öÏñËØ£©
Sum=Sum*10+KeyNum; //ͨ¹ýÀÛ³ËʵÏÖ±£´æÊäÈëµÄÖµ
OLED_ShowNum(Show_Num, 16,KeyNum, 1, OLED_8X16);
OLED_Update();
}
if(KeyNum==12) //s12 ½âËø¼ü
{
if(Sum==password) //Èç¹ûÃÜÂëÕýÈ·
{
DisUnLock(); //¿ªËø
Show_Num=10;
Sum=0;
Delay_ms(1500);
return 1;
}
else
{
OLED_Clear();
OLED_ShowChinese(17, 18, "ÃÜÂë´íÎó£¡");
OLED_Update();
Delay_ms(1500);
OLED_Clear();
Show_Num=10;
Sum=0;
Lock_screen();
}
}
if(KeyNum==9)
{
Lock_screen();//ËøÆÁ½çÃæ
Show_Num=10;
Sum=0;
}
}
四、展示和总结
基本上就写这么多吧,能力有限 目前这个程序还有一个小bug就是开启了密码输入模式就不能用刷卡模式 必须要在主函数注释了一个功能才能使用另一个功能 但蓝牙模式可以和其他功能共存 实在不想解决了 看你们有什么好的方法解决 下一步就是将这个项目移植的freertos上面了
用的ANSI编码 不然你们看代码会乱码
通过百度网盘分享的文件:智能门锁 - .zip
链接:https://pan.baidu.com/s/1kL-HyodTHlPDknv_kwFj6g?pwd=abcd
提取码:abcd