此程序包含IC卡读写,数码管显示,按键处理~~提取出来就可以用了!
[复制链接]
#include <reg52.h> #include <stdlib.h> #include <intrins.h> #include <stdio.h> #define uchar unsigned char #define uint unsigned int char code DAT1[13]={0x14,0xD7,0x4C,0x45,0x87,0x25,0x24,0x57,0x04,0x05,0xef,0xff,0x3c,}; char code DAT2[11]={0x82,0xE7,0x4A, 0x46,0x27,0x16,0x12,0xC7,0x02,0x06,0x7f,}; sbit ledout = P3^1; sbit heatout = P3^2; sbit mout= P1^3; sbit spk =P2^5; sbit ld0=P2^0; sbit ld1=P2^1; sbit ld2=P2^2; sbit ld3=P1^4; sbit ld4=P1^5;
sbit k0 = P3^4; sbit k1 = P3^3; sbit k2 = P1^2; sbit k3 = P1^6;
sbit SDA=P3^6; sbit SCL=P3^7; sbit sw=P3^5; sbit aled = P2^6; sbit bled = P2^4;
bit aledk=0; bit yx=0; bit yb=0; bit led0=0; bit ick=1; bdata char com_data; sbit mos_bit=com_data^7; sbit low_bit=com_data^0; uchar cc=40,ccc=170; char key=0,keyt=0,minaok=0,minbok=0;ickk=0,ickkk=0,min=0,sec=60,cshc=0; char led1=0xff,table_2=0,table_3=0,table_4=0,table_5=0,table_8=0; char cnt=0,cnttme=0,cnt6=0,cnt7=0,cnt8=0,dcnt=0,kcnt=0;
void delay( uint z ) { uint x,y; for(x=z;x>0;x--) for(y=150;y>0;y--); }
void BEEP (void) { spk =0; delay(80); spk =1; }
void start() { SDA=1;_nop_();_nop_();_nop_();_nop_(); SCL=1;_nop_();_nop_();_nop_();_nop_(); SDA=0;_nop_();_nop_();_nop_();_nop_(); SCL=0;_nop_();_nop_();_nop_();_nop_(); }
void stop() { SDA=0;_nop_();_nop_();_nop_();_nop_(); SCL=1;_nop_();_nop_();_nop_();_nop_(); SDA=1;_nop_();_nop_();_nop_();_nop_(); }
void ack() { SCL=1;_nop_();_nop_();_nop_();_nop_(); SCL=0;_nop_();_nop_();_nop_();_nop_(); }
void shift8(char a) { data uchar i; com_data=a; for(i=0;i<8;i++) { SDA=mos_bit; SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); SCL=0;_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); com_data=com_data*2; } }
uchar rd_24c01(char a) { data uchar i,command; SDA=1;_nop_();_nop_();_nop_();_nop_(); SCL=0;_nop_();_nop_();_nop_();_nop_(); start(); command=160; shift8(command); ack(); shift8(a); ack(); start(); command=161; shift8(command); ack(); SDA=1;_nop_();_nop_();_nop_();_nop_(); for(i=0;i<8;i++) { com_data=com_data*2; SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); low_bit=SDA; SCL=0;_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); } stop(); return(com_data); }
void wr_24c01(char a,b) { data uchar command; _nop_();_nop_();_nop_();_nop_();_nop_(); SDA=1;_nop_();_nop_();_nop_();_nop_(); SCL=0;_nop_();_nop_();_nop_();_nop_(); start(); command=160; shift8(command); ack(); shift8(a); ack(); shift8(b); ack(); stop(); _nop_();_nop_();_nop_();_nop_();_nop_(); }
void kay(void) {
if(minaok>=20) { min++; heatout=1; if((k3)&&(key==0)){minaok=0;} delay(100); if(min>=99){min=99;minaok=0;} } else{if((k3)&&(key==0)){minaok=0;}} if(minbok>=20) { min--; heatout=1; if((k2)&&(key==0)){minbok=0;} delay(100); if(min<=1){min=1;minbok=0;} } else{if((k2)&&(key==0)){minbok=0;}} if((keyt!=key)&&((!k1)||(!k2)||(!k3)||(!k0)))delay(5); if((keyt!=key)&&((!k1)||(!k2)||(!k3)||(!k0))) { if(key==1) { min++; BEEP(); if(min>98)min=99; } if(key==2) { min--; BEEP(); if(min<1)min=1; } if(key==3) { yb=~yb; if(!yb){bled=1;led1=0xff;cnt7=0;} BEEP(); } if(key==4) { yx=~yx; if(yx){ledout =1;aled=0;} else{ledout=0;aled=1;led1=0xff;}BEEP(); } keyt=key; } } void display(void) { if((yb)&&(cc<ccc)) { mout=0; } else { mout=1; } table_4=min%10; table_5=min/10;
if(cc>=90) { table_2=(cc%100)%10; table_3=(cc%100)/10; table_8=cc/100; heatout=1; if(yb){bled=0;} } if(cc<90) { heatout=0; if(yb) { if(aledk)bled=1; if(!aledk)bled=0; } switch(cnt7) { case 0:{ table_2=10;table_3=10;table_8=10; }break; case 1:{ table_2=11;table_3=11;table_8=12; }break; case 2:{ table_2=11;table_3=12;table_8=12; }break; case 3:{ table_2=12;table_3=12;table_8=12; } } } } void LcmInit( void ) { TMOD = 0x11; TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; ET0 = 1;TR0 = 1; TH1= (65536-1000)/256; TL1 = (65536-1000)%256; ET1 = 1;TR1 = 1;EA = 1; } void time(void) { if(cnt>=19) { cnt=0; sec--; led0=~led0; if(led0)led1=0xfd; if(!led0)led1=0xff; if(sec<=0){if(min!=0){sec=59;min--;} } } } void time1 (void) interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; if(yb||yx)cnt++; /***此处屏蔽为商业用的***/ } void time2 (void)interrupt 3 { TH1= (65536-1500)/256; TL1=(65536-1500)%256; P0=0xff;ld0=1;ld1=1;ld2=1;ld3=1;ld4=1; switch(dcnt) { case 0:{P0=DAT1[table_8];ld4=1;ld0=0;}break; case 1:{P0=DAT1[table_3];ld0=1;ld1=0;}break; case 2:{P0=DAT1[table_2];ld1=1;ld2=0;}break; case 3:{P0=DAT2[table_5];ld2=1;ld3=0;}break; case 4:{P0=DAT2[table_4]&led1;ld3=1;ld4=0;}break; case 5:{ k0=1;k1=1;k2=1;k3=1; switch(kcnt) { case 0:{if(!k0)key=3;}break; case 1:{if(!k1)key=4;}break; case 2:{if(!k2){key=2;minbok++;if(minbok>25)minbok=25;}}break; case 3:{if(!k3){key=1;minaok++;if(minaok>25)minaok=25;}}break; } if((k0)&&(k1)&&(k2)&&(k3)){key=0;keyt=10;} } } dcnt++; if(dcnt>=6) { dcnt=0; kcnt++; if(kcnt>=4) { kcnt=0; } } } void Main( void ) { LcmInit(); icaa: heatout=0; mout=1; ledout=0; ick=1;ickk=0;ickk=0; min=0;cshc=0;yx=0;yb=0; aled=1;bled=1;led1=0xff; table_4=10;table_5=10;table_2=10; table_3=10;table_8=10;cnt7=0; do{ if(cc>=88)display(); aled =~aled;delay(150); if(!sw)delay(10); if(!sw) { ickk=rd_24c01(1);delay(10); ickkk=rd_24c01(2);delay(10); min=rd_24c01(3);delay(10); cshc=rd_24c01(4);delay(10); BEEP(); if((ickk==1)&&(ickkk==2)&&(cshc>=1)&&(cshc<=99)) { table_4=min%10; table_5=min/10; table_3=cshc%10; table_8=cshc/10; table_2=11; aled=1;bled=1;ick=0; } } } while(ick); do{bled =~bled;delay(80);}while(k0&&(!sw));delay(5); if(sw){goto icaa;} cshc--; wr_24c01(4,cshc);delay(10); table_8=10; table_2=cshc%10; table_3=cshc/10; BEEP(); bled=1; delay(3000); while(1) { if(min==0&&(yb||yx)) { BEEP();delay(500); BEEP();delay(500); BEEP();delay(500); goto icaa; } if((sw)||(cshc<=0))delay(10); if((sw)||(cshc<=0)) { goto icaa; } time(); kay(); display(); } }
|