版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、#include"reg52.h"#include <intrins.h>#define uint unsigned int#define ulong unsigned long#define uchar unsigned char#define IF 10700sbit K1=P32; /功能键sbit K2=P33; /移位按键sbit K3=P34; /调整加按键sbit K4=P35; /调整减按键sbit scl_1302 = P10;sbit sda_1302 = P11;sbit rst_1302 = P12;sbit LE=P15;/11脚sbi
2、t DAT=P14;/10脚sbit SCLK=P13;/9脚uint temp=0;uint R=320;/晶振8.00MHz,参考频率fr=25kHzuchar const LED=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, 0x88,0x83,0xc6,0xa1,0x86,0x8e, /数码管显示代码:0-9,a-f 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03/*加小数点 0 1 2 3 4 ,*/ /* 5 6 7 8 9 */ ;void delay_
3、ms(void) /延时X毫秒函数 uchar i; for(i=0; i<200;i+)_nop_();/机器延时 1us_nop_();_nop_();_nop_();void Delay_x10us(uint i) /i*10 us(17+26*i)us(8051_11.0592M) while(i-);/*1302变量定义*/uchar Time_1302 = 0,0x14,0x11,0x19,0x0A,0x01,0x0A;/初始化时钟设置(0秒,20分,17时,25日,10月,星期1,10年)char sec,min,hou,day,week,mon,year; /时钟变量uc
4、har time_data_13027; /存储时间数据/*BCD码转二进制*/uchar turn_from_BCD(uchar BCD)uchar temp; temp = (BCD>>4)&0X0f)%10)*10)+(BCD&0x0f)%10);return(temp);/*二进制转BCD码*/uchar turn_to_BCD(uchar BCD)uchar temp;temp = (BCD/10)<<4)&0xF0)|(BCD%10)&0x0F);return(temp);/*写1302地址和数据*/void write_13
5、02(uchar add_1302,data_to_1302)uchar i;rst_1302 = 0;rst_1302 = 1; /读写时序时rst_1302保持高电平scl_1302 = 0;for(i=0;i<8;i+)sda_1302 = (bit)(add_1302&0x01); Delay_x10us(1);scl_1302 = 1;Delay_x10us(1);add_1302>>=1;scl_1302 = 0;for(i=0;i<8;i+) /写数据 sda_1302 = (bit)(data_to_1302&0x01); Delay_x
6、10us(1);scl_1302 = 1;Delay_x10us(1);data_to_1302>>=1;scl_1302 = 0;rst_1302 = 0;/*读1302某地址数据*/uchar read_1302(uchar add_1302)uchar i;uchar data_from_1302;rst_1302 = 0;rst_1302 = 1; /读写时序时rst_1302保持高电平for(i=0;i<8;i+) /写入地址scl_1302 = 0;sda_1302 = (bit)(add_1302&0x01); Delay_x10us(1);scl_13
7、02 = 1;Delay_x10us(1);add_1302>>=1;for(i=0;i<8;i+) /读数据scl_1302 = 0;data_from_1302>>=1;if(sda_1302=1)data_from_1302|=0x80;scl_1302 = 1;Delay_x10us(1);scl_1302 = 0;rst_1302 = 0;return(data_from_1302);/*启动或停止1302(TEMP=1时启动)*/void star_stop_1302(uchar temp) /8eH的最高位决定是否启动1302if(temp=1)wr
8、ite_1302(0x82,0x00); /启动1302elsewrite_1302(0x82,0x80); /停止1302/*初始化1302(设置时间*/void Init_ds1302() uchar i; star_stop_1302(1); for(i=0;i<7;i+) write_1302(0x80+2*i,turn_to_BCD(Time_1302i); /*读1302时间数据*/void read_time_1302()uchar i;for(i=0;i<7;i+) /取出时间数据(BCD码)time_data_1302i = read_1302(0x81+i*2)
9、; sec = turn_from_BCD(time_data_13020); /BCD码转二进制码min = turn_from_BCD(time_data_13021);hou = turn_from_BCD(time_data_13022);day = turn_from_BCD(time_data_13023);mon = turn_from_BCD(time_data_13024);week = turn_from_BCD(time_data_13025);year = turn_from_BCD(time_data_13026);void display(uchar add,uch
10、ar con) /显示子程序 P2=add; P0=LEDcon; delay_ms();/*时间显示子程序*/*显示时间和星期几*/void display_time(void) uchar j=0xfe; read_time_1302(); display(j,hou/10); j=_crol_ (j, 1); display(j,hou%10+16); j=_crol_ (j, 1); display(j,min/10); j=_crol_ (j, 1); display(j,min%10+16); j=_crol_ (j, 1); display(j,sec/10); j=_crol_
11、 (j, 1); display(j,sec%10); j=_crol_ (j, 2); display(j,week); display(0xff,0);/*日期显示子程序*/void display_date(void ) uchar j=0xfe; read_time_1302(); display(j,year/10); j=_crol_ (j, 1); display(j,year%10); j=_crol_ (j, 1); P2=j; P0=0Xf7; delay_ms();j=_crol_ (j, 1); display(j,mon/10); j=_crol_ (j, 1); d
12、isplay(j,mon%10); j=_crol_ (j, 1); P2=j; P0=0Xf7; delay_ms(); j=_crol_ (j, 1); display(j,day/10); j=_crol_ (j, 1); display(j,day%10); display(0xff,0); void display_Freq(ulong Freq) /显示给定的频率 uchar j=0xef; display(0xfb,15); display(j,Freq/100000); Freq%=100000; j=_crol_ (j, 1); display(j,Freq/10000);
13、Freq%=10000; j=_crol_ (j, 1); display(j,Freq/1000+16); Freq%=1000; j=_crol_ (j, 1); display(j,Freq/100); display(0xff,0); void write_R(void) uchar i; LE=0;_nop_(); DAT=0; _nop_(); SCLK=0;_nop_();_nop_();_nop_(); DAT=1;_nop_();_nop_();_nop_();/SW位,前置分频,0为64/65,1为32/33 SCLK=1;_nop_();_nop_();_nop_();
14、SCLK=0;_nop_(); temp=R;_nop_(); for(i=0;i<14;i+) if(temp&0x2000) DAT=1; /写R分频系数共14位,先写高位 else DAT=0;_nop_();_nop_();_nop_();SCLK=1;_nop_();_nop_();_nop_();_nop_();SCLK=0;_nop_();_nop_();temp=temp<<1; DAT=1;_nop_();_nop_();_nop_();/控制位C,1时写入R寄存器SCLK=1;_nop_();_nop_();_nop_();SCLK=0;_nop_
15、();_nop_();_nop_();_nop_();_nop_(); LE=1;_nop_();_nop_();_nop_();_nop_();_nop_(); /使能信号,上升沿数据打入对应寄/存器LE=0;_nop_();_nop_(); /ENB=0; void write_NA(uint N,uint A) uchar i; LE=0;_nop_(); DAT=0; _nop_(); SCLK=0;_nop_(); temp=N; _nop_(); for(i=0;i<11;i+) /写N,共11位 if(temp&0x400) DAT=1; else DAT=0;_n
16、op_();_nop_();_nop_();SCLK=1;_nop_();_nop_();_nop_();SCLK=0;_nop_();_nop_();temp=temp<<1; _nop_(); temp=A; for(i=0;i<7;i+) /写A,共7位 if(temp&0x40) DAT=1; else DAT=0;_nop_();_nop_();_nop_();SCLK=1;_nop_();_nop_();_nop_();SCLK=0;_nop_();_nop_();temp=temp<<1; DAT=0;_nop_();_nop_();_nop
17、_();/控制为C,0时数据写入NA寄存器SCLK=1;_nop_();_nop_();_nop_();SCLK=0;_nop_();_nop_();_nop_();_nop_(); LE=1;_nop_();_nop_();_nop_();_nop_(); /使能信号,上升沿数据打入对应寄存器LE=0;_nop_();_nop_(); DAT=1;_nop_(); void Write_MB1504(ulong Frequ) uint Total_Counter=(uint)(Frequ+IF)/25); uint Prog_Counter=Total_Counter/32; uint Sw
18、al_Counter=Total_Counter%32; write_R(); write_NA(Prog_Counter,Swal_Counter);void main(void) ulong Freq=97100; uchar k_dis=0; /显示状态变量 uchar k_crol=0; /调整移位变量 Init_ds1302(); /DS1032初始化 Write_MB1504(Freq); while(1) if(!K1) Delay_x10us(5); k_dis+=1; k_crol=0; while(k_dis=3) k_dis=0; while(!K1); Delay_x1
19、0us(5); if(k_dis=0) /显示频率 display_Freq(Freq); if(!K2) Delay_x10us(5); k_crol+=1;while(k_crol=4)k_crol=0;while(!K2); Delay_x10us(5); if(k_crol=1) /可调整频率是十位 if(!K3) Delay_x10us(5); Freq+=10000; while(Freq>110000) Freq=110000; Write_MB1504( Freq); while(!K3); Delay_x10us(5); if(!K4) Delay_x10us(5);
20、Freq-=10000; while(Freq<86000) Freq=86000; Write_MB1504( Freq); while(!K4); Delay_x10us(5); if(k_crol=2) if(!K3) Delay_x10us(5); Freq+=1000; while(Freq>110000) Freq=110000; Write_MB1504( Freq); while(!K3); Delay_x10us(5); if(!K4) Delay_x10us(5); Freq-=1000; while(Freq<86000) Freq=86000; Wri
21、te_MB1504( Freq); while(!K4); Delay_x10us(5); if(k_crol=3) if(!K3) Delay_x10us(5); Freq+=100; while(Freq>110000) Freq=110000; Write_MB1504( Freq); while(!K3); Delay_x10us(5); if(!K4) Delay_x10us(5); Freq-=100; while(Freq<86000) Freq=86000; Write_MB1504( Freq); while(!K4); Delay_x10us(5); if(k_
22、dis=1) /显示时钟和星期 display_time(); if(!K2) Delay_x10us(5); k_crol+=1;while(k_crol=4)k_crol=0;while(!K2); Delay_x10us(5); if(k_crol=1) /调整小时 if(!K3) Delay_x10us(5); Time_13022+=1; while(Time_13022=24) Time_13022=0; while(!K3); Delay_x10us(5); if(!K4) Delay_x10us(5); hou-=1; while(hou<0) hou=23; Time_
23、13022=hou; while(!K4); Delay_x10us(5); Init_ds1302(); if(k_crol=2) /调整分钟 if(!K3) Delay_x10us(5); min+=1; while(min=60) min=0; Time_13021=min; while(!K3); Delay_x10us(5); if(!K4) Delay_x10us(5); min-=1; while(min<0) min=59; Time_13021=min; while(!K4); Delay_x10us(5); Init_ds1302(); if(k_crol=3) /调整星期 if(!K3) Delay_x10us(5); week+=1; while(week=8) week=1; Time_13025=week; while(!K3); Delay_x10us(5); if(!K4) Delay_x10us(5); week-=1; while(week=0) week=7; Time_13025=we
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 颐养之家消防安全制度
- 土地一级开发财务制度
- 小食品加工厂财务制度
- 医保基金财务制度
- 企业财务制度及编制流程
- 幼儿园财务制度实施细则
- 养羊公司财务制度
- 煤矿托管财务制度范本
- 抖音运营财务制度
- 2026新疆恒海国有资产经营有限公司招聘7人备考题库及答案详解(新)
- 2026年益阳医学高等专科学校单招职业技能笔试参考题库含答案解析
- 国家自然基金形式审查培训
- 2026马年卡通特色期末评语(45条)
- NCCN临床实践指南:肝细胞癌(2025.v1)
- 免租使用协议书
- 2025 AHA心肺复苏与心血管急救指南
- 2026年九江职业大学单招职业适应性测试题库带答案详解
- 危化品库区风险动态评估-洞察与解读
- 激光焊接技术规范
- 消防联动排烟天窗施工方案
- 二手房提前交房协议书
评论
0/150
提交评论