程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 51時鐘含鬧鐘(可按鍵設置)

51時鐘含鬧鐘(可按鍵設置)

編輯:C++入門知識

      51時鐘(含鬧鐘)是我做的第一個小東西,包括焊版,包括寫代碼,完了以後雖然覺得沒什麼,但是過程還是很享受的,下一個小東西是集成12864,紅外,18B20和DS12C887來做個可以顯示萬年歷和溫度並且用紅外來選擇功能的小東西。下面是時鐘的代碼:

 

 

[cpp]  <SPAN style="FONT-SIZE: 18px">#include<reg52.h> 
#include<define.h>  
/*延時函數,毫秒級*/ 
void delay(uint z) 

    uint x,y; 
    for(x=z;x>0;x--) 
        for(y=110;y>0;y--); 

/*鬧鈴響函數*/ 
void music()  

    beep=0; 
    delay(50); 
    beep=1; 
    delay(100); 
    beep=0; 
    delay(100); 
    beep=1; 

/*寫命令函數,1604液晶*/ 
void write_com(uchar com) 

    rs=0; 
    wr=0; 
    P1=com; 
    lcden=0; 
    delay(10); 
    lcden=1; 
    delay(10); 
    lcden=0;     

/*寫數據函數,1604液晶*/ 
void write_date(uchar date) 

    rs=1; 
    wr=0; 
    P1=date; 
    lcden=0; 
    delay(10); 
    lcden=1; 
    delay(10); 
    lcden=0;     

/*初始化函數*/ 
void init() 

    uchar num; 
    EA=1; 
    EX0=1; 
    IT0=1; 
 
    lcden=0; 
    //write_ds(0x0A,0x20);//打開振蕩器  
    read_ds(0x0c); 
    write_com(0x80); 
    for(num=0;num<15;num++) 
    { 
        write_date(table[num]); 
        delay(5); 
    } 
    write_com(0x80+0x40); 
    for(num=0;num<12;num++) 
    { 
        write_date(table1[num]); 
        delay(5); 
    } 

/*寫時間函數*/ 
void write_sfm(uchar add,uchar date) 

    uchar shi,ge; 
    shi=date/10; 
    ge=date%10; 
    write_com(0x80+0x40+add); 
    write_date(0x30+shi); 
    write_date(0x30+ge); 

/*寫年月日函數*/ 
void write_ymd(uchar add,uint num) 

 
    write_com(0x80+add); 
    temp=num/10; 
    write_date(0x30+temp); 
    temp=num%10; 
    write_date(0x30+temp); 
 

