標(biāo)題:
DS1302與液晶1602顯示時(shí)鐘
[打印本頁(yè)]
作者:
fcl
時(shí)間:
2017-7-30 10:29
標(biāo)題:
DS1302與液晶1602顯示時(shí)鐘
/************************************************
名稱(chēng):DS1302實(shí)時(shí)時(shí)鐘顯示程序
說(shuō)明:從DS1302中讀取時(shí)鐘數(shù)據(jù),在LCD上顯示日期和時(shí)間
*************************************************/
#include <reg51.h>
#include <intrins.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
sbit IO = P1^0; //數(shù)據(jù)線(xiàn)
sbit SCLK = P1^1; //時(shí)鐘線(xiàn)
sbit RST = P1^2; //復(fù)位線(xiàn)
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
uchar *WEEK[] = {"SUN","MON","TUS","WEN","THU","FRI","SAT"}; //周一至周六
uchar LCD_BUF_1[]={"DATE 00-00-00 "};
uchar LCD_BUF_2[]={"TIME 00:00:00 "}; //寫(xiě)此條時(shí)注意空格
void GetDateTime();
uchar DateTime[7];
void Delayms(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void Write_LCD_Command(uchar cmd)
{
//while((Busy_Check()&0x80)==0x80);
//Busy_Check(); //兩種寫(xiě)法都行
RS = 0;
RW = 0; //寫(xiě)狀態(tài)
EN = 0;
P0 = cmd;
EN = 1;
Delayms(1);
EN = 0;
}
void Write_LCD_Data(uchar dat) //發(fā)送數(shù)據(jù)
{
// while((Busy_Check()&0x80)==0x80);
// Busy_Check();
RS = 1; //數(shù)據(jù)寄存器
RW = 0; //寫(xiě)狀態(tài)
EN = 0;
P0 = dat; //數(shù)據(jù)字節(jié)放到LCD端口
EN = 1;
Delayms(1);
EN = 0;
}
void Initialize_LCD() //初始化
{
Write_LCD_Command(0x38); //置功能 ,8位,雙行,5*7點(diǎn)陣
Delayms(1);
Write_LCD_Command(0x01); //清屏
Delayms(1);
Write_LCD_Command(0x06); //字符進(jìn)入模式遞增
Delayms(1);
Write_LCD_Command(0x0c); //顯示開(kāi)關(guān)
Delayms(1);
}
void ShowString(uchar x,uchar y,uchar *str) //在指定位置顯示
{
uchar i = 0;
if(y == 0)
Write_LCD_Command(0x80 | x); //x為第x列字符位置,y為第y行
if(y == 1)
Write_LCD_Command(0xc0 | x);
for(i=0;i<16;i++)
{
Write_LCD_Data(str[i]);
}
}
//向DS1302寫(xiě)入一字節(jié)
void Write_A_Byte_TO_DS1302(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
IO=x&0x01;SCLK=1;SCLK=0;x>>=1;
}
}
//從DS1302讀取一個(gè)字節(jié)
uchar Get_A_Byte_FROM_DS1302()
{
uchar i,b=0x00;
for(i=0;i<8;i++)
{
b |= _crol_((uchar)IO,i);
SCLK=1;SCLK=0;
}
return b/16*10+b%16;
}
//從DS1302指定位置讀數(shù)據(jù)
uchar Read_Date(uchar addr)
{
uchar dat;
RST = 0;SCLK=0;RST=1;
Write_A_Byte_TO_DS1302(addr);
dat = Get_A_Byte_FROM_DS1302();
SCLK=1;RST=0;
return dat;
}
//讀取當(dāng)前日期時(shí)間
void GetDateTime()
{
uchar i,addr = 0x81;
for(i=0;i<7;i++)
{
DateTime[i]=Read_Date(addr);addr+=2;
}
}
void Format_DateTime(uchar d,uchar *a)
{
// *a=(d>>4) + '0';
// *(a+1) = (d & 0x0f) + '0';
a[0]=d/10+'0';
a[1]=d%10+'0';
}
void main()
{
Initialize_LCD();
while(1)
{
GetDateTime();
Format_DateTime(DateTime[6],LCD_BUF_1 + 5);
Format_DateTime(DateTime[4],LCD_BUF_1 + 8);
Format_DateTime(DateTime[3],LCD_BUF_1 + 11);
strcpy(LCD_BUF_1 + 13,WEEK[DateTime[5]-1]);
Format_DateTime(DateTime[2],LCD_BUF_2 + 5);
Format_DateTime(DateTime[1],LCD_BUF_2 + 8);
Format_DateTime(DateTime[0],LCD_BUF_2 + 11);
ShowString(0,0,LCD_BUF_1);
ShowString(0,1,LCD_BUF_2);
}
}
作者:
陌路狂花
時(shí)間:
2017-11-20 20:41
你這個(gè)時(shí)間為什么不走,附件有圖片
IMG_20171120_203932.jpg
(1.37 MB, 下載次數(shù): 16)
下載附件
2017-11-20 20:41 上傳
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1