找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6621|回复: 1
收起左侧

带闹钟的单片机万年历代码说明(详细注释)

[复制链接]
ID:430932 发表于 2018-11-22 09:53 | 显示全部楼层 |阅读模式
基于清翔单片机开发板80c51的万年日历。
部分代码和使用方法如下
附件有代码和代码说明
功能:带闹钟的万年历
闹钟 万年历 调节时间 调节闹钟 可以区分闰年,28 29 30 31.
思路:利用定时器中断进行计时以此来进行秒数加一,分钟加一……。注意区分满十进一,满60分钟进一,还是小时的满24天数进一,以及判断月份的天数,闰年否。
按键扫描函数:进行时间的日期的调节。按下S2进入时间调节,
再按矩阵按键的S6-S19中的一个,分别对应选中要调节2018 0501 12:31:53 中的某一位
再按矩阵的S6-S15分别对应把刚选中的那一位设置为1或2或3.....0
(操作原理图)
闹钟:闹钟设置及检测函数
按下s3时显示闹钟如1231
再按s4一下对应调节第一位数据,此时按s2对应数据在0——9循环变化
s4按两下对应调节第二位,再s2调节数据大小
s4三下第三位,s4四下第四位
再按s4又到第一位,一次循环
s5用于闹钟的开启关闭。
当闹钟响时,可以按s4关闭蜂鸣器,不干扰时,响一分钟
LCD1602:液晶显示函数,根据课本操作时序图进行数据的写入
代码具体说明如下
由于代码模块性较强,内容多,分若干个.c,.h文件


1.Main.c
/****************************************************
程序说明
带闹钟的万年历
闹钟 万年历 调节时间 调节闹钟 可以区分闰年,28 29 30 31.
*****************************************************/
#include <reg52.h>
#include<stdlib.h>
#include<Lcd1602.h>
#include<main.h>
#include<key_Scan.h>
#include<Clock_Set.h>
#include<W_Time.h>
sbit dula = P2^6;//              数码管位选段选用于关闭数码管显示
sbit wela = P2^7;//数码管位选段选用于关闭数码管显示

/*******************************************************
变量说明
*flag0是定义在了万年c历.c中的作为判断一秒钟中断产生的标志位。
*ClockFlag2(Clock-Set.c)用于在闹钟响着的时候(闹钟中断时)关闭或开启闹钟给beep赋值变量。
*Clock[5]用于储存设置的闹钟的点数
*Y M D               :把字符串型的年月日转或为int型时分别付给他三个,再利用Y M D判断闰年否,28 29 30 31天?
*ClockFlag=1;用于判断显示时间还是闹钟的标志位
***********************************************************/
extern uchar flag0;            
extern int ClockFlag2;
extern uchar Clock[5];
int Y,M,D,ClockFlag=1;
uchar num=0;                                                                      //定时器中断
uchar time0[]={"2027-12-31"};              //储存时间的
uchar time1[]={"23:59:53"};
            
