ds1302 clock program controlled by microcontroller with alarm function

Publisher:逍遥游侠Latest update time:2015-06-26 Source: 51heiKeywords:ds1302 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Modify the port yourself to use the 1602 LCD display

#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit nz=P2^3;//alarm clock
sbit lcdrs=P3^5;
sbit lcdwr =P3^6;
sbit lcden=P3^4;//LCD enable
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit ajjd=P3^ 7;//Button
sbit clk=P2^1;
sbit io=P2^0;
sbit rst=P2^5;
void naozhongscan();
uchar num0,num1,num2,num3,num4;
void init();
void delay( uchar);
void keyscan();
void didi();
uchar lcd_date_add[7]={0x40+9,0x40+6,0x40+3,0xa,7,0xd,4};
uchar date_write_add[7]={0x80,0x82,0x84,0x86,0x88,0x8a ,0x8c};//miao fen shi ri yue zhou nian
uchar date_read_add[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar date[7]={30,32,16,27, 2,7,11};
uchar date1[7];
uchar naozhong[7]={35,32,16,27,2,7,11};
uchar flag,flag1,flag2;
uchar code table0[]=" - - ";
uchar code table1[]=" : : ";
void lcd_com(uchar);
void lcd_date(uchar);
void lcd_time(uchar,uchar);
void write_ds1302(uchar add,uchar date);
uchar read_ds1302(uchar add);
void read_rtc();
void set_ds1302(uchar add,uchar time);
void display();
void init_ds1302();
void main()
{
 wela=1;
 P1=0xff;
 wela=0;
 init ();
    init_ds1302();
 while(1)
 {
  if(flag1==1)
  naozhongscan();
  read_rtc();
  if(flag==0)
  display();
  keyscan();
 } 
}
void init()
{
 flag =0;
 flag1=0;
 flag2=0;
 lcden=0;
 lcdwr=0;
 num2=0;
 num3=0;
    num4=0;
 lcd_com(0x38); //Display mode setting, 16*2 display, 5*7 dot matrix, 8-bit data interface
 lcd_com(0x01); //Clear screen, display, Pointer clear
 lcd_com(0x0c); //Display switch and cursor setting, open display, display cursor, cursor flashing
 lcd_com(0x06); //Pointer plus one after reading and writing cursor plus one
 lcd_com(0x80); //From the first
 for(num0=0;num0<16;num0++) lcd_date
  (table0[num0]);
    lcd_com(0x80+0x40);
 for(num0=0;num0<11;num0++)
  lcd_date(table1[num0] );
}
void init_ds1302()
{
   uchar i,j;
 uchar flag3;
  flag3=read_ds1302(0x81);
  if(flag3&0x80)//If ch=1, initialize
 {
  write_ds1302(0x8e,0x00);
 for(i=0;i<7;i++)
 {
 j=date[i]/10;
 date[i]=date[i]%10;
 date[i]=date[i]+j*16;
 write_ds1302(date_write_add[i],date[i]);
 }
 write_ds1302(0x8e,0x80);
  } 
}
void delay(uchar z)
{
 uchar x,y;
 for(x=110;x>0;x--)
  for(y=z;y>0;y--); 
}
void didi()
{
 nz=0;
 delay(50);
 nz=1;
 delay(100);
 nz=0;
 delay(50);
 nz=1;
}
void lcd_com(uchar com)
{
 lcdrs=0;
 P0=com;
 delay(5);
 lcden= 1;
 delay(5);
 lcden=0; 
}
void lcd_date(uchar date)
{
 lcdrs=1;
 P0=date;
 delay(5);
 lcden=1;
 delay(5);
 lcden=0;
}
void lcd_time(uchar add,uchar Date)
 {
  uchar shi,ge;
  shi=Date/10;
  ge=Date%10;
  lcd_com(0x80+add);
  lcd_date(0x30+shi) ;
  lcd_date(0x30+ge);  
 }
void keyscan()
 {
  ajjd=0;
  if(num3!=2)
  {
  if(k1==0)
  {
   delay(5);
   if(k1==0)
   {
    while(! k1);
    
    num2++;
    if(num2==1)
    {
     flag=1;
     lcd_com(0x80+lcd_date_add[0]);
        lcd_com(0x0f); //Display switch and cursor setting, turn on display, display cursor, cursor flashing
    }
    if (num2==2)
    {
     lcd_com(0x80+lcd_date_add[1]);
    }
    if(num2==3)
    {
     lcd_com(0x80+lcd_date_add[2]);
    }
    if(num2==4)
    {
     lcd_com(0x80+lcd_date_add[3]);
    }
    if(num2==5)
    {
     lcd_com(0x80+ lcd_date_add[4]);
    }
    if(num2==6)
    {
     lcd_com(0x80+lcd_date_add[5]);
    }
    if(num2==7)
    {
     lcd_com(0x80+lcd_date_add[6]);
    }
    if(num2== 8)
    {
        lcd_com(0x0c);//Close the cursor
     num2=0;
     flag=0;
    }
    }
  }
 if(num2!=0)
{
  if(k2==0)
  {
   delay(5);
   if(k2==0 )
   {
      while(!k2);
       if(num2==1)
    {
     date[0]++;
     if(date[0]==60)
      date[0]=0;
     _nop_();
                    lcd_time(lcd_date_add[0],date[0]);
     lcd_com(0x80+lcd_date_add[0]);
                    set_ds1302(date_write_add[0],date[0]);
    }
       if(num2==2)
    {
     date[1]+ +;
     if(date[1]==60)
      date[1]=0;
     _nop_();
                    lcd_time(lcd_date_add[1],date[1]);
     lcd_com(0x80+lcd_date_add[1]);
                    set_ds1302(date_write_add[ 1],date[1]);
    }
       if(num2==3)
    {
     date[2]++;
     if(date[2]==24)
      date[2]=0;
     _nop_();
                    lcd_time(lcd_date_add[ 2],date[2]);
     lcd_com(0x80+lcd_date_add[2]);
                    set_ds1302(date_write_add[2],date[2]);
    }
       if(num2==4)
    {
     date[3]++;
     if(date[3]==32)
      date [3]=1;
     _nop_();
                    lcd_time(lcd_date_add[3],date[3]);
     lcd_com(0x80+lcd_date_add[3]);
                    set_ds1302(date_write_add[3],date[3]);
    }
       if(num2 ==5)
    {
     date[4]++;
     if(date[4]==13)
      date[4]=1;
     _nop_();
                    lcd_time(lcd_date_add[4],date[4]);
     lcd_com(0x80+ lcd_date_add[4]);
                    set_ds1302(date_write_add[4],date[4]);
    }
       if(num2==6)
    {
     date[5]++;
     if(date[5]==8)
      date[5]=1;
     _nop_();
                    lcd_time(lcd_date_add[5],date[5]);
     lcd_com(0x80+lcd_date_add[5]);
                    set_ds1302(date_write_add[5],date[5]);
    }
       if(num2==7)
    {
     date[6]+ +;
     if(date[6]==99)
      date[6]=1;
     _nop_();
                    lcd_time(lcd_date_add[6],date[6]);
     lcd_com(0x80+lcd_date_add[6]);
                    set_ds1302(date_write_add[ 6],date[6]);
    }
   }
  }
  if(k3==0)
  {
   delay(5);
   if(k3==0)
   {
      while(!k3);
       if(num2==1)
    {
     date[0]--;
     if(date[0]== -1)
      date[0]=59;
     _nop_();
                    lcd_time(lcd_date_add[2],date[0]);
     lcd_com(0x80+lcd_date_add[0]);
                    set_ds1302(date_write_add[0],date[0]);
    }
       if(num2==2)
    {
     date[1]--;
     if(date[1]==-1)
      date[1]=59;
     _nop_() ;
                    lcd_time(lcd_date_add[1],date[1]);
     lcd_com(0x80+lcd_date_add[1]);
                    set_ds1302(date_write_add[1],date[1]);
    }
       if(num2==3)
    {
     date[2] --;
     if(date[2]==-1)
      date[2]=23;
     _nop_();
                    lcd_time(lcd_date_add[2],date[2]);
     lcd_com(0x80+lcd_date_add[2]);
                    set_ds1302( date_write_add[2],date[2]);
    }
       if(num2==4)
    {
     date[3]--;
     if(date[3]==0)
      date[3]=31;
     _nop_();
                    lcd_time(lcd_date_add[3],date[3]);
     lcd_com (0x80+lcd_date_add[3]);
                    set_ds1302(date_write_add[3],date[3]);
    }
       if(num2==5)
    {
     date[4]--;
     if(date[4]==0)
      date[ 4]=12;
     _nop_();
                    lcd_time(lcd_date_add[4],date[4]);
     lcd_com(0x80+lcd_date_add[4]);
                    set_ds1302(date_write_add[4],date[4]);
    }
       if(num2= =6)
    {
     date[5]--;
     if(date[5]==0)
      date[5]=7;
     _nop_();
                    lcd_time(lcd_date_add[5],date[5]);
     lcd_com(0x80+lcd_date_add[5]);
                    set_ds1302(date_write_add[5],date[5]);
    }
       if( num2==7)
    {
     date[6]--;
     if(date[6]==0)
      date[6]=99;
     _nop_();
                    lcd_time(lcd_date_add[6],date[6]);
     lcd_com(0x80 +lcd_date_add[6]);
                    set_ds1302(date_write_add[6],date[6]);
    }
   }
  }    
 }
}[page]
   if(k4==0)
   {
    delay(5);
    if(k4==0)
    {
     while (!k4);
     num3++;
    }
   }
     if(num3==1)
     {
      flag1=1;
                        lcd_com(0x80+1);
      lcd_date(0x41); 
     }
     if(num3==2)
      {       
             ajjd=0;
             flag1=0;
             if(num4==0)
             {
             for(num0=0;num0<7;num0++)
                                                    lcd_time(lcd_date_add[num0],naozhong[num0]);
             
                               lcd_com(0x80+1);
             lcd_com(0x0c);//Turn off the cursor
             lcd_date(0x42);
             }
             //lcd_com(0x0f);
             flag=1;
             if(k1==0)
             {
              delay(5);
              if(k1==0)
              {
               while(!k1);
               
               num4++;
               if(num4==1)
               {
                
                lcd_com(0x80+lcd_date_add[0]);
                   lcd_com(0x0f);//Display switch and cursor setting, turn on display, display cursor, cursor blinking
               }
               if(num4==2)
               {
                lcd_com(0x80+lcd_date_add[1]);
               }
               if(num4==3)
               {
                lcd_com(0x80+lcd_date_add[2]);
               }
               if(num4==4)
               {
                lcd_com(0x80+lcd_date_add[3]);
               }
               if(num4==5)
               {
                lcd_com(0x80+lcd_date_add[4]);
               }
               if(num4==6)
               {
                lcd_com(0x80+lcd_date_add[5]);
               }
               if(num 4==7)
               {
                lcd_com(0x80+lcd_date_add[6]);
               }
               if(num4==8)
               {
                   lcd_com(0x0c);//Turn off the cursor
                num4=0;
                flag=1;
               }
              }
             }
            if(num4!=0)
           {
             if(k2==0)
             {
              delay(5);
              if(k2==0)
              {
                 while(!k2);
                  if(num4==1)
               {
                naozhong[0]++;
                if(naozhong[0]==60)
                 naozhong[0]=0;
                _nop_();
                               lcd_time(lcd_date_add[0],naozhong[0]);
                lcd_com(0x80+lcd_date_add[0]);
                               
               }
                  if(num4==2)
               {
                naozhong[1]++;
                if(naozhong[1]==60)
                 naozhong[1]=0;
                _nop_();
                               lcd_time(lcd_date_add[1],naozhong[1]);
                lcd_com(0x80+lcd_date_add[1]) ;
                               
               }
                  if(num4==3)
               {
                naozhong[2]++;
                if(naozhong[2]==24)
                 naozhong[2]=0;
                _nop_();
                               lcd_time(lcd_date_add[2],naozhong[2]) ;
                lcd_com(0x80+lcd_date_add[2]);
                               
               }
                  if(num4==4)
               {
                naozhong[3]++;
                if(naozhong[3]==32)
                 naozhong[3]=1;
                _nop_();
                               lcd_time(lcd_date_add[3],naozhong[3]);
                lcd_com(0x80+lcd_date_add[3]);
                              
               }
                  if(num4==5)
               {
                naozhong[4]++;
                if(naozhong[4]==13)
                 naozhong [4]=1;
                _nop_();
                               lcd_time(lcd_date_add[4],naozhong[4]);
                lcd_com(0x80+lcd_date_add[4]);
                             
               }
                  if(num4==6)
               {
                naozhong[5]++;
                if (naozhong[5]==8)
                 naozhong[5]=1;
                _nop_();
                               lcd_time(lcd_date_add[5],naozhong[5]);
                lcd_com(0x80+lcd_date_add[5]);
                            
               }
                  if(num4==7 )
               {
                naozhong[6]++;
                if(naozhong[6]==99)
                 naozhong[6]=1;
                _nop_();
                               lcd_time(lcd_date_add[6],naozhong[6]);
                lcd_com(0x80+lcd_date_add[6]) ;
                              
               }
              }
             }
             if(k3==0)
             {
              delay(5);
              if(k3==0)
              {
                 while(!k3);
                  if(num4==1)
               {
                naozhong[0]--;
                if(naozhong[ 0]==-1)
                 naozhong[0]=59;
                _nop_();
                               lcd_time(lcd_date_add[2],naozhong[0]);
                lcd_com(0x80+lcd_date_add[0]);
                             
               }
                  if(num4==2)
               {
                naozhong[1]--;
                if(naozhong[1]==-1)
                 naozhong[1]=59;
                _nop_();
                               lcd_time(lcd_date_add[1],naozhong[1]);
                lcd_com(0x80+lcd_date_add[1]);
                           
               }
                  if(num4==3)
               {
                naozhong[2]--;
                if(naozhong[2]==-1)
                 naozhong[2]=23;
                _nop_();
                               lcd_time(lcd_date_add[2],naozhong[2]);
                lcd_com(0x80+lcd_date_add[2]);
                             
               }
                  if(num4= =4)
               {
                naozhong[3]--;
                if(naozhong[3]==0)
                 naozhong[3]=31;
                _nop_();
                               lcd_time(lcd_date_add[3],naozhong[3]);
                lcd_com(0x80+lcd_date_add[3] );
                            
               }
                  if(num4==5)
               {
                naozhong[4]--;
                if(naozhong[4]==0)
                 naozhong[4]=12;
                _nop_();
                               lcd_time(lcd_date_add[4],naozhong[4] );
                lcd_com(0x80+lcd_date_add[4]);
                           
               }
                  if(num4==6)
               {
                naozhong[5]--;
                if(naozhong[5]==0)
                 naozhong[5]=7;
                _nop_();
                               lcd_time(lcd_date_add[5],naozhong[5]);
                lcd_com(0x80+lcd_date_add[5]);
                      
               }
                  if(num4==7)
               {
                naozhong[6]--;
                if(naozhong[6]==0)
                 naozhong [6]=99;
                _nop_();
                               lcd_time(lcd_date_add[6],naozhong[6]);
                lcd_com(0x80+lcd_date_add[6]);
                        
               }
              }
             }               } }
            if          (num3==3)          {          flag=0;          flag1=0;          lcd_com(0x80+1);






         lcd_date(20);//Let LCD display nothing
         num3=0;
         num4=0;
         
                        }
       
}
 
 void write_ds1302(uchar add,uchar date)
 {
  uchar i,temp;
 rst=0;
 clk=0;
 _nop_();
 rst=1;
 for(i=0;i<8;i++)
 {
  clk=0;
  temp=add;
  io=temp&0x01;
  add>>=1;
  clk=1;
  _nop_();
 }
 for(i=0; i<8;i++)
 {
  clk=0;
  temp=date;
  io=temp&0x01;
  date>>=1;
  clk=1;
  _nop_(); 
 }
 rst=0;
 }
 uchar read_ds1302(uchar add)
 {
  uchar i,temp ,data1,j;
 rst=0;
 clk=0;
 _nop_();;
 rst=1;
 for(i=0;i<8;i++)
 {
  clk=0;
  temp=add;
  io=temp&0x01;
  add>>=1;
  clk=1;
  _nop_();
 }
 for(i=0; i<8;i++)
 {
    data1>>=1;
  clk=0;
  if(io)
  data1=data1|0x80;
  clk=1;
 }
 rst=0;
 _nop_();
 j=data1%16;
 data1=data1/ 16;
 data1=data1*10+j;
 return(data1);
 }
 void set_ds1302(uchar add,uchar time)
 {
  uchar j;
  write_ds1302(0x8e,0x00);
 j=time/10;
 time=time%10;
 time= time+j*16;
 write_ds1302(add,time);
 write_ds1302(0x8e,0x80);
 }
 void read_rtc()
 {
  uchar i;
 for(i=0;i<7;i++)
 {
  date[i]=read_ds1302(date_read_add[i]); 
 } 
 }
void display()
{
        uchar i;
  for(i=0;i<7;i++)
  {
   lcd_time(lcd_date_add[i],date[i]); 
  }
}
void naozhongscan()
{
 uchar i=0;
 uchar j=0;
   while((j= =0)&&(i<7))
{
 j=naozhong[i]^date[i];
 i++; 
}
 if(i==7)
    didi();
}

Keywords:ds1302 Reference address:ds1302 clock program controlled by microcontroller with alarm function

Previous article:Digital tube electronic calendar made by 51 single chip microcomputer
Next article:Microcontroller measuring bicycle speed program - with simulation

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号