用Protues7.8仿真测试,使用KEIL4,语言是KEIL4,制作了12864的驱动程序,希望能帮到初学者,程序都有一定注释。建议零基础的师兄下载来用。
单片机源程序如下:
- #include "reg52.h"
- #include "string.h"
- #include "stdio.h"
- sbit RS = P1^7; //控制端口
- sbit RW = P1^6;
- sbit E = P1^5;
- sbit PSB = P3^2;
- sbit PAUSE = P0^3;
- sbit RES = P0^2;
- #define DataPort P2 //单片机 P2<------> 液晶DB0-DB7
- sbit BEEP=P1^0 ;//蜂鸣器IO口定义
- sbit KEY1=P1^3; //撤销按键IO口定义
- sbit LED=P1^1;//继电器控制IO口定义
- #define SET_BEEP BEEP = 0
- #define CLR_BEEP BEEP = 1
- signed long dat=0;
- unsigned char iii=0;
- char string[]=" ";
- //M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
- //4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
- unsigned char code data2[4] = {0x12,0,0,0};
- unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- unsigned char idata MLastSelectedSnr[4];
- unsigned char idata RevBuffer[30];
- //unsigned char data SerBuffer[20];
- bit CmdValid;
- unsigned char code ASCII_NUM[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- unsigned char code Yankey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //原始密码
- unsigned char code XiugaiYunKey[16]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0x07,0x80,0x69,0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //修改后的密码
- unsigned char code Nowkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}; //xianzia 密码
- unsigned char code XiugaiNowKey[16]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x80,0x69,0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- unsigned char idata Read_Data[16]={0x00};
- unsigned char data OperationCard;
- unsigned char data bWarn,bPass,ttt=0,tt=0;
- #define REGCARD 0xa1
- #define CONSUME 0xa2
- #define READCARD 0xa3
- #define ADDMONEY 0xa4
- #define CHEXIAO 0xa5
- /*------------------------------------------------
- uS延时函数,含有输入参数 unsigned char t,无返回值
- unsigned char 是定义无符号字符变量,其值的范围是
- 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
- 长度如下 T=tx2+5 uS
- ------------------------------------------------*/
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- /*------------------------------------------------
- mS延时函数,含有输入参数 unsigned char t,无返回值
- unsigned char 是定义无符号字符变量,其值的范围是
- 0~255 这里使用晶振12M,精确延时请使用汇编
- ------------------------------------------------*/
- void DelayMs(unsigned char t)
- {
-
- while(t--)
- {
- //大致延时1mS
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- void STC_send_bluetooth(unsigned char a) //注意:若单片机TXD(P3.1)无上拉能力,必须在P3.1端接上拉电阻。本次测试需要接上拉电阻
- {
- TI=0;
- SBUF=a;
- while(TI==0);
- TI=0;}
- void KeyScan(void) //独立按键扫描
- {
- char string[]=" ";
- if(KEY1 == 0)//撤销按键
- {
- delay_10ms(1);
- if(KEY1 == 0 )
- {
- OperationCard=CHEXIAO;
- tt=5;}
- }
- if(REGCARDBTN == 0)//注册按键
- {
- delay_10ms(1);
- if(REGCARDBTN == 0 )
- OperationCard = REGCARD;
- tt=1;
- }
- if(READCARDBTN == 0)//读卡信息
- {
- delay_10ms(1);
- if(READCARDBTN == 0 )
- OperationCard = READCARD; tt=4;
- }
- }
- void Warn(void) //蜂鸣器报警 长
- {
- uchar data ii;
- for(ii=0;ii<3;ii++)
- {
- SET_BEEP;//ShortBeep();如果是有源蜂鸣器就一句SET_BEEP就ok了
- delay_10ms(5);
- CLR_BEEP;
- delay_10ms(5);
- }
- }
- void WaitCardOff(void)
- {
- char status;
- unsigned char TagType[2];
- while(1)
- {
- status = PcdRequest(PICC_REQALL , TagType);
- if(status)
- {
- status = PcdRequest(PICC_REQALL , TagType);
- if(status)
- {
- status = PcdRequest(PICC_REQALL , TagType);
- if(status)
- {
- return;
- }
- }
- }
- //delay_10ms(5);
- }
- }
- void Pass(void)//蜂鸣器报警 短
- {
- SET_BEEP;//LongBeep();如果是有源蜂鸣器就一句SET_BEEP就ok了
- delay_10ms(30);
- CLR_BEEP;
- }
- void CtrlProcess(void)//射频读卡程序
- {
-
- unsigned char idata Write_Consume_Data[16];
-
- signed long money_value;
- unsigned char ii;
- char status;
- status=PcdRequest(PICC_REQIDL,&RevBuffer[0]);//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
-
- if(status!=MI_OK)
- return;
-
- for(ii=0;ii<2;ii++)
- {
- string[2*ii] = ASCII_NUM[(RevBuffer[ii]>>4)&0x0f];
- string[2*ii+1] = ASCII_NUM[RevBuffer[ii]&0x0f];
-
- }
- string[4] = '\0';
- LCD_PutString(0,2,"类型: 学校卡 ");//液晶显示卡类型
- LCD_PutString(0,3,"卡号: ");
- LCD_PutString(0,1," ");
- status=PcdAnticoll(&RevBuffer[2]);//防冲撞,返回卡的序列号 4字节
-
- if(status!=MI_OK)
- return;
- memcpy(MLastSelectedSnr,&RevBuffer[2],4);//4字节序列号 放到MLastSelectedSnr
- for(ii=0;ii<4;ii++)
- {
- string[2*ii] = ASCII_NUM[(MLastSelectedSnr[ii]>>4)&0x0f];
- string[2*ii+1] = ASCII_NUM[MLastSelectedSnr[ii]&0x0f];
- }
- string[9] = '\0';
- LCD_PutString(3,3,string);// LCD显示序列号
-
- status=PcdSelect(MLastSelectedSnr);//选卡
- if(status!=MI_OK)
- return;
-
- if(OperationCard==REGCARD)//注册程序
- {
- while(1)
- {
- status=PcdAuthState(PICC_AUTHENT1A,7,Yankey,MLastSelectedSnr);//
- //DelayNS(1);
- if(status!=MI_OK)//已经注册显示
- { LCD_PutString(0,4," 已注册:"); Warn(); WaitCardOff(); break; }
- status=PcdWrite(7,XiugaiYunKey);
-
- if(status!=MI_OK)
- {
- // bWarn=1;
- break;
- }
- for(ii=0;ii<16;ii++)
- {
- Write_Consume_Data[ii]=0xaa;//Write_First_Data
- }
- memset( Write_Consume_Data, 0, sizeof(Write_Consume_Data) );//Write_First_Data Write_First_Data
- money_value = 0000;
- memcpy( Write_Consume_Data, ( uchar * )&money_value, 4 ); //Write_First_Data
-
- status=PcdWrite(4,&Write_Consume_Data[0]);//Write_First_Data
- if(status!=MI_OK)
- {
- bWarn=1;
- break;
- }
- LCD_PutString(0,4,"注册: 成功");
- //LCD_PutString(0,4," ");
- Pass();
- PcdHalt();
- }
- }
- if(OperationCard==CHEXIAO)//撤销程序
- {
- while(1)
- {
- status = PcdAuthState(PICC_AUTHENT1A,7,Nowkey,MLastSelectedSnr);
- //DelayNS(1);
- if(status!=MI_OK)
- { LCD_PutString(0,4," 已撤销:"); Warn(); WaitCardOff(); break; }
- status=PcdWrite(7,XiugaiNowKey);
-
- if(status!=MI_OK)
- {
- // bWarn=1;
- break;
- }
- for(ii=0;ii<16;ii++)
- {
- Write_Consume_Data[ii]=0xaa;//Write_First_Data
- }
- memset( Write_Consume_Data, 0, sizeof(Write_Consume_Data) );//Write_First_Data Write_First_Data
- money_value = 0000;
- memcpy( Write_Consume_Data, ( uchar * )&money_value, 4 ); //Write_First_Data
-
- status=PcdWrite(4,&Write_Consume_Data[0]);//Write_First_Data
- if(status!=MI_OK)
- {
- bWarn=1;
- break;
- }
- LCD_PutString(0,4,"注销: 成功");
- Pass();
- PcdHalt();
- }
- }
-
- else if(OperationCard==READCARD)//读卡
- {
- while(1)
- {
- status=PcdAuthState(PICC_AUTHENT1A,7,Nowkey,MLastSelectedSnr);//????KUAI4?
-
- if(status!=MI_OK) //无效卡显示
- { LCD_PutString(0,4," 无效卡: ");iii=2; Warn(); WaitCardOff(); break; }
-
- status=PcdRead(4,Read_Data);
- LED=0;
- if(status!=MI_OK)
- {
- //bWarn=1;
- break;
- }
- memcpy( (uchar *)&money_value, Read_Data, 4 );
- PcdHalt();
- }
-
- if(iii==1){ Warn(); }if(iii==0) Pass();iii=0;
- delay_10ms(10);
- }
-
- WaitCardOff();
- PcdHalt();//中止
- delay_10ms(100);
- LED=1;
- }
- void iccardcode()
- {
- unsigned char cmd;
- unsigned char status;
-
- cmd = RevBuffer[0];
- switch(cmd)
- {
- case 1: // Halt the card //终止卡的操作
- status= PcdHalt();;
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 2: // Request,Anticoll,Select,return CardType(2 bytes)+CardSerialNo(4 bytes)
- // 寻卡,防冲突,选择卡 返回卡类型(2 bytes)+ 卡系列号(4 bytes)
- status= PcdRequest(RevBuffer[1],&RevBuffer[2]);
- if(status!=0)
- {
- status= PcdRequest(RevBuffer[1],&RevBuffer[2]);
- if(status!=0)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- }
- }
- RevBuffer[0]=3;
- RevBuffer[1]=status;
- break;
-
- case 3: // 防冲突 读卡的系列号 MLastSelectedSnr
- status = PcdAnticoll(&RevBuffer[2]);
- if(status!=0)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- }
- memcpy(MLastSelectedSnr,&RevBuffer[2],4);
- RevBuffer[0]=5;
- RevBuffer[1]=status;
- break;
- case 4: // 选择卡 Select Card
- status=PcdSelect(MLastSelectedSnr);
- if(status!=MI_OK)
- {
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- }
- RevBuffer[0]=3;
- RevBuffer[1]=status;
- break;
- case 5: // Key loading into the MF RC500's EEPROM
- status = PcdAuthState(RevBuffer[1], RevBuffer[3], DefaultKey, MLastSelectedSnr);// 校验卡密码
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 6:
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 7:
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 8: // Read the mifare card
- // 读卡
- status=PcdRead(RevBuffer[1],&RevBuffer[2]);
- if(status==0)
- {RevBuffer[0]=17;}
- else
- {RevBuffer[0]=1;}
- RevBuffer[1]=status;
- break;
- case 9: // Write the mifare card
- // 写卡 下载密码
- status=PcdWrite(RevBuffer[1],&RevBuffer[2]);
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 10:
- PcdValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3]);
- RevBuffer[0]=1;
- RevBuffer[1]=status;
- break;
- case 12: // 参数设置
- PcdBakValue(RevBuffer[1], RevBuffer[2]);
- RevBuffer[0]=1; //contact
- RevBuffer[1]=0;
- break;
- }
- }
- /*------------------------------------------------
- 检测忙位
- ------------------------------------------------*/
- void Check_Busy()
- {
- RS=0;
- RW=1;
- E=1;
- DataPort=0xff;
- while((DataPort&0x80)==0x80);//忙则等待
- E=0;
- }
- /*------------------------------------------------
- 写命令
- ------------------------------------------------*/
- void Write_Cmd(unsigned char Cmd)
- {
- Check_Busy();
- RS=0;
- RW=0;
- E=1;
- DataPort=Cmd;
- DelayUs2x(5);
- E=0;
- DelayUs2x(5);
- }
- /*------------------------------------------------
- 写数据
- ------------------------------------------------*/
- void Write_Data(unsigned char Data)
- {
- Check_Busy();
- RS=1;
- RW=0;
- E=1;
- DataPort=Data;
- DelayUs2x(5);
- E=0;
- DelayUs2x(5);
- }
- /*------------------------------------------------
- 液晶屏初始化
- ------------------------------------------------*/
- void Init_ST7920()
- {
- DelayMs(40); //大于40MS的延时程序
- PSB=1; //设置为8BIT并口工作模式
- DelayMs(1); //延时
- RES=0; //复位
- DelayMs(1); //延时
- RES=1; //复位置高
- DelayMs(10);
- Write_Cmd(0x30); //选择基本指令集
- DelayUs2x(50); //延时大于100us
- Write_Cmd(0x30); //选择8bit数据流
- DelayUs2x(20); //延时大于37us
- Write_Cmd(0x0c); //开显示(无游标、不反白)
- DelayUs2x(50); //延时大于100us
- Write_Cmd(0x01); //清除显示,并且设定地址指针为00H
- DelayMs(15); //延时大于10ms
- Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动
- DelayUs2x(50); //延时大于100us
- }
- /*------------------------------------------------
- 显示字符串
- x:横坐标值,范围0~8
- y:纵坐标值,范围1~4
- ------------------------------------------------*/
- void LCD_PutString(unsigned char x,unsigned char y,unsigned char *s)
- {
- switch(y)
- {
- case 1: Write_Cmd(0x80+x);break;
- case 2: Write_Cmd(0x90+x);break;
- case 3: Write_Cmd(0x88+x);break;
- case 4: Write_Cmd(0x98+x);break;
- default:break;
- }
- while(*s>0)
- {
- Write_Data(*s);
- s++;
- DelayUs2x(50);
- }
- }
- /*------------------------------------------------
- 清屏
- ------------------------------------------------*/
- void ClrScreen()
- {
- Write_Cmd(0x01);
- DelayMs(15);
- }
- /////////////////////////////////////////////////////////////////////
- //系统初始化
- /////////////////////////////////////////////////////////////////////
- void InitializeSystem()//射频初始化
- {
- CmdValid=0;
- LED_GREEN = 1;
- PcdReset();
- PcdAntennaOff();
- PcdAntennaOn();
- M500PcdConfigISOType( 'A' );
- LED_GREEN = 0;
- delay_10ms(10);
- LED_GREEN = 1;
- delay_10ms(10);
- LED_GREEN = 0;
- }
- /////////////////////////////////////////////////////////////////////
- //用T2做延时子程序
- /////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////
- // 接收和发送中断
- ///////////////////////////////////////////////////////////////////////
- void isr_UART(void) interrupt 4 using 1
- {
- unsigned char len, i;
- unsigned int j=0;
-
- if(RI)
- {
- len=SBUF;
- RI=0;
- for(i=0;i<len;i++)
- {
- while(!RI)
- {
- j++;
- if(j>1000)
- {
- break;
- }
- }
- if(j<1000)
- {
- RevBuffer[i]=SBUF;
- RI=0;
- j=0;
- }
- else
- {
- break;
- }
- }
- if(i==len)
- {
- REN=0;
- CmdValid=1;
- }
- }
- else if(!RI && TI)
- {
- TI=0;
- len=RevBuffer[0];
- for(i=0;i<len+1;i++)
- {
- SBUF=RevBuffer[i];
- while(!TI);
- TI=0;
- }
- REN=1;
- }
- }
- void UartProcess(void)
- {
- unsigned char baud;
- if(RevBuffer[0]==11) //为了加快相应速度 测试风鸣器
- {
- RevBuffer[2]=RevBuffer[1];
- RevBuffer[0]=1; //contact
- RevBuffer[1]=0;
- CALL_isr_UART();
- LED_GREEN = 1;
- delay_10ms(RevBuffer[2]);
- LED_GREEN = 0;
- }
- else if(RevBuffer[0]==13) //设置通讯波特率
- {
- switch(RevBuffer[1])
- {
- case 0:
- baud=BAUD_9600;
- break;
-
- }
- RevBuffer[0]=1; //contact
- RevBuffer[1]=0;
- CALL_isr_UART();
- delay_10ms(5);
- TR1 = 0;
- TH1 = baud;
- TL1 = TH1;
- delay_10ms(2);
- TR1 = TRUE;
- }
- else
- {
- iccardcode();
- CALL_isr_UART();
- }
- }
- void InitializeSystem1()
- {
- P0 = 0xFF; P1 = 0xFF; P3 = 0xFF;P2 = 0xFF;
- TMOD = 0x21;
- TH1 = 0xFd; //11.0592M晶振,9600波特率
- TL1 = 0xFd;
- TH0 = 0xfe; //11.0592M晶振,9600波特率
- TL0 = 0x33;
-
- ET0 = 1;
- TR0 = 1;
- SM0 = 0;
- SM1 = 1;
- REN = 1;//允许串口接收
- TR1 = 1;
- ES = 1;//开串口 中断
- EA = 1;
- }
- void timer0() interrupt 1 /* T0中断服务程序 */
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
全部资料51hei下载地址:
32.12864显示内容.zip
(250.12 KB, 下载次数: 40)
|