void T0_init()                                                            //定时器0初始化
{
              TMOD=0x01;
              TH0=(65536-45872)/256;  //装初值50ms单位
              TL0=(65536-45872)%256;
              EA=1;                //开总中断
              ET0=1;               //开启定时器0中断
              TR0=1;               //开启定时器0
}
void main()
{
              int i=0,n=0,j;
              char str1[3],str2[3];                 //用于储存字符串中的月份或日子

              for(j=5;j<=6;j++)                 //把字符串中的月份拿出来
     {str1[ i]=time0[j];
         i++;
              }
              for(j=8;j<=9;j++)                  //把字符串中的日子拿出来
      {str2[n]=time0[j];
         n++;
                }
                    Y=atoi(time0);              //利用stdlib.h中的atoi()函数把年月日转化为int型,以此来写算法判断闰年,28 29 30 31 ?
                              M=atoi(str1);
                              D=atoi(str2);
                dula = 0;
                wela = 0;//关闭数码管显示
                T0_init();//定时器0初始化
                LCD_Init();//1602初始化
while(1)
{
                                                        W_Time();                              //万年历函数
                                              Clock_Check();              //闹钟检测函数,看是否有闹钟产生
                                                            key_scan();                    //用于调整时间的按键扫描函数
                                                        ClockSet();                              //闹钟设置函数
                                               if(ClockFlag==1)   //用于判断显示时间还是闹钟的标志位
                                                        {
                                                                        LCD_displaystr(0, 0, &time0[0]);              //显示时间
                                 LCD_displaystr(0, 1, &time1[0]);            
                                                          }
                                                          if(ClockFlag<0)
                                                        {                           
                                                           LCD_displaystr(0, 1, &Clock[0]);              //显示闹钟
                                                        }
}            

}

   void T0_time() interrupt 1                            //定时器0中断服务函数
{
              TH0=(65536-45872)/256;
              TL0=(65536-45872)%256;
              num++;
              if(num==20)
              {
                num=0;
                flag0=1;
              }

}
2.main.h
#define uchar unsigned char
#define uint unsigned int

3.万年历.c
/*********************************************************************************
万年历函数
通过定时器0产生一秒中断,判断flag0进入函数。进行秒数加一,进位时看是否达到六十秒,六十分钟,24小时。
第一个if(flag0)进行时间的判断,若到了24小时则进入if(flag1)天数加一及年月判断
可以区分闰年及月份的具体天数
具体说明在下面。
*********************************************************************************/
#include <reg52.h>            
#include<main.h>            
extern uchar time0[];
extern uchar time1[];
uchar flag0=0,flag1=0;              //flag1用于判断是否该天数加一了
extern int Y,M,D;