/*寫星期函數*/ 
void write_week() 

    write_com(0x80+0x0d); 
    switch(week) 
    { 
    case 1:write_date('M'); 
           write_date('O'); 
           write_date('N'); 
           break; 
    case 2:write_date('T'); 
           write_date('U'); 
           write_date('E'); 
           break; 
    case 3:write_date('W'); 
           write_date('E'); 
           write_date('D'); 
           break; 
    case 4:write_date('T'); 
           write_date('H'); 
           write_date('U'); 
           break; 
    case 5:write_date('F'); 
           write_date('R'); 
           write_date('I'); 
           break; 
    case 6:write_date('S'); 
           write_date('A'); 
           write_date('T'); 
           break; 
    case 7:write_date(0x53); 
           write_date('U'); 
           write_date('N'); 
           break; 
    } 
}        
/*鍵盤掃描函數*/ 
void keyscan() 

    /*若是功能鍵被按下*/     
    if(s1==0)    
    { 
        delay(5); 
        if(s1==0) 
        {    
            s1num++; 
            flag=1; 
            while(!s1); 
            if(s1num==1)//秒  
            { 
                write_com(0x80+0x40+10); 
                write_com(0x0f); 
            } 
            if(s1num==2)//分  
            { 
                write_com(0x80+0x40+7); 
            } 
            if(s1num==3)//時  
            { 
                write_com(0x80+0x40+4); 
            } 
            if(flag1==1)s1num=8;//若是設置鬧鈴,則跳過年月日的設置  
            if(s1num==4)//星期  
            { 
                write_com(0x80+0x0D); 
            } 
            if(s1num==5)//天  
            { 
                write_com(0x80+0x0a); 
            } 
            if(s1num==6)//月  
            { 
                write_com(0x80+0x07); 
            } 
            if(s1num==7)//年  
            { 
                write_com(0x80+0x04); 
            } 
            if(s1num==8)//退出  
            { 
                s1num=0; 
                write_com(0x0c); 
                flag=0;//退出功能鍵  
                if(flag1==0)//若不是設置鬧鈴  
                { 
                    lcd_reset(); 
                    init(); 
                    write_ds(0,miao); 
                    write_ds(2,fen); 
                    write_ds(4,shi); 
                    write_ds(6,week); 
                    write_ds(7,day); 
                    write_ds(8,month); 
                    write_ds(9,year); 
                } 
                else//若是鬧鈴設置,則將設定的時間付給鬧鈴時間變量,回到初始化界面  
                { 
                    write_ds(1,miao);//寫入鬧鈴單元  
                    write_ds(3,fen); 
                    write_ds(5,shi); 
                    lcd_reset(); 
                    init(); 
                } 
 
            } 
 
         }//if(s2==0)         
        }//if(s2==0)  
        if(s1num!=0) 
        { 
            //增加鍵  
            if(s2==0) 
            { 
                delay(1); 
                if(s2==0) 
                { 
                    while(!s2); 
                    if(s1num==1) 
                    { 
                        miao++; 
                        if(miao==60) 
                            miao=0; 
                        write_sfm(10,miao); 
                        write_com(0x80+0x40+10); 
                    } 
                    if(s1num==2) 
                    { 
                        fen++; 
                        if(fen==60) 
                            fen=0; 
                        write_sfm(7,fen); 
                        write_com(0x80+0x40+7); 
                    } 
                    if(s1num==3) 
                    { 
                        shi++; 
                        if(shi==24) 
                            shi=0; 
                        write_sfm(4,shi); 
                        write_com(0x80+0x40+4); 
                    } 
                    if(s1num==4) 
                    { 
                        week++; 
                        if(week==8)week=1; 
                        write_week(); 
                        write_com(0x80+0x0D); 
                    } 
                    if(s1num==5)//日  
                    { 
                        day++; 
                        if(day==31)day=1; 
                        write_ymd(0x0a,day); 
                        write_com(0x80+0x0a); 
                    } 
                    if(s1num==6)//月  
                    { 
                        month++; 
                        if(month==12)month=1; 
                        write_ymd(7,month); 
                        write_com(0x80+0x07); 
                    } 
                    if(s1num==7)//年  
                    { 
                        year++; 
                        if(year==100)year=0; 
                        write_ymd(4,year); 
                        write_com(0x80+0x04); 
                    } 
                }//if(s2==0)  
            }//if(s2==0)  
            //減少鍵  
            if(s3==0) 
            { 
                delay(5); 
                if(s3==0) 
                { 
                    while(!s3); 
                    if(s1num==1) 
                    { 
                        if(miao==0) 
                        { 
                            miao=59; 
                            write_sfm(10,miao); 
                            write_com(0x80+0x40+10); 
                        } 
                        miao--; 
                        if(miao==-1) 
                            miao=59; 
                        write_sfm(10,miao); 
                        write_com(0x80+0x40+10); 
                    } 
                    if(s1num==2) 
                    { 
                        fen--; 
                        if(fen==-1) 
                            fen=59; 
                        write_sfm(7,fen); 
                        write_com(0x80+0x40+7); 
                    } 
                    if(s1num==3) 
                    { 
                        shi--; 
                        if(shi==-1) 
                            shi=23; 
                        write_sfm(4,shi); 
                        write_com(0x80+0x40+4); 
                    } 
                    if(s1num==4)//  
                    { 
                        week--; 
                        if(week==-1)week=7; 
                        write_week(); 
                        write_com(0x80+0x0C); 
                    } 
                    if(s1num==5)//日  
                    { 
                        day--; 
                        if(day==-1)day=31; 
                        write_ymd(0x0a,day); 
                        write_com(0x80+0x0a); 
                    } 
                    if(s1num==6)//月  
                    { 
                        month--; 
                        if(month==-1)month=12; 
                        write_ymd(7,month); 
                        write_com(0x80+0x07); 
                    } 
                    if(s1num==7)//年  
                    { 
                        year--; 
                        if(year==-1)year=99; 
                        write_ymd(4,year); 
                        write_com(0x80+0x04); 
                    } 
                }//if(s3==0)  
            }//if(s3==0)  
            //鬧鈴鍵  
            if(s4==0) 
            { 
                delay(5); 
                if(s4==0) 
                { 
                    flag1=1;//鬧鈴設置開啟  
                    while(!s4); 
                    /*在液晶上顯示設置鬧鈴的界面*/ 
                    lcd_reset(); 
                    write_com(0x80); 
                    while(table2[i]!='\0') 
                    { 
                        write_date(table2[i]); 
                        i++; 
                    } 
                    i=0; 
                    write_com(0x80+0x40); 
                    while(table1[i]!='\0') 
                    { 
                        write_date(table1[i]); 
                        i++; 
                    } 
                    write_sfm(10,miao); 
                    write_sfm(7,fen); 
                    write_sfm(4,shi); 
                    /*顯示出光標*/ 
                    write_com(0x80+0x40+10); 
                    write_com(0x0f); 
                    i=0; 
                }//if(s4==0)  
            }//if(s4==0)  
        }//if(s1num!=0)  
        /*若鬧鈴響則按下鬧鈴鍵取消*/ 
        if(flag2)  
        { 
                if(s4==0) 
                { 
                    delay(5); 
                    if(s4==0) 
                    { 
                        while(!s4); 
                        flag2=0; 
                    } 
                } 
        } 
}  
/*寫數據函數*/ 
void write_ds(uchar add,uchar date) 

    dscs=0; 
    dsas=1; 
    dsds=1; 
    dsrw=1; 
    P0=add; 
    dsas=0; 
    dsrw=0; 
    P0=date; 
    dsrw=1; 
    dsas=1; 
    dscs=1;              

