- #include<reg51.h>
- sbit LED=P3^1;
- sbit BEEP=P3^2;
- sbit PWM=P3^0;
- sbit INA=P2^4;
- sbit INB=P2^5;
- sbit INC=P2^6;
- sbit IND=P2^7;
- unsigned char code led_mod[10]={0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- unsigned char code led_sel[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- unsigned char DisplayCode[8]={0x00,0x00,0x00,0x3f,0x3f,0x40,0x3f,0x3f};
- unsigned char code f_rotation[8]={0xfe,0xfd,0xfb,0xf7,0x7f,0xbf,0xdf,0xef};
- unsigned char code b_rotation[4]={0X6F,0x3F,0x9F,0xCF }; typedef unsigned int u16; typedef unsigned char u8;
- bit king_flag=0 ;
- bit Time_adjust=0;////????????????????????????
- bit timer_1sec_flag=0;//???????????1???????????
- bit alarm_flag=0;//?????????????????????????????
- bit alarm_fen_flag=0;
- bit alarm_h_flag=0;
- bit led_flag=0;//????????????LED??10????
- unsigned int motor=32;//?????????????????
- bit dir=1;//???岽????????????????????????
- bit r_run=0; bit timer_1min_flag=0; bit flag=0;
- unsigned char min=0;
- unsigned char h=0; u8 Motor_Step=0;
- unsigned char alarm_min=10;//????????
- unsigned char alarm_h=0;//?????С?
- unsigned char key_value;
- unsigned char led_timer_counter; double count=2;unsigned char ID;unsigned char kind;
- sbit K1=P1^0;//????1
- sbit K2=P1^1;
- sbit K3=P1^2;
- sbit K4=P1^3;
- sbit K5=P1^4;
- unsigned char pre_sta[5]={1,1,1,1,1};//??????????????I/O????λ
- unsigned char cur_sta[5]={1,1,1,1,1};//??????????????I/O????λ
- unsigned char key_buff[5]={0xff,0xff,0xff,0xff,0xff};
- void delay(unsigned int z)//??????
- {
- unsigned int i,j;
- for(i=0;i<z;i++)
- for(j=0;j<125;j++);
- }
- void led7seg_scan()//??????????
- {
- unsigned char i;
- for(i=0;i<4;i++)
- {
- P2=led_sel[i];//λ????????????λ????????
- P0=DisplayCode[i];//????????????????????
- delay(1);//delay 1ms
- P0=0x00;//?????????
- }
- }
- void Motor_Delay1ms(u16 c) //??? 0us
- {
- for (; c>0; c--)
- {
- // for (b=199;b>0;b--)
- // {
- //
- // }
- }
-
- }
- void Motor_Run(bit dir)
- {
-
- if(r_run){
- switch(Motor_Step)
- {
- case 0: INA=1;INB=0;INC=0;IND=0;Motor_Step++;delay(12);break;
-
- case 1: INA=1;INB=0;INC=1;IND=0;Motor_Step++;delay(12);break;
-
- case 2: INA=0;INB=0;INC=1;IND=0;Motor_Step++;delay(12);break;
-
- case 3: INA=0;INB=1;INC=1;IND=0;Motor_Step++;delay(12);break;
-
- case 4: INA=0;INB=1;INC=0;IND=0;Motor_Step++;delay(12);break;
-
- case 5: INA=0;INB=1;INC=0;IND=0;Motor_Step++;delay(12);break;
-
- case 6: INA=0;INB=1;INC=1;IND=0;Motor_Step++;delay(12);break;
-
- case 7: INA=0;INB=0;INC=1;IND=0;Motor_Step++;delay(12);break;
- case 8: INA=1;INB=0;INC=0;IND=0;Motor_Step=0;delay(12);break;
-
- /* case 9: INA=0;INB=1;INC=1;IND=0;if(dir)Motor_Step++;else Motor_Step--;Motor_Delay1ms(20);break;
-
- case 10: INA=0;INB=0;INC=1;IND=0;if(dir)Motor_Step++;else Motor_Step--;Motor_Delay1ms(20);break;
-
- case 11: INA=0;INB=1;INC=1;IND=0;if(dir)Motor_Step++;else Motor_Step--;Motor_Delay1ms(20);break;
-
- case 12: INA=0;INB=1;INC=0;IND=0;if(dir)Motor_Step++;else Motor_Step--;Motor_Delay1ms(20);break;
-
- case 13: INA=0;INB=1;INC=0;IND=1;if(dir)Motor_Step++;else Motor_Step--;Motor_Delay1ms(20);break;
-
- case 14: INA=0;INB=0;INC=0;IND=1;if(dir)Motor_Step=0;else Motor_Step--;Motor_Delay1ms(20);break; */
-
-
-
- }
- } else INA=0;INB=0;INC=0;IND=0;
- delay(2);
- }
- void timer0_init()
- {
- TR0=1;//???????0
- TMOD=0x11;//????????0??1????????????????????????1
- EA=1;//???ж????ж?
- ET0=1;//???????0???ж?
- ET1=1;//???????1???ж?
- TR1=0;//???????1
- TH0=(65536-4000)/256;//?????0????????
- TL0=(65536-4000)%256;
- TH1=(65536-4000)/256; //?????1????????
- TL1=(65536-4000)%256;
- }
- void key_action(unsigned char key_value)//???????????
- {
- if(key_value==1)
- delay(100);//?????????????
- {
- if(key_value==1) {
- LED=~LED; king_flag=~king_flag;
- TR1=~TR1; delay(100);
- }
- else if(key_value==2)//?????????????
- { if(king_flag)
- { if(K2==0) { delay(2); //????????????????????ж???????
- if(K2==0) { count+=1; //?????μ???????????0.5
- if(count>=8) //???????????
- { count=2; } } while(!K1); } } }
-
- else if(key_value==3 )//??3??????
- { if(king_flag) ID++;if(ID==3){
- ID=0;
- } }
- //Motor_Run(dir); }
-
-
-
- else if(key_value==4)//????4????
- { if(king_flag){ kind++;if(kind==4){
- kind=0; } }
-
- /* else if(key_value==5)
- delay(100);
- { if(key_value==5)if(king_flag) {
- if(alarm_fen_flag)//???????????????£??????????????
- {
- alarm_min++;
- if(alarm_min==60) alarm_min=0;
- DisplayCode[3]=led_mod[alarm_min%10];
- DisplayCode[2]=led_mod[alarm_min/10];
- }
-
- }
- else if(alarm_h_flag)//???????????????£??????????????
- {
- alarm_h++;
- if(alarm_h==24) alarm_h=0;
- DisplayCode[1]=led_mod[alarm_h%10];
- DisplayCode[0]=led_mod[alarm_h/10];
- }
-
- } } */
-
-
-
- } }
- } unsigned char key_scan(void)//???????
- {
- unsigned char i;
- unsigned char key_value=0;
- for(i=0;i<5;i++)
- {
- if(cur_sta[i]!=pre_sta[i])//??????????????????
- {
- if(pre_sta[i]==1)//???????????,????????
- {
- key_value=i+1;
- }
- pre_sta[i]=cur_sta[i];//??????????????
- }
- }
- return (key_value);//?????????????????????????????????
- }
- void cur_sta_update()//???????????
- {
- unsigned char i;
- key_buff[0]=(key_buff[0]<<1)|K1;//??????????K1?????key_buff[0]?е?????λ
- key_buff[1]=(key_buff[1]<<1)|K2;//??????????K2?????key_buff[1]?е?????λ
- key_buff[2]=(key_buff[2]<<1)|K3;//??????????K3?????key_buff[2]?е?????λ
- key_buff[3]=(key_buff[3]<<1)|K4;//??????????K4?????key_buff[3]?е?????λ
- key_buff[4]=(key_buff[4]<<1)|K5;//??????????K4?????key_buff[3]?е?????λ
- for(i=0;i<5;i++)
- {
- if((key_buff[i]&0x0f)==0x00)//???????4?γ???????
- {
- cur_sta[i]=0;//????μ????0
- }
- else if((key_buff[i]&0x0f)==0x0f)//???????4?γ???????
- {
- cur_sta[i]=1;//????μ????1
- }
- }
- }
- void DisplayCode_update_time()//??????????????
- {
- if(Time_adjust==0)//?????δ???????????£??????1????У????????????С?????
- {
- if(timer_1sec_flag)//1 sec ?????
- {
- timer_1sec_flag=0;
-
-
- min++;
- if(min==60)
- {
- min=0;
- h++;
- if(h==24)
- h=0;
- }
- }
-
- if(alarm_flag==0)//????δ???????????????????????????
- {DisplayCode[3]=led_mod[min%10];
- DisplayCode[2]=led_mod[min/10];
- DisplayCode[1]=led_mod[h%10];
- DisplayCode[0]=led_mod[h/10];
- } delay(1);
- }
- } void DisplayCode_update_time25()//??????????????
- {
- if(flag)//?????δ???????????£??????1????У????????????С?????
- {
- if(timer_1min_flag)//1 sec ?????
- {
- timer_1min_flag=0;
-
-
- alarm_min--;
- if(alarm_min==0)
- { alarm_min=10;
- kind=0; ID=0;LED=~LED;PWM=0;TR1=~TR1;
- }
- }
- }}
- void main()
- { unsigned char i,j;
- unsigned char k=0;
- unsigned char key_value;LED=0;r_run=0;dir=0;
- timer0_init(); IT0=1;
- while(1)
- {
- led7seg_scan();
- key_value=key_scan();
- key_action(key_value);
- DisplayCode_update_time();
- DisplayCode_update_time25();
- if(key_value!=0)
- { delay(2);
- while(key_value==0);
- if(key_value!=0)
- {
- for(i=10;i>0;i--){
- for(j=5;j>0;j--){
- BEEP=0;
- delay(1);
- BEEP=1;
- delay(1);
- }
- }
- BEEP=1;
- }
- }
-
-
-
-
- switch(ID)
- {
- case 0: r_run=0; break; //??/??
- case 1: r_run=1; break; //???
-
- }
- if(r_run) Motor_Run(dir);
- switch(kind)
- {
- case 0: alarm_flag=0; break; //??/??
- case 1: alarm_flag=1;alarm_fen_flag=1; break; //???
- case 2: alarm_flag=1;alarm_h_flag=1;alarm_fen_flag=0;DisplayCode[3]=led_mod[alarm_min%10];
- DisplayCode[2]=led_mod[alarm_min/10]; break; //???
- case 3: alarm_flag=1;alarm_h_flag=0;alarm_fen_flag=0;flag=1;DisplayCode[1]=led_mod[alarm_h%10];
- DisplayCode[0]=led_mod[alarm_h/10]; DisplayCode[3]=led_mod[alarm_min%10];
- DisplayCode[2]=led_mod[alarm_min/10]; break;
- }
-
-
-
- if(key_value==5)
- delay(100);if(key_value==5){if(king_flag) {
-
- if(alarm_fen_flag)//???????????????£??????????????
- {
- alarm_min++;
- if(alarm_min==60) alarm_min=0;
- DisplayCode[3]=led_mod[alarm_min%10];
- DisplayCode[2]=led_mod[alarm_min/10];while(key_value!=5);}
- else if(alarm_h_flag)//???????????????£??????????????
- {
- alarm_h++;
- if(alarm_h==24) alarm_h=0;
- DisplayCode[1]=led_mod[alarm_h%10];
- DisplayCode[0]=led_mod[alarm_h/10];}}delay(100); while(key_value!=5);} }
- }
- void timer0_4ms() interrupt 1
- {
- static unsigned char timer_count=0; static unsigned char count=0;static unsigned char count_timer=0;static unsigned char timer_count12=0;
- key_value=key_scan();
-
- TH0=(65536-4000)/256;
- TL0=(65536-4000)%256;
-
- timer_count++;
- if(timer_count==250)//1 sec
- {
-
- timer_count=0;
- timer_1sec_flag=1;
- }
- if(alarm_flag)
- {
- if(alarm_fen_flag){if(count_timer==100) DisplayCode[3]=led_mod[alarm_min%10];
- if(count_timer==100) DisplayCode[2]=led_mod[alarm_min/10];
- count_timer++;
- if(count_timer==170){ count_timer=0;
- DisplayCode[3]=0x00;
- DisplayCode[2]=0x00;
-
- }
- }}if(alarm_h_flag)//???????????????£??????????????
-
- {if(count==100) DisplayCode[1]=led_mod[alarm_h%10];
- if(count==100) DisplayCode[0]=led_mod[alarm_h/10];
- count++;
- if(count==170){ count=0;
- DisplayCode[1]=0x00;
- DisplayCode[0]=0x00;
- }
- } if(flag)
- {
- timer_count12++;
- if(timer_count12==250)
- {
- timer_count=0;
- timer_1min_flag=1;
- }
- }
- cur_sta_update(); {
- }
- }void timer1_1ms() interrupt 3
- {
- static unsigned char led_timer_counter=0;
- TR1=0;
- TH1=(65536-4000)/256;
- TL1=(65536-4000)%256;
- TR1=1;
- led_timer_counter++;
- //????
-
- {
- if(led_timer_counter<count) //count?????
- {
- PWM=1; //???????
-
- }
- else
- PWM=0;
-
- if(led_timer_counter>8)
- {
- led_timer_counter=0;
-
- }
-
- }
- }
复制代码
|