void W_Time()                                          
{            
              if(flag0==1)                              //一秒中断发生
                            {
                                            if(time1[0]=='1'||time1[0]=='0') //小时处未到20点以上
                                          {
                                                        time1[7]++;                                                        //秒数加一
                                                        if(time1[7]>'9')
                                                        {
                                                                      time1[7]='0';
                                                        time1[6]++;                                    //秒数进位
                                                        if(time1[6]=='6')                            //达到了60秒
                                                        {
                                                                      time1[6]='0';
                                                                      time1[4]++;                                          //分钟加一
                                                                      if(time1[4]>'9')
                                                                      {
                                                                                    time1[4]='0';
                                                                                    time1[3]++;                            //分钟进位
                                                                                    if(time1[3]=='6') //达到了60分钟
                                                                                    {
                                                                                                  time1[3]='0';
                                                                                   
                                                                                    time1[1]++;                               //小时加一
                                                                                    if(time1[1]>'9')
                                                                                    {
                                                                                                  time1[1]='0';
                                                                                                  time1[0]++;                            //小时进位
                                                                                    }            
                                                                                    }
                                                                      }            
                                                        }
                                                        }              
                                          }
                                    if(time1[0]=='2')                            //小时处20点以上了
                                          {
                                                        time1[7]++;                                                        //以下分别是秒数,分钟,小时加一进位,方法同上
                                          if(time1[7]>'9')
                                          {
                                                        time1[7]='0';
                                                        time1[6]++;
                                                        if(time1[6]=='6')
                                                        {
                                                                      time1[6]='0';
                                                                      time1[4]++;
                                                                      if(time1[4]>'9')
                                                                      {
                                                                                    time1[4]='0';
                                                                                    time1[3]++;
                                                                                    if(time1[3]=='6')
                                                                                    {
                                                                                    time1[3]='0';
                                                                                    time1[1]++;
                                                                                    if(time1[1]=='4')              //判断是否到了24小时,则天数该加一了
                                                                                    {
                                                                                                  time1[1]='0';
                                                                                                  time1[0]='0';
                                                                                                  flag1=1;                            //天数加一标志位
                                                                                    }            
                                                                                    }
                                                                      }
                                                        }
                                              }
                           
                                 }
            


                                          if(flag1==1)                                          //达到了24小时,天数加一
                                          {                             
                                                        if(M==0x02)                                          //              M=2,二月
                              {                           
                                                                        
                                                        if((Y%4==0&&Y%100!=0)||(Y%100==0))                //看是否为闰年,此时为闰年
                                                        {
                                                                      if(time0[8]=='0'||time0[8]=='1')              //天数在20天以下
                                                                      {              time0[9]++;                                                                                      //天数加一
                                                                                    if(time0[9]>'9')
                                                                                    {
                                                                                                  time0[9]='0';
                                                                                                  time0[8]++;                                                          //天数进位
                                                                                    }
                                                                      }
                                                                      if(time0[8]=='2')                                                            //20天以上
                                                                      {              time0[9]++;                                                            //天数加一及到了28天月份加一
                                                                                    if(time0[9]>'8')
                                                                                    {
                                                                                                  time0[9]='1';
                                                                                                  time0[8]='0';
                                                                                                  time0[6]='3';                              //月份加一
                                                                                    }
                                                                      }
                                                        }
                                                        else                                                                                                                                  //              不是闰年,天数29天
                                                        {
                                                                      if(time0[8]=='0'||time0[8]=='1')              //天数在20天以下
                                                                      {              time0[9]++;                                                                         //天数加一
                                                                                    if(time0[9]>'9')
                                                                                    {
                                                                                                  time0[9]='0';
                                                                                                  time0[8]++;                                                        //天数进位
                                                                                    }
                                                                      }
                                                                      if(time0[8]=='2')                                             //天数在20以上,当29天时月份要加一了
                                                                      {              time0[9]++;
                                                                                    if(time0[9]>'9')
                                                                                    {
                                                                                                  time0[9]='1';
                                                                                                  time0[8]='0';
                                                                                                  time0[6]='3';                 //月份加一
                                                                                    }
                                                                      }
                                                        }
                              }
                            if(M==0x04||M==0x06||M==0x09||M==0x0b)                               //4 6 9 11月天数30
                                          {
                                                                            if(time0[8]=='0'||time0[8]=='1'||time0[8]=='2')              //30天以下
                                                                      {              time0[9]++;                                                                                    //天数加一
                                                                                    if(time0[9]>'9')
                                                                                    {
                                                                                                  time0[9]='0';
                                                                                                  time0[8]++;                                                           //天数进位
                                                                                                
                                                                                    }
                                                                      }            
                                                                                    if(time0[8]=='3')                                          //第30天,月份加一
                                                                                    {
                                                                                                  time0[9]='1';
                                                                                                  time0[8]='0';
                                                                                                  time0[6]++;                                              //月份加一
                                                                                    }
                                          }
                                          if(M==0x01||M==0x03||M==0x05||M==0x07||M==0x08||M==0x0a)  //1 3 5 7 8 10  月 31天
                                             {                                         
                                                                            if(time0[8]=='0'||time0[8]=='1'||time0[8]=='2')                 //天数小于30天
                                                                      {              time0[9]++;
                                                                                    if(time0[9]>'9')
                                                                                    {
                                                                                                  time0[9]='0';
                                                                                                  time0[8]++;
                                                                                                
                                                                                    }
                                                                      }            
                                                                                    if(time0[8]=='3'&&time0[9]=='0')
                                                                                                                                                                                                    time0[9]++;                            //30号时继续加一
                                                                                    if(time0[8]=='3'&&time0[9]=='1')                               //31号了,月份加一
                                                                                    {
                                                                                                  time0[9]='1';
                                                                                                  time0[8]='0';
                                                                                                  time0[6]++;
                                                                                    }
                                          }
                                          if(M==0x0c)                                                                                                                                              //12月,31天
                                          {
                                                                        if(time0[8]=='0'||time0[8]=='1'||time0[8]=='2')
                                                                      {              time0[9]++;
                                                                                    if(time0[9]>'9')
                                                                                    {
                                                                                                  time0[9]='0';
                                                                                                  time0[8]++;            
                                                                                    }
                                                  }
                                                                      if(time0[8]=='3'&&time0[9]=='0')
                                                                                                                                                                                                    time0[9]++;
                                                                      if(time0[8]=='3'&&time0[9]=='1')              //12-31号了,年份加一,日子变为01-01
                                                                      {
                                                                                                  time0[9]='1';
                                                                                                  time0[8]='0';
                                                                                                  time0[6]='1';
                                                                                                  time0[5]='0';
                                                                                                  time0[3]++;
                                                                                                  if(time0[3]>'9')
                                                                                                  {
                                                                                                
                                                                                                                time0[3]='0';
                                                                                                                time0[2]++;                               //年份满十年进位
                                                                                                  if(time0[2]>'9')
                                                                                                                {
                                                                                                                              time0[1]++;                               //年份满十年进位
                                                                                                                              time0[2]='0';
                                                                                                               
                                                                                                                              if(time0[1]>'9')
                                                                                                                              {
                                                                                                                                            time0[1]='0';

                                                                                                                                            time0[0]++;                              //年份满十年进位
                                                                                                                              }
                                                                                                                }
                                                                                                  }
                                                                      }
                                         
                              }
                              flag1=0;                            //清0
                            }
                                         
                           
                                                      
                                                                      flag0=0;//清0            
              }
              }
