这套模拟题最难的应该就是密码的输入了,用了笨方法,用swtich一个一个的写出来的
timer.c
#include
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=ET0=1;
}
timer.h
#ifndef _TIMER_H_
#define _TIMER_H_
#include
void Timer0Init(void);
#endif
smg.c
#include
unsigned char code smgduan[16]={
0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,
0XF8,0X80,0X90,0XBF,0XFF,0XC6,0X86,0XC8,0X8C};
//10:- 11:灭 12:C 13:E 14:N 15:P
unsigned char dspbuf[8];
void smg_display(void)
{
static unsigned char i=0;
P2=(P2&0X1F)|0XE0;
P0=0XFF;
P2&=0X1F;
P2=(P2&0X1F)|0XC0;
P0=1<<i;
P2&=0X1F;
P2=(P2&0X1F)|0XE0;
P0=smgduan[dspbuf[i]];
P2&=0X1F;
if(++i==8)
i=0;
}
keybord.c
#include
unsigned char i,key_value,key_press,key_sum=0;
void key_read(void)
{
P3=0XF0;
P44=P42=1;
if(P44==0||P42==0||P35==0||P34==0)
{
for(i=0;i<4;i++)
{
P3=0XFF;
P44=P42=1;
P3&=~(0X01<<i);
if(P44==0) key_value=4*i;
else if(P42==0) key_value=4*i+1;
else if(P35==0) key_value=4*i+2;
else if(P34==0) key_value=4*i+3;
}
if(++key_sum==1)
key_press=1;
}
else
key_sum=0;
}
keybord.h
#ifndef _KEYBORD_H_
#define _KEYBORD_H_
#include
extern unsigned char key_value,key_press;
void key_read(void);
#endif
iic.c
#include
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
//等待应答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
/**********eeprom**********/
void eeprom_write(unsigned char addr,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsigned char eeprom_read(unsigned char addr)
{
unsigned char dat=0;
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0XA1);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_Stop();
return dat;
}
iic.h
#ifndef _IIC_H_
#define _IIC_H_
#include
void eeprom_write(unsigned char addr,unsigned char dat);
unsigned char eeprom_read(unsigned char addr);
#endif
other.c
#include
void bee_off(void)
{
P2=(P2&0X1F)|0XA0;
P0=0X00;
P2&=0X1F;
}
void relay_on(void)
{
P2=(P2&0X1F)|0XA0;
P0=0X10;
P2&=0X1F;
}
void relay_off(void)
{
P2=(P2&0X1F)|0XA0;
P0=0X00;
P2&=0X1F;
}
void led1_on(void)
{
P2=(P2&0X1F)|0X80;
P0=0XFE;
P2&=0X1F;
}
void led7_on(void)
{
P2=(P2&0X1F)|0X80;
P0=0XBF;
P2&=0X1F;
}
void led8_on(void)
{
P2=(P2&0X1F)|0X80;
P0=0X7F;
P2&=0X1F;
}
void led_off(void)
{
P2=(P2&0X1F)|0X80;
P0=0XFF;
P2&=0X1F;
}
other.h
#ifndef _OTHER_H_
#define _OTHER_H_
#include
void bee_off(void);
void relay_on(void);
void relay_off(void);
void led1_on(void);
void led7_on(void);
void led8_on(void);
void led_off(void);
#endif
main.c
#include
#include
#include
#include
#include
#include
unsigned char flag_key;
unsigned char flag_smg=0;
unsigned char flag_relay;
unsigned char coded_set[6]={
11,11,11,11,11,11};
unsigned char coded[6]={
8,8,8,8,8,8};
unsigned char coded_change[6]={
11,11,11,11,11,11};
unsigned char count=0;
unsigned char k=0,n=0,m=0,j=0;
unsigned char pass=0;
unsigned char flag_led=0;
unsigned char flag_20ms;
unsigned char count1=0;
unsigned char key_count=0;
unsigned char compare;
unsigned char flag_return=0;
unsigned char flag_5s=0;
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void main(void)
{
if(eeprom_read(0x69)==0x99)
{
for(k=0;k<6;k++)
{
coded[k]=eeprom_read(k);
Delay5ms();
}
}
else
{
for(k=0;k<6;k++)
{
eeprom_write(k,8);
Delay5ms();
}
eeprom_write(0x69,0x99);
}
bee_off();
relay_off();
led_off();
Timer0Init();
while(1)
{
/**********keybord**********/
if(flag_key)
{
flag_key=0;
key_read();
}
if(key_press)
{
key_press=0;
if(flag_smg==0)
{
for(j=0;j<6;j++)
{
coded_set[j]=11;
count=0;
}
}
/*****mode1*****/
if(key_value==15&&flag_smg==0)
{
flag_smg=1;
compare=1;
}
if(flag_smg==1)
{
if(key_value>=0&&key_value<=9)
{
count++;
switch(count)
{
case 1:
coded_set[5]=key_value;
break;
case 2:
coded_set[4]=coded_set[5];
coded_set[5]=key_value;
break;
case 3:
coded_set[3]=coded_set[4];
coded_set[4]=coded_set[5];
coded_set[5]=key_value;
break;
case 4:
coded_set[2]=coded_set[3];
coded_set[3]=coded_set[4];
coded_set[4]=coded_set[5];
coded_set[5]=key_value;
break;
case 5:
coded_set[1]=coded_set[2];
coded_set[2]=coded_set[3];
coded_set[3]=coded_set[4];
coded_set[4]=coded_set[5];
coded_set[5]=key_value;
break;
case 6:
coded_set[0]=coded_set[1];
coded_set[1]=coded_set[2];
coded_set[2]=coded_set[3];
coded_set[3]=coded_set[4];
coded_set[4]=coded_set[5];
coded_set[5]=key_value;
break;
}
if(count>6) count=6;
}
if(key_value==13)
{
switch(count)
{
case 1:
coded_set[5]=11;
count--;
break;
case 2:
coded_set[5]=coded_set[4];
coded_set[4]=11;
count--;
break;
case 3:
coded_set[5]=coded_set[4];
coded_set[4]=coded_set[3];
coded_set[3]=11;
count--;
break;
case 4:
coded_set[5]=coded_set[4];
coded_set[4]=coded_set[3];
coded_set[3]=coded_set[2];
coded_set[2]=11;
count--;
break;
case 5:
coded_set[5]=coded_set[4];
coded_set[4]=coded_set[3];
coded_set[3]=coded_set[2];
coded_set[2]=coded_set[1];
coded_set[1]=11;
count--;
break;
case 6:
coded_set[5]=coded_set[4];
coded_set[4]=coded_set[3];
coded_set[3]=coded_set[2];
coded_set[2]=coded_set[1];
coded_set[1]=coded_set[0];
coded_set[0]=11;
count--;
break;
}
}
}
/*****mode2*****/
else if(flag_smg==2)
{
if(key_value==14)
flag_smg=3;
}
/*****mode3*****/
else if(flag_smg==3)
{
if(key_value>=0&&key_value<=9)
{
count1++;
switch(count1)
{
case 1:coded_change[0]=key_value;break;
case 2:coded_change[1]=key_value;break;
case 3:coded_change[2]=key_value;break;
case 4:coded_change[3]=key_value;break;
case 5:coded_change[4]=key_value;break;
case 6:coded_change[5]=key_value;break;
}
}
if(key_value==13)
{
switch(count1)
{
case 1:coded_change[0]=11;count1--;break;
case 2:coded_change[1]=11;count1--;break;
case 3:coded_change[2]=11;count1--;break;
case 4:coded_change[3]=11;count1--;break;
case 5:coded_change[4]=11;count1--;break;
case 6:coded_change[5]=11;count1--;break;
}
}
if(count1>6) count1=6;
if(count1==6&&key_value==14)
{
flag_smg=0;
for(n=0;n<6;n++)
{
coded[n]=coded_change[n];
}
for(n=0;n<6;n++)
{
eeprom_write(n,coded_change[n]);
Delay5ms();
}
}
}
}
/**********pass**********/
if(compare==1)
{
for(k=0;k<6;k++)
{
if(coded[k]!=coded_set[k])
{
pass=0;
flag_5s=0;
break;
}
else
{
pass=1;
compare=0;
}
}
}
if(compare==0&&pass==1)
{
pass=0;
flag_smg=2;
flag_5s=1;
}
/**********return**********/
if(flag_return==1&&flag_smg==2)
{
flag_return=0;
flag_smg=0;
}
/**********led**********/
if((count==6)&&(compare==1))
flag_led=1;
else
flag_led=0;
if(flag_20ms)
{
flag_20ms=0;
if(flag_led==0) led_off();
else if(flag_led==1) led1_on();
else if(flag_led==2) led7_on();
else if(flag_led==3) led8_on();
}
/**********relay**********/
if(flag_relay)
relay_on();
else
relay_off();
/**********smg**********/
if(flag_smg==0)
{
flag_relay=0;
dspbuf[0]=11;
dspbuf[1]=11;
dspbuf[2]=11;
dspbuf[3]=11;
dspbuf[4]=11;
dspbuf[5]=11;
dspbuf[6]=11;
dspbuf[7]=11;
}
else if(flag_smg==1)
{
flag_relay=0;
dspbuf[0]=10;
dspbuf[1]=11;
dspbuf[2]=coded_set[0];
dspbuf[3]=coded_set[1];
dspbuf[4]=coded_set[2];
dspbuf[5]=coded_set[3];
dspbuf[6]=coded_set[4];
dspbuf[7]=coded_set[5];
}
else if(flag_smg==2)
{
flag_relay=1;
dspbuf[0]=0;
dspbuf[1]=11;
dspbuf[2]=11;
dspbuf[3]=11;
dspbuf[4]=0;
dspbuf[5]=15;
dspbuf[6]=13;
dspbuf[7]=14;
}
else if(flag_smg==3)
{
flag_relay=0;
dspbuf[0]=12;
dspbuf[1]=11;
dspbuf[2]=coded_change[0];
dspbuf[3]=coded_change[1];
dspbuf[4]=coded_change[2];
dspbuf[5]=coded_change[3];
dspbuf[6]=coded_change[4];
dspbuf[7]=coded_change[5];
}
}
}
void timer0_isr(void) interrupt 1
{
static unsigned char flag_key_time=0;
static unsigned char flag_20ms_time=0;
static unsigned int flag_return_time=0;
if(++flag_key_time==30)
{
flag_key_time=0;
flag_key=1;
}
if(++flag_20ms_time>=20)
{
flag_20ms_time=0;
flag_20ms=1;
}
if(flag_5s&&key_press==0)
{
if(++flag_return_time>=5000)
{
flag_return_time=0;
flag_return=1;
}
}
else
flag_return_time=0;
smg_display();
}