標(biāo)題: 12864+ds1302時(shí)鐘 [打印本頁]
作者: forrest 時(shí)間: 2014-11-14 13:29
標(biāo)題: 12864+ds1302時(shí)鐘
程序編譯沒錯(cuò)誤,下載到開發(fā)板就顯示不出來,到網(wǎng)上找別人的程序下進(jìn)去也不行,求大神分析
#include<reg52.h>
#include<intrins.h>
#defineuchar unsigned char
#defineuint unsigned int
/*DS1302 端口設(shè)置 */
sbitSCK=P3^6; //DS1302時(shí)鐘
sbitSDA=P3^4; //DS1302 IO
sbit RST= P3^5; // DS1302復(fù)位
bitReadRTC_Flag; //讀DS1302全局變量
/* 12864端口定義*/
#defineLCD_data P0 //帶字庫液晶12864數(shù)據(jù)口
sbitLCD_RS = P2^4; //寄存器選擇輸入
sbitLCD_RW = P2^5; //液晶讀/寫控制
sbitLCD_EN = P2^6; //液晶使能控制
sbit PSB=P2^1; //并口控制
sbit RES=P2^3;
ucharcode dis1[] = {" 電子設(shè)計(jì)天地"}; //液晶顯示的漢字
ucharcode dis2[] = {"有志者,事竟成!"};
ucharcode dis4[] = {'0','1','2','3','4','5','6','7','8','9'};
unsignedchar temp;
#definedelayNOP(); {_nop_();_nop_();_nop_();_nop_();};
voidlcd_pos(uchar X,uchar Y); //確定顯示位置
unsignedchar l_tmpdate[7]={0,7,16,19,10,1,9};//秒分時(shí)日月周年09-10-1916:07:00
codeunsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分時(shí)日月周年 最低位讀寫位
codeunsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
voidWrite_Ds1302_byte(unsigned char temp);
voidWrite_Ds1302( unsigned char address,unsigned char dat );
unsignedchar Read_Ds1302 ( unsigned char address );
voidRead_RTC(void);//read RTC
voidSet_RTC(void); //set RTC
voidInitTIMER0(void);//inital timer0
/*******************************************************************/
/* */
/* 延時(shí)函數(shù) */
/* */
/*******************************************************************/
void delay(unsigned int m) //延時(shí)程序
{
unsigned int i,j;
for(i=0;i<m;i++)
for(j=0;j<10;j++);
}
/*******************************************************************/
/* */
/*檢查LCD忙狀態(tài) */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫指令與數(shù)據(jù)。 */
/* */
/*******************************************************************/
bitlcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
/*******************************************************************/
/* */
/*寫指令數(shù)據(jù)到LCD */
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
/* */
/*******************************************************************/
voidlcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delay(1);
LCD_EN = 1;
delay(1);
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*寫顯示數(shù)據(jù)到LCD */
/*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。 */
/* */
/*******************************************************************/
voidlcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delay(1);
LCD_EN = 0;
}
/*******************************************************************/
/* */
/* LCD初始化設(shè)定 */
/* */
/*******************************************************************/
voidlcd_init()
{
P0=0xFF;
P2=0xFF;
delay(40);
PSB=1; //并口方式。
delay(1);
RES=0;
delay(1);
RES=1;
delay(10);
lcd_wcmd(0x30);
delay(100);
lcd_wcmd(0x30);
delay(37);
lcd_wcmd(0x08);
delay(100);
lcd_wcmd(0x10);
delay(100);
lcd_wcmd(0x0C); //顯示開,關(guān)光標(biāo)
delay(100);
lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容
delay(100);
lcd_wcmd(0x06);
delay(100);
}
voidmain()
{
uchar i;
InitTIMER0();
Set_RTC();
lcd_init();
//初始化LCD
while(1)
{
if(ReadRTC_Flag)
{
ReadRTC_Flag=0;
Read_RTC();
lcd_pos(0,0); //設(shè)置顯示位置為第一行的第1個(gè)字符
i = 0;
while(dis1 != '\0')
{ //顯示字符
lcd_wdat(dis1);
i++;
}
lcd_pos(1,0); //設(shè)置顯示位置為第二行的第1個(gè)字符
i = 0;
while(dis2 != '\0')
{
lcd_wdat(dis2); //顯示字符
i++;
}
lcd_pos(2,0);
lcd_wdat(dis4[l_tmpdate[6]/16]); //DS1302是BCD碼,進(jìn)行處理得到十位與個(gè)位數(shù)。
lcd_pos(2,1);
lcd_wdat(dis4[l_tmpdate[6]%16]);
lcd_pos(2,2);
lcd_wdat('-');
lcd_pos(2,3);
lcd_wdat(dis4[l_tmpdate[4]/16]);
lcd_pos(2,4);
lcd_wdat(dis4[l_tmpdate[4]%16]);
lcd_pos(2,5);
lcd_wdat('-');
lcd_pos(2,6);
lcd_wdat(dis4[l_tmpdate[3]/16]);
lcd_pos(2,7);
lcd_wdat(dis4[l_tmpdate[3]%16]); //設(shè)置顯示位置為第三行的第1個(gè)字符
lcd_pos(3,0); //設(shè)置顯示位置為第四行的第1個(gè)字符
lcd_wdat(dis4[l_tmpdate[2]/16]);
lcd_pos(3,1);
lcd_wdat(dis4[l_tmpdate[2]%16]);
lcd_pos(3,2);
lcd_wdat(':');
lcd_pos(3,3);
lcd_wdat(dis4[l_tmpdate[1]/16]);
lcd_pos(3,4);
lcd_wdat(dis4[l_tmpdate[1]%16]);
lcd_pos(3,5);
lcd_wdat(':');
lcd_pos(3,6);
lcd_wdat(dis4[l_tmpdate[0]/16]);
lcd_pos(3,7);
lcd_wdat(dis4[l_tmpdate[0]%16]);
}
}
}
/*********************************************************/
/* */
/* 設(shè)定顯示位置 */
/* */
/*********************************************************/
voidlcd_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_wcmd(pos); //顯示地址
}
voidInitTIMER0(void)
{
TMOD|=0x01;//定時(shí)器設(shè)置 16位
TH0=0xef;//初始化值
TL0=0xf0;
ET0=1;
TR0=1;
EA=1;
}
voidWrite_Ds1302_Byte(unsigned char temp)
{
unsignedchar i;
for(i=0;i<8;i++) //循環(huán)8次 寫入數(shù)據(jù)
{
SCK=0;
SDA=temp&0x01; //每次傳輸?shù)妥止?jié)
temp>>=1; //右移一位
SCK=1;
}
}
/****************************************************************************/
voidWrite_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_(); //啟動(dòng)
Write_Ds1302_Byte(address); //發(fā)送地址
Write_Ds1302_Byte(dat); //發(fā)送數(shù)據(jù)
RST=0; //恢復(fù)
}
/****************************************************************************/
unsignedchar Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循環(huán)8次 讀取數(shù)據(jù)
{
if(SDA)
temp|=0x80; //每次傳輸?shù)妥止?jié)
SCK=0;
temp>>=1; //右移一位
SCK=1;
}
RST=0;
_nop_(); //以下為DS1302復(fù)位的穩(wěn)定時(shí)間
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return(temp); //返回
}
/****************************************************************************/
voidRead_RTC(void) //讀取 日歷
{
unsignedchar i,*p;
p=read_rtc_address;//地址傳遞
for(i=0;i<7;i++) //分7次讀取 秒分時(shí)日月周年
{
l_tmpdate=Read_Ds1302(*p);
p++;
}
}
/***********************************************************************/
voidSet_RTC(void) //設(shè)定 日歷
{
unsignedchar i,*p,tmp;
for(i=0;i<7;i++){ //BCD處理
tmp=l_tmpdate/10;
l_tmpdate=l_tmpdate%10;
l_tmpdate=l_tmpdate+tmp*16;
}
Write_Ds1302(0x8E,0X00);
p=write_rtc_address; //傳地址
for(i=0;i<7;i++) //7次寫入 秒分時(shí)日月周年
{
Write_Ds1302(*p,l_tmpdate);
p++;
}
Write_Ds1302(0x8E,0x80);
}
voidtim(void) interrupt 1 using 1//中斷,用于數(shù)碼管掃描
{
static unsigned char i,num;
TH0=0xf5;
TL0=0xe0;
//P0=table[l_tmpdisplay]; //查表法得到要顯示數(shù)字的數(shù)碼段
//P2=table1;
i++;
if(i==8)
{
i=0;
num++;
if(20==num) //隔段時(shí)間讀取1302的數(shù)據(jù)。時(shí)間間隔可以調(diào)整
{
ReadRTC_Flag=1; //使用標(biāo)志位判斷
num=0;
}
}
}
作者: jiege 時(shí)間: 2014-11-15 18:08
io不對(duì)吧
作者: nojiya88 時(shí)間: 2014-11-15 19:32
你這個(gè)并口顯示,如果你開發(fā)板接口是串口顯示的肯定就不能顯示了吧。。。還有你開發(fā)板接口你改對(duì)了么??晶振一般是12和11.0592兩種,你注意看清仔細(xì)了。
我也碰到過這個(gè)問題的,多找?guī)讉(gè)程序試試就行了。
作者: nojiya88 時(shí)間: 2014-11-15 19:37
這個(gè)是我的板子上能正常顯示的程序,你也可以改一下接口或者顯示的字體。
//頭文件:
#include <reg52.h>
/********IO引腳定義***********************************************************/
sbit LCD_RS=P2^3;//定義引腳
sbit LCD_RW=P2^4;
sbit LCD_E=P2^5;
sbit PSB =P2^6; //PSB腳為12864-12系列的串、并通訊功能切換,使用8位并行接口,PSB=1
/********宏定義***********************************************************/
#define LCD_Data P0
#define Busy 0x80 //用于檢測LCD狀態(tài)字中的Busy標(biāo)識(shí)
/********函數(shù)聲明*************************************************************/
void WriteDataLCD(unsigned char WDLCD);
void WriteCommandLCD(unsigned char WCLCD,BuysC);
unsigned char ReadDataLCD(void);
unsigned char ReadStatusLCD(void);
void LCDInit(void);
void LCDClear(void);
void LCDFlash(void);
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
void Delay5Ms(void);
void Delay400Ms(void);
/********數(shù)據(jù)定義*************************************************************/
code unsigned char uctech[] = {"5 1 單片機(jī)實(shí)驗(yàn)板"};
code unsigned char net[] = {"我們要努力"};
code unsigned char mcu[] = {"好好學(xué)不要放棄"};
code unsigned char qq[] = {" QQ: 292799213 "};
/***********主函數(shù)開始********************************************************/
void main(void)
{
Delay400Ms(); //啟動(dòng)等待,等LCD講入工作狀態(tài)
LCDInit(); //LCM初始化
Delay5Ms(); //延時(shí)片刻(可不要)
while(1){
LCDClear();
DisplayListChar(0,1,uctech); //顯示字庫中的中文數(shù)字
DisplayListChar(0,2,net); //顯示字庫中的中文數(shù)字
DisplayListChar(0,3,mcu); //顯示字庫中的中文
DisplayListChar(0,4,qq); //顯示字庫中的中文數(shù)字
Delay400Ms();
Delay400Ms();
Delay400Ms();
Delay400Ms();
LCDFlash(); //閃爍效果
}
}
/***********寫數(shù)據(jù)********************************************************/
void WriteDataLCD(unsigned char WDLCD)
{
ReadStatusLCD(); //檢測忙
LCD_RS = 1;
LCD_RW = 0;
LCD_Data = WDLCD;
LCD_E = 1;
LCD_E = 1;
LCD_E = 0;
}
/***********寫指令********************************************************/
void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時(shí)忽略忙檢測
{
if (BuysC) ReadStatusLCD(); //根據(jù)需要檢測忙
LCD_RS = 0;
LCD_RW = 0;
LCD_Data = WCLCD;
LCD_E = 1;
LCD_E = 1;
LCD_E = 0;
}
/***********讀數(shù)據(jù)********************************************************/
unsigned char ReadDataLCD(void)
{
unsigned char LCDDA;
LCD_RS = 1;
LCD_RW = 1;
LCD_E = 1;
LCD_E = 1;
LCDDA=LCD_Data;
LCD_E = 0;
return(LCDDA);
}
/***********讀狀態(tài)*******************************************************/
unsigned char ReadStatusLCD(void)
{
LCD_Data = 0xFF;
LCD_RS = 0;
LCD_RW = 1;
LCD_E = 1;
LCD_E = 1;
while (LCD_Data & Busy); //檢測忙信號(hào)
LCD_E = 0;
return(1);
}
/***********初始化********************************************************/
void LCDInit(void)
{
WriteCommandLCD(0x30,1); //顯示模式設(shè)置,開始要求每次檢測忙信號(hào)
WriteCommandLCD(0x01,1); //顯示清屏
WriteCommandLCD(0x06,1); // 顯示光標(biāo)移動(dòng)設(shè)置
WriteCommandLCD(0x0C,1); // 顯示開及光標(biāo)設(shè)置
}
/***********清屏********************************************************/
void LCDClear(void)
{
WriteCommandLCD(0x01,1); //顯示清屏
WriteCommandLCD(0x34,1); // 顯示光標(biāo)移動(dòng)設(shè)置
WriteCommandLCD(0x30,1); // 顯示開及光標(biāo)設(shè)置
}
/***********閃爍效果********************************************************/
void LCDFlash(void)
{
WriteCommandLCD(0x08,1); //顯示清屏
Delay400Ms();
WriteCommandLCD(0x0c,1); // 顯示開及光標(biāo)設(shè)置
Delay400Ms();
WriteCommandLCD(0x08,1); //顯示清屏
Delay400Ms();
WriteCommandLCD(0x0c,1); // 顯示開及光標(biāo)設(shè)置
Delay400Ms();
WriteCommandLCD(0x08,1); //顯示清屏
Delay400Ms();
}
/***********按指定位置顯示一個(gè)字符*******************************************/
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
if(Y<1)
Y=1;
if(Y>4)
Y=4;
X &= 0x0F; //限制X不能大于16,Y不能大于1
switch(Y){
case 1:X|=0X80;break;
case 2:X|=0X90;break;
case 3:X|=0X88;break;
case 4:X|=0X98;break;
}
WriteCommandLCD(X, 0); //這里不檢測忙信號(hào),發(fā)送地址碼
WriteDataLCD(DData);
}
/***********按指定位置顯示一串字符*****************************************/
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength,X2;
ListLength = 0;
X2=X;
if(Y<1)
Y=1;
if(Y>4)
Y=4;
X &= 0x0F; //限制X不能大于16,Y在1-4之內(nèi)
switch(Y){
case 1:X2|=0X80;break; //根據(jù)行數(shù)來選擇相應(yīng)地址
case 2:X2|=0X90;break;
case 3:X2|=0X88;break;
case 4:X2|=0X98;break;
}
WriteCommandLCD(X2, 1); //發(fā)送地址碼
while (DData[ListLength]>=0x20){ //若到達(dá)字串尾則退出
if (X <= 0x0F){ //X坐標(biāo)應(yīng)小于0xF
WriteDataLCD(DData[ListLength]);
ListLength++;
X++;
Delay5Ms();
}
}
}
/***********短延時(shí)********************************************************/
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
/***********長延時(shí)********************************************************/
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--){
TempCycB=7269;
while(TempCycB--);
}
}
作者: 15374515640 時(shí)間: 2014-11-27 22:52
為什么有的程序讀寫的地址一樣也能夠成功?
作者: 15374515640 時(shí)間: 2014-11-27 22:56
我跟你寫的差不多,也是讀取不到數(shù)據(jù)
作者: dgahz 時(shí)間: 2014-11-28 15:45
forrest 發(fā)表于 2014-11-14 13:29:50 | 只看該作者 回帖獎(jiǎng)勵(lì)
程序編譯沒錯(cuò)誤,下載到開發(fā)板就顯示不出來,到網(wǎng)上找別人的程序下進(jìn)去也不行,求大神分析
#include<reg52.h>
看來你對(duì)數(shù)組和指針不是很熟,程序錯(cuò)誤很多,我費(fèi)了很大的勁,把它改好了。給你現(xiàn)成別人做好的程序?qū)δ銢]幫助,這樣你永遠(yuǎn)不知你的程序錯(cuò)在哪里。
作者: dgahz 時(shí)間: 2014-11-28 15:49
上調(diào)好的圖
IMG_20141128_153455.jpg (365.37 KB, 下載次數(shù): 91)
下載附件
2014-11-28 15:48 上傳
作者: dgahz 時(shí)間: 2014-11-28 15:50
本帖最后由 dgahz 于 2014-11-28 16:05 編輯
經(jīng)改動(dòng)過的程序:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
/*DS1302 端口設(shè)置 */
sbit SCK=P3^6; //DS1302時(shí)鐘
sbit SDA=P3^4; //DS1302 IO
sbit RST= P3^5; // DS1302復(fù)位
bit ReadRTC_Flag; //讀DS1302全局變量
/* 12864端口定義*/
#define LCD_data P0 //帶字庫液晶12864數(shù)據(jù)口
sbit LCD_RS = P2^4; //寄存器選擇輸入
sbit LCD_RW = P2^5; //液晶讀/寫控制
sbit LCD_EN = P2^6; //液晶使能控制
sbit PSB=P2^1; //并口控制
sbit RES=P2^3;
uchar code dis1[] = {" 電子設(shè)計(jì)天地 "}; //液晶顯示的漢字***注意輸入漢字前或中間要加空格的話要加兩個(gè),不然亂碼。
uchar code dis2[] = {"有志者,事竟成!"};
uchar code dis4[] = {'0','1','2','3','4','5','6','7','8','9'};
unsigned char temp;
#define delayNOP() {_nop_();_nop_();_nop_();_nop_();};
void lcd_pos(uchar X,uchar Y); //確定顯示位置
unsigned char l_tmpdate[7]={0,7,16,19,10,1,9};//秒分時(shí)日月周年09-10-1916:07:00
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分時(shí)日月周年 最低位讀寫位
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
void Write_Ds1302_byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void Read_RTC(void);//read RTC
void Set_RTC(void); //set RTC
void InitTIMER0(void);//inital timer0
/*******************************************************************/
/* */
/* 延時(shí)函數(shù) */
/* */
/*******************************************************************/
void delay(unsigned int m) //延時(shí)程序
{
unsigned int i,j;
for(i=0;i<m;i++)
for(j=0;j<10;j++);
}
/*******************************************************************/
/* */
/*檢查LCD忙狀態(tài) */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫指令與數(shù)據(jù)。 */
/* */
/*******************************************************************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
/*******************************************************************/
/* */
/*寫指令數(shù)據(jù)到LCD */
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
/* */
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delay(1);
LCD_EN = 1;
delay(1);
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*寫顯示數(shù)據(jù)到LCD */
/*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。 */
/* */
/*******************************************************************/
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delay(1);
LCD_EN = 0;
}
/*******************************************************************/
/* */
/* LCD初始化設(shè)定 */
/* */
/*******************************************************************/
void lcd_init()
{
P0=0xFF;
P2=0xFF;
delay(40);
PSB=1; //并口方式。
delay(1);
RES=0;
delay(1);
RES=1;
delay(10);
lcd_wcmd(0x30);
delay(100);
lcd_wcmd(0x30);
delay(37);
lcd_wcmd(0x08);
delay(100);
lcd_wcmd(0x10);
delay(100);
lcd_wcmd(0x0c); //顯示開,關(guān)光標(biāo)
delay(100);
lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容
delay(100);
lcd_wcmd(0x06);
delay(100);
}
/*********************************************************/
/* */
/* 設(shè)定顯示位置 */
/* */
/*********************************************************/
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_wcmd(pos); //顯示地址
}
void InitTIMER0(void)
{
TMOD|=0x01;//定時(shí)器設(shè)置 16位
TH0=0xef;//初始化值
TL0=0xf0;
ET0=1;
TR0=1;
EA=1;
}
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for(i=0;i<8;i++) //循環(huán)8次 寫入數(shù)據(jù)
{
SCK=0;
SDA=temp&0x01; //每次傳輸?shù)妥止?jié)
temp>>=1; //右移一位
SCK=1;
}
}
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_(); //啟動(dòng)
Write_Ds1302_Byte(address); //發(fā)送地址
Write_Ds1302_Byte(dat); //發(fā)送數(shù)據(jù)
RST=0; //恢復(fù)
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循環(huán)8次 讀取數(shù)據(jù)
{
if(SDA)
temp|=0x80; //每次傳輸?shù)妥止?jié)
SCK=0;
temp>>=1; //右移一位
SCK=1;
}
RST=0;
_nop_(); //以下為DS1302復(fù)位的穩(wěn)定時(shí)間
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return(temp); //返回
}
/****************************************************************************/
void Read_RTC(void) //讀取 日歷
{
unsigned char i,*P;
P=read_rtc_address;//地址傳遞
for(i=0;i<7;i++) //分7次讀取 秒分時(shí)日月周年
{
l_tmpdate=Read_Ds1302 (*P);
P++;
}
}
/***********************************************************************/
void Set_RTC(void) //設(shè)定 日歷
{
unsigned char i,*p,tmp;
for(i=0;i<7;i++)
{ //BCD處理
tmp=l_tmpdate]/10;
l_tmpdate=l_tmpdate%10;
l_tmpdate=l_tmpdate+tmp*16;
}
Write_Ds1302(0x8E,0X00);
p=write_rtc_address; //傳地址
for(i=0;i<7;i++) //7次寫入 秒分時(shí)日月周年
{
Write_Ds1302(*p,l_tmpdate);
p++;
}
Write_Ds1302(0x8E,0x80);
}
void tim(void) interrupt 1 using 1//中斷,用于數(shù)碼管掃描
{
static unsigned char i,num;
TH0=0xf5;
TL0=0xe0;
//P0=table[l_tmpdisplay]; //查表法得到要顯示數(shù)字的數(shù)碼段
//P2=table1;
i++;
if(i==8)
{
i=0;
num++;
if(20==num) //隔段時(shí)間讀取1302的數(shù)據(jù)。時(shí)間間隔可以調(diào)整
{
ReadRTC_Flag=1; //使用標(biāo)志位判斷
num=0;
}
}
}
void main()
{
uchar i;
InitTIMER0();
Set_RTC();
lcd_init();
delay(100);
//初始化LCD
while(1)
{
if(ReadRTC_Flag)
{
ReadRTC_Flag=0;
Read_RTC();
lcd_pos(0,0); //設(shè)置顯示位置為第一行的第1個(gè)字符
i = 0;
while(dis1 != '\0')
{ //顯示字符
lcd_wdat(dis1);
i++;
}
lcd_pos(1,0); //設(shè)置顯示位置為第二行的第1個(gè)字符
i = 0;
delay(10);
while(dis2 != '\0')
{
lcd_wdat(dis2); //顯示字符
i++;
}
lcd_pos(2,0);
lcd_wdat(dis4[l_tmpdate[6]/16]); //DS1302是BCD碼,進(jìn)行處理得到十位與個(gè)位數(shù)。
lcd_pos(2,1);
lcd_wdat(dis4[l_tmpdate[6]%16]);
lcd_pos(2,2);
lcd_wdat('-');
lcd_pos(2,3);
lcd_wdat(dis4[l_tmpdate[4]/16]);
lcd_pos(2,4);
lcd_wdat(dis4[l_tmpdate[4]%16]);
lcd_pos(2,5);
lcd_wdat('-');
lcd_pos(2,6);
lcd_wdat(dis4[l_tmpdate[3]/16]);
lcd_pos(2,7);
lcd_wdat(dis4[l_tmpdate[3]%16]); //設(shè)置顯示位置為第三行的第1個(gè)字符
lcd_pos(3,0); //設(shè)置顯示位置為第四行的第1個(gè)字符
lcd_wdat(dis4[l_tmpdate[2]/16]);
lcd_pos(3,1);
lcd_wdat(dis4[l_tmpdate[2]%16]);
lcd_pos(3,2);
lcd_wdat(':');
lcd_pos(3,3);
lcd_wdat(dis4[l_tmpdate[1]/16]);
lcd_pos(3,4);
lcd_wdat(dis4[l_tmpdate[1]%16]);
lcd_pos(3,5);
lcd_wdat(':');
lcd_pos(3,6);
lcd_wdat(dis4[l_tmpdate[0]/16]);
lcd_pos(3,7);
lcd_wdat(dis4[l_tmpdate[0]%16]);
}
}
}
作者: dgahz 時(shí)間: 2014-11-28 16:09
網(wǎng)站復(fù)制粘貼有問題,粘貼過來會(huì)改動(dòng)。上面貼過來錯(cuò)的,看來要發(fā)附件才行。
作者: dgahz 時(shí)間: 2014-11-28 16:14
上附件
1302 12864.rar
(2.48 KB, 下載次數(shù): 102)
2014-11-28 16:14 上傳
點(diǎn)擊文件名下載附件
作者: liuzhaoxin1020 時(shí)間: 2017-1-8 16:31
不錯(cuò)的文件頂一下
作者: xuxuxuxu 時(shí)間: 2017-12-21 12:41
樓主 這個(gè)程序還有嗎
作者: xuxuxuxu 時(shí)間: 2017-12-21 12:42
我也需要一個(gè)利用帶字庫的12864還有ds1302實(shí)現(xiàn)的時(shí)鐘顯示 要求顯示時(shí)間年月日星期和復(fù)位功能的程序
歡迎光臨 (http://www.torrancerestoration.com/bbs/) |
Powered by Discuz! X3.1 |