4.W_Time.h
void W_Time();

5.Key_Scan.c
/******************************************************
按键函数
可以调节时间及日期
按下S2进入时间调节,
再按矩阵按键的S6-S19中的一个,分别对应选中要调节2018 0501 12:31:53 中的某一位
再按矩阵的S6-S15分别对应把刚选中的那一位设置为1或2或3.....0
按键原理及操作方法将word文档
******************************************************/
#include <reg52.h>
#include<main.h>
#include<Lcd1602.h>
sbit S2=P3^0;

/**********************************************
变量说明
flag第一次进入矩阵按键的标志位,即此时矩阵按键是用于选择你要调节时间日期的哪一位
flag3第二次进入矩阵按键的标志位,即此时矩阵按键是用于设置刚才选择的那一位为数字几
TimeSet是Time0或者Time1数组的下标            
Key_Flag=0作为作为检测S2的标志位,当检测矩阵按键时Key—Flag=1。因为S6和s2为一个I/O口,这样做为了稳定,否则S6也会有s2的功能
*********************************************/                                                                        
int flag=0,temp,TimeSet,flag3=0;
uchar Key_Flag=0;                                   
extern uchar time0[];
extern uchar time1[];

void delayms(uint xms)                            //延时函数
{
  uint i,j;
  for(i=xms;i>0;i--)
      for(j=110;j>0;j--);
}

