久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3201|回復(fù): 13
打印 上一主題 下一主題
收起左側(cè)

12864+ds1302時(shí)鐘

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:68513 發(fā)表于 2014-11-14 13:29 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
程序編譯沒(méi)錯(cuò)誤,下載到開(kāi)發(fā)板就顯示不出來(lái),到網(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             //帶字庫(kù)液晶12864數(shù)據(jù)口
sbitLCD_RS = P2^4;            //寄存器選擇輸入
sbitLCD_RW = P2^5;            //液晶讀/寫(xiě)控制
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í)日月周年 最低位讀寫(xiě)位
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_busy1時(shí),忙,等待。lcd-busy0時(shí),閑,可寫(xiě)指令與數(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);
}
/*******************************************************************/
/*                                                                */
/*寫(xiě)指令數(shù)據(jù)到LCD                                                 */
/*RS=LRW=LE=高脈沖,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;
}
/*******************************************************************/
/*                                                                */
/*寫(xiě)顯示數(shù)據(jù)到LCD                                                 */
/*RS=HRW=LE=高脈沖,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);      //顯示開(kāi),關(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]);          //DS1302BCD碼,進(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次 寫(xiě)入數(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次寫(xiě)入 秒分時(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;
   }
   
    }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:51088 發(fā)表于 2014-11-15 18:08 來(lái)自手機(jī) | 只看該作者
io不對(duì)吧
回復(fù)

使用道具 舉報(bào)

板凳
ID:66729 發(fā)表于 2014-11-15 19:32 | 只看該作者
你這個(gè)并口顯示,如果你開(kāi)發(fā)板接口是串口顯示的肯定就不能顯示了吧。。。還有你開(kāi)發(fā)板接口你改對(duì)了么??晶振一般是12和11.0592兩種,你注意看清仔細(xì)了。
我也碰到過(guò)這個(gè)問(wèn)題的,多找?guī)讉(gè)程序試試就行了。
回復(fù)

使用道具 舉報(bào)

地板
ID:66729 發(fā)表于 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 //用于檢測(cè)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ù)開(kāi)始********************************************************/
void main(void)
{
        Delay400Ms();         //啟動(dòng)等待,等LCD講入工作狀態(tài)
        LCDInit();                 //LCM初始化
        Delay5Ms();         //延時(shí)片刻(可不要)
        while(1){
                LCDClear();
                DisplayListChar(0,1,uctech);        //顯示字庫(kù)中的中文數(shù)字
                DisplayListChar(0,2,net);                //顯示字庫(kù)中的中文數(shù)字
                DisplayListChar(0,3,mcu);                //顯示字庫(kù)中的中文
                DisplayListChar(0,4,qq);                //顯示字庫(kù)中的中文數(shù)字
                Delay400Ms();
                Delay400Ms();
                Delay400Ms();
                Delay400Ms();
                LCDFlash();                                                //閃爍效果
        }
}

/***********寫(xiě)數(shù)據(jù)********************************************************/
void WriteDataLCD(unsigned char WDLCD)
{
        ReadStatusLCD(); //檢測(cè)忙
        LCD_RS = 1;
        LCD_RW = 0;
        LCD_Data = WDLCD;
        LCD_E = 1;
        LCD_E = 1;
        LCD_E = 0;
}

/***********寫(xiě)指令********************************************************/
void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時(shí)忽略忙檢測(cè)
{
        if (BuysC) ReadStatusLCD(); //根據(jù)需要檢測(cè)忙
        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); //檢測(cè)忙信號(hào)        
        LCD_E = 0;
        return(1);
}

/***********初始化********************************************************/
void LCDInit(void)
{
        WriteCommandLCD(0x30,1); //顯示模式設(shè)置,開(kāi)始要求每次檢測(cè)忙信號(hào)
        WriteCommandLCD(0x01,1); //顯示清屏
        WriteCommandLCD(0x06,1); // 顯示光標(biāo)移動(dòng)設(shè)置
        WriteCommandLCD(0x0C,1); // 顯示開(kāi)及光標(biāo)設(shè)置
}

/***********清屏********************************************************/        
void LCDClear(void)
{
        WriteCommandLCD(0x01,1); //顯示清屏
        WriteCommandLCD(0x34,1); // 顯示光標(biāo)移動(dòng)設(shè)置
        WriteCommandLCD(0x30,1); // 顯示開(kāi)及光標(biāo)設(shè)置
}

/***********閃爍效果********************************************************/
void LCDFlash(void)
{
        WriteCommandLCD(0x08,1); //顯示清屏
        Delay400Ms();
        WriteCommandLCD(0x0c,1); // 顯示開(kāi)及光標(biāo)設(shè)置
        Delay400Ms();
        WriteCommandLCD(0x08,1); //顯示清屏
        Delay400Ms();
        WriteCommandLCD(0x0c,1); // 顯示開(kāi)及光標(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); //這里不檢測(cè)忙信號(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ù)來(lái)選擇相應(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--);
}

/***********長(zhǎng)延時(shí)********************************************************/
void Delay400Ms(void)
{
        unsigned char TempCycA = 5;
        unsigned int TempCycB;
        while(TempCycA--){
                  TempCycB=7269;
                  while(TempCycB--);
        }
}
回復(fù)

使用道具 舉報(bào)

