#include "stc15f2k60s2.h"
#include "iic.h"
#include "ds1302.h"
#define u8 unsigned char
#define u16 unsigned int
u8 tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};
u8 t_com[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
u8 volt[8]={11,1,11,10,10,10,10,10};
u8 set_volt[]={10,10,0,0,10,10,0,0};
u8 time[8]={10,10,11,10,10,11,10,10};
u8 set_time[8]={10,10,11,10,10,11,10,10};
u8 freq[8]={11,2,11,10,10,10,10,10};
idata u8 cyc[8]={11,2,11,10,10,10,10,10};
idata u8 sear_mode[8]={10,10,10,10,10,10,0,10};
idata u8 sear_time[8]={10,10,11,10,10,11,10,10};
u8 dspcom=0;
u8 key_temp,vol_temp;
u8 shi,fen,miao,thr_shi,thr_fen,thr_miao,save_shi,save_fen,save_miao;
u8 vol_mode=1;
u8 menu_num=1,set_num,vol_set_num,s_num,key_num,vol_num;
u16 blink_num;
u16 vol,vol_high,vol_low;
u16 count,frequency,cycle;
bit key_flag=0;
bit vol_flag=0;
bit blink_flag=0;
bit s_flag=0;
bit freq_flag=0;
bit search_flag=0;
u8 key_read()
{
static u8 key_state=0;
u8 key1,key2,key_press,key_return=0;
P30=0;P31=0;P32=0;P33=0;P34=1;P35=1;P42=1;P44=1;
if(P44==0)key1=0x70;
if(P42==0)key1=0xb0;
if(P35==0)key1=0xd0;
//if(P34==0)key1=0xe0;
if(P44==1&&P42==1&&P35==1&&P34==1)key1=0xf0;
P30=1;P31=1;P32=1;P33=1;P34=0;P35=0;P42=0;P44=0;
if(P33==0)key2=0x07;
if(P32==0)key2=0x0b;
if(P31==0)key2=0x0d;
if(P30==0)key2=0x0e;
if(P33==1&&P32==1&&P31==1&&P30==1)key2=0x0f;
key_press=key1|key2;
switch(key_state)
{
case 0:if(key_press!=0xff)
{
key_state=1;
}
break;
case 1:if(key_press!=0xff)
{
if(key_press==0x77)key_return=4;
if(key_press==0x7b)key_return=5;
if(key_press==0x7d)key_return=6;
if(key_press==0x7e)key_return=7;
if(key_press==0xb7)key_return=8;
if(key_press==0xbb)key_return=9;
if(key_press==0xbd)key_return=10;
if(key_press==0xbe)key_return=11;
if(key_press==0xd7)key_return=12;
if(key_press==0xdb)key_return=13;
if(key_press==0xdd)key_return=14;
if(key_press==0xde)key_return=15;
if(key_press==0xe7)key_return=10;
if(key_press==0xeb)key_return=17;
if(key_press==0xed)key_return=18;
if(key_press==0xee)key_return=19;
key_state=2;
}
else
{
key_state=0;
}
break;
case 2:if(key_press==0xff)
{
key_state=0;
}
}
return key_return;
}
void display()
{
P2=(P2&0x1f)|0xe0;P0=0xff;
P2=(P2&0x1f)|0xc0;P0=t_com[dspcom];
P2=(P2&0x1f)|0xe0;
if(menu_num==1)
{
if(set_num==0)
{
P0=~tab[time[dspcom]];
}
else
{
P0=~tab[set_time[dspcom]];
}
}
if(menu_num==2)
{
if(vol_set_num==0)
{
P0=~tab[volt[dspcom]];
}
else
{
P0=~tab[set_volt[dspcom]];
}
}
if(menu_num==3)
{
if(freq_flag==0)
{
P0=~tab[freq[dspcom]];
}
else
{
P0=~tab[cyc[dspcom]];
}
}
if(menu_num==4)
{
if(search_flag==1)
{
P0=~tab[sear_mode[dspcom]];
}
else
{
P0=~tab[sear_time[dspcom]];
}
}
P2&=0x1f;
if(++dspcom>=8)
{
dspcom=0;
}
}
void Delay2ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
void Timer0Init(void)
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD = 0x04; //设置定时器模式
TL0 = 0xff; //设置定时初值
TH0 = 0xff; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
}
void Timer1Init(void) //50毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xB0; //设置定时初值
TH1 = 0x3C; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
}
void Timer2Init(void) //2毫秒@12.000MHz
{
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0x40; //设置定时初值
T2H = 0xA2; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2 |= 0x04;
EA = 1;
}
void time_init(u8 shi,u8 fen,u8 miao)
{
Write_Ds1302(0x8e,0x00);
Write_Ds1302(0x84,shi/10*16+shi%10);
Write_Ds1302(0x82,fen/10*16+fen%10);
Write_Ds1302(0x80,miao/10*16+miao%10);
Write_Ds1302(0x8e,0x80);
}
void time_read()
{
time[0]=Read_Ds1302(0x85)/16;
time[1]=Read_Ds1302(0x85)%16;
time[3]=Read_Ds1302(0x83)/16;
time[4]=Read_Ds1302(0x83)%16;
time[6]=Read_Ds1302(0x81)/16;
time[7]=Read_Ds1302(0x81)%16;
shi=time[0]*10+time[1];
fen=time[3]*10+time[4];
miao=time[6]*10+time[7];
}
void main()
{
P2=0xa0;P0=0x00;P2=0x80;P0=0xff;P2=0x00;
time_init(23,59,55);
vol_high=eeprom_read(0);
vol_low=eeprom_read(1);
vol_temp=vol_read();
vol=(u16)(vol_temp/255.0*5000);
if(vol>vol_high*100)vol_mode=1;
else if((vol>=vol_low*100)&&(vol<vol_high*100))vol_mode=0;
else vol_mode=2;
thr_shi=eeprom_read(2);
thr_fen=eeprom_read(3);
thr_miao=eeprom_read(4);
sear_mode[7]=eeprom_read(5);
sear_time[0]=thr_shi/10;sear_time[1]=thr_shi%10;sear_time[3]=thr_fen/10;
sear_time[4]=thr_fen%10;sear_time[6]=thr_miao/10;sear_time[7]=thr_miao%10;
Timer0Init();
Timer1Init();
Timer2Init();
while(1)
{
time_read();
if(vol_flag)
{
vol_flag=0;
vol_temp=vol_read();
vol=(u16)(vol_temp/255.0*5000);
volt[4]=vol/1000;volt[5]=vol/100%10;volt[6]=vol/10%10;volt[7]=vol%10;
}
if(key_flag)
{
key_flag=0;
key_temp=key_read();
switch(key_temp)
{
case 4:if(menu_num==1)
{
if(set_num==0)
{
save_shi=shi;save_fen=fen;save_miao=miao;
}
set_num++;
if(set_num==4)
{
set_num=1;
}
}
if(menu_num==2)
{
vol_set_num++;
if(vol_set_num==3)
{
vol_set_num=1;
}
}
if(menu_num==3)
{
freq_flag=!freq_flag;
}
break;
case 5:menu_num=3;
break;
case 6:menu_num=2;set_num=0;vol_set_num=0;
eeprom_write(0,vol_high);Delay2ms();
eeprom_write(1,vol_low);
break;
case 7:menu_num=1;vol_set_num=0;
if(set_num!=0)
{
time_init(save_shi,save_fen,save_miao);
}
set_num=0;
break;
case 9:menu_num=4;
search_flag=!search_flag;
break;
case 10:if(menu_num==1)
{
if(set_num==1)
{
if(save_shi--<=0)
{save_shi=0;}
}
if(set_num==2)
{
if(save_fen--<=0)
{save_fen=0;}
}
if(set_num==3)
{
if(save_miao--<=0)
{save_miao=0;}
}
}
if(menu_num==2)
{
if(vol_set_num==1)
{
vol_high-=5;
if(vol_high<=0)
{vol_high=0;}
}
if(vol_set_num==2)
{
vol_low-=5;
if(vol_low<=0)
{vol_low=0;}
}
}
break;
case 11:if(menu_num==1)
{
if(set_num==1)
{
if(++save_shi>=24)
{save_shi=23;}
}
if(set_num==2)
{
if(++save_fen>=60)
{save_fen=59;}
}
if(set_num==3)
{
if(++save_miao>=60)
{save_miao=59;}
}
}
if(menu_num==2)
{
if(vol_set_num==1)
{
vol_high+=5;
}
if(vol_set_num==2)
{
vol_low+=5;
}
}
break;
}
}
if(menu_num==1)
{
if(set_num==1)
{
if(blink_flag)
{
set_time[0]=save_shi/10;set_time[1]=save_shi%10;set_time[3]=save_fen/10;
set_time[4]=save_fen%10;set_time[6]=save_miao/10;set_time[7]=save_miao%10;
}
else
{
set_time[0]=10;set_time[1]=10;set_time[3]=save_fen/10;
set_time[4]=save_fen%10;set_time[6]=save_miao/10;set_time[7]=save_miao%10;
}
}
if(set_num==2)
{
if(blink_flag)
{
set_time[0]=save_shi/10;set_time[1]=save_shi%10;set_time[3]=save_fen/10;
set_time[4]=save_fen%10;set_time[6]=save_miao/10;set_time[7]=save_miao%10;
}
else
{
set_time[0