void  key_scan()
{            
            
                              if(Key_Flag==0)                            //检测S2,S2按下进入时间调节
              {
                                             if(S2==0)
                            {
                                          delayms(20);
                                          if(!S2)
                                          {
                                                        while(!S2);
                                                        flag=1;
                                          }
                   }
              }

              while(flag==1)                                                        //以下是第一次按矩阵按键,设置要调节时间的哪一位

              {               
                            P3=0xfe;
                            temp=P3;
                            temp=temp&0xf0;
                            if(temp!=0xf0)
              {                             
                      delayms(10);
                                temp=P3;
                                 temp=temp&0xf0;
                                if(temp!=0xf0)
                                          {
                                temp=P3;
                              switch(temp)
                              {                           
                                case 0xee:                       
                                                          TimeSet=0;                                                          //S6对应为要调节Time0的0位,即2018的2
                                                          flag=0;                                                                        //跳出第一次检测矩阵案件的while循环
                                                          flag3=1;                                                                        //进入第二次矩阵按键的标志位,即开始设置选中的那一位为数字几
                                                          break;
                                          case 0xde:                        //S7     2018 12 31 12:31:54 中的0
                                                TimeSet=1;
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                          case 0xbe:                        //S8                2018 12 31 12:31:54 中的1
                                                        TimeSet=2;                                                                        //以下均同上
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                case 0x7e:                         //s9
                                                        TimeSet=3;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                              }
                              while(temp!=0xf0)                                                                        //等待按键释放
                              {
                                temp=P3;
                                          temp=temp&0xf0;
                              }

                            }
                    }
                               P3=0xfd;
                            temp=P3;
                            temp=temp&0xf0;
                            if(temp!=0xf0)
              {                             
                      delayms(10);
                                temp=P3;
                                 temp=temp&0xf0;
                                if(temp!=0xf0)
                                          {
                                temp=P3;
                              switch(temp)
                              {                           
                                case 0xed:                         //s10
                                                          TimeSet=5;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                          case 0xdd:                          //s11
                                                TimeSet=6;
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                          case 0xbd:                          //s12
                                                        TimeSet=8;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                case 0x7d:                          //s13
                                                        TimeSet=9;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                              }
                              while(temp!=0xf0)
                              {
                                temp=P3;
                                          temp=temp&0xf0;
                              }

                            }
                    }
                              P3=0xfb;
                            temp=P3;
                            temp=temp&0xf0;
                            if(temp!=0xf0)
              {                             
                      delayms(10);
                                temp=P3;
                                 temp=temp&0xf0;
                                if(temp!=0xf0)
                                          {
                                temp=P3;
                              switch(temp)
                              {                           
                                case 0xeb:                         //s14
                                                          TimeSet=11;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                          case 0xdb:                          //s15
                                                TimeSet=12;
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                          case 0xbb:                          //s16
                                                        TimeSet=14;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                case 0x7b:                          //s17
                                                        TimeSet=15;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                              }
                              while(temp!=0xf0)
                              {
                                temp=P3;
                                          temp=temp&0xf0;
                              }

                            }
                    }

                              P3=0xf7;
                            temp=P3;
                            temp=temp&0xf0;
                            if(temp!=0xf0)
              {                             
                      delayms(10);
                                temp=P3;
                                 temp=temp&0xf0;
                                if(temp!=0xf0)
                                          {
                                temp=P3;
                              switch(temp)
                              {                           
                                case 0xe7:                         //s18
                                                          TimeSet=17;            
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                          case 0xd7:                          //s19
                                                TimeSet=18;
                                                          flag=0;
                                                          flag3=1;
                                                          break;
                                                           }
                              while(temp!=0xf0)
                              {
                                temp=P3;
                                          temp=temp&0xf0;
                              }

                            }
                    }
                              P3=0xff;                                                        //最后把P3的I/O口拉高,防止对后续有共同I/O口的按键影响
                                 if(S2==0)                                                          //S2按下时退出时间设置
                                                                                    {
                                                                                                  delayms(20);
                                                                                                  if(!S2)
                                                                                                  {
                                                                                                                while(!S2);
                                                                                   
                                                                                                    flag=0;
                                                                                                  }
                                                                           }                    
}

                                                        while(flag3==1)
                                          {                                                        //第二次矩阵按键检测标志位,即开始设置被选中的那一位为数字几
                                                             P3=0xfe;
                                                                      temp=P3;
                                                                      temp=temp&0xf0;
                                                                      if(temp!=0xf0)
                                                                      {                           
                                                                          delayms(10);
                                                                          temp=P3;
                                                                                    temp=temp&0xf0;
                                                                                    if(temp!=0xf0)
                                                                                    {
                                                                                                  temp=P3;
                                                                                                  switch(temp)
                                                                                                    {
                                                                                                                   case 0xee:                          //s6  设置为1
                                                                                                                                  time0[TimeSet]='1';
                                                                                                                        break;
                                                                                                         case 0xde:                          //s7  2
                                                                                                                                  time0[TimeSet]='2';
                                                                                                                                  break;
                                                                                                                                            case 0xbe:                          //s8  3
                                                                                                                                            time0[TimeSet]='3';
                                                                                                                                  break;
                                                                                                                                  case 0x7e:                          //s9  4
                                                                                                                                            time0[TimeSet]='4';
                                                                                                                                  break;
                                                                                                     }
                                                                                                  while(temp!=0xf0)
                                                                                                  {
                                                                                                      temp=P3;
                                                                                                                temp=temp&0xf0;
                                                                                                  }
                                                        LCD_displaystr(0, 0, &time0[0]);              //随时显示时间,观察你设置的几,是否设置正确
                 LCD_displaystr(0, 1, &time1[0]);
                                                                                    }
                                                                      }
                                                                      P3=0xfd;
                                                                      temp=P3;
                                                                      temp=temp&0xf0;
                                                                      if(temp!=0xf0)
                                                                      {
                                                                          delayms(10);
                                                                          temp=P3;
                                                                                    temp=temp&0xf0;
                                                                                    if(temp!=0xf0)
                                                                                    {
                                                                                                  temp=P3;
                                                                                                  switch(temp)
                                                                                                    {
                                                                                                                   case 0xed:                          //s10 5
                                                                                                                                  time0[TimeSet]='5';
                                                                                                                        break;
                                                                                                         case 0xdd:                          //s11 6
                                                                                                                                  time0[TimeSet]='6';
                                                                                                                                  break;
                                                                                                                                            case 0xbd:                          //s12 7
                                                                                                                                            time0[TimeSet]='7';
                                                                                                                                  break;
                                                                                                                                  case 0x7d:                          //s13  8
                                                                                                                                            time0[TimeSet]='8';
                                                                                                                                  break;
                                                                                                     }
                                                                                                  while(temp!=0xf0)
                                                                                                  {
                                                                                                      temp=P3;
                                                                                                                temp=temp&0xf0;
                                                                                                  }
                                                        LCD_displaystr(0, 0, &time0[0]);              //同上
                 LCD_displaystr(0, 1, &time1[0]);
                                                                                    }
                                                                      }
                                                                     
                                                                      P3=0xfb;
                                                                      temp=P3;
                                                                      temp=temp&0xf0;
                                                                      if(temp!=0xf0)
                                                                      {
                                                                          delayms(10);
                                                                          temp=P3;
                                                                                    temp=temp&0xf0;
                                                                                    if(temp!=0xf0)
                                                                                    {
                                                                                                  temp=P3;
                                                                                                  switch(temp)
                                                                                                    {
                                                                                                                   case 0xeb:                 //s14 9      
                                                                                                                                  time0[TimeSet]='9';
                                                                                                                        break;
                                                                                                         case 0xdb:
                                                                                                                                time0[TimeSet]='0';                            //s15  0
                                                                                                                        break; }
                                                                                                  while(temp!=0xf0)
                                                                                                  {
                                                                                                      temp=P3;
                                                                                                                temp=temp&0xf0;
                                                                                                  }
                                                        LCD_displaystr(0, 0, &time0[0]);            
                 LCD_displaystr(0, 1, &time1[0]);
                                                                                    }
                                                                      }
                                                                     
                                                                     
                                                           P3=0xff;                               //同上
                                                            if(S2==0)                                                        //按下s2跳出时间设置
                                                                                    {
                                                                                                  delayms(20);
                                                                                                  if(!S2)
                                                                                                  {
                                                                                                                while(!S2);
                                                                                   
                                                                                                    flag3=0;
                                                                                                  }
                                                                           }   
             }                                         
}

