找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2281|回復: 0
收起左側

時鐘

[復制鏈接]
ID:129350 發(fā)表于 2016-7-5 20:50 | 顯示全部樓層 |閱讀模式
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
/*DS1302 端口設置 */
sbit SCK=P3^6;        //DS1302時鐘
sbit SDA=P3^4;      //DS1302 IO
sbit RST= P3^5;   // DS1302復位
bit ReadRTC_Flag;      //讀DS1302全局變量
/* 12864端口定義*/
#define LCD_data P0             //帶字庫液晶12864數據口
sbit LCD_RS = P2^4;            //寄存器選擇輸入
sbit LCD_RW = P2^5;            //液晶讀/寫控制
sbit LCD_EN = P2^6;     //液晶使能控制
sbit    PSB=P2^1;          //并口控制
sbit    RES=P2^7;
uchar code dis1[] = {"  電子設計天地 "};        //液晶顯示的漢字
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};//秒分時日月周年09-10-1916:07:00
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分時日月周年 最低位讀寫位
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
/*******************************************************************/
/*                                                                */
/* 延時函數                                                       */
/*                                                                */
/*******************************************************************/
void delay(unsigned int m)            //延時程序
{
  unsigned int i,j;
  for(i=0;i<m;i++)
     for(j=0;j<10;j++);
}
/*******************************************************************/
/*                                                                 */
/*檢查LCD忙狀態(tài)                                                   */
/*lcd_busy為1時,忙,等待。lcd-busy為0時,閑,可寫指令與數據。      */
/*                                                                */
/*******************************************************************/
bit lcd_busy()
{                          
    bit result;
    LCD_RS = 0;
    LCD_RW = 1;
    LCD_EN = 1;
    delayNOP();
    result = (bit)(P0&0x80);
    LCD_EN = 0;
    return(result);
}
/*******************************************************************/
/*                                                                */
/*寫指令數據到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;
}
/*******************************************************************/
/*                                                                */
/*寫顯示數據到LCD                                                 */
/*RS=H,RW=L,E=高脈沖,D0-D7=數據。                               */
/*                                                                */
/*******************************************************************/
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初始化設定                                                 */
/*                                                                */
/*******************************************************************/
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);  //顯示開,關光標
delay(100);
lcd_wcmd(0x01);  //清除LCD的顯示內容
delay(100);
lcd_wcmd(0x06);
delay(100);
     



}
/*********************************************************/
/*                                                      */
/* 設定顯示位置                                          */
/*                                                      */
/*********************************************************/
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;//定時器設置 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次 寫入數據
{
      SCK=0;
      SDA=temp&0x01;     //每次傳輸低字節(jié)
      temp>>=1;    //右移一位
      SCK=1;
}
}  
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )     
{
   RST=0;
   _nop_();
   SCK=0;
   _nop_();
   RST=1;
   _nop_();  //啟動
   Write_Ds1302_Byte(address); //發(fā)送地址
   Write_Ds1302_Byte(dat);   //發(fā)送數據
   RST=0;   //恢復
}
/****************************************************************************/
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次 讀取數據
   {  
     if(SDA)
     temp|=0x80;    //每次傳輸低字節(jié)
     SCK=0;
     temp>>=1;    //右移一位
     SCK=1;
   }
    RST=0;
_nop_();   //以下為DS1302復位的穩(wěn)定時間
    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次讀取 秒分時日月周年
{
  l_tmpdate[i]=Read_Ds1302 (*P);
  P++;
}
}
/***********************************************************************/
void Set_RTC(void)   //設定 日歷
{
    unsigned char i,*p,tmp;
for(i=0;i<7;i++)
{           //BCD處理
    tmp=l_tmpdate[i]/10;
    l_tmpdate[i]=l_tmpdate[i]%10;
    l_tmpdate[i]=l_tmpdate[i]+tmp*16;
}
    Write_Ds1302(0x8E,0X00);
    p=write_rtc_address; //傳地址
    for(i=0;i<7;i++)   //7次寫入 秒分時日月周年
    {
     Write_Ds1302(*p,l_tmpdate[i]);
     p++;
    }
    Write_Ds1302(0x8E,0x80);
}
void tim(void) interrupt 1 using 1//中斷,用于數碼管掃描
{
    static unsigned char i,num;
    TH0=0xf5;
    TL0=0xe0;
   //P0=table[l_tmpdisplay];   //查表法得到要顯示數字的數碼段
//P2=table1;
i++;
if(i==8)
    {
     i=0;
     num++;
     if(20==num)       //隔段時間讀取1302的數據。時間間隔可以調整
     {
         ReadRTC_Flag=1; //使用標志位判斷
         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);             //設置顯示位置為第一行的第1個字符
      i = 0;
    while(dis1[i] != '\0')
       {                         //顯示字符
         lcd_wdat(dis1[i]);
         i++;
       }
     
      lcd_pos(1,0);             //設置顯示位置為第二行的第1個字符
      i = 0;
    delay(10);
      while(dis2[i] != '\0')
      {
         lcd_wdat(dis2[i]);      //顯示字符
         i++;
      }
   lcd_pos(2,0);
   lcd_wdat(dis4[l_tmpdate[6]/16]);          //DS1302是BCD碼,進行處理得到十位與個位數。
   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]);           //設置顯示位置為第三行的第1個字符
      
   lcd_pos(3,0);             //設置顯示位置為第四行的第1個字符
        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]);
   
  }
}
}
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表