本人菜鸟一个仅供初学者参考,后期会越来越好
主函数程序:
/************************1号修改,可以验证密码正确**********************************************
----------------1.开发环境:Keil v4 ----------------
----------------2.使用单片机型号:STC89C52RC ----------------
----------------3.单片机使用晶振:11.0592M ----------------
----------------4.功能:实现密码解锁,和存储记录----------------
----------------5.名称:智能保险柜 ----------------
***********************************************************************/
#include <reg52.h>
#include <intrins.h>
#include <main.h>
#include <chuankou.h>
#include <delay.h>
#include <eeprom.h>
#define uchar unsigned char
#define uint unsigned int
bit I2C_WriteData(uchar ADDR,uchar DAT);
uchar I2C_ReadData(uchar ADDR);
void main()
{ uchar xdata i,z,old[25],daihao[6],b[9],b1,ok_1,k1=0,w1,w2,w3,w4,w5,w6,w7,w8,j,k2=0;//k2必须等于0,虽然我也不知道为啥init(); if(tr0==1) //调取发送数值{ tr0=0;}while(1){ I2C_WriteData(1,0x30);delay(5);I2C_WriteData(2,0x30);delay(5);//24c02的第110到115地址单元作为密码存储区I2C_WriteData(3,0x30);delay(5); //必须放到while函数里I2C_WriteData(4,0x31);delay(5);//一定要加延时,否则会接受不到I2C_WriteData(5,0x30);delay(5);I2C_WriteData(6,0x30);delay(5);//24c02的第110到115地址单元作为密码存储区I2C_WriteData(7,0x30);delay(5); //必须放到while函数里I2C_WriteData(8,0x32);delay(5);//一定要加延时,否则会接受不到I2C_WriteData(9,0x30);delay(5);I2C_WriteData(10,0x30);delay(5);//24c02的第110到115地址单元作为密码存储区I2C_WriteData(11,0x30);delay(5); //必须放到while函数里I2C_WriteData(12,0x33);delay(5);//一定要加延时,否则会接受不到I2C_WriteData(13,0x30);delay(5);I2C_WriteData(14,0x30);delay(5);//24c02的第110到115地址单元作为密码存储区I2C_WriteData(15,0x30);delay(5); //必须放到while函数里I2C_WriteData(16,0x34);delay(5);//一定要加延时,否则会接受不到I2C_WriteData(17,0x30);delay(5);I2C_WriteData(18,0x30);delay(5);//24c02的第110到115地址单元作为密码存储区I2C_WriteData(19,0x30);delay(5); //必须放到while函数里I2C_WriteData(20,0x35);delay(5);//一定要加延时,否则会接受不到I2C_WriteData(21,0x30);delay(5);I2C_WriteData(22,0x30);delay(5);//24c02的第110到115地址单元作为密码存储区I2C_WriteData(23,0x30);delay(5); //必须放到while函数里I2C_WriteData(24,0x36);delay(5);//一定要加延时,否则会接受不到if(tr0_ok==1) //必须把验正密码也包括进去,否则会容易因为进入密码验证过多而出错{tr0_ok=0;ES=0; for(i=1;i<25;i++){old[i]=I2C_ReadData(i); //将从地址1开始读取的数依次存入olddelay(5);}ES=1;if(a[1]==old[1]&&a[2]==old[2]&&a[3]==old[3]&&a[4]==old[4]) //第一组密码验证{for(i=0;i<9;i++) //跳转页面{SBUF = num10[i]; delay(5);while(!TI); TI = 0; };P1=0;k1++;}if(a[1]==old[5]&&a[2]==old[6]&&a[3]==old[7]&&a[4]==old[8]) //第一组密码验证{for(i=0;i<9;i++) //发送数据1给文本t0{SBUF = num10[i]; delay(5);while(!TI); TI = 0; };P1=0xfe;k2++;}if(a[1]==old[9]&&a[2]==old[10]&&a[3]==old[11]&&a[4]==old[12]) //第一组密码验证{for(i=0;i<9;i++) //发送数据1给文本t0{SBUF = num10[i]; delay(5);while(!TI); TI = 0; };P1=0xfb;k1++;}if(a[1]==old[13]&&a[2]==old[14]&&a[3]==old[15]&&a[4]==old[16]) //第一组密码验证{P1=0xfa;k1++;}if(a[1]==old[17]&&a[2]==old[18]&&a[3]==old[19]&&a[4]==old[20])//第一组密码验证{for(i=0;i<9;i++) //发送数据1给文本t0{SBUF = num10[i]; delay(5);while(!TI); TI = 0; };P1=0xff;k1++;}if(a[1]==old[21]&&a[2]==old[22]&&a[3]==old[23]&&a[4]==old[24]) //第一组密码验证{for(i=0;i<9;i++) //发送数据1给文本t0{SBUF = num10[i]; delay(5);while(!TI); TI = 0; };P1=0xfe;k1++;}else{P1=0xfd; }}
/*=======================第一个====================================*/w1=k1/1000%10;w2=k1/100%10;w3=k1/10%10;w4=k1%10;b[1]=w1+48;b[2]=w2+48;b[3]=w3+48;b[4]=w4+48;I2C_WriteData(27,b[1]);delay(5); //必须放到while函数里I2C_WriteData(28,b[2]);delay(5);//一定要加延时,否则会接受不到I2C_WriteData(29,b[3]);delay(5);I2C_WriteData(30,b[4]);delay(5);//24c02的第110到115地址单元作为密码存储区c[1]=I2C_ReadData(27);delay(5);c[2]=I2C_ReadData(28);delay(5);c[3]=I2C_ReadData(29);delay(5);c[4]=I2C_ReadData(30);delay(5);/*=======================第二个====================================*/w5=k2/1000%10;w6=k2/100%10;w7=k2/10%10;w8=k2%10;b[5]=w5+48;b[6]=w6+48;b[7]=w7+48;b[8]=w8+48;I2C_WriteData(31,b[5]);delay(5); //必须放到while函数里I2C_WriteData(32,b[6]);delay(5);//一定要加延时,否则会接受不到I2C_WriteData(33,b[7]);delay(5);I2C_WriteData(34,b[8]);delay(5);//24c02的第110到115地址单元作为密码存储区I2C_ReadData(31);delay(5); //必须放到while函数里I2C_ReadData(32);delay(5);//一定要加延时,否则会接受不到I2C_ReadData(33);delay(5); //必须放到while函数里I2C_ReadData(34);delay(5);//一定要加延时,否则会接受不到d[1]=I2C_ReadData(31);delay(5);d[2]=I2C_ReadData(32);delay(5);d[3]=I2C_ReadData(33);delay(5);d[4]=I2C_ReadData(34);delay(5);if(tr0==1) //调取发送数值{fasong(); tr0=0;}if(tr1==1) //调取发送数值{//fssong();tr1=0;}}
}
串口部分的程序:
串口.h程序
#ifndef __CHUANKOU_H__
#define __CHUANKOU_H__#define uchar unsigned char
#define uint unsigned intextern bit tr0_ok;
extern uchar tr0,tr1,n,a[5],b[5],EEPROM_DATA,z,i,k,c[7],d[5],z1;
extern uchar xdata num10[];
void init();
void chuankou();
void fasong();
#define AT24C02_ADDR 0xa0#endif
chuankou.c
#include <reg52.h>
#include <chuankou.h>
#include <delay.h>
#include <eeprom.h>
#define uchar unsigned char
#define uint unsigned int
bit tr0_ok;
uchar tr0,n,a[5],b[5],EEPROM_DATA,z,i,tr1,c[7],d[5],z1;
uchar I2C_ReadData(uchar ADDR);
uchar xdata num1[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x31,0x22,0xff,0xff,0xff};//
uchar xdata num2[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x32,0x22,0xff,0xff,0xff};//
uchar xdata num3[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x33,0x22,0xff,0xff,0xff};//
uchar data num4[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x34,0x22,0xff,0xff,0xff};//
uchar data num5[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x35,0x22,0xff,0xff,0xff};//
uchar data num6[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x36,0x22,0xff,0xff,0xff};//
uchar xdata num7[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x37,0x22,0xff,0xff,0xff};//
uchar xdata num8[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x38,0x22,0xff,0xff,0xff};//
uchar xdata num9[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x39,0x22,0xff,0xff,0xff};//
uchar xdata num0[]={0x74,0x30,0x2e,0x74,0x78,0x74,0x3d,0x74,0x30,0x2e,0x74,0x78,0x74,0x2b,0x22,0x30,0x22,0xff,0xff,0xff};//
uchar xdata num10[]={0x70,0x61,0x67,0x65,0x20,0x31,0xff,0xff,0xff};///*=======初始化=========*/
void init()
{/*定义占空比*/TMOD = 0x20; /*=============使用工作模式1会失败 ================*//*定义串口*/SM0 = 0;SM1 = 1; //工作模式1REN = 1;TH1 = 0xfd;TL1 = 0xfd; //比特率9600TR1 = 1; EA = 1; //开总中断ES = 1; //串口中断打开
}
/*=========串口============*/
void fasong()
{
/*========================发送第一个人==================================*/if(EEPROM_DATA==0xb1){for(z=1;z<5;z++){ES=0;switch(c[z]){case 0x31: for(i=0;i<20;i++) //发送数据1给文本t0{SBUF = num1[i]; delay(5);while(!TI); TI = 0; };k++;break; case 0x32: for(i=0;i<20;i++) //发送数据2给文本t0{SBUF = num2[i];while(!TI); TI = 0; }; k++;break;case 0x33: for(i=0;i<20;i++) //发送数据3给文本t0{SBUF = num3[i];while(!TI); TI = 0; };k++;break;case 0x34: for(i=0;i<20;i++) //发送数据4给文本t0{SBUF = num4[i];while(!TI); TI = 0; };k++;break;case 0x35: for(i=0;i<20;i++) //发送数据5给文本t0{SBUF = num5[i]; while(!TI); TI = 0;};k++;break;case 0x36: for(i=0;i<20;i++) //发送数据6给文本t0{SBUF = num6[i]; while(!TI); TI = 0; };k++;break;case 0x37: for(i=0;i<20;i++) //发送数据7给文本t0{SBUF = num7[i];while(!TI); TI = 0; }; k++;break;case 0x38: for(i=0;i<20;i++) //发送数据8给文本t0{ SBUF = num8[i]; while(!TI); TI = 0; }; k++;break;case 0x39: for(i=0;i<20;i++) //发送数据9给文本t0{SBUF = num9[i]; while(!TI); TI = 0;};k++;break;case 0x30: for(i=0;i<20;i++) //发送数据0给文本t0{SBUF = num0[i]; while(!TI); TI = 0;};k++;break; } ES=1;}EEPROM_DATA=0; }
/*========================发送第二个人==================================*/if(EEPROM_DATA==0xb2){for(z1=1;z1<5;z1++){ES=0;switch(d[z1]){case 0x31: for(i=0;i<20;i++) //发送数据1给文本t0{SBUF = num1[i]; delay(5);while(!TI); TI = 0; };k++;break; case 0x32: for(i=0;i<20;i++) //发送数据2给文本t0{SBUF = num2[i];while(!TI); TI = 0; }; k++;break;case 0x33: for(i=0;i<20;i++) //发送数据3给文本t0{SBUF = num3[i];while(!TI); TI = 0; };k++;break;case 0x34: for(i=0;i<20;i++) //发送数据4给文本t0{SBUF = num4[i];while(!TI); TI = 0; };k++;break;case 0x35: for(i=0;i<20;i++) //发送数据5给文本t0{SBUF = num5[i]; while(!TI); TI = 0;};k++;break;case 0x36: for(i=0;i<20;i++) //发送数据6给文本t0{SBUF = num6[i]; while(!TI); TI = 0; };k++;break;case 0x37: for(i=0;i<20;i++) //发送数据7给文本t0{SBUF = num7[i];while(!TI); TI = 0; }; k++;break;case 0x38: for(i=0;i<20;i++) //发送数据8给文本t0{ SBUF = num8[i]; while(!TI); TI = 0; }; k++;break;case 0x39: for(i=0;i<20;i++) //发送数据9给文本t0{SBUF = num9[i]; while(!TI); TI = 0;};k++;break;case 0x30: for(i=0;i<20;i++) //发送数据0给文本t0{SBUF = num0[i]; while(!TI); TI = 0;};k++;break; } ES=1;}EEPROM_DATA=0; }} void UART() interrupt 4
{if(RI){ RI = 0;EEPROM_DATA = SBUF;if(EEPROM_DATA==0xb1) //接受0xbb调取储存数值{tr0=1;}if(EEPROM_DATA==0xb2) //接受0xbb调取储存数值{tr0=1;}if(EEPROM_DATA==0xaa) //接受0xaa开始储存数值{tr1=1; }if(tr1==1) //开始储存数值到a[n];{ a[n]=EEPROM_DATA;n++; if(n==5) //4个一组{n=0;tr1=0;tr0_ok=1;} }}
}
eeprom.h:
#ifndef __MAIN_H__
#define __MAIN_H__#define uchar unsigned char
#define uint unsigned int
sbit SDA = P2^0;
sbit SCL = P2^1;void I2C_init();
void I2C_Start();
void I2C_Stop();
void Master_ACK(bit i);
extern bit Test_ACK();
void I2C_send_byte(uchar byte);
uchar I2C_read_byte();#endif
eeprom.c
#include <reg52.h>
#include <eeprom.h>
#include <intrins.h>
#include <delay.h>
#define uchar unsigned char
#define uint unsigned int
#define AT24C02_ADDR 0xa0
void I2C_init() //I2C3
{SDA = 1;_nop_();SCL = 1;_nop_();
}void I2C_Start() //I2C?eê?D?o?
{SCL = 1;_nop_();SDA = 1;delay_5us();SDA = 0;delay_5us();
}void I2C_Stop()
{SDA = 0;_nop_();SCL = 1;delay_5us();SDA = 1;delay_5us();
}void Master_ACK(bit i) // ?÷?ú·¢?íó|′e
{SCL = 0; // à-μíê±?ó×ü???êDíSDAêy?Y×ü??é?μ?êy?Y±??ˉ_nop_(); // è?×ü???è?¨if (i) //è?1?i = 1 ???′à-μíêy?Y×ü?? ±íê??÷?úó|′e{SDA = 0;}else {SDA = 1; //·¢?í·?ó|′e}_nop_();//è?×ü???è?¨SCL = 1;//à-??ê±?ó×ü?? è?′ó?ú′óSDA??é??á×? ?÷?úμ?ó|′eD?o?_nop_();SCL = 0;//à-μíê±?ó×ü??£? ??ó?×ü???ìD?í¨D?_nop_();SDA = 1;//êí·?SDAêy?Y×ü???£_nop_();
}bit Test_ACK() // ?ì2a′ó?úó|′e
{SCL = 1;//ê±?ó×ü???a??μ????ú???éò??áè?′ó?úó|′eD?o?delay_5us();if (SDA){SCL = 0;I2C_Stop();return(0);}else{SCL = 0;return(1);}
}void I2C_send_byte(uchar byte) //·¢?íò???×??ú
{uchar i;for(i = 0 ; i < 8 ; i++){SCL = 0;_nop_();if (byte & 0x80) //{SDA = 1;_nop_();}else{SDA = 0;_nop_();}SCL = 1;_nop_();byte <<= 1;}SCL = 0;_nop_();SDA = 1;_nop_();
}uchar I2C_read_byte()
{uchar i, dat;SCL = 0 ;_nop_();SDA = 1;_nop_();for(i = 0 ; i < 8 ; i++){SCL = 1;_nop_();dat <<= 1; if (SDA){dat |= 0x01; }_nop_();SCL = 0;_nop_();}return(dat);
}bit I2C_WriteData(uchar ADDR,uchar DAT) //I2CD′êy?Y
{I2C_Start();I2C_send_byte(AT24C02_ADDR+0);if (!Test_ACK()){return(0); }I2C_send_byte(ADDR);if (!Test_ACK()){return(0); }I2C_send_byte(DAT);if (!Test_ACK()){return(0); }I2C_Stop();return(1);
}uchar I2C_ReadData(uchar ADDR) //I2C?áêy?Y
{uchar dat;I2C_Start();I2C_send_byte(AT24C02_ADDR+0);if (!Test_ACK()){return(0); }I2C_send_byte(ADDR);if (!Test_ACK()){return(0); }Master_ACK(0);I2C_Start();I2C_send_byte(AT24C02_ADDR+1);if (!Test_ACK()){return(0); }dat = I2C_read_byte();Master_ACK(0);I2C_Stop();return(dat);
}
delay.h
#ifndef __DELAY_H__
#define __DELAY_H__
#define uchar unsigned char
#define uint unsigned int
void delay(uint z);
void delay_5us() ;#endif
dekay.c
#include <reg52.h>
#include <delay.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
void delay(uint z)
{uint x,y;for(x = z; x > 0; x--)for(y = 114; y > 0 ; y--);
}
void delay_5us() //5us
{_nop_();
}
main.h
#include <reg52.h>
#include <delay.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
void delay(uint z)
{uint x,y;for(x = z; x > 0; x--)for(y = 114; y > 0 ; y--);
}
void delay_5us() //5us
{_nop_();
}