微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > DS1302测试程序

DS1302测试程序

时间:11-13 来源:互联网 点击:
#include

#include

#define uint unsigned int
#define uchar unsigned char

/************声明*************/
void ds1302_init();

/*************LCD12864*************/

sbit beer=P2^0;

#define P0 P0

uchar code table1[]={"电子时钟"};
uchar code table2[]={"时间:"};
uchar code table3[]={"日期:"};

sbit lcd_rs=P1^0;//定义LCD引脚
sbit lcd_rw=P1^1;
sbit lcd_en=P1^2;
sbit lcd_psb=P2^4;
sbit lcd_rst=P2^3;

void delay(uint z) //延时
{
uint x,y;
for(x=110;x>0;x--);
for(y=z;y>0;y--);
}

void lcd_w_com(uchar com)//写命令
{
lcd_rs=0;
lcd_rw=0;
lcd_en=0;
P0=com;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}

void lcd_w_data(uchar date)// 写数据
{
lcd_rs=1;
lcd_rw=0;
lcd_en=0;
P0=date;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
}

void lcd_init()//液晶初始化
{
lcd_rst=0;
delay(10);
lcd_rst=1;
delay(10);
lcd_psb=1;

P3=0xff;
key5=1;
key6=1;

lcd_w_com(0x30);
lcd_w_com(0x0c);
lcd_w_com(0x14);
lcd_w_com(0x01);
}

void lcd_pos(uchar x,uchar y)//选择写入位置
{
uchar pos;
if(x==0)
{x=0x80;}
else if(x==1)
{x=0x90;}
else if(x==2)
{x=0x88;}
else if(x==3)
{x=0x98;}
pos=x+y;
lcd_w_com(pos);
}

void clr_screen()//清屏
{
lcd_w_com(0x34);
delay(5);
lcd_w_com(0x30);
delay(5);
lcd_w_com(0x01);
}

void disp_chinese(uchar code *s)
{
while(*s>0)
{
lcd_w_data(*s);
s++;
delay(500);
}
}

void disp_title()
{
uchar i;
lcd_pos(0,2);
i=0;
while(table1[i]!=\0)
{
lcd_w_data(table1[i]);
i++;
}

lcd_pos(1,0);
i=0;
while(table2[i]!=\0)
{
lcd_w_data(table2[i]);
i++;
}

lcd_pos(2,0);
i=0;
while(table3[i]!=\0)
{
lcd_w_data(table3[i]);
i++;
}

lcd_pos(3,0);
i=0;

}

/***********DS1302************/
sbit ds1302_sclk=P3^4;// 时钟线引脚
sbit ds1302_io=P3^5; //数据线引脚
sbit ds1302_rst=P3^6; //复位线引脚

uchar year,month,day,week,hour,minute,second;
uchar time[20];

uchar ds1302_r_data(uchar addr)//读DS1302数据
{
uchar i,j,k;
ds1302_rst=0;
ds1302_sclk=0;
ds1302_rst=1;
for(i=0x01,j=0;j<8;j++)
{
if(addr&i)
ds1302_io=1;
else
ds1302_io=0;
ds1302_sclk=1;
i=i<1;
ds1302_sclk=0;
}
k=0;
for(i=0;i<8;i++)
{
k>>=1;
ds1302_sclk=0;
if(ds1302_io)
k|=0x80;
ds1302_sclk=1;
}
ds1302_rst=0;
ds1302_sclk=1;
return(k);
}

void ds1302_w_data(uchar addr,uchar dat)//向DS1302写入数据
{
uchar i,j;
ds1302_rst=0;
ds1302_sclk=0;
ds1302_rst=1;
for(i=0x01,j=0;j<8;j++)
{
if(addr&i)
ds1302_io=1;
else
ds1302_io=0;
ds1302_sclk=1;
i=i<1;
ds1302_sclk=0;
}
for(i=0x01,j=0;j<8;j++)
{
if(dat&i)
ds1302_io=1;
else
ds1302_io=0;
ds1302_sclk=1;
i=i<1;
ds1302_sclk=0;
}
ds1302_rst=0;
ds1302_sclk=1;
}

void ds1302_init()//DS1302初始化 (如果有初始化,每次上电都将
//重新设置时间,无法让1302断电保持!)
{
ds1302_w_data(0x8e,0x00);
ds1302_w_data(0x90,0x00);
ds1302_w_data(0x80,0x00);//秒
ds1302_w_data(0x82,0x30);//分
ds1302_w_data(0x84,0x12);//时
ds1302_w_data(0x86,0x07);//日
ds1302_w_data(0x88,0x10);//月
ds1302_w_data(0x8c,0x08);//年
ds1302_w_data(0x8a,0x02);//星期
ds1302_w_data(0x8e,0x80);
}

void dotime()//时间处理
{
time[0]=ds1302_r_data(0x81);
time[1]=ds1302_r_data(0x83);
time[2]=ds1302_r_data(0x85);
time[3]=ds1302_r_data(0x87);
time[4]=ds1302_r_data(0x89);
time[5]=ds1302_r_data(0x8d);
time[6]=ds1302_r_data(0x8b);
second=((time[0]&0x70)>>4)*10+(time[0]&0x0f);
minute=((time[1]&0x70)>>4)*10+(time[1]&0x0f);
hour=((time[2]&0x70)>>4)*10+(time[2]&0x0f);
day=((time[3]&0x70)>>4)*10+(time[3]&0x0f);
month=((time[4]&0x70)>>4)*10+(time[4]&0x0f);
year=((time[5]&0x70)>>4)*10+(time[5]&0x0f);
week=((time[6]&0x70)>>4)*10+(time[6]&0x0f);
}

void disp_week()//显示星期
{
dotime();
switch(week)
{
case 1:
lcd_pos(3,0);
disp_chinese("星期一 ");
break;
case 2:
lcd_pos(3,0);
disp_chinese("星期二");
break;
case 3:
lcd_pos(3,0);
disp_chinese("星期三");
break;
case 4:
lcd_pos(3,0);
disp_chinese("星期四");
break;
case 5:
lcd_pos(3,0);
disp_chinese("星期五");
break;
case 6:
lcd_pos(3,0);
disp_chinese("星期六");
break;
case 7:
lcd_pos(3,0);
disp_chinese("星期日");
break;
}
}

void disp_data()//显示时间
{
dotime();
lcd_pos(1,3);
lcd_w_data(0x30+hour/10);
lcd_w_data(0x30+hour%10);
lcd_w_data(0x3a);
lcd_w_data(0x30+minute/10);
lcd_w_data(0x30+minute%10);
lcd_w_data(0x3a);
lcd_w_data(0x30+second/10);
lcd_w_data(0x30+second%10);
lcd_pos(2,3);
lcd_w_data(0x30+year/10);
lcd_w_data(0x30+year%10);
lcd_w_data(-);
lcd_w_data(0x30+month/10);
lcd_w_data(0x30+month%10);
lcd_w_data(-);
lcd_w_data(0x30+day/10);
lcd_w_data(0x30+day%10);
}

上一篇:msp430电压监控
下一篇:STM32 GPIO入门学习

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top