5#
ID:67210 發(fā)表于 2014-11-27 22:52 | 只看該作者
為什么有的程序讀寫(xiě)的地址一樣也能夠成功啊?
回復(fù)

使用道具 舉報(bào)

6#
ID:67210 發(fā)表于 2014-11-27 22:56 | 只看該作者
我跟你寫(xiě)的差不多,也是讀取不到數(shù)據(jù)
回復(fù)

使用道具 舉報(bào)

7#
ID:67992 發(fā)表于 2014-11-28 15:45 | 只看該作者
forrest 發(fā)表于 2014-11-14 13:29:50 | 只看該作者 回帖獎(jiǎng)勵(lì)
程序編譯沒(méi)錯(cuò)誤,下載到開(kāi)發(fā)板就顯示不出來(lái),到網(wǎng)上找別人的程序下進(jìn)去也不行,求大神分析
#include<reg52.h>

看來(lái)你對(duì)數(shù)組和指針不是很熟,程序錯(cuò)誤很多,我費(fèi)了很大的勁,把它改好了。給你現(xiàn)成別人做好的程序?qū)δ銢](méi)幫助,這樣你永遠(yuǎn)不知你的程序錯(cuò)在哪里。
回復(fù)

使用道具 舉報(bào)

8#
ID:67992 發(fā)表于 2014-11-28 15:49 | 只看該作者
上調(diào)好的圖
回復(fù)

使用道具 舉報(bào)

9#
ID:67992 發(fā)表于 2014-11-28 15:50 | 只看該作者
本帖最后由 dgahz 于 2014-11-28 16:05 編輯

經(jīng)改動(dòng)過(guò)的程序:
#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             //帶字庫(kù)液晶12864數(shù)據(jù)口
sbit LCD_RS = P2^4;            //寄存器選擇輸入
sbit LCD_RW = P2^5;            //液晶讀/寫(xiě)控制
sbit LCD_EN = P2^6;     //液晶使能控制
sbit    PSB=P2^1;          //并口控制
sbit    RES=P2^3;
uchar code dis1[] = {"  電子設(shè)計(jì)天地 "};        //液晶顯示的漢字***注意輸入漢字前或中間要加空格的話(huà)要加兩個(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í)日月周年 最低位讀寫(xiě)位
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++);
}
/*******************************************************************/
/*                                                                 */
/*檢查L(zhǎng)CD忙狀態(tài)                                                   */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫(xiě)指令與數(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);
}
/*******************************************************************/
/*                                                                */
/*寫(xiě)指令數(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;
}
/*******************************************************************/
/*                                                                */
/*寫(xiě)顯示數(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);                //顯示開(kāi),關(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次 寫(xiě)入數(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次寫(xiě)入 秒分時(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]);
                  
                }
        }
}
回復(fù)

使用道具 舉報(bào)

10#
ID:67992 發(fā)表于 2014-11-28 16:09 | 只看該作者
網(wǎng)站復(fù)制粘貼有問(wèn)題,粘貼過(guò)來(lái)會(huì)改動(dòng)。上面貼過(guò)來(lái)錯(cuò)的,看來(lái)要發(fā)附件才行。
回復(fù)

使用道具 舉報(bào)

11#
ID:67992 發(fā)表于 2014-11-28 16:14 | 只看該作者
上附件 1302 12864.rar (2.48 KB, 下載次數(shù): 102)
回復(fù)

使用道具 舉報(bào)

12#
ID:153199 發(fā)表于 2017-1-8 16:31 | 只看該作者
不錯(cuò)的文件頂一下
回復(fù)

使用道具 舉報(bào)

13#
ID:264604 發(fā)表于 2017-12-21 12:41 | 只看該作者
樓主 這個(gè)程序還有嗎
回復(fù)

使用道具 舉報(bào)

14#
ID:264604 發(fā)表于 2017-12-21 12:42 | 只看該作者
我也需要一個(gè)利用帶字庫(kù)的12864還有ds1302實(shí)現(xiàn)的時(shí)鐘顯示 要求顯示時(shí)間年月日星期和復(fù)位功能的程序
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 蜜桃一区二区三区 | 美女久久久久久久久 | 亚洲 欧美 日韩 精品 | 在线观看成人小视频 | 久久综合一区二区三区 | 天天色天天 | 久久久2o19精品 | 夜夜爽99久久国产综合精品女不卡 | 久操国产 | 精品久久久久久亚洲精品 | av激情在线 | 91亚洲视频在线 | 国产精品一区在线观看你懂的 | 成在线人视频免费视频 | 成人免费视频网站在线看 | 国产98色在线 | 亚洲精品日韩在线 | 欧美一级在线免费 | 97精品久久 | 国产精品av久久久久久毛片 | 激情婷婷成人 | 97视频人人澡人人爽 | 久久久天天| 日韩一区二区av | 精品福利在线 | 精品国产乱码久久久久久老虎 | www.成人免费视频 | a在线视频 | 男女网站免费 | 一区在线免费视频 | 天天干天天操天天看 | 夜夜草| 国产羞羞视频在线观看 | 九九热在线观看视频 | 91国内外精品自在线播放 | 久久久久国 | 国产乱码高清区二区三区在线 | 色视频在线免费观看 | 欧美日韩精品影院 | 国产精品久久久久久久久久 | 日韩中文字幕一区二区三区 |