5.Key_Scan.h
#include<reg52.h>
#include<main.h>
void key_scan();
void delayms(uint xms);

6.lcd1602.c
/***********************************************
lcd1602函数
此函数实现液晶屏上的字符显示
仿照课本函数,学习后,根据时序图自己又打了一遍
***********************************************/
#include <reg52.h>
#include<main.h>
sbit LCDRS = P3^5;              //RS端
sbit LCDRW = P3^6;              //RW端
sbit LCDEN = P3^4;              //EN端            


/************************************
判断液晶忙函数,因为lcd接收数据是有速度限制的,如果单片机工作较快,会造成显示不正常的结果
************************************/               
void If_Busy()
{
              uchar busy;
              P0 = 0xff;//拉高数据总线,复位
              LCDRS = 0;                //拉低RS
              LCDRW = 1;                //拉高RW               
              do
              {
                            LCDEN = 1;//使能EN
                            busy = P0;//读数据
                            LCDEN = 0;              //拉低使能,等待下一次
              }while(busy & 0x80); //判断状态字BIT7位是否为1,为1则表示忙,程序等待
}

void LCD_Write_data(uchar date)                            //写数据函数
{
    If_Busy();                 //忙则等待
              LCDRS = 1;              //由基本操作时序知,选择写数据模式要拉高它
              LCDRW = 0;  //同上
    P0=date;              //写入数据
              LCDEN = 1;              //拉高使能端
              LCDEN = 0;              //拉低使能以便于等待下一次
}

