十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
//给你发一个数码管输出的可调节时钟的程序,DS1302.H在最后
你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事网站设计、成都网站制作、企业网站建设、手机网站开发、网页设计、高端网站设计、网页制作、做网站、建网站。创新互联拥有实力坚强的技术研发团队及素养的视觉设计专才。
#include at89x52.h
#include DS1302.h
//定义共阳极字型码0123456789-
unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
unsigned char time[]={0,0,0,0,0,0,0,0};//用来储存时间
unsigned char date[]={0,0,0,0,0,0,0,0};//用来储存日期
SYSTEMTIME time1; //在ds1302.h文件中已经定义了一个名字为SYSTEMTIME的结构体,在这里我们定义一个变量名
//为time的SYSTEMTIME结构体
void delay(unsigned char i) //延时子程序
{
unsigned char j;
while((i--)!=0)
{
for(j=123;j0;j--);
}
}
unsigned char button_time(n,x,y) //时钟调整子程序
unsigned char n,x,y;
{
if(P1_7==0)
{
delay(50);
if(P1_7==0)
{
n++;
if(n==x)
n=0;
while(P1_7==0);
}
}
if(P1_5==0)
{
delay(50);
if(P1_5==0)
{
if(n==0)
n=y;
else
n--;
while(P1_5==0);
}
}
return n;
}
unsigned char button_date(n,x,y) //日期调整子程序
unsigned char n,x,y;
{
if(P1_7==0)
{
delay(50);
if(P1_7==0)
{
n++;
if(n==x)
n=1;
while(P1_7==0);
}
}
if(P1_5==0)
{
delay(50);
if(P1_5==0)
{
if(n==1)
n=y;
else
n--;
while(P1_5==0);
}
}
return n;
}
void display(second10,second1,minute10,minute1,hour10,hour1) //显示子程序
unsigned char second10,second1,minute10,minute1,hour10,hour1;
{
P2=0xfe;
P0=dispcode[second10];//显示秒的十位
delay(1);
P2=0xfd;
P0=dispcode[second1]; //显示秒的个位
delay(1);
P2=0xfb;
P0=dispcode[10]; //显示"-"
delay(1);
P2=0xf7;
P0=dispcode[minute10];//显示分的十位
delay(1);
P2=0xef;
P0=dispcode[minute1]; //显示分的个位
delay(1);
P2=0xdf;
P0=dispcode[10]; //显示"-"
delay(1);
P2=0xbf;
P0=dispcode[hour10]; //显示时的十位
delay(1);
P2=0x7f;
P0=dispcode[hour1]; //显示时的个位
delay(1);
}
void main()
{
unsigned char flag;
Initial_DS1302(); //初始化DS1302这个时钟芯片,
while(1)
{
DS1302_GetTime(time1); //读取时间参数
time[5]=(time1.Second)%10; //把秒的个位数据存入time[5]
time[4]=(time1.Second)/10; //把秒的十位数据存入time[4]
time[3]=(time1.Minute)%10; //把分的个位数据存入time[3]
time[2]=(time1.Minute)/10; //把分的十位数据存入time[2]
time[1]=(time1.Hour)%10; //把时的个位数据存入time[1]
time[0]=(time1.Hour)/10; //把时的十位数据存入time[0]
date[5]=(time1.Day)%10;
date[4]=(time1.Day)/10;
date[3]=(time1.Month)%10;
date[2]=(time1.Month)/10;
date[1]=(time1.Year)%10;
date[0]=(time1.Year)/10;
if(P1_4==0) //如果按下Time Start键一下,时钟开始正常显示时间,再按一下,显示日期
{
delay(50);
if(P1_4==0)
{
flag++;
if(flag1)
{
flag=0;
}
}
while(P1_4==0);
}
if(P1_6==0) //如果按下Time Set键一下,开始显示日期,再按一下进入日期跟时钟的调节模式
{
delay(50);
if(P1_6==0)
{
flag++;
if(flag7)
{
flag=0;
}
}
while(P1_6==0);
}
switch(flag)
{
case 0:display(time[0],time[1],time[2],time[3],time[4],time[5]); //调用子函数display,把存入数组time的数据给显示出来
break;
case 1:display(date[0],date[1],date[2],date[3],date[4],date[5]); //调用子函数display,把存入数组date的数据给显示出来
break;
case 2:time1.Year=button_date(time1.Year,100,99); //调整年
DS1302_SetTime(0x8c,time1.Year);
display(date[0],date[1],10,10,10,10);
break;
case 3:time1.Month=button_date(time1.Month,13,12); //调整月
DS1302_SetTime(0x88,time1.Month);
display(10,10,date[2],date[3],10,10);
break;
case 4:time1.Day=button_date(time1.Day,32,31); //调整日
DS1302_SetTime(0x86,time1.Day);
display(10,10,10,10,date[4],date[5]);
break;
case 5:time1.Hour=button_time(time1.Hour,24,23); //调整时
DS1302_SetTime(0x84,time1.Hour);
display(time[0],time[1],10,10,10,10);
break;
case 6:time1.Minute=button_time(time1.Minute,60,59); //调整分
DS1302_SetTime(0x82,time1.Minute);
display(10,10,time[2],time[3],10,10);
break;
case 7:time1.Second=button_time(time1.Second,60,59); //调整秒
DS1302_SetTime(0x80,time1.Second);
display(10,10,10,10,time[4],time[5]);
break;
}
}
}
//DS1302.H
#ifndef _REAL_TIMER_DS1302_2003_7_21_
#define _REAL_TIMER_DS1302_2003_7_21_
#include at89x52.h
sbit DS1302_CLK = P1^0; //实时时钟时钟线引脚
sbit DS1302_IO = P1^1; //实时时钟数据线引脚
sbit DS1302_RST = P1^2; //实时时钟复位线引脚
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
typedef struct __SYSTEMTIME__
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[9];
unsigned char TimeString[9];
}SYSTEMTIME; //定义的时间类型
#define AM(X) X
#define PM(X) (X+12) // 转成24小时制
#define DS1302_SECOND 0x80 //秒寄存器
#define DS1302_MINUTE 0x82 //分寄存器
#define DS1302_HOUR 0x84
#define DS1302_WEEK 0x8A
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_YEAR 0x8C
#define DS1302_RAM(X) (0xC0+(X)*2) //用于计算 DS1302_RAM 地址的宏
void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数)
{
unsigned char i;
ACC = d;
for(i=8; i0; i--)
{
DS1302_IO = ACC0; //相当于汇编中的 RRC
DS1302_CLK = 1;
DS1302_CLK = 0; //发一个高跳变到低的脉冲
ACC = ACC 1;
}
}
unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数)
{
unsigned char i;
for(i=8; i0; i--)
{
ACC = ACC 1; //相当于汇编中的 RRC
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0; //发一个高跳变到低的脉冲
}
return(ACC);
}
void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // 地址,命令
DS1302InputByte(ucDa); // 写1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0; //RST 0-1-0,CLK 0-1
}
unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1; //enable
DS1302InputByte(ucAddr|0x01); // 地址,命令
ucData = DS1302OutputByte(); // 读1Byte数据
DS1302_CLK = 1; //RST 0-1-0,CLK 0-1
DS1302_RST = 0;
return(ucData);
}
void DS1302_SetProtect(bit flag) //是否写保护
{
if(flag)
Write1302(0x8E,0x10); //WP=1,不能写入
else
Write1302(0x8E,0x00);//WP=0,可以写入
}
void DS1302_SetTime(unsigned char Address, unsigned char Value) // 设置时间函数
{
DS1302_SetProtect(0);
Write1302(Address, ((Value/10)4 | (Value%10))); //高4位为十位,低4位为个位
}
void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = Read1302(DS1302_SECOND);
Time-Second = ((ReadValue0x70)4)*10 + (ReadValue0x0F);//转换成10进制的秒
ReadValue = Read1302(DS1302_MINUTE);
Time-Minute = ((ReadValue0x70)4)*10 + (ReadValue0x0F);
ReadValue = Read1302(DS1302_HOUR);
Time-Hour = ((ReadValue0x70)4)*10 + (ReadValue0x0F);
ReadValue = Read1302(DS1302_DAY);
Time-Day = ((ReadValue0x70)4)*10 + (ReadValue0x0F);
ReadValue = Read1302(DS1302_WEEK);
Time-Week = ((ReadValue0x70)4)*10 + (ReadValue0x0F);
ReadValue = Read1302(DS1302_MONTH);
Time-Month = ((ReadValue0x70)4)*10 + (ReadValue0x0F);
ReadValue = Read1302(DS1302_YEAR);
Time-Year = ((ReadValue0xf0)4)*10 + (ReadValue0x0F);
}
/*
void DateToStr(SYSTEMTIME *Time)
{
Time-DateString[0] = Time-Year/10 + '0'; //数字类型转化为字符类型
Time-DateString[1] = Time-Year%10 + '0';
Time-DateString[2] = '-';
Time-DateString[3] = Time-Month/10 + '0';
Time-DateString[4] = Time-Month%10 + '0';
Time-DateString[5] = '-';
Time-DateString[6] = Time-Day/10 + '0';
Time-DateString[7] = Time-Day%10 + '0';
Time-DateString[8] = '\0';
}
void TimeToStr(SYSTEMTIME *Time)
{
Time-TimeString[0] = Time-Hour/10 + '0'; //数字类型转化为字符类型
Time-TimeString[1] = Time-Hour%10 + '0';
Time-TimeString[2] = ':';
Time-TimeString[3] = Time-Minute/10 + '0';
Time-TimeString[4] = Time-Minute%10 + '0';
Time-TimeString[5] = ':';
Time-TimeString[6] = Time-Second/10 + '0';
Time-TimeString[7] = Time-Second%10 + '0';
Time-DateString[8] = '\0';
}
*/
void Initial_DS1302(void)
{
unsigned char Second=Read1302(DS1302_SECOND);
if(Second0x80) //如果第七为1(表明没有启动), 则启动时钟
DS1302_SetTime(DS1302_SECOND,0);
}
/********************************************************************************
void BurstWrite1302(unsigned char *pWClock) //往DS1302写入时钟数据(多字节方式)
{
unsigned char i;
Write1302(0x8e,0x00); // 控制命令,WP=0,写操作?
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(0xbe); // 0xbe:时钟多字节写命令
for (i = 8; i0; i--) //8Byte = 7Byte 时钟数据 + 1Byte 控制
{
DS1302InputByte(*pWClock); // 写1Byte数据
pWClock++;
}
DS1302_CLK = 1;
DS1302_RST = 0;
}
void BurstRead1302(unsigned char *pRClock) //读取DS1302时钟数据(时钟多字节方式)
{
unsigned char i;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(0xbf); // 0xbf:时钟多字节读命令
for (i=8; i0; i--)
{
*pRClock = DS1302OutputByte(); // 读1Byte数据
pRClock++;
}
DS1302_CLK = 1;
DS1302_RST = 0;
}
void DS1302_TimeStop(bit flag) // 是否将时钟停止
{
unsigned char Data;
Data=Read1302(DS1302_SECOND);
DS1302_SetProtect(0);
if(flag)
Write1302(DS1302_SECOND, Data|0x80);//停止
else
Write1302(DS1302_SECOND, Data0x7F);不停止
}
********************************************************************************/
#endif
在你原来的函数基础上,加上三个参数,分别是:
1 要操作的PORT口
2 要进行的操作
3 要操作的引脚
当然,如果你的目标很明确,可以把以上三个参数裁剪掉一些,利于系统的效率提高.
下面是我写的示例程序,你可能参考一下:
#includereg52.h
#define Pin0 0x01
#define Pin1 0x02
#define Pin2 0x04
#define Pin3 0x08
#define Pin4 0x10
#define Pin5 0x20
#define Pin6 0x40
#define Pin7 0x80
#define OPT_CPL 0x00 //----取反某位操作
#define OPT_CLR 0x01 //----清零某位操作
#define OPT_SET 0x02 //----置一某位操作
void Operate(
/*这里写你原来的参数,*/
unsigned char Port_Num,
unsigned char Opr_Style,
unsigned char Pins
)
{
//----如果在单片机PORT口范围之内,刚读取当前PORT口的状
//----如果不在范围之内,死循环
//----注意,为了效率起见,这个部分可以根据实际应用进行裁剪
if(Port_Num == 0)
{
Port_Num = P0;
}
else if(Port_Num == 1)
{
Port_Num = P1;
}
else if(Port_Num == 2)
{
Port_Num = P2;
}
else if(Port_Num == 3)
{
Port_Num = P3;
}
else
{
while(1);
}
//----如果是取反相应位,进行异或操作
//----如果清零相应位,则位与操作
//----如果置位相应位,则位或操作
//----如果都不在范围之内,则死循环
//----这段代码也根据实际情况裁剪
if(Opr_Style == OPT_CPL)
{
Port_Num ^= Pins;
}
else if(Opr_Style == OPT_CLR)
{
Port_Num = ~Pins;
}
else if(Opr_Style == OPT_SET)
{
Port_Num |= Pins;
}
else
{
while(1);
}
//----如果在单片机PORT口范围之内,刚读取当前PORT口的状
//----如果不在范围之内,死循环
//----注意,为了效率起见,这个部分可以根据实际应用进行裁剪
if(Port_Num == 0)
{
P0 = Port_Num;
}
else if(Port_Num == 1)
{
P1 = Port_Num;
}
else if(Port_Num == 2)
{
P2 = Port_Num;
}
else if(Port_Num == 3)
{
P3 = Port_Num;
}
else
{
while(1);
}
//----以上的内容,可根据实际情况进行裁剪,以实现程序最优化
//----添加你的功能的其他代码---------------
}
void main(void)
{
//----使用示例:函数功能的同时,置位P2口的P2.0,P2.5,P2.7
Operate(/*这里写你原来的参数*/2,OPT_SET,(Pin0|Pin5|Pin7));
//----使用示例:函数功能的同时,清零P1口的P1.2,P1.5,P2.6
Operate(/*这里写你原来的参数*/1,OPT_CLR,(Pin2|Pin5|Pin6));
}
/*
用proteus做的时钟
可以显示年月日时分秒
可调整时间
有图
*/
#includereg52.h
#define uchar unsigned char
#define uint unsigned int
#define timh 0xee
#define timl 0x00
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
sbit K4=P1^3;
sbit K5=P1^4;
sbit K6=P1^5;
sbit poit=P0^7;
uint year=2011,year0=0;
uchar mon=6,mon0=0,day=26,day0=0,hour=23,hour0=0,min=15,min0=0,sec=0,sec0=10,temp0=0;
uchar R;
uchar cod1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar temp[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
void delay(uint z)
{
uint x,y;
for(x=0;xz;x++)
for(y=0;y100;y++);
}
void exter_init()
{
IT0=1;
EX0=1;
}
void time_init()
{
TMOD=0x01;
TH0=timh;
TL0=timl;
ET0=1;
EA=1;
TR0=1;
}
void re_time(uchar a)
{
uchar i,j;
for(j=0;ja;j++)
{ for(i=0;i14;i++)
{
P0=cod1[temp[i]];
if(i==3||i==5||i==9||i==11) poit=0;
P2=i;
delay(1);
P2=0x0f;
}
}
}
void syn()
{
if(sec!=sec0)
{sec0=sec;
temp[12]=sec0/10;
temp[13]=sec0%10;}
if(year!=year0)
{year0=year;
temp[0]=year0/1000;
temp[14]=year0%1000;
temp[1]=temp[14]/100;
temp[14]=temp[14]%100;
temp[2]=temp[14]/10;
temp[3]=temp[14]%10;}
if(mon!=mon0)
{mon0=mon;
temp[4]=mon0/10;
temp[5]=mon0%10;}
if(day!=day0)
{day0=day;
temp[6]=day0/10;
temp[7]=day0%10;}
if(hour!=hour0)
{hour0=hour;
temp[8]=hour0/10;
temp[9]=hour0%10;}
if(min!=min0)
{min0=min;
temp[10]=min0/10;
temp[11]=min0%10;}
}
main()
{
exter_init();
time_init();
while(1)
{
re_time(10);
}
}
void txter() interrupt 0
{
uchar F;
F=1;
EA=0;
while(!K6);
delay(1);
do
{ re_time(1);
if(!K1)
{
min++;
if(min=60) min=0;
while(!K1);
}
syn();
re_time(1);
if(!K2)
{
hour++;
if(hour=24) hour=0;
while(!K2);
}
syn();
re_time(1);
if(!K3)
{
day++;
R=(year%4==0)(year%100!=0);
R=(R(day=29)(mon==2))||(!R(day=30)(mon==2))||((mon!=2)(day=31));
if(R) day=1;
while(!K3);
}
syn();
re_time(1);
if(!K4)
{
mon++;
if(mon12) mon=1;
while(!K4);
}
syn();
re_time(1);
if(!K5)
{
year++;
if(year9999) year=2000;
while(!K5);
}
syn();
re_time(1);
if(!K6)
{
F=!F;
while(!K6);
}
re_time(1);
syn();
re_time(1);
}while(F);
sec=0;
EA=1;
}
/*****************************************************
************定时器0函数
*******************************************************/
void timer() interrupt 1
{
temp0++;
if(temp0=200)
{
temp0=0;
sec++;
if(sec=60)
{
sec=0;
min++;
if(min=60)
{
min=0;
hour++;
if(hour=24)
{
hour=0;
day++;
R=(year%4==0)(year%100!=0);
R=(R(day=29)(mon==2))||(!R(day=30)(mon==2))||((mon!=2)(day=31));
if(R)
{
day=0;
mon++;
if(mon=13)
{
mon=1;
year++;
}
}
}
}
}
}
syn();
TH0=timh;
TL0=timl;
}
#include reg51.h
#define uchar unsigned char //定义unsigned int为uint
#define uint unsigned int //定义unsigned uchar为uchar
sbit LCD_RS = P2^0 ;
sbit LCD_RW = P2^1 ;
sbit LCD_EN = P2^2 ;
sbit D_SDA = P2^6; //定义74HC164数据线为P2.6端口
sbit D_SCL = P2^7; //定义74HC164数据线为P2.7端口
sbit CLK = P1^3; /*实时时钟时钟线引脚 */
sbit IO = P1^4; /*实时时钟数据线引脚 */
sbit RST = P1^5; /*实时时钟复位线引脚 */
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
uchar time[8] = {0x50,0x30,0x19,0x30,0x12,0x06,0x06};
//========= 延时函数 ============
//延时时间以1ms为单位
//s决定延时时间长短
void delay_ms(uint s)
{
uint x;
for(s;s0;s--)
{
x = 200;
while(x--);
}
}
//========= 送出一个字节给74HC164(实现串并转换) ==========
void send_out(unsigned char out)//传送一个字节8位
{
uchar i;
D_SCL = 0;
for (i=8;i=1;i--)
{
D_SDA = out0x80; //送数据到数据口
D_SCL = 1; //时钟线置1
D_SCL = 0; //送一时钟
out=1; //左移
}
}
//========= 写命令函数 ==========
void lcd_wcmd(uchar cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
send_out(cmd);
LCD_EN = 1;
LCD_EN = 0 ;
}
//========= 写数据函数 ==========
void lcd_wdat(uchar dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
send_out(dat);
LCD_EN = 1;
LCD_EN = 0;
}
//========= LCD初始化函数 ==========
void lcd_init()
{
lcd_wcmd(0x38);
delay_ms(1);
lcd_wcmd(0x0c); //显示开,关光标
delay_ms(1);
lcd_wcmd(0x06); //向右移动光标
delay_ms(1);
lcd_wcmd(0x01); //清除LCD显示屏
delay_ms(1);
}
//========== 往DS1302写入1Byte数据 (内部函数) =============
void w_byte(uchar dat)
{
uchar i;
for(i=8; i0; i--)
{
IO = dat 0x01;
CLK = 1;
CLK = 0;
dat = dat 1;
}
}
//======== 从DS1302读取1Byte数据 (内部函数) ===================
uchar r_byte(void)
{
uchar i;
for(i=8; i0; i--)
{
ACC = ACC 1;
ACC7 = IO;
CLK = 1;
CLK = 0;
}
return(ACC);
}
//========== 指定地址往DS1302写入1Byte数据 (内部函数) =============
void write_byte(uchar addr, uchar dat)
{
RST = 0;
CLK = 0;
RST = 1;
w_byte(addr);
w_byte(dat);
CLK = 1;
RST = 0;
}
//========== 指定地址往DS1302读1Byte数据 (内部函数) =============
uchar read_byte(uchar addr)
{
uchar ucData;
RST = 0;
CLK = 0;
RST = 1;
w_byte(addr);
ucData = r_byte();
CLK = 1;
RST = 0;
return(ucData);
}
//============ 设置ds1302日期和时间 =============
void write_ds1302(uchar *p)
{
uchar i;
uchar addr = 0x80;
write_byte(0x8e,0x00); // 控制命令,WP=0,写操作
for(i =7; i0; i--)
{
write_byte(addr,*p); // 秒 分 时 日 月 星期 年
p++;
addr +=2;
}
write_byte(0x8e,0x80); // 控制命令,WP=1,写保护
}
//============ 读ds1302当前日期和时间 =============
void read_ds1302(uchar *p)
{
uchar i;
uchar addr = 0x81;
for (i=0; i7; i++)
{
*p = read_byte(addr); //格式为: 秒 分 时 日 月 星期 年
addr += 2;
p++;
}
}
//============ 显示函数 ===================
void lcd_disp()
{
uchar addr = 4;
lcd_wcmd(0x80 + addr);
lcd_wdat(((time[2]4)0x0f)+0x30); //显示小时
addr++;
lcd_wcmd(0x80 + addr);
lcd_wdat((time[2]0x0f)+0x30);
addr++;
lcd_wcmd(0x80 + addr);
lcd_wdat(':'); //显示":"
addr++;
lcd_wcmd(0x80 + addr);
lcd_wdat(((time[1]4)0x0f)+0x30); //显示分
addr++;
lcd_wcmd(0x80 + addr);
lcd_wdat((time[1]0x0f)+0x30);
addr++;
lcd_wcmd(0x80 + addr);
lcd_wdat(':'); //显示":"
addr++;
lcd_wcmd(0x80 + addr);
lcd_wdat(((time[0]4)0x0f)+0x30); //显示秒
addr++;
lcd_wcmd(0x80 + addr);
lcd_wdat((time[0]0x0f)+0x30);
addr = 2;
lcd_wcmd(0xc0 + addr); //在第二行显示年月日和星期
lcd_wdat('2'); //显示2
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat('0'); //显示0
addr++;
lcd_wdat(((time[6]4)0x0f)+0x30); //年
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat((time[6]0x0f)+0x30);
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat(' ');
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat(((time[4]4)0x0f)+0x30); //显示月
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat((time[4]0x0f)+0x30);
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat(' ');
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat(((time[3]4)0x0f)+0x30); //显示日
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat((time[3]0x0f)+0x30);
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat(' ');
addr++;
lcd_wcmd(0xc0 + addr);
lcd_wdat((time[5]0x0f)+0x30); //显示星期
}
//=========== 主函数 ===============
void main()
{
lcd_init(); // 初始化LCD
write_ds1302(time);
while(1)
{
read_ds1302(time); //读DS1302数据
lcd_disp(); //LCD显示
delay_ms(500); //延时0.5秒
}
}
这是一个电子时钟,在LCD1602上显示,时钟芯片是DS1302
定义一个延时xms毫秒的延时函数
void delay(unsigned int xms) // xms代表需要延时的毫秒数
{
unsigned int x,y;
for(x=xms;x》0;x--)
for(y=110;y》0;y--);
}
使用:
void Delay10us(uchar Ms)
{
uchar data i;
for(;Ms》0;Ms--)
for(i=26;i》0;i--);
}
i=[(延时值-1.75)*12/Ms-15]/4
扩展资料
1、在C51中进行精确的延时子程序设计时,尽量不要或少在延时子程序中定义局部变量,所有的延时子程序中变量通过有参函数传递。
2、在延时子程序设计时,采用do?while,结构做循环体要比for结构做循环体好。
3、在延时子程序设计时,要进行循环体嵌套时,采用先内循环,再减减比先减减,再内循环要好。