本文引用地址:http://m.butianyuan.cn/article/201611/321018.htm#include
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit s1=P3^0;//功能鍵選擇
sbit s2=P3^1;//數(shù)值加1
sbit s3=P3^2;//數(shù)值減1
sbit s4=P3^6;//鬧鐘查看設(shè)置鍵
sbit rd=P3^7;//
sbit beep=P2^3;//蜂鳴器
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dsirq=P3^3;
sbit DQ=P2^2; //溫度數(shù)據(jù)通信線
uint temp,tplsb,tpmsb; //溫度
uchar s1num,s4num,flag,flag1;
uchar miao,shi,fen,week,ri,yue,nian,amiao,afen,ashi;
uchar code table1[]=" - - ";
uchar code table2[]=" : : ";
// 周一周二 周三 周四周五 周六周日
uchar code table3[]={0xff,0x4d,0x54,0x57,0x54,0x46,0x53,0x53};
// M T W T F S S
uchar code table4[]={0xff,0x4f,0x55,0x45,0x48,0x52,0x41,0x55};
// O U E H R A U
uchar code table5[]={0xff,0x4e,0x45,0x44,0x55,0x49,0x54,0x4e};
// N E D U I T N
void write_ds(uchar,uchar);
void set_alarm(uchar,uchar,uchar);
uchar read_ds(uchar);
//void set_time();
void read_alarm();
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Delay_DS18B20(uint num)
{
while(num--) ;
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
delay(3);
lcden=1;
delay(3);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay(3);
lcden=1;
delay(3);
lcden=0;
}
void init()
{
uchar num;
EA=1; //打開總中斷
EX1=1;//開定時(shí)器0中斷
IT1=1;//
flag1=0;//鬧鐘設(shè)置,0為不響
s1num=0;
dula=0;//關(guān)閉數(shù)碼管
wela=0;//關(guān)閉數(shù)碼管
lcden=0;
rd=0;
//set_time();
read_alarm();
write_ds(0x0a,0x20);//時(shí)鐘芯片寄存器A
write_ds(0x0b,0x26);//時(shí)鐘芯片寄存器B
//read_ds(0x0c); //時(shí)鐘芯片寄存器C
write_com(0x38); //液晶顯示模式
write_com(0x0c); //液晶顯示、開關(guān)、光標(biāo)打開
write_com(0x06); //液晶地址指針及光標(biāo)移動(dòng)情況
write_com(0x01); //清屏
write_com(0x80); //屏幕第一行
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(1);
}
write_com(0x80+0x40);//屏幕第二行
for(num=0;num<16;num++)
{
write_date(table2[num]);
delay(1);
}
}
void Init_DS18B20()
{
uchar x=0;
DQ = 1; //DQ復(fù)位
Delay_DS18B20(8);//稍做延時(shí)
DQ = 0; //單片機(jī)將DQ拉低
Delay_DS18B20(80);//精確延時(shí),大于480us
DQ = 1; //拉高總線
Delay_DS18B20(14);
x = DQ; //稍做延時(shí)后,如果x=0則初始化成功,x=1則初始化失敗
Delay_DS18B20(20);
}
uchar ReadOneChar()
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 給脈沖信號(hào)
dat>>=1;
DQ = 1; // 給脈沖信號(hào)
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;
}
}
評(píng)論