#include
#define uchar unsigned char
#define uint unsigned int
***it rs=P3^5;
***it lcden=P3^4;
***it beep=P2^3;
***it s1=P3^0;
***it s2=P3^1;
***it s3=P3^2;
***it s4=P3^6;
***it DQ=P2^2;
***it dula=P2^6;
***it wela=P2^7;
bit flag1,flag_ri;
***it dscs=P1^4;
***it dsas=P1^5;
***it dsrw=P1^6;
***it dsds=P1^7;
***it dsiqr=P3^3;
***it rd=P3^7;
bit flag1,flag_ri;
uchar H0,L0,cnt,t,b,a;
code uint cyc[]={1800,1600,1440,1351,1200,1079,960};//音阶1-7的半周期数
code uchar tone[]={13,15,16,16,15,16,13,12,12,13,15,16,16,15,16,13,13, 13,15,16,
16,15,16,13,12,12,15,13,12,13,12,11,12,6,6,12,15,13,12,6,6,15,13,12,13,12,11,
12,6,5,6,0xff};// 乐曲《康定情歌》的简谱表
code uchar time[]={8,8,8,4,4,8,8,12,4,8,8,8,4,4,8,16,8,8,8,8,4,4,8,8,12,4,8,8,4,
4,4,4,8,24,8,24,8,24,8,16,8,8,8,4,4,4,4,8,16,8,32};// 节拍表
void set_time();
void read_alarm();
void write_ds(uchar,uchar );
uchar num, s1num,flag,temp;
char miao,shi,fen,year,month,day,week,amiao,afen,ashi;
uchar table[]=" 20 - - STA";
uchar table1[]=" : : ";
uchar table2[]=" Alarm : ";
void delay(uint z) //延时子程序
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/**********定时器0中断用于产生音阶方波***********/
void cntint0(void) interrupt 1
{
TH0=H0;
TL0=L0;
beep=~beep; // beep是音乐信号输出脚,beep反相,产生方波
}
/**********定时器1中断用于产生节拍延时***********/
void cntint1(void) interrupt 3
{
cnt++; // 计数初值为0,所以不用赋值
}
void Delay_DS18B20(uint num)
{
while(num--) ;
}
void didi()
{
beep=0;
delay(100);
beep=1;
}
void play_music()
{
uchar i,j=0,a,t;
uint b;
for(j=0;j<2;j++) //放两遍音乐 一分钟
{
TMOD=0x11;
EA=1;//开总中断
ET0=1;
ET1=1;
cnt=0;
TR1=1;
i = 0;
while(1)
{
//扫描按键 如有按键按下退出闹钟
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
delay(10);
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
break;
}
while(!(s1&&s2&&s3&&s4));
}
t=tone[i]; //读音调
if(t==0xff) break; //0xff是结束符
if(t!=0) //0是休止符
{
b=cyc[t%10-1]; //根据基本音阶,求出半周期数
if(t<10) b=b*2; //若是低八度音阶,半周期数加倍
if(t>20) b=b/2; //若是高八度音阶,半周期数减半
H0=(65536-b)/256; //根据半周期数,计算T0初值的高字节和低字节
L0=(65536-b)%256;
TR0=1; //启动定时器0发音
}
cnt=0;
a=time[i]; //读节拍
while(a>cnt)
{
//扫描按键 如有按键按下退出闹钟
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
delay(10);
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
break;
}
while(!(s1&&s2&&s3&&s4));
}
}
// TR0=0;
i++;
for(b=0;b<1000;b++); //稍加延时,增强节奏感
}
EA = 0;
}
// beep=0; //关闭喇叭
// flag_ri=0;
j=0;
}
void write_com(uchar com) //液晶写指令函数
{
rs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) //液晶写数据函数
{
rs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
dula=wela=0;
lcden=0;
rd=0;
s1num=0;
week=1;
flag1=0;
EA=1;
EX1=1;
IT1=1;
// write_ds(0x0A,0x20); // ds12c887首次设置
// write_ds(0x0B,0x26);
// set_time();
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<18;num++)
{
write_date(table1[num]);
delay(5);
}
}
void write_sfm(uchar add,uchar date)
{
char shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_wd(uchar add,uchar date)
{
char bai,shi,ge;
bai=date/100;
shi=date%100/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+bai);
write_date(0x30+shi);
write_date('.');
write_date(0x30+ge);
}
void write_nyr(uchar add,uchar date)
{
char shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_week(char week)
{
write_com(0x80+12);
switch(week)
{
case 1: write_date('M');delay(5);
write_date('O');delay(5);
write_date('N');
break;
case 2: write_date('T');delay(5);
write_date('U');delay(5);
write_date('E');
break;
case 3: write_date('W');delay(5);
write_date('E');delay(5);
write_date('D');
break;
case 4: write_date('T');delay(5);
write_date('H');delay(5);
write_date('U');
break;
case 5: write_date('F');delay(5);
write_date('R');delay(5);
write_date('I');
break;
case 6: write_date('S');delay(5);
write_date('A');delay(5);
write_date('T');
break;
case 7: write_date('S');delay(5);
write_date('U');delay(5);
write_date('N');
break;
}
}
void write_ds(uchar add,uchar date)
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add; //写地址
dsas=0;
dsrw=0;
P0=date; //写数据
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add)
{ //读12C887函数
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add;
dsas=0;
dsds=0;
P0=0xff;
ds_date=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
/*void set_time()
{
write_ds(0,0);
write_ds(1,0);
write_ds(2,0);
write_ds(3,0);
write_ds(4,0);
write_ds(5,0);
write_ds(6,1);
write_ds(7,1);
write_ds(8,1);
write_ds(9,8);
}*/
void read_alarm()
{
amiao=read_ds(1);
afen=read_ds(3);
ashi=read_ds(5);
}
void alarm_keyscan()
{
if(flag_ri==1)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
flag_ri=0;
}
}
}
}
void keyscan()
{
alarm_keyscan();
if(flag_ri==1)
{
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
delay(5);
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
while(!(s1&&s2&&s3&&s4));
flag_ri=0;
}
}
}
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
if(flag1==1)
{
if(s1num==4)//在闹钟按键按下时调闹钟时光标闪烁四次
s1num=1;
}
flag=1;
while(!s1);
didi();
switch(s1num)
{
case 1:write_com(0x80+0x40+9);
write_com(0x0f);
break;
case 2: write_com(0x80+0x40+6);
break;
case 3: write_com(0x80+0x40+3);
break;
case 4: write_com(0x80+12);
break;
case 5: write_com(0x80+9);
break;
case 6: write_com(0x80+6);
break;
case 7: write_com(0x80+3);
break;
case 8:s1num=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,week);
write_ds(7,day);
write_ds(8,month);
write_ds(9,year);
break;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
didi();
switch(s1num)
{
case 1: miao++;
if(miao==60)
miao=0;
write_sfm(9,miao);
write_com(0x80+0x40+9);//光标会原位置,会自动加1
break;
case 2: fen++;
if(fen==60)
fen=0;
write_sfm(6,fen);
write_com(0x80+0x40+6);
break;
case 3: shi++;
if(shi==24)
shi=0;
write_sfm(3,shi);
write_com(0x80+0x40+3);
break;
case 4: week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+12);
break;
case 5: day++;
if(day==32)
day=1;
write_nyr(9,day);
write_com(0x80+9);
break;
case 6: month++;
if(month==13)
month=1;
write_nyr(6,month);
write_com(0x80+6);
break;
case 7: year++;
if(year==100)
year=0;
write_nyr(3,year);
write_com(0x80+3);
break;
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
didi();
switch(s1num)
{
case 1: miao--;
if(miao==-1)
miao=59;
write_sfm(9,miao);
write_com(0x80+0x40+9);//光标会原位置,会自动加1
break;
case 2: fen--;
if(fen==-1)
fen=59;
write_sfm(6,fen);
write_com(0x80+0x40+6);
break;
case 3: shi--;
if(shi==-1)
shi=23;
write_sfm(3,shi);
write_com(0x80+0x40+3);
break;
case 4: week--;
if(week==0)
week=7;
write_week(week);
write_com(0x80+12);
break;
case 5: day--;
if(day==0)
day=31;
write_nyr(9,day);
write_com(0x80+9);
break;
case 6: month--;
if(month==0)
month=12;
write_nyr(6,month);
write_com(0x80+6);
break;
case 7: year--;
if(year==-1)
year=99;
write_nyr(3,year);
write_com(0x80+3);
break;
}
}
}
}
if((s4==0)||(flag_ri==1))
{
delay(5);
if((s4==0)||(flag_ri==1))
{
flag1=~flag1;
while(!s4);
didi();
if(flag1==0)//退出是保存闹钟
{
flag=0;
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);//恢复原来的年月日
delay(5);
}
/* write_com(0x80+0x40);
for(num=0;num<15;num++)
{
write_date(table1[num]);//原来年月日被覆盖了
delay(5);
} */
write_com(0x0c);
write_ds(1,miao);
write_ds(3,fen);
write_ds(5,shi);
}
else //进入闹钟设置
{
read_alarm();
miao=amiao;
fen=afen;
shi=ashi;
// write_com(0x80+0x40);
// write_date('R');
// write_date('i');
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table2[num]);//原来年月日被覆盖了
delay(5);
}
/* write_com(0x80+0x40);
for(num=0;num<15;num++)
{
write_date(table1[num]);//原来年月日被覆盖了
delay(5);
} */
write_com(0x80+0x40+3);
write_sfm(3,ashi);
write_sfm(6,afen);
write_sfm(9,amiao);
}
}
}
}
void Init_DS18B20()
{
uchar x=0;
DQ = 1; //DQ复位
Delay_DS18B20(8);//稍做延时
DQ = 0; //单片机将DQ拉低
Delay_DS18B20(80);//精确延时,大于480us
DQ = 1; //拉高总线
Delay_DS18B20(14);
x = DQ; //稍做延时后,如果x=0则初始化成功,x=1则初始化失败
Delay_DS18B20(20);
}
uchar ReadOneChar()
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay_DS18B20(4);
}
return(dat);
}
void WriteOneChar(uchar dat)
{
uchar i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay_DS18B20(5);
DQ = 1;
dat>>=1;
}
}
uint Readtemp()
{
uchar a=0;
uchar b=0;
uint t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x44); //启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5; //放大10倍输出并四舍五入
return(t);
}
void display(uint temp)
{
uchar A1,A2,A3;
temp=temp-30;
A1=temp/100;
A2=temp%100/10;
A3=temp%10;
write_com(0x80+0x40+12);
write_date(0x30+A1);//十位
write_date(0x30+A2);//个位
write_date(0x2e);//小数点
write_date(0x30+A3);//小数点后一位
// write_date(0xdf);//
// write_date(0x43);//C
}
void main()
{
Readtemp(); // 读温度
init();
while(1)
{
//temp=readtemp();
//write_wd(13,temp);
keyscan();
if(flag_ri==1)
{
play_music();
}
if(flag==0&&flag1==0) //有按键按下时不执行里面的,即时间停止
{
keyscan();
year=read_ds(9);
month=read_ds(8);
day=read_ds(7);
week=read_ds(6);
shi=read_ds(4);
fen=read_ds(2);
miao=read_ds(0);
write_sfm(9,miao);
write_sfm(6,fen);
write_sfm(3,shi);
write_week(week);
write_nyr(3,year);
write_nyr(6,month);
write_nyr(9,day);
display(Readtemp());//显示温度
}
}
}
void exter() interrupt 2
{
uchar c;
c=read_ds(0x0c);
flag_ri=1;
}
|