/*讀數據函數*/ 
uchar read_ds(uchar add) 

    uchar ds_date; 
    dsas=1; 
    dsds=1; 
    dsrw=1; 
    dscs=0; 
    P0=add; 
    dsas=0; 
    dsds=0; 
    P0=0xff; 
    ds_date=P0; 
    dsds=1; 
    dsas=1; 
    dscs=1; 
    return ds_date;  

/*液晶初始化函數*/ 
void lcd_reset() 

    delay(15); 
    write_com(0x38); 
    delay(5); 
    write_com(0x38); 
    delay(5); 
    write_com(0x38); 
    delay(5); 
    write_com(0x08); 
    write_com(0x01); 
    write_com(0x06); 
    write_com(0x0c);     

/*主函數*/ 
void main() 

    lcd_reset(); 
    init(); 
    while(1) 
    { 
        keyscan(); 
        asec=read_ds(1);//讀出鬧鈴時間  
        amin=read_ds(3); 
        ahour=read_ds(5); 
        if(flag==0)//若是功能鍵未被按下,則運行時鐘  
        { 
            miao=read_ds(0);     
            fen=read_ds(2); 
            shi=read_ds(4); 
            week=read_ds(6); 
            day=read_ds(7); 
            month=read_ds(8); 
            year=read_ds(9); 
            if(asec==miao&&amin==fen&&ahour==shi)flag2=1;//鬧鈴標志  
            write_sfm(10,miao); 
            write_sfm(7,fen); 
            write_sfm(4,shi); 
            write_week(); 
            write_ymd(0x0a,day); 
            write_ymd(7,month); 
            write_ymd(4,year); 
             
        } 
        if(flag2)music();//鬧鈴響  
    } 

