《基于单片机的保险箱c语言课程设计报告书.doc》由会员分享,可在线阅读,更多相关《基于单片机的保险箱c语言课程设计报告书.doc(31页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、#include #include #include vir_buzzer_cfg.h #includevir_buzzer.h #includevir_key_cfg.h #includevir_key.h #includevir_lock_cfg.h #includevir_lock.h #includevir_memory_cfg.h #includevir_memory.h #includevir_show.h #includebuzzer.h #include delay_cfg.h #include delay.h #include lock.h /* #include i2c.h
2、 */ #includedelay_cfg.h #includedelay.h#includeled_display_cfg.h#includeled_display.h#includeI2C_cfg.h static void _zyI2cBitSend(void)if(ucData&0x80)_ZY_I2C_SDA=1;else_ZY_I2C_SDA=0;_ZY_I2C_DELAY();_ZY_I2C_SCL=1;_ZY_I2C_DELAY();_ZY_I2C_SCL=0;unsigned char _zyI2cBitReceive(void)unsigned char ucRt;_ZY_
3、I2C_DELAY();_ZY_I2C_SCL=1;_ZY_I2C_DELAY();ucRt=_ZY_I2C_SDA;_ZY_I2C_SCL=0;return ucRt;static void _zyI2cStartSend(void)_ZY_I2C_DELAY();_ZY_I2C_SDA=0;_ZY_I2C_DELAY();_ZY_I2C_SCL=0;static void _zyI2cStopSend(void)_ZY_I2C_SDA=0;_ZY_I2C_DELAY();_ZY_I2C_SCL=1;_ZY_I2C_DELAY();_ZY_I2C_SDA=1; static void _zy
4、I2cRestartSend(void)_ZY_I2C_SDA=1;_ZY_I2C_DELAY();_ZY_I2C_SCL=1;_ZY_I2C_DELAY();_ZY_I2C_SDA=0;_ZY_I2C_DELAY();_ZY_I2C_SCL=0;static void _zyI2cAckSend(void)_ZY_I2C_SDA=0;_ZY_I2C_DELAY();_ZY_I2C_SCL=1;_ZY_I2C_DELAY();_ZY_I2C_SCL=0;static void _zyI2cNakSend(void)_ZY_I2C_SDA=1;_ZY_I2C_DELAY();_ZY_I2C_SC
5、L=1;_ZY_I2C_DELAY();_ZY_I2C_SCL=0;static char _zyI2cIsAck(void)_ZY_I2C_SDA=1;_ZY_I2C_DELAY();_ZY_I2C_SCL=1;_ZY_I2C_DELAY();if(_ZY_I2C_SDA=1)_ZY_I2C_SCL=0;return 0;_ZY_I2C_SCL=0;return 1;static void_zyI2cByteSend(unsigned char ucData)unsigned char i;i=8;do_zyI2cBitSend(ucData);ucData=ucData1;while(-i
6、!=0);static unsigned char _zyI2cBitReceive(void)unsigned char ucRt;unsigned char i;ucRt=0;_ZY_I2C_SDA=1;i=8; do ucRt=(ucRt=6)ucIndex=0;char zyI2cInit(void)_ZY_I2C_SCL=0;_zyI2cStopSend();return 0;unsigned char zyI2cWrite(unsigned char ucAddr, unsigned int uiRegAddr,unsigned char ucRegAddrLen,unsigned
7、 char *pucData,unsigned char ucDataLen)unsigned char i;if(ucDataLen=0|pucData=0)return 0;i=ucDataLen;_zyI2cStartSend();_zyI2cByteSend(ucAddr&0xfe);if(_zyI2IsAck()while(ucRegAddrLen0)_zyI2cByteSend(uiRegAddr);if(!_zyI2IsAck()break;ucRegAddrLen-;uiRegAddr=uiRegAddr8;if(ucRegAddrLen=0)do_zyI2cByteSend(
8、*pucData+);if(!_zyI2IsAck)break;while(-i!=0);_zyI2cStopSend();return(ucDataLen-i);unsigned char zyI2cRead(unsigned char ucAddr, unsigned int uiRegAddr, unsigned char ucRegAddrLen, unsigned char *pucData, unsigned char ucDataLen)unsigned char i;if (ucDataLen=0|pucData=0)return 0;_zyI2cStartSend();_zy
9、I2cByteSend(ucAddr&0xfe);if(_zyI2cIsAck()while(ucRegAddrLen0)_zyI2cByteSend(uiRegAddr);if(!_zyI2cIsAck()break;ucRegAddrLen-;uiRegAddr=uiRegAddr8;if(ucRegAddrLen=0)_zyIc2RestartSend();_zyI2cByteSend(ucAddr|0x01);if(_zyI2cIsAck()i=ucDataLen-1;while(i-1=0)*pucData+=_zyI2cByteReceive();_zyI2cAckSend();*
10、pucData=_zyI2cByteReceive();_zyI2cNakSend();elseucDataLen=0;_zyI2cStopSend();return ucDataLen; char zyLedDisplayInit(void)return 0;void zyLedDisplayScan(void)static unsigned char ucIndex=0;/定义扫描位置索引静态变量/* * 关闭显示*/_ZY_LED_DIG1_OFF();/关闭数码管_ZY_LED_DIG2_OFF();_ZY_LED_DIG3_OFF();_ZY_LED_DIG4_OFF();_ZY_L
11、ED_DIG5_OFF();_ZY_LED_DIG6_OFF();/* *显示ucIndex位:将待显示数字字符的数值送到段选位控制I/O口*/_ZY_LED_SEG(GucZyLedDisplayShowBufucIndex);switch (ucIndex)case 0:_ZY_LED_DIG1_ON();/点亮数码管 1break;case 1:_ZY_LED_DIG2_ON();/点亮数码管 2break;case 2:_ZY_LED_DIG3_ON();/点亮数码管 3break;case 3:_ZY_LED_DIG4_ON();/点亮数码管 4break;case 4:_ZY_LE
12、D_DIG5_ON();/点亮数码管 5break;case 5:_ZY_LED_DIG6_ON();/点亮数码管 6break;default:break;/* * 准备显示下一位*/ucIndex+;if (ucIndex=6)ucIndex=0;/*延时驱动实现代码*/ /* * 全局变量定义 */ static unsigned int _GuiSum; /32位定时器高16位 static volatile unsigned char _GucTimerFlg; /32位定时器溢出标志 /* * Description: 延时初始化 */ char delayInit(void) T
13、MOD=(TMOD & 0xf0)|0x01; ET0=1; return 0; /* * Description: 毫秒延时开始 */ char delayMsStart(unsigned int uiDly); unsigned long ulTmp1; unsigned int uiTmp2; /* * 获得定时器初始值 */ ulTmp1 =DELAY_CYCLES(uiDly); /计算需要的机器周期数 ulTmp1 = -ulTmp1; /加计数,所以取负 /* * 设置定时器初始值 */ uiTmp2 =ulTmp1 % 0x10000; /定时器低16位 TL0 = uiTmp
14、2 % 256; TH0 = uiTmp2 / 256; _GuiSum =ulTmp1 / 0x10000; /定时器高16位 _GueTimerFlg=0; /清处溢出标志 TR0=1; /启动定时器 return 0; /* * Description: 检查延时是否结束 */ char delayMslsEnd(void); return _GueTimerFlg; /* * Description: 毫秒延时 */ char delayMs(unsigned int uiDly) /* * 开始延时 */ if (delayMsStart(uiDly)=_ZY_VIR_MEMORY_
15、SIZE) return 0; /*虚拟键盘驱动实现代码*/ char zyKeyGet(void) char ucRt; /返回键值unsigned char ucKeySum; /按键数目计数ucRt =-1;ucKeySum =0; /* * 将所有“列线”设置为输入状态 */_ZY_KEY_KC1_PIN=1;_ZY_KEY_KC2_PIN=1;_ZY_KEY_KC3_PIN=1;/* * 将所有“列线”设置为高电平 */ _ZY_KEY_KR1_PIN=1;_ZY_KEY_KR2_PIN=1;_ZY_KEY_KR3_PIN=1;_ZY_KEY_KR4_PIN=1;/* *扫描第一行*
16、/_ZY_KEY_KR1_PIN=0;/将KR1清0if(_ZY_KEY_KC1_PIN=0)/如果kc1为0,说明k1键已经按下ucRt=0;/按键为0ucKeySum+;if(_ZY_KEY_KC2_PIN=0)/如果kc2为0,则说明k5按键已经按下ucRt=1;/键值为1ucKeySum+;if(_ZY_KEY_KC3_PIN=0)/如果kc3为0,则说明k9按键已经按下ucRt=2;/键值为2ucKeySum+;_ZY_KEY_KR1_PIN=1;/第一行扫描完毕,将kr1恢复为1/* * 扫描第二行*/_ZY_KEY_KR2_PIN=0;if(_ZY_KEY_KC1_PIN=0)u
17、cRt=3;ucKeySum+;if(_ZY_KEY_KC2_PIN=0)ucRt=4;ucKeySum+;if(_ZY_KEY_KC3_PIN=0)ucRt=5;ucKeySum+;_ZY_KEY_KR2_PIN=1;/* * 扫描第三行*/_ZY_KEY_KR3_PIN=1;if(_ZY_KEY_KC1_PIN=0)ucRt=6;ucKeySum+;if(_ZY_KEY_KC2_PIN=0)ucRt=7;ucKeySum+;if(_ZY_KEY_KC3_PIN=0)ucRt=8;ucKeySum+;_ZY_KEY_KR3_PIN=1;/* * 扫描第四行*/_ZY_KEY_KR4_PIN=
18、1;if(_ZY_KEY_KC1_PIN=0)ucRt=9;ucKeySum+;if(_ZY_KEY_KC2_PIN=0)ucRt=10;ucKeySum+;if(_ZY_KEY_KC3_PIN=0)ucRt=11;ucKeySum+;_ZY_KEY_KR4_PIN=1;/* * 超过一个按键,返回-1*/if(ucKeySum!=1)return-1;return ucRt; /返回键值 static code char_GcKeyTable= /按键转换表 #,0,*,9,8,7,6,5,4,3,2,1,0 ; /* * Description: 获得按键的ASC码,没有按键一直等待 *
19、Returned value: 键的ASC码 */ static char _virKeyGet1(void) /返回值:键的ASC码 char cTmp1,cTmp2; /* *等待按键释放 */ while (1) while (zyKeyGet()=0) /无键闭合,退出 delayMs(10);/4.12 /* *去抖 */ delayMs(10); if (zyKeyGet()=0) /有按键闭合,退出 break; delayMs(10); /* *去抖 */ delayMs(10); cTmp2=zyKeyGet(); /两次按键状态一样,去抖成功 if (cTmp2=cTmp1
20、) break; cTmp1=_GcKeyTablecTmp1; /键码转换成ASC码 return cTmp1; /* * Description: 在指定时间获得按键的ASC码 */ static char _virKeyGet2(unsigned int uiDly) /uiDly:以毫秒为单位,设定最大等待时间,0为无穷等待 char cTmp1,cTmp2; unsigned char I;/4.13 delayMsStart(uiDly); /* *等待按键释放 */ while (1) while (zyKeyGet()=0) /无键闭合,退出 zyLedDisplayScan(
21、); /代替延时 if(delayMslsEnd() /超时则返回 return -1; /* *去抖 */ for(i=0;i50;i+) zyLedDisplayScan(); /代替延时 if (delayMslsEnd() /超时则返回 return -1; if (zyKeyGet()=0) /有按键闭合,退出 break; zyLedDisplayScan(); /代替延时 if (delayMslsEnd() /超时则返回 return -1; /* *去抖 */ for(i=0;i50;i+) zyLedDisplayScan(); /代替延时 if (delayMslsEnd() /超时则返回 return -1; cTmp2=zyKeyGet(); /两次按键状态一样,去抖成功 if (cTmp2=cTmp1) break; cTmp1=_GcKeyTablecTmp1; /键码转换成ASC码 return cTmp1; /4.14 /* * Description: 虚拟键盘模块初始化 */ char virKeyInit(void)
限制150内