|
单片机源程序如下:
- #include"stc15f2k60s2.h"
- #include"ds1302.h"
- #include"onewire.h"
- #include"iic.h"
- #define u8 unsigned char
- #define u16 unsigned int
- code u8 dx[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
- code u8 wx[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- void init_ct107d();
- void Timer0Init();
- void dig_display();
- void key_deal();
- void dig_deal();
- void led_deal();
- u8 sj[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
- u8 read_key_val();
- u8 dig_count=0;
- u8 key_read;
- u8 key_lock_20ms=0;
- u8 ds1302_ds200ms=0;
- u8 hour,min,sec;
- u8 show_mode=0;
- u8 change_mode=0;
- u8 parameter[3]={17,25,4};
- u8 change_parameter[3]={0};
- u16 led_ds3s=0;
- u16 pcf8591_ds400ms=0;
- u16 V;
- u16 temp;
- u16 ds18b20_ds1s=0;
- bit sys_mode=0;
- bit key_lock = 1;
- bit ds1302_key=1;
- bit ds18b20_key=1;
- bit led_ron;
- bit pcf8591_key=1;
- bit l1_sign=0,l2_sign=0;l3_sign=0;led_sign=0;
- bit led_ds_sign=0;
- void main()
- {
- init_ct107d();
- Timer0Init();
- set_sfm(16,59,50);
- while(1)
- {
- //按键
- if(key_lock==1)
- {
- key_lock = 0;
- key_read = read_key_val();
- if(key_read!=0)
- {
- key_deal();
- }
- }
- //时间
- if(ds1302_key==1)
- {
- hour = Read_Ds1302_Byte(0x85)/16*10+Read_Ds1302_Byte(0x85)%16;
- min = Read_Ds1302_Byte(0x83)/16*10+Read_Ds1302_Byte(0x83)%16;
- sec = Read_Ds1302_Byte(0x81)/16*10+Read_Ds1302_Byte(0x81)%16;
- ds1302_key=0;
- if(parameter[0]<8){if(hour<8&&hour>=parameter[0])l1_sign=1;else l1_sign=0;}
- else{if(hour>=parameter[0])l1_sign=1;else l1_sign=0;}
- led_deal();
- }
- //温度
- if(ds18b20_key==1)
- {
- temp = rd_temperature();
- if(temp<parameter[1]*100)l2_sign=1;
- else l2_sign=0;
- ds18b20_key=0;
- //led处理
- led_deal();
- }
- //电压
- if(pcf8591_key==1)
- {
- V=pcf8591_adc();
- V = V/2.55*5;
- if(V>=200)
- {
- if(led_ron==1){led_ron=0;led_ds3s=0;led_ds_sign=1;led_sign=0;}
- else
- {
- if(led_ds_sign==0){led_ds3s=0;led_ds_sign=1;}
- }
- }
- else
- {
- if(led_ron==0){led_ron=1;led_ds3s=0;led_ds_sign=1;led_sign=1;}
- else
- {
- if(led_ds_sign==0){led_ds3s=0;led_ds_sign=1;}
- }
- }
- pcf8591_key=0;
- led_deal();
- }
- //数码管处理
- dig_deal();
- }
- }
- void init_ct107d()
- {
- P0=0;
- P2=(P2&0X1F)|0XC0; //数码管
- P2=(P2&0X1F)|0XA0; //外设
- P2=0;
- P0=0XFF;
- P2=(P2&0X1F)|0X80; //led
- P2=0;
- }
- void Timer0Init(void) //1毫秒@12.000MHz
- {
- AUXR &= 0x7F; //定时器时钟12T模式
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0x18; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0=1;
- EA=1;
- }
- void time0() interrupt 1
- {
- TL0 = 0x18; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- dig_display();
- //按键读取定时
- key_lock_20ms++;
- if(key_lock_20ms>=20)
- {
- key_lock_20ms=0;
- key_lock=1;
- }
- //时钟定时
- if(ds1302_key==0)
- {
- ds1302_ds200ms++;
- if(ds1302_ds200ms>=200)
- {
- ds1302_ds200ms=0;
- ds1302_key=1;
- }
- }
- //温度定时
- if(ds18b20_key==0)
- {
- ds18b20_ds1s++;
- if(ds18b20_ds1s>=1000)
- {
- ds18b20_ds1s=0;
- ds18b20_key=1;
- }
- }
- //电压定时
- if(pcf8591_key==0)
- {
- pcf8591_ds400ms++;
- if(pcf8591_ds400ms>=400)
- {
- pcf8591_ds400ms=0;
- pcf8591_key=1;
- }
- }
- //led3定时
- if(led_ds_sign==1)
- {
- led_ds3s++;
- if(led_ds3s>=3000)
- {
- led_ds_sign=0;
- led_ds3s=0;
- if(led_ron==0)l3_sign=0;
- else l3_sign=1;
- led_deal();
- TR1=0;
- }
- }
- }
- void dig_display()
- {
- P0=0xff;P2=(P2&0X1F)|0XE0;P2=0;
- P0=wx[dig_count];P2=(P2&0X1F)|0XC0;P2=0;
- P0=sj[dig_count];P2=(P2&0X1F)|0XE0;P2=0;
- dig_count++;
- if(dig_count>7)dig_count=0;
- }
- u8 read_key_val()
- {
- static u8 key_state=0;
- u8 key_new;
- u8 key_val=0;
- P44=0;P42=1;P35=1;P34=1;
- key_new=P3;
- P44=1;P42=0;
- key_new=(key_new<<4)|(P3&0x0f);
- switch(key_state)
- {
- case(0):
- if(key_new!=0xff){key_state=1;}break;
- case(1):
- if(key_new==0xff){key_state=0;}
- else
- {
- switch(~key_new)
- {
- case(0x80):key_val=4;break;
- case(0x40):key_val=5;break;
- case(0x08):key_val=8;break;
- case(0x04):key_val=9;break;
- default:key_val=0;break;
- }
- key_state=2;
- } break;
- case(2):if(key_new==0xff){key_state=0;}break;
- }
- return key_val;
- }
- void key_deal()
- {
- u8 i;
- switch(key_read)
- {
- case(4):
- sys_mode=~sys_mode;
- if(sys_mode==0)for(i=0;i<3;i++){parameter[/font][i][font=Arial]=change_parameter[/font][i][font=Arial];ds18b20_key=1;pcf8591_key=1;ds1302_key=1;}
- else for(i=0;i<3;i++)change_parameter[/font][i][font=Arial]=parameter[/font][i][font=Arial];
- show_mode=0; change_mode=0;
- break;
- case(5):
- if(sys_mode==0)
- {
- show_mode++;
- if(show_mode>=3)show_mode=0;
- }
- else
- {
- change_mode++;
- if(change_mode>=3)change_mode=0;
- }
- break;
- case(8):
- if(sys_mode==1)
- {
- switch(change_mode)
- {
- case(0):
- if(change_parameter[0]!=0)change_parameter[0]--;
- else change_parameter[0]=23;
- break;
- case(1):
- if(change_parameter[1]!=0)change_parameter[1]--;
- else change_parameter[1]=99;
- break;
- case(2):
- if(change_parameter[2]!=4)change_parameter[2]--;
- else change_parameter[2]=8;
- break;
- }
- }
- break;
- case(9):
- if(sys_mode==1)
- {
- switch(change_mode)
- {
- case(0):
- if(change_parameter[0]!=23)change_parameter[0]++;
- else change_parameter[0]=0;
- break;
- case(1):
- if(change_parameter[1]!=99)change_parameter[1]++;
- else change_parameter[1]=0;
- break;
- case(2):
- if(change_parameter[2]!=8)change_parameter[2]++;
- else change_parameter[2]=4;
- break;
- }
- }
- break;
- }
- }
- void dig_deal()
- {
- u8 i;
- if(sys_mode==0)
- {
- switch(show_mode)
- {
- case(0):
- sj[0]=dx[hour/10];sj[1]=dx[hour%10];
- sj[2]=0xbf;sj[5]=0xbf;
- sj[3]=dx[min/10];sj[4]=dx[min%10];
- sj[6]=dx[sec/10];sj[7]=dx[sec%10];
- break;
- case(1):
- sj[0]=0xc6;
- for(i=1;i<5;i++)sj[/font][i][font=Arial]=0xff;
- sj[5]=dx[temp%10000/1000];sj[6]=dx[temp%1000/100]&0x7f;
- sj[7]=dx[temp%100/10];
- break;
- case(2):
- sj[0]=0x86;sj[1]=0xff;sj[5]=0xff;sj[6]=0xff;
- sj[2]=dx[V%1000/100]&0x7f; sj[3]=dx[V%100/10]; sj[4]=dx[V%10];
- sj[7]=dx[led_ron];
- break;
- }
- }
- else
- {
- switch(change_mode)
- {
- case(0):
- sj[0]=0x8c;sj[1]=dx[1];
- for(i=2;i<6;i++)sj[/font][i][font=Arial]=0xff;
- sj[6]=dx[change_parameter[0]/10];sj[7]=dx[change_parameter[0]%10];
- break;
- case(1):
- sj[0]=0x8c;sj[1]=dx[2];
- for(i=2;i<6;i++)sj[/font][i][font=Arial]=0xff;
- sj[6]=dx[change_parameter[1]/10];sj[7]=dx[change_parameter[1]%10];
- break;
- case(2):
- sj[0]=0x8c;sj[1]=dx[3];
- for(i=2;i<6;i++)sj[/font][i][font=Arial]=0xff;
- sj[6]=dx[change_parameter[2]/10];sj[7]=dx[change_parameter[2]%10];
- break;
- }
- }
- }
- void led_deal()
- {
- u8 m;
- m=0xff;
- if(led_sign==1)
- {
- switch(parameter[2])
- {
- case(4):m=0xf7;break;
- case(5):m=0xef;break;
- case(6):m=0xdf;break;
- case(7):m=0xbf;break;
- case(8):m=0x7f;break;
- }
- }
- if(l1_sign==1)m&=0xfe;
- if(l2_sign==1)m&=0xfd;
- if(l3_sign==1)m&=0xfb;
- P0=m;
- P2=(P2&0X1F)|0X80;
- P2=0;
- }
复制代码
|
-
-
-
决赛.zip
450.95 KB, 下载次数: 35, 下载积分: 黑币 -5
题目+源程序
评分
-
查看全部评分
|