void LCD_Write_com(uchar com)                //写命令函数
{              If_Busy();                 //忙则等待
              LCDRS = 0;                //选择写命令模式
              LCDRW = 0;              //拉低RW操作,由时序图可知
              P0 = com;//写入命令
              LCDEN = 1;              //拉高使能端 数据被传输到LCD1602内
              LCDEN = 0;              //拉低使能以便于等待下一次
}
/******************************************
在指定位置显示字符串函数,L对应一行中的哪个位置,H对应第一行还是第二行
******************************************/
void LCD_displaystr(uchar L, uchar H, uchar *str)
{
              if(H) L |= 0x40;
              L |= 0x80;
              LCD_Write_com(L);
              while(*str != '\0')
              {
                            LCD_Write_data(*str++);
              }
}

void LCD_Init()                              //初始化
{
              LCD_Write_com(0x38); //              设置16*2显示,5*7点阵,8位数据接口
              LCD_Write_com(0x0c); //开显示
              LCD_Write_com(0x06); //读写一字节后地址指针加1
              LCD_Write_com(0x01); //清除显示
}

7.Lcd1602.h
#include<reg52.h>
#include<main.h>
void If_Busy();
void LCD_Write_com (uchar com);
void LCD_Write_data(uchar date);
void LCD_displaystr(uchar L, uchar H, uchar *str);
void LCD_Init();