/*外部中斷函數*/ 
void exter() interrupt 0 
{    
    uchar c; 
    c=read_ds(0x0c); 

 
 
 
</SPAN> 

#include<reg52.h>
#include<define.h>
/*延時函數,毫秒級*/
void delay(uint z)
{
 uint x,y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}
/*鬧鈴響函數*/
void music()
{
 beep=0;
 delay(50);
 beep=1;
 delay(100);
 beep=0;
 delay(100);
 beep=1;
}
/*寫命令函數,1604液晶*/
void write_com(uchar com)
{
 rs=0;
 wr=0;
 P1=com;
 lcden=0;
 delay(10);
 lcden=1;
 delay(10);
 lcden=0; 
}
/*寫數據函數,1604液晶*/
void write_date(uchar date)
{
 rs=1;
 wr=0;
 P1=date;
 lcden=0;
 delay(10);
 lcden=1;
 delay(10);
 lcden=0; 
}
/*初始化函數*/
void init()
{
 uchar num;
 EA=1;
 EX0=1;
 IT0=1;

 lcden=0;
 //write_ds(0x0A,0x20);//打開振蕩器
 read_ds(0x0c);
 write_com(0x80);
 for(num=0;num<15;num++)
 {
  write_date(table[num]);
  delay(5);
 }
 write_com(0x80+0x40);
 for(num=0;num<12;num++)
 {
  write_date(table1[num]);
  delay(5);
 }
}
/*寫時間函數*/
void write_sfm(uchar add,uchar date)
{
 uchar shi,ge;
 shi=date/10;
 ge=date%10;
 write_com(0x80+0x40+add);
 write_date(0x30+shi);
 write_date(0x30+ge);
}
/*寫年月日函數*/
void write_ymd(uchar add,uint num)
{

 write_com(0x80+add);
 temp=num/10;
 write_date(0x30+temp);
 temp=num%10;
 write_date(0x30+temp);

}
/*寫星期函數*/
void write_week()
{
 write_com(0x80+0x0d);
 switch(week)
 {
 case 1:write_date('M');
     write_date('O');
     write_date('N');
     break;
 case 2:write_date('T');
     write_date('U');
        write_date('E');
     break;
 case 3:write_date('W');
     write_date('E');
     write_date('D');
     break;
 case 4:write_date('T');
     write_date('H');
     write_date('U');
     break;
 case 5:write_date('F');
     write_date('R');
     write_date('I');
     break;
 case 6:write_date('S');
     write_date('A');
     write_date('T');
     break;
 case 7:write_date(0x53);
     write_date('U');
     write_date('N');
     break;
 }
}  
/*鍵盤掃描函數*/
void keyscan()
{
 /*若是功能鍵被按下*/ 
 if(s1==0) 
 {
  delay(5);
  if(s1==0)
  { 
   s1num++;
   flag=1;
   while(!s1);
   if(s1num==1)//秒
   {
    write_com(0x80+0x40+10);
    write_com(0x0f);
   }
   if(s1num==2)//分
   {
    write_com(0x80+0x40+7);
   }
   if(s1num==3)//時
   {
    write_com(0x80+0x40+4);
   }
   if(flag1==1)s1num=8;//若是設置鬧鈴,則跳過年月日的設置
   if(s1num==4)//星期
   {
    write_com(0x80+0x0D);
   }
   if(s1num==5)//天
   {
    write_com(0x80+0x0a);
   }
   if(s1num==6)//月
   {
    write_com(0x80+0x07);
   }
   if(s1num==7)//年
   {
    write_com(0x80+0x04);
   }
   if(s1num==8)//退出
   {
    s1num=0;
    write_com(0x0c);
    flag=0;//退出功能鍵
    if(flag1==0)//若不是設置鬧鈴
    {
     lcd_reset();
     init();
     write_ds(0,miao);
     write_ds(2,fen);
     write_ds(4,shi);
     write_ds(6,week);
     write_ds(7,day);
     write_ds(8,month);
     write_ds(9,year);
    }
    else//若是鬧鈴設置,則將設定的時間付給鬧鈴時間變量,回到初始化界面
    {
     write_ds(1,miao);//寫入鬧鈴單元
     write_ds(3,fen);
     write_ds(5,shi);
     lcd_reset();
     init();
    }

   }

   }//if(s2==0)  
  }//if(s2==0)
  if(s1num!=0)
  {
   //增加鍵
   if(s2==0)
   {
    delay(1);
    if(s2==0)
    {
     while(!s2);
     if(s1num==1)
     {
      miao++;
      if(miao==60)
       miao=0;
      write_sfm(10,miao);
      write_com(0x80+0x40+10);
     }
     if(s1num==2)
     {
      fen++;
      if(fen==60)
       fen=0;
      write_sfm(7,fen);
      write_com(0x80+0x40+7);
     }
     if(s1num==3)
     {
      shi++;
      if(shi==24)
       shi=0;
      write_sfm(4,shi);
      write_com(0x80+0x40+4);
     }
     if(s1num==4)
     {
      week++;
      if(week==8)week=1;
      write_week();
      write_com(0x80+0x0D);
     }
     if(s1num==5)//日
     {
      day++;
      if(day==31)day=1;
      write_ymd(0x0a,day);
      write_com(0x80+0x0a);
     }
     if(s1num==6)//月
     {
      month++;
      if(month==12)month=1;
      write_ymd(7,month);
      write_com(0x80+0x07);
     }
     if(s1num==7)//年
     {
      year++;
      if(year==100)year=0;
      write_ymd(4,year);
      write_com(0x80+0x04);
     }
    }//if(s2==0)
   }//if(s2==0)
   //減少鍵
   if(s3==0)
   {
    delay(5);
    if(s3==0)
    {
     while(!s3);
     if(s1num==1)
     {
      if(miao==0)
      {
       miao=59;
       write_sfm(10,miao);
       write_com(0x80+0x40+10);
      }
      miao--;
      if(miao==-1)
       miao=59;
      write_sfm(10,miao);
      write_com(0x80+0x40+10);
     }
     if(s1num==2)
     {
      fen--;
      if(fen==-1)
       fen=59;
      write_sfm(7,fen);
      write_com(0x80+0x40+7);
     }
     if(s1num==3)
     {
      shi--;
      if(shi==-1)
       shi=23;
      write_sfm(4,shi);
      write_com(0x80+0x40+4);
     }
     if(s1num==4)//
     {
      week--;
      if(week==-1)week=7;
      write_week();
      write_com(0x80+0x0C);
     }
     if(s1num==5)//日
     {
      day--;
      if(day==-1)day=31;
      write_ymd(0x0a,day);
      write_com(0x80+0x0a);
     }
     if(s1num==6)//月
     {
      month--;
      if(month==-1)month=12;
      write_ymd(7,month);
      write_com(0x80+0x07);
     }
     if(s1num==7)//年
     {
      year--;
      if(year==-1)year=99;
      write_ymd(4,year);
      write_com(0x80+0x04);
     }
    }//if(s3==0)
   }//if(s3==0)
   //鬧鈴鍵
   if(s4==0)
   {
    delay(5);
    if(s4==0)
    {
     flag1=1;//鬧鈴設置開啟
     while(!s4);
     /*在液晶上顯示設置鬧鈴的界面*/
     lcd_reset();
     write_com(0x80);
     while(table2[i]!='\0')
     {
      write_date(table2[i]);
      i++;
     }
     i=0;
     write_com(0x80+0x40);
        while(table1[i]!='\0')
     {
      write_date(table1[i]);
      i++;
     }
     write_sfm(10,miao);
     write_sfm(7,fen);
     write_sfm(4,shi);
     /*顯示出光標*/
     write_com(0x80+0x40+10);
     write_com(0x0f);
     i=0;
    }//if(s4==0)
   }//if(s4==0)
  }//if(s1num!=0)
  /*若鬧鈴響則按下鬧鈴鍵取消*/
  if(flag2)
  {
    if(s4==0)
    {
     delay(5);
     if(s4==0)
     {
      while(!s4);
      flag2=0;
     }
    }
  }
}
/*寫數據函數*/
void write_ds(uchar add,uchar date)
{
 dscs=0;
 dsas=1;
 dsds=1;
 dsrw=1;
 P0=add;
 dsas=0;
 dsrw=0;
 P0=date;
 dsrw=1;
 dsas=1;
 dscs=1;    
}
/*讀數據函數*/
uchar read_ds(uchar add)
{
  uchar ds_date;
 dsas=1;
 dsds=1;
 dsrw=1;
 dscs=0;
 P0=add;
 dsas=0;
 dsds=0;
 P0=0xff;
 ds_date=P0;
 dsds=1;
 dsas=1;
 dscs=1;
 return ds_date; 
}
/*液晶初始化函數*/
void lcd_reset()
{
 delay(15);
 write_com(0x38);
 delay(5);
 write_com(0x38);
 delay(5);
 write_com(0x38);
 delay(5);
 write_com(0x08);
 write_com(0x01);
 write_com(0x06);
 write_com(0x0c); 
}
/*主函數*/
void main()
{
 lcd_reset();
 init();
 while(1)
 {
  keyscan();
  asec=read_ds(1);//讀出鬧鈴時間
  amin=read_ds(3);
  ahour=read_ds(5);
  if(flag==0)//若是功能鍵未被按下,則運行時鐘
  {
   miao=read_ds(0); 
   fen=read_ds(2);
   shi=read_ds(4);
   week=read_ds(6);
   day=read_ds(7);
   month=read_ds(8);
   year=read_ds(9);
   if(asec==miao&&amin==fen&&ahour==shi)flag2=1;//鬧鈴標志
   write_sfm(10,miao);
   write_sfm(7,fen);
   write_sfm(4,shi);
   write_week();
   write_ymd(0x0a,day);
   write_ymd(7,month);
   write_ymd(4,year);
   
  }
  if(flag2)music();//鬧鈴響
 }
}
/*外部中斷函數*/
void exter() interrupt 0

 uchar c;
 c=read_ds(0x0c);
}

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved