《基于单片机DS1302的时钟万年历(带闹钟).doc》由会员分享,可在线阅读,更多相关《基于单片机DS1302的时钟万年历(带闹钟).doc(29页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、.#include#define uchar unsigned char #define uint unsigned intsbit lcdws=P21; /1602管脚定义sbit lcden=P20;sbit lcdrs=P22;sbit key1=P23; /闹钟设置键 sbit key2=P24; sbit key3=P15;/功能键定义sbit key4=P16;/增大键定义sbit key5=P17;/减小键定义uchar shi,fen,miao,ashi,afen,amiao,year,month,day,week;uchar s1num=0,s1num2=0,temp;bit
2、 flag; float f_temp;uint i,d;uchar code table2=0x20,0x20,0x2e,0x20,0xdf,0x43;uchar code table=20 - - ;uchar code table1= : : ;sbit ds=P10;/ds18b20信号线sbit beep=P14; /蜂鸣器管脚定义sbit ACC_7 = ACC7; /位寻址寄存器定义sbit SCLK = P12; / DS1302时钟信号 sbit DIO= P11; / DS1302数据信号 sbit CE = P13;/地址、数据发送子程序 /void delay2(uin
3、t ms)/ / uchar t;/ while(ms-); for(t=0;t120;t+); / void playmusic()/ / uint i2=0,j2,k2;/ while(jiepaii2!=0|songi2!=0)/ / for(j2=0;j2jiepaii2*20;j2+)/ / beep=beep;/ for(k2=0;k20; i- ) /循环8次移位 SCLK = 0; temp = addr; DIO = (bit)(temp&0x01); /每次传输低字节 addr = 1; /右移一位 SCLK = 1; /发送数据 for ( i=8; i0; i- ) S
4、CLK = 0; temp = dat; DIO = (bit)(temp&0x01); dat = 1; SCLK = 1; CE = 0; /数据读取子程序 uchar Read1302 ( uchar addr ) unsigned char i,temp,dat1,dat2; CE=0; SCLK=0; CE = 1; /发送地址 for ( i=8; i0; i- ) /循环8次移位 SCLK = 0; temp = addr; DIO = (bit)(temp&0x01); /每次传输低字节 addr = 1; /右移一位 SCLK = 1; /读取数据 for ( i=8; i0
5、; i- ) ACC_7=DIO; SCLK = 1; ACC=1; SCLK = 0; CE=0; dat1=ACC; dat2=dat1/16; /数据进制转换 /十六进制转十进制 dat1=dat1%16; dat1=dat1+dat2*10; return (dat1); /初始化 DS1302 void Initial(void) /初始化1302 2010年09月30日23点58分56秒 星期一 write_1302 (0x8e,0x00); /允许写保护 write_1302 (0x80,0x56); /秒位初始化 write_1302 (0x82,0x58); /分钟初始化 w
6、rite_1302 (0x84,0x23); /小时初始化 write_1302 (0x86,0x30); /日初始化 write_1302 (0x88,0x09); /月初始化 write_1302 (0x8c,0x10); /年初始化 write_1302 (0x8a,0x01); /日期始化 write_1302 (0x8e,0x80); /禁止写保护void delay(uint z)/延时1msuint x,y;for(x=z;x0;x-)for(y=110;y0;y-);void write_(uchar )/1602写命令lcdrs=0;lcden=0;P0=;delay(5);
7、lcden=1;delay(5);lcden=0;void write_data(uchar date)/1602写数据lcdrs=1;lcden=0;P0=date;delay(5);lcden=1;delay(5);lcden=0;void dsreset(void) /DS18B20,初始化函数uint i;ds=0;i=103;while(i0)i-;ds=1;i=4;while(i0)i-;bit tempreadbit(void)/读1位数据函数uint i;bit dat;ds=0;i+;ds=1;i+;i+;/i+起到延时作用dat=ds;i=8;while(i0)i-;ret
8、urn(dat);uchar tempread(void) /读一个字节数据函数uchar i,j,dat;dat=0;for(i=1;i=8;i+)j=tempreadbit();dat=(j1);return(dat);void tempwritebyte(uchar dat) /向DS18B20写一个字节数据函数uint i;uchar j;bit testb;for(j=1;j1;if(testb)/写1ds=0;i+;i+;ds=1;i=8;while(i0)i-;elseds=0; /写0i=8;while(i0)i-;ds=1;i+;i+;void tempchange(void
9、) /DS18B20开始获取温度并转换dsreset();delay(1);tempwritebyte(0xcc);tempwritebyte(0x44);uint get_temp() /获取温度uchar a,b;dsreset();delay(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread(); /读高8位b=tempread(); /读低8位temp=b;temp=8; /两个字节组合成1个字节temp=temp|a;f_temp=temp*0.0625;temp=f_temp*10+0.5;f_temp=f_temp+0.0
10、5; /温度在寄存器中为12位,分辨率为0.0625return temp;void display(uchar addr,uchar dat)write_(0x80+0x40+addr);write_data(0x30+dat);delay(5);void dis_temp(uint t)uchar i;i=t/100;display(10,i);i=t%100/10;display(11,i);i=t%100%10;display(13,i);void init()/初始化程序uchar num;flag=1;lcden=0;write_(0x38);write_(0x0c);write_
11、(0x06);write_(0x01);write_(0x80);for(num=0;num15;num+)write_data(tablenum);delay(5);write_(0x80+0x40);for(num=0;num8;num+)write_data(table1num);delay(5);write_(0x80+0x40+10);for(num=0;num0;i-) dis_temp(get_temp(); while(1)keyscan();/按键扫描/while(key1&key2&key3&key4&key5)&(fen=afen)&(shi=ashi)/ / beep=
12、0;/ delay(500);/beep=1;/ if(flag=1)miao=Read1302(0x81);/显示秒write_sfm(6,miao);fen=Read1302(0x83);/显示分write_sfm(3,fen);shi=Read1302(0x85);/显示小时write_sfm(0,shi);day=Read1302(0x87);/显示日 write_nyrz(8,day);month=Read1302(0x89);/显示月write_nyrz(5,month);year=Read1302(0x8d);/显示年write_nyrz(2,year);week=Read1302(0x8b);/显示日期display_week(week); elsewrite_1302(0x80,0x80);/时钟振荡器停止 set_alarm();29 / 29