8.Clock_Set.c
/*****************************************************
闹钟设置及检测函数
按下s3时显示闹钟如1231
再按s4一下对应调节第一位数据,此时按s2对应数据在0——9循环变化
s4按两下对应调节第二位,再s2调节数据大小
s4三下第三位,s4四下第四位
再按s4又到第一位,一次循环
s5用于闹钟的开启关闭。
当闹钟响时,可以按s4关闭蜂鸣器,不干扰时,响一分钟
*****************************************************/
#include <reg52.h>
#include<main.h>
#include<Lcd1602.h>
sbit S2=P3^0;
sbit S3=P3^1;
sbit S4=P3^2;
sbit S5=P3^3;
sbit beep=P2^3;
extern void delayms(uint xms);
/***********************************************
变量说明
ClockFlag3作为是否检测s4的标志位,即是否进入闹钟时间调节
Clock_Set=1作为是否开启闹钟的标志位,通过s5让其取反,以此控制闹钟的开启关闭
ClockFlag2=1用于开启beep,通过s4取反判断大于0否,以此给beep赋值
ClockFlag1作为给闹钟第几位赋值的标志位,即储存闹钟数据数组的下标
***********************************************/
int ClockFlag3=1,Clock_Set=1,i,j,ClockFlag2=1;
uchar Clock[5]={"0800"},ClockFlag1=0;
uchar code table[]={'0','1','2','3','4','5','6','7','8','9'};              //0——9循环
extern uchar time1[];
extern uchar time0[];
extern int ClockFlag;
extern uchar Key_Flag;
/***********************************************************
闹钟设置函数
***********************************************************/
void ClockSet()
{                              if(Key_Flag==0)  //同Key_Scan.c文件中此变量的解释说明
   {
                                            if(S3==0)                                          //按下s3时显示闹钟如1231,再按下又退出
                            {
                                          delayms(20);
                                          if(!S3)
                                          {
                                                        while(!S3);               
                                                        LCD_Write_com(0x01); //清除显示
                                                        ClockFlag=~ClockFlag;                           
                                                           ClockFlag3=~ClockFlag3;              //ClockFlag3作为是否检测s4的标志位,即是否进入闹钟时间调节                                                                                                   
                                          }
                            }
              }
   if(ClockFlag3<0)                            //开始闹钟调节,选择调节哪一位
{
              if(S4==0)
              {
                            delayms(10);
                            if(!S4)
                            {
                                          while(!S4);
                                Key_Flag=1;
                                          ClockFlag1++;              //记录按下s4的次数,依次对应每一位
                                          if(ClockFlag1>4)ClockFlag1=1;
                            }            
              }
              if(ClockFlag1)                               //进入被选择的这一位的数值调整0——9循环
              {
                                 if(S2==0)
                            {
                                          delayms(20);
                                          if(!S2)
                                          {              while(!S2);
                                                        Clock[ClockFlag1-1]=table[ i]; //0——9循环
                                                        i++;
                                                        if(i==10)i=0;
                                          }
                   }
              }
            
                                                          if(S3==0)                            //完成调节退出
                            {
                                          delayms(20);
                                          if(!S3)
                                          {
                                                        while(!S3);               
                                                        LCD_Write_com(0x01); //清除显示
                                                        ClockFlag=~ClockFlag;                           
                                                           ClockFlag3=~ClockFlag3;            
                                                        Key_Flag=0;                                                                                                   
                                          }
                            }
}
            
}
/******************************
检测闹钟函数
******************************/
void Clock_Check()
{
                   if(S5==0)//开启闹钟
                            {
                                          delayms(20);
                                          if(!S5)
                                          {              while(!S5);
                                                        Clock_Set=~Clock_Set;
                                                      
                                          }
                   }
                            if(Clock_Set<0)              //闹钟开启并进行检测是否到点
                            {
                                          P1=0xfe;   //闹钟开启时亮灯,作为提示
                                         
                            if(time1[0]==Clock[0]&&time1[1]==Clock[1]&&time1[3]==Clock[2]&&time1[4]==Clock[3]) //到点
                                          {
                                          for(j=0;j<10;j++)                           
                                          beep=ClockFlag2>0?0:1;                            //响
                                         
                                          if(S4==0)                            //开关蜂鸣器
                            {
                                          delayms(20);
                                          if(!S4)
                                          {              while(!S4);
                                                        ClockFlag2=~ClockFlag2;            
                                          }
                   }
                            }
                            beep=1;P1=0xff;   
                            }
}


9.Clock_Set.h
#include <reg52.h>
#include<main.h>
void ClockSet();
void Clock_Check();


完整的Word格式文档51黑下载地址:
代码说明2,万年历.docx (161.71 KB, 下载次数: 46)
回复

使用道具 举报

ID:423639 发表于 2018-11-26 20:37 | 显示全部楼层
老哥,你这个软件有点牛逼的呀(在下新手,刚开始接触单片机)
而且你发表的认真
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表