/*2015_11-02*/
#include<reg52.h>
#include "intrins.h"
#define uint unsigned int
#define uchar unsigned char
#define lcd_date P0
#define delay5us(){_nop_();_nop_();_nop_();_nop_();_nop_();}
uchar a,miao,shi,fen,ri,yue,nian,week,keynum,temp,count;
uchar code set_time[7]={0x00,0x46,0x11,0x26,0x10,0x06,0x14};
uchar time_buf[]="date:00-00-00";
uchar time_buf1[]="time:00:00:00";
bit c_moon;
data uchar year_moon,month_moon,day_moon;
//??1602????
sbit lcdrs=P0^0;
sbit lcden=P0^2;
sbit lcdrw=P0^1;
//DS1302???????
sbit DS_reset=P1^3;
sbit DS_io=P1^2;
sbit DS_sclk=P1^1;
sbit ACC0=ACC^0;//?????
sbit ACC7=ACC^7;
void delay(uint z)//????
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delayms(uchar x)
{
uchar t;
while(x--)
for(t=0;t<120;t++);
}
void lcd_busy_wait()
{
uchar Hi,Lo;
do
{
lcd_date |=0xf0;//4?????????????
lcdrs=0;
lcdrw=1;//??????? ?
lcden=1;
delay5us();
Hi=lcd_date;
delay5us();
lcden=0;
delay5us();
lcden=1;
delay5us();
Lo=lcd_date;
delay5us();
lcden=0;
delay5us();
}
while(Hi&0x80);
}
void lcd_1602_write_com(uchar com)
{
lcd_busy_wait();//?????
lcd_date=com&0xf0|0x04;//?????,?????rs,rw,en?????
delay5us();
lcden=0;
delay5us();
lcd_date=com<<4|0x04;//?????
delay5us();
lcden=0;
delay5us();
}
void lcd_1602_write_dat(uchar dat)
{
lcd_busy_wait();//?????
lcd_date=dat&0xf0|0x05;//?????
delay5us();
lcden=0;
delay5us();
lcd_date=dat<<4|0x05;//?????
delay5us();
lcden=0;
delay5us();
}
void lcd_string(uchar r,uchar c,char *str)
{
uchar i=0;
code uchar ddram[]={0x80,0xc0};//???????????
lcd_1602_write_com(ddram[r]|c);//????????
//?????
for(i=0;i<16&&str[i]!= ' ';i++)
{
lcd_1602_write_dat(str[i]);
}
}
void Lcd_1602_init()
{
lcd_date=0xff;//?????????????
lcd_date=0x24;//4???,?lcden,lcdrw,lcdrs=100(4),?????????
lcden=0;//?????????
lcd_1602_write_com(0x28); //4?,2?,5x7??
delayms(15);
lcd_1602_write_com(0x0c); //???
delayms(15);
lcd_1602_write_com(0x06); //?????,????
delayms(15);
lcd_1602_write_com(0x01);//??
delayms(15);
lcd_1602_write_com(0x02);//????????
delayms(15);
// lcd_string(0,0,time_buf);
//lcd_string(1,0,time_buf1);
}
/*?DS1302?????????*/
void DS1302_write_byte(uchar inbyte)
{
uchar i;
for(i=0;i<8;i++)
{
DS_sclk=0; //?????????
if(inbyte&0x01) {DS_io=1;}
else {DS_io=0;}
DS_sclk=1; //????????DS1302
inbyte=inbyte>>1;
}
}
uchar DS1302_read_byte() //???DS1302????????
{
uchar i,tp=0;
for(i=0;i<8;i++)
{
if(DS_io==1) tp=tp|0x80;
else tp=tp&0x7f;
DS_sclk=1; //?????
_nop_();
DS_sclk=0;
tp=tp>>1;
}
return (tp);
}
void DS1302_write(uchar cmd,uchar indata) //?DS1302??, ?DS1302?????????
{
DS_sclk=0;
DS_reset=1;
DS1302_write_byte(cmd);
DS1302_write_byte(indata);
_nop_();
DS_sclk=0;
DS_reset=0;
}
uchar DS1302_read(uchar addr) //?DS1302??,?DS1302???????
{
uchar backdata;
DS_reset=0;
DS_sclk=0;
DS_reset=1;
DS1302_write_byte(addr); //????
backdata=DS1302_read_byte(); //?????
DS_sclk=0;
DS_reset=0;
return (backdata);
}
void DS1302_timeset()
{
uchar i;
DS1302_write(0x8e,0x00); //????,?????
for (i=0;i<7;i++)
{
DS1302_write(0x80+2*i,set_time[i]);
}
DS1302_write(0x8e,0x80); //???,????
}
void DS1302_init() //???DS1302??
{
DS_reset=0;
DS_sclk=0;
DS1302_timeset();
}
void DS1302_get_time()
{
uchar i,tp,add=0x81;
DS1302_write(0x8e,0x80);
for (i=0;i<7;i++)
{
tp=DS1302_read(add);
add+=2;
delay(1);
time_buf[15-2*i]=tp%16;
time_buf[14-2*i]=tp/16;
time_buf1[i]=(tp/16)*10+tp%16;
}
}
void main()
{
lcd_1602_init();
delayms(15);
DS1302_init();
while(1)
{
//DS1302_init();
DS1302_timeset();
DS1302_get_time();
lcd_string(0,0,time_buf);
delayms(15);
lcd_string(1,0,time_buf1);
}
}
此帖出自
Linux与安卓论坛
已经捣鼓啦很长一段时间了,本来觉得很简单,但一直没有攻克
一周热门 更多>