




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/带温度和时间显示程序/? C /neo/V1.1 2011-7-16/MCS-51 12MHZ C?/?: DS1302?DS18B20*/#include <reg52.h>#include <intrins.h>#define uint8 unsigned char#define uchar unsigned char#define uint unsigned intuchar Disp_Buff4;char SB=4; / clock set time bit /bit Set_Flag; / set time flagbit T=0; / display Te
2、mperature and Time flag bit TT=0; / 10S Auto change display Temperature and Time flag/#define T0_H 0xf0 / 4ms/#define T0_L 0x60#define T0_H 0xf5 / 2666us#define T0_L 0x96#define ST 20#define T1_H 0xb1 /20ms #define T1_L 0xe0#define NumLed_Bit P2#define Disp P0#define Key P3#define Key1 1#define Key2
3、 2#define Key3 3#define Key4 4#define Key_NULL 0sbit Beep=P07;void Play(uchar t);sbit Red_Led=P37;/ Driver DS1302/*/sbit RST=P12; sbit IO=P10; sbit SCLK=P11;#define second_write 0x80#define minute_write 0x82#define hour_write 0x84#define week_write 0x8a#define day_write 0x86#define month_write 0x88#
4、define year_write 0x8c#define second_read 0x81#define minute_read 0x83#define hour_read 0x85#define week_read 0x8b#define day_read 0x87#define month_read 0x89#define year_read 0x8d#define sclk_high SCLK=1;#define sclk_low SCLK=0;#define io_high IO=1;#define io_low IO=0;#define io_read IO#define rst_
5、high RST=1;#define rst_low RST=0;struct uint8 second; uint8 minute; uint8 hour; uint8 day; uint8 week;uint8 month;uint8 year; current_time;/*/static void ds1302write(uint8 content) uint8 i; for(i=8;i>0;i-) if(content&0x01) io_high else io_low content>>=1; sclk_high sclk_low /*/static ui
6、nt8 ds1302read(void) uint8 i,readvalue; io_high for(i=8;i>0;i-) readvalue>>=1; if(io_read) readvalue|=0x80; else readvalue&=0x7f;sclk_high sclk_low return readvalue; /*/void ds1302write_byte (uint8 address,uint8 content) rst_low sclk_low rst_high ds1302write(address); ds1302write(conten
7、t); rst_low sclk_highuint8 ds1302read_byte(uint8 address) uint8 readvalue; rst_low sclk_low rst_high ds1302write(address); readvalue=ds1302read(); rst_low sclk_high return readvalue; /*/void Clock_Init(void) uchar R; if(ds1302read_byte(0xc1)!=0xf0) ds1302write_byte(0x8e,0x00); ds1302write_byte(year_
8、write,0x11); ds1302write_byte(week_write,0x06); ds1302write_byte(month_write,0x04); ds1302write_byte(day_write,0x09); ds1302write_byte(hour_write,0x23); ds1302write_byte(minute_write,0x59); ds1302write_byte(second_write,0x30); ds1302write_byte(0x90,0xa5); ds1302write_byte(0xc0,0xf0); for (R=0;R<1
9、0;R+=2) ds1302write_byte(0xc2+R,0); ds1302write_byte(0x8e,0x80); /*/void Clock_Updata(void) current_time.second=ds1302read_byte(second_read); current_time.minute=ds1302read_byte(minute_read); current_time.hour= ds1302read_byte(hour_read); current_time.day= ds1302read_byte(day_read); current_time.mon
10、th= ds1302read_byte(month_read); current_time.week= ds1302read_byte(week_read); current_time.year= ds1302read_byte(year_read);/*/void Write_Time(void) ds1302write_byte(0x8e,0x00); ds1302write_byte(0x82,(Disp_Buff2<<4)|Disp_Buff3); ds1302write_byte(0x84,(Disp_Buff0<<4)|Disp_Buff1); ds1302
11、write_byte(0x8e,0x80); /*/void Write_Alarm(void) ds1302write_byte(0x8e,0x00); ds1302write_byte(0xca,(Disp_Buff2<<4)|Disp_Buff3); / alarm minute ds1302write_byte(0xc8,(Disp_Buff0<<4)|Disp_Buff1); / alarm hour ds1302write_byte(0xc6,1); ds1302write_byte(0x8e,0x80); /*/void Alarm(void) if(ds
12、1302read_byte(0xc7)=1&& current_time.minute=ds1302read_byte(0xcb)&¤t_time.hour=ds1302read_byte(0xc9) ) ds1302write_byte(0x8e,0x00); ds1302write_byte(0xc6,0); ds1302write_byte(0x8e,0x80); Play(3); /*/ DS18B20 driversbit io_DQ=P06;#define DQ_HIGH io_DQ=1;#define DQ_LOW io_DQ=0;#d
13、efine DQ_READ io_DQunsigned char Temperature4; void Delay_10Us (unsigned int Count) while(-Count) _nop_(); /*/ unsigned char Ds18b20_Init(void) unsigned char Flag; DQ_HIGH Delay_10Us(3); DQ_LOW Delay_10Us(80); DQ_HIGH Delay_10Us(15); Flag=DQ_READ; return Flag; /*/ void Write_f(unsigned char cmd) uns
14、igned char i; for(i=8;i>0;i-) EA=0; DQ_LOW DQ_READ=cmd&0x01; Delay_10Us(5); DQ_HIGH EA=1; cmd>>=1; /*/ unsigned char Read_f(void) unsigned char Read_Value,i; for (i=8;i>0;i-) EA=0; DQ_LOW Read_Value>>=1; DQ_HIGH if (DQ_READ=1) Read_Value|=0x80; EA=1; Delay_10Us(3); return Read_
15、Value; /*/ unsigned int Read_Temp(void) unsigned char Temp_H, Temp_L; unsigned int Return_Temp; EA=0; / close interrupt; Ds18b20_Init(); Write_f(0xcc); Write_f(0x44); Ds18b20_Init(); Write_f(0xcc); Write_f(0xbe); Temp_L=Read_f(); Temp_H=Read_f(); EA=1; Return_Temp=Temp_H; Return_Temp<<=8; Retu
16、rn_Temp|=Temp_L; return Return_Temp; /*/ void Temperature_Update(void) unsigned char T_flag=0; unsigned int Temp_Dat; float Temp; Temp_Dat=Read_Temp(); if(Temp_Dat&0xf000) T_flag=1; Temp_Dat=Temp_Dat+1; Temp=Temp_Dat*0.0625; Temp_Dat=Temp*10; Temperature0=T_flag; Temperature1=Temp_Dat/100; Tempe
17、rature2=Temp_Dat%100/10; Temperature3=Temp_Dat%10; /*/void Timer0_Init(void) TMOD|=0x01; TH0=T0_H; TL0=T0_L; EA=1; ET0=1; TR0=1;/*/bit T_10S,T_1S;bit S;/ shining flagbit pp=1; / open or close :unsigned int ms1,ms2,ms3;uchar Bit;/ Number Led select uchar code Disp_NumCode=0x3f,0x06,0x5b,0x4f, 0x66,0x
18、6d,0x7d,0x07, 0x7f,0x6f, / dp-a P07-P00 0-9, 0x39, /c10 0x37,0x79,0x3f, /NEO 11-13 0x6d,0x39,0xf7, / S,C,A 14-16 0x7f,0x73,0x71, / d,p,F 17-19 0x00, / " " 20 0xc7,0xf7 / L 21 _ 22 ; /*/uchar code Open_Logo=11,12,13,20;uchar code S_C=14,22,15,20;uchar code S_A=14,22,16,20;uchar code d_p=17,
19、22,18,20;uchar code BELL=17,12,21,21;uchar code ON= 20,13,11,20;uchar code OFF=20,13,19,19; void Put_In(uchar *P) uchar a; for (a=0;a<4;a+) Disp_Buffa=Pa; /*/void Timer0_Isr(void) interrupt 1 TH0=T0_H; /4ms TL0=T0_L; Disp=0x00; /* switch(Bit) case 0 : NumLed_Bit=0xfe; break; case 1 :NumLed_Bit=0x
20、fd; break; case 2 :NumLed_Bit=0xfb; break; case 3 :NumLed_Bit=0xf7; break; case 4 : NumLed_Bit=0xf3; break; / display : default : break; */ switch(Bit) case 0 : if(SB=0) if (S) NumLed_Bit=0xfe; /如共阳是0X01 以下都是要改 if(S=0) NumLed_Bit=0xff; /0X00 else NumLed_Bit=0xfe;/0X01 break; case 1 : if(SB=1) if (S)
21、 NumLed_Bit=0xfd; /0X02 if(S=0) NumLed_Bit=0xff; /0X00 else NumLed_Bit=0xfd; / 0X02 break; case 2 : if(SB=2) if (S) NumLed_Bit=0xfb; /0X04 if(S=0) NumLed_Bit=0xff; /0X00 else NumLed_Bit=0xfb; /0X04 break; case 3 : if(SB=3) if (S) NumLed_Bit=0xf7; /0X08 if(S=0) NumLed_Bit=0xff; /0X00 else NumLed_Bit=
22、0xf7; /0X08 break; case 4 : if(T=0|TT=0) NumLed_Bit=0xf3; / display : 0X0C if( T | TT ) NumLed_Bit=0xfb; / display .0X04 break; default : break; if(Bit<4) Disp=Disp_NumCodeDisp_BuffBit; else if(pp=1) if (T=0|TT=0) if(current_time.second%2) Disp=0x80; else Disp=0x00; if( T | TT ) Disp=0x80; else D
23、isp=0x00; / close : if(+Bit>4) Bit=0; /*/void Timer1_Init(void) TMOD|=0x10; TH1=T1_H; /20ms TL1=T1_L; EA=1; ET1=1; TR1=1; /*/uchar Key_Time;void Timer1_Isr(void) interrupt 3 TH1=T1_H; TL1=T1_L; if(Key!=0xff) Key_Time+; if(+ms1>ST) ms1=0; S=S; /when set time Shiningbit if(+ms2>=50) ms2=0; T_
24、1S=1; if(+ms3>500) ms3=0; T_10S=1; /*/void DelayM(unsigned int a)/-? 1MS/?unsigned char i;while(a-) for(i = 0; i < 125; i+) /i ?0?125,CPU?1?/*/void Play(uchar t) uchar i; switch(t) case 1: Beep=0; DelayM(100); break; case 2: Beep=0;DelayM(400); Beep=1; DelayM(500);Beep=0;DelayM(100); break; ca
25、se 3: for(i=0;i<20;i+)Beep=0;DelayM(100); Beep=1;DelayM(100);Beep=0;DelayM(100);Beep=1; DelayM(500); break; default : break; Beep=1; /*/ uchar Key_Scan(void) static bit Key_Down; static uchar K; uchar K_Value=0; if(Key_Time>250) Key_Time=3; if(Key_Time>=3&&Key_Down=0) Key_Time=0; Ke
26、y_Down=1; switch(Key) case 0xfe: K=1; break; case 0xfd: K=2; break; case 0xfb: K=3; break; case 0xf7: K=4; break; default : K=0; break; if(Key=0xff&&Key_Down=1) Key_Down=0;K_Value=K; Play(1); return K_Value; /*/void Disp_Time(void) Disp_Buff3=current_time.minute%16; Disp_Buff2=current_time.m
27、inute/16; Disp_Buff1=current_time.hour%16; Disp_Buff0=current_time.hour/16; /*/void Disp_Temperature_3bit(void) Disp_Buff3=20; Disp_Buff2=10; / display 'c' Disp_Buff1=Temperature2; Disp_Buff0=Temperature1; /*void Disp_Temperature_4bit(void) Disp_Buff3=Temperature3; Disp_Buff2=10; / display c
28、 Disp_Buff1=Temperature2; Disp_Buff0=Temperature1; */uchar Select_M=4;void MENU (void) uchar K; char Menu=0; pp=0; / close ':' while(Menu!=4) K=Key_Scan(); switch (Menu) case 0 : Put_In(S_C); break; case 1 : Put_In(S_A); break; case 2 : Put_In(d_p); break; case 3 : Put_In(BELL); default : br
29、eak; if(K=Key3) Menu=4; if(K=Key2) Menu=(Menu+1)%4; if(K=Key1) Select_M=Menu; Menu=4; pp=1; /*/void Set_Time(void) bit Set_Flag=1; / set time flag uchar K; SB=0; Disp_Time(); T=0; TT=0; while(Set_Flag) K=Key_Scan();if(K=Key1) switch(SB) case 0 : if(Disp_Buff1>3) Disp_Buff0=(Disp_Buff0+1)%2; else
30、Disp_Buff0=(Disp_Buff0+1)%3; break; case 1 : if(Disp_Buff0=2) Disp_Buff1=(Disp_Buff1+1)%4;else Disp_Buff1=(Disp_Buff1+1)%10; break; case 2 : Disp_Buff2=(Disp_Buff2+1)%6; break; case 3 : Disp_Buff3=(Disp_Buff3+1)%10; break; default : break; if(K=Key2) SB= (SB+1)%4; if(K=Key3) Set_Flag=0; SB=4; if(K=K
31、ey4) Set_Flag=0; SB=4; Write_Time(); T_10S=0; /*/void Write_Ds1302Ram(uchar address,uchar dat) ds1302write_byte(0x8e,0x00); ds1302write_byte(address,dat); ds1302write_byte(0x8e,0x80); /*/uchar dp; / display modle select bitvoid Set_Dp(void)bit dp_Set=1; uchar K;pp=0; / close ':'/dp=ds1302read_byte(0xc3); while(dp_Set) K=Key_Scan(); switch(dp) case 1 : Put_In(ON); break; case 0 : Put_In(OFF); break; default : break; if(K=Key3) dp_Set=0; if(K=Key2) dp=(dp+1)%2; if(K=Key
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年江苏省农村土地承包经营权流转合同范本
- 2025实验室装修合同范本
- 2025精简版装修施工合同样本
- 代付款三方协议书范本
- 海口市房屋租赁合同书
- 保险车辆维修协议书
- 道路硬化捐赠协议书
- 2025年03月河南安阳市事业单位招才引智“绿色通道”引进129人笔试历年典型考题(历年真题考点)解题思路附带答案详解
- 生物试题2025年东北三省四城市联考暨沈阳市高三质量监测(二)及答案
- 广西警察学院《药理学A(实验)》2023-2024学年第二学期期末试卷
- 成语故事-闻鸡起舞-课件
- 杭州市市属事业单位招聘真题2024
- DEEPSEEK了解及使用攻略高效使用技巧培训课件
- 2024年河北衡水冀州区招聘社区工作者考试真题
- 2025年心理b证笔试试题及答案
- 2024-2025学年人教版七下地理第一单元测验卷
- 玩具的创业计划书
- 共价键+周测卷 高二下学期化学选择性必修2
- 麻疹知识培训课件
- 精神科护理学焦虑障碍
- 程序员个人职业生涯规划
评论
0/150
提交评论