qq_40552787
qq_40552787
2020-05-25 22:07

51单片机多功能时钟程序 LCD显示温度,实物显示不了。

20
  • c语言

实物LCD1602显示温度为0,其他信息正常显示,仿真一切正常,若将显示温度的函数单独提取成文件,则正常显示温度,不知道怎么改
PS只需看主函数,LCD1602.h和temp.h就行

#include"key.h"
#include"lcd1602.h"
#include"ds1302.h"
#include"naozhong.h"
#include"temp.h"
#include"nongli.h"
#include "intrins.h"

#define uchar unsigned char  
#define uint  unsigned int

uchar flag_nl,temperature; 

/*在LCD1602的坐标(x, y)位置显示星期*/
void LcdShowWeek(uchar x, uchar y, uchar dat)
{
 uchar addr;

  if (y == 0)
  {
    addr = 0x00 + x; //第一行的x位置显示
  }
  else
  {
    addr = 0x40 + x; //第二行x的位置显示
  }
  LCD1602WriteCmd(addr + 0x80);
  switch(dat)
    {
        case 1:LCD1602WriteData('M');//星期数为1时,显示
               LCD1602WriteData('O');
               LCD1602WriteData('N');
               break;

        case 2:LCD1602WriteData('T');//星期数据为2时显示
               LCD1602WriteData('U');
               LCD1602WriteData('E');
               break;

        case 3:LCD1602WriteData('W');//星期数据为3时显示
               LCD1602WriteData('E');
               LCD1602WriteData('D');
               break;

        case 4:LCD1602WriteData('T');//星期数据为4是显示
               LCD1602WriteData('H');
               LCD1602WriteData('U');
               break;

        case 5:LCD1602WriteData('F');//星期数据为5时显示
               LCD1602WriteData('R');
               LCD1602WriteData('I');
               break;

        case 6:LCD1602WriteData('S');//星期数据为6时显示
               LCD1602WriteData('T');
               LCD1602WriteData('A');
               break;

        case 7:LCD1602WriteData('S');//星期数据为7时显示
               LCD1602WriteData('U');
               LCD1602WriteData('N');
               break;
    }   
 }



 /*显示温度*/


void LcdTimeDisplay(int temp)    //lcd显示
{   
    unsigned char  datas[]={0,0}; //定义数组,室温比较小,所以只显示两位温度
    float tp;    
    if(temp< 0)             //当温度值为负数
    {
        LCD1602WriteCmd(0x80+0x0c);     //写地址 
        LCD1602WriteData('-');
        temp=temp-1;
        temp=~temp;
        tp=temp;
        temp=tp*0.0625*100+0.5; 
    }
    else
    {    LCD1602WriteCmd(0x80+0x0c);        //写地址 
         LCD1602WriteData('+');         
        tp=temp;
        temp=tp*0.0625*100+0.5; 
    }
    datas[0] = temp / 10000;
    datas[1] = temp % 10000 / 1000;
    datas[2] = temp % 1000 / 100;
    datas[3] = temp % 100 / 10;
    datas[4] = temp % 10;                                                                                                                                                                             
    LCD1602WriteCmd(0x8d);       //写地址 
    LCD1602WriteData('0'+datas[1]); //十位
    Delay_nms(1);
    LCD1602WriteCmd(0x8e);      //写地址 
    LCD1602WriteData('0'+datas[2]); //个位
    Delay_nms(1);        
}


/*初始化计时器*/
void Timer0Init()
{
    EA   = 1;         //开总中断
    TMOD = 0X01;      //定时器0、工作方式1
    ET0  = 1;         //开定时器0中断 
    TR0  = 1;         //允许定时器0定时
}
/*读取DS1302中的时间*/
void Ds1302ReadTime()
{
 miao = Ds1302Read(READ_RTC_ADDR[0]);   //读秒
 fen  = Ds1302Read(READ_RTC_ADDR[1]);   //读分
 shi  = Ds1302Read(READ_RTC_ADDR[2]);   //读时
 ri   = Ds1302Read(READ_RTC_ADDR[3]);   //读日
 yue  = Ds1302Read(READ_RTC_ADDR[4]);   //读月
 week = Ds1302Read(READ_RTC_ADDR[5]);   //读星期
 nian = Ds1302Read(READ_RTC_ADDR[6]);   //读年

 Conversion(0,nian,yue,ri); //农历转换                   
 n_nian = year_moon ;
 n_yue  = month_moon ;
 n_ri   = day_moon ;    
} 

void DS1302ShowPart()         //LCD显示时间、温度
{

 LcdShowWeek(12,1,week);
 LcdShowNum(2,1,shi);
 LcdShowNum(5,1,fen);
 LcdShowNum(8,1,miao);

 if(flag_nl == 0)  //显示阳历
    {
     LcdShowNum(2,0,nian); 
     LcdShowNum(5,0,yue);
     LcdShowNum(8,0,ri);
    }
 else
    {
     LcdShowNum(2,0,n_nian); 
     LcdShowNum(5,0,n_yue);
     LcdShowNum(8,0,n_ri);
    }
}

/*时钟+*/
void Inc_Time()
{
  if(bit_flag == 1)  //设置年
     {
        nian+=0x01;
        if((nian & 0x0f) >= 0x0a)
            nian = (nian & 0xf0) + 0x10;
        if(nian >= 0x9a)
            nian = 1;    
     }
  if(bit_flag == 2)  //设置月
     {
        yue+=0x01;
        if((yue & 0x0f) >= 0x0a)
            yue = (yue & 0xf0) + 0x10;
        if(yue >= 0x13)
        yue = 1;     
     }
  if(bit_flag == 3)  //设置日
     {
        ri+=0x01;
        if((ri & 0x0f) >= 0x0a)
            ri = (ri & 0xf0) + 0x10;
        if(ri >= 0x32)
            ri = 0;      
     }
  if(bit_flag == 4)       //设置星期
     {
        week+=0x01;
        if((week & 0x0f) >= 0x0a)
            week = (week & 0xf0) + 0x10;
        if(week >= 0x08)
            week = 1;     
     }
  if(bit_flag == 5)       //设置时
     {
        shi+=0x01;
        if((shi & 0x0f) >= 0x0a)
        shi = (shi & 0xf0) + 0x10;     ///***shi & 0xf0低四位清零,(shi & 0xf0) + 0x10向高位进1(高四位加1)***///              if(shi >= 0x24)
        shi = 0;
     }
  if(bit_flag == 6)       //设置分
     {          
       fen+=0x01;
       if((fen & 0x0f) >= 0x0a)
           fen = (fen & 0xf0) + 0x10;
       if(fen >= 0x60)
           fen = 0;                  
     }
  if(bit_flag == 7)       //设置秒
    {
       miao+=0x01;
       if((miao & 0x0f) >= 0x0a)
           miao = (miao & 0xf0) + 0x10;
       if(miao >= 0x60)
           miao = 0;
    }
}

/*时钟-*/
void Dec_Time()
{
  if(bit_flag == 1) 
     {
       if(nian == 0x01)
          nian = 0x9a;
       if((nian & 0x0f) == 0x00)
          nian = (nian | 0x0a) - 0x10;
       nian -- ;             
     }
  if(bit_flag == 2)
     {
        if(yue == 0x01)
           yue = 0x13;
        if((yue & 0x0f) == 0x00)
            yue = (yue | 0x0a) - 0x10;
        yue -- ;         
     }
  if(bit_flag == 3)
     {
        if(ri == 0x01)
            ri = 0x32;
        if((ri & 0x0f) == 0x00)
            ri = (ri | 0x0a) - 0x10;
        ri -- ;               
     }
  if(bit_flag == 4)       //设置星期
     {
        if(week == 0x01)
            week = 0x08;
        if((week & 0x0f) == 0x00)
            week = (week | 0x0a) - 0x10;
        week -- ;
     }
  if(bit_flag == 5)       //设置时
     {
        if(shi == 0x00)   
            shi = 0x24;
        if((shi & 0x0f) == 0x00)  
            shi = (shi | 0x0a) - 0x10;   ///***如果个位为0,高四位减1***///
        shi -- ;         
     }
  if(bit_flag == 6)       //设置分
     {
        if(fen == 0x00)
            fen = 0x5a;
        if((fen & 0x0f) == 0x00)
            fen = (fen | 0x0a) - 0x10;
        fen -- ;     
     }
  if(bit_flag == 7)  
     {
        if(miao == 0x00)
            miao = 0x5a;
        if((miao & 0x0f) == 0x00)
            miao = (miao | 0x0a) - 0x10;
        miao -- ;        
     } 
}

/*具体按键功能*/
void KeyAction()
{
    if (KeyValue == 1)
     {
        mode_flag++;
        if(mode_flag == 1)     //   设置时钟
           {
            bit_flag=1;       //设置位为年的十位
            LcdShowStr(0,0,"20  -  -       C");
            LcdShowStr(0,1,"    :  :       ");         
           }

        if(mode_flag == 2)    //设置闹钟
          {
            bit_flag = 1;      //设置位为时的十位
            LcdShowStr(0,0,"   set alarm    ");         
            LcdShowStr(0,1,"ON              ");
          }
        if(mode_flag > 2)    //回到正常显示
        {
            mode_flag = 0;
            bit_flag = 0;
            write_guangbiao(2,0,0);  //关闭光标
            Int_1602_Dis();      //初始化液晶显示        
        }
     }
    if(KeyValue == 2)   //选择键
    {
        flag_200ms = 1;
        if(mode_flag == 1)        //设置时间
        {
            bit_flag ++;         //设置位右移
            if(bit_flag > 7)
               bit_flag= 1;
        }
        if(mode_flag == 2)       //设置闹钟
        {
            bit_flag ++;
            if(bit_flag > 5)
                bit_flag = 1;               
        }
    }
    if(mode_flag == 1)
    {   
            if(KeyValue == 3)   //加
                {
                  Inc_Time();
                }
            if(KeyValue == 4)   //减     
                {
                  Dec_Time();
                }       
        LcdShowNum(2,0,nian); //显示时间
        LcdShowNum(5,0,yue);
        LcdShowNum(8,0,ri);
        LcdShowWeek(12,1,week);
        LcdShowNum(2,1,shi);
        LcdShowNum(5,1,fen);
        LcdShowNum(8,1,miao);
        LcdTimeDisplay(temperature); 
    switch(bit_flag)       // 光标显示
        {
            case 1:  write_guangbiao(2,0,1);  break;
            case 2:  write_guangbiao(5,0,1);  break;
            case 3:  write_guangbiao(8,0,1);  break;
            case 4:  write_guangbiao(12,1,1);  break;
            case 5:  write_guangbiao(2,1,1);  break;
            case 6:  write_guangbiao(5,1,1);  break;
            case 7:  write_guangbiao(8,1,1);  break;
        }
    SetTimetoDS1302();
   }
/***************设置闹钟*********************/
    if(mode_flag == 2)
       {
        if(bit_flag == 1)         //设置闹钟开关
         {
            if(KeyValue == 3)   
            {
                alarm_open = 1;   //闹钟功能开
            }       
            if(KeyValue == 4)   
            {
                alarm_open = 0;   //闹钟功能关
            }
         }

        if(alarm_open == 1)
         {
          LcdShowStr(0,1,"ON ");
         }
        else
         {
          LcdShowStr(0,1,"OFF");
         }
         if(bit_flag == 2)        //设置第一个闹钟时
          {
            if(KeyValue == 3)   //加
            {
                shi1+=0x01;
                if((shi1 & 0x0f) >= 0x0a)
                    shi1 = (shi1 & 0xf0) + 0x10;
                if(shi1 >= 0x24)
                    shi1 = 0;
            }       
            if(KeyValue == 4)   //减     
            {
                if(shi1 == 0x00)
                    shi1 = 0x5a;
                if((shi1 & 0x0f) == 0x00)
                    shi1 = (shi1 | 0x0a) - 0x10;
                shi1 -- ;
            }   
          }
        if(bit_flag == 3)         //设置第一个闹钟分
          {
            if(KeyValue == 3)   //加
            {
                fen1+=0x01;
                if((fen1 & 0x0f) >= 0x0a)
                    fen1 = (fen1 & 0xf0) + 0x10;
                if(fen1 >= 0x60)
                    fen1 = 0;
            }   
            if(KeyValue == 4)   //减     
            {
                if(fen1 == 0x00)
                    fen1 = 0x5a;
                if((fen1 & 0x0f) == 0x00)
                    fen1 = (fen1 | 0x0a) - 0x10;
                fen1 -- ;           
            }
          }
         if(bit_flag == 4)        //设置第二个闹钟时
          {
            if(KeyValue == 3)   //加
            {
                shi2+=0x01;
                if((shi2 & 0x0f) >= 0x0a)
                    shi2 = (shi2 & 0xf0) + 0x10;
                if(shi2 >= 0x24)
                    shi2 = 0;
            }       
            if(KeyValue == 4)   //减     
            {
                if(shi2 == 0x00)
                    shi2 = 0x5a;
                if((shi2 & 0x0f) == 0x00)
                    shi2 = (shi2 | 0x0a) - 0x10;
                shi2 -- ;
            }   
          }
         if(bit_flag == 5)        //设置第二个闹钟分
          {
            if(KeyValue == 3)   //加
            {
                fen2+=0x01;
                if((fen2 & 0x0f) >= 0x0a)
                    fen2 = (fen2 & 0xf0) + 0x10;
                if(fen2 >= 0x60)
                    fen2 = 0;
            }   
            if(KeyValue == 4)   //减     
            {
                if(fen2 == 0x00)
                    fen2 = 0x5a;
                if((fen2 & 0x0f) == 0x00)
                    fen2 = (fen2 | 0x0a) - 0x10;
                fen2 -- ;           
            }
          }

         LcdShowNum(4,1,shi1);
         LcdShowNum(7,1,fen1);
         LcdShowNum(10,1,shi2);
         LcdShowNum(13,1,fen2); 
        switch(bit_flag)       // 光标显示
        {
            case 1:  write_guangbiao(0,1,1);  break;
            case 2:  write_guangbiao(4,1,1);  break;
            case 3:  write_guangbiao(7,1,1);  break;
            case 4:  write_guangbiao(10,1,1);  break;
            case 5:  write_guangbiao(13,1,1);  break;
        }       
       }
}

void main()
{
    InitLCD1602();
    InitDS1302();

    Timer0Init();        //初始化定时器
    Int_1602_Dis();
    Delay_nms(500);
    while(1)
    {
     KeyDown();
     if(KeyValue<10)
     {
      if(beep_flag ==0)   //闹钟不响的状态下可以设置
       {
         KeyAction();
       }
      else 
       {
         beep_flag= 0;   //按下任意键可关闭闹钟
         beep = 1;
         Ds1302ReadTime();
       }
     if(mode_flag == 0)
         {
          if(KeyValue == 4)  
             {
              flag_nl = ~flag_nl;         //切换农历和阳历的显示标志位 
              if(flag_nl == 1)
                 LcdShowStr(0,0,"N ");                 
             }
         }
     }

    if(mode_flag == 0)
            {
             Ds1302ReadTime();        //读时间            
             DS1302ShowPart();
             LcdTimeDisplay(Ds18b20ReadTemp());  
            }
        naozhong_alarm();      //闹钟报警函数
        Delay_nms(1);
    }
}

/**************定时器0中断程序*****************/
void Timer0() interrupt 1
{            
    static uchar value;
    TH0 = 0X3C;
    TL0 = 0XB0;    //50ms
    value ++;      
    if((value % 2) == 0)     //100ms  
        flag_100ms = 1;  
}
/****************************************************************/
#ifndef _temp_H_
#define _temp_H_

#include <reg51.h>


//---重定义关键词---//
#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint 
#define uint unsigned int
#endif

uchar temp;
//--定义使用的IO口--//
sbit DSPORT=P3^7;

/*******************************************************************************
* 函 数 名         : Delay1ms
* 函数功能         : 延时函数
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

void Delay1ms(uint y)
{
    uint x;
    for( ; y>0; y--)
    {
        for(x=110; x>0; x--);
    }
}
/*******************************************************************************
* 函数名         : Ds18b20Init
* 函数功能         : 初始化
* 输入           : 无
* 输出             : 初始化成功返回1,失败返回0
*******************************************************************************/

unsigned char Ds18b20Init()
{
    unsigned int i;
    DSPORT=0;            //将总线拉低480us~960us
    i=70;   
    while(i--);//延时642us
    DSPORT=1;           //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
    i=0;
    while(DSPORT)   //等待DS18B20拉低总线
    {
        i++;
        if(i>5000)//等待>5MS
            return 0;//初始化失败  
    }
    return 1;//初始化成功
}

/*******************************************************************************
* 函数名         : Ds18b20WriteByte
* 函数功能         : 向18B20写入一个字节
* 输入           : com
* 输出             : 无
*******************************************************************************/

void Ds18b20WriteByte(unsigned char dat)
{
    unsigned int i,j;
    for(j=0;j<8;j++)
    {
        DSPORT=0;           //每写入一位数据之前先把总线拉低1us
        i++;
        DSPORT=dat&0x01; //然后写入一个数据,从最低位开始
        i=6;
        while(i--); //延时68us,持续时间最少60us
        DSPORT=1;   //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
        dat>>=1;
    }
}
/*******************************************************************************
* 函数名         : Ds18b20ReadByte
* 函数功能         : 读取一个字节
* 输入           : com
* 输出             : 无
*******************************************************************************/


unsigned char Ds18b20ReadByte()
{
    unsigned char byte,bi;
    unsigned int i,j;   
    for(j=8;j>0;j--)
    {
        DSPORT=0;//先将总线拉低1us
        i++;
        DSPORT=1;//然后释放总线
        i++;
        i++;//延时6us等待数据稳定
        bi=DSPORT;   //读取数据,从最低位开始读取
        /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
        byte=(byte>>1)|(bi<<7);                       
        i=4;        //读取完之后等待48us再接着读取下一个数
        while(i--);
    }               
    return byte;
}
/*******************************************************************************
* 函数名         : Ds18b20ChangTemp
* 函数功能         : 让18b20开始转换温度
* 输入           : com
* 输出             : 无
*******************************************************************************/

void  Ds18b20ChangTemp()
{
    Ds18b20Init();
    Delay1ms(1);
    Ds18b20WriteByte(0xcc);     //跳过ROM操作命令      
    Ds18b20WriteByte(0x44);     //温度转换命令
//  Delay1ms(100);  //等待转换成功,而如果你是一直刷着的话,就不用这个延时了

}
/*******************************************************************************
* 函数名         : Ds18b20ReadTempCom
* 函数功能         : 发送读取温度命令
* 输入           : com
* 输出             : 无
*******************************************************************************/

void  Ds18b20ReadTempCom()
{   

    Ds18b20Init();
Delay1ms(1);
    Ds18b20WriteByte(0xcc);  //跳过ROM操作命令
    Ds18b20WriteByte(0xbe);  //发送读取温度命令
}
/*******************************************************************************
* 函数名         : Ds18b20ReadTemp
* 函数功能         : 读取温度
* 输入           : com
* 输出             : 无
*******************************************************************************/

int Ds18b20ReadTemp()
{
    int temp=0;
    unsigned char tmh,tml;
    Ds18b20ChangTemp();             //先写入转换命令
    Ds18b20ReadTempCom();           //然后等待转换完后发送读取温度命令
    tml=Ds18b20ReadByte();      //读取温度值共16位,先读低字节
    tmh=Ds18b20ReadByte();      //再读高字节
    temp=tmh;
    temp<<=8;
    temp|=tml;
    return temp;
}
#endif
/************************************************************************/
#ifndef _lcd1602_H_
#define _lcd1602_H_
#include<reg51.h>

#define LCD1602_DB P0
#define uchar unsigned char  
#define uint  unsigned int
#define LCD1602_4PINS
//引脚定义
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_EN = P2^7;
uchar code table_num[]="0123456789abcdefg";

/*延时函数,延时1ms*/
void Lcd1602_Delay1ms(uint c)   //误差 0us
{
    uchar a,b;
    for (; c>0; c--)
    {
         for (b=199;b>0;b--)
         {
            for(a=1;a>0;a--);
         }      
    }       
}

/*LCD1602写命令*/
void LCD1602WriteCmd(uchar cmd)
{
   LCD1602_RS = 0;
   LCD1602_RW = 0;
   LCD1602_EN = 0;
   LCD1602_DB = cmd;
   Lcd1602_Delay1ms(1);     //等待数据稳定
   LCD1602_EN = 1;//高脉冲
   Lcd1602_Delay1ms(5);     //等待数据稳定
   LCD1602_EN = 0;//关闭液晶输出
   LCD1602_DB = cmd<<4;
   Lcd1602_Delay1ms(1);
   LCD1602_EN = 1;//高脉冲
   Lcd1602_Delay1ms(5);     //等待数据稳定
   LCD1602_EN = 0;//关闭液晶输出
}



void LCD1602WriteData(uchar dat)
{
  LCD1602_RS = 1;
  LCD1602_RW = 0;
  LCD1602_EN = 0;
  LCD1602_DB = dat;//送入数据
  Lcd1602_Delay1ms(1);
  LCD1602_EN = 1;//高脉冲
  Lcd1602_Delay1ms(5);
  LCD1602_EN = 0;//关闭液晶输出
  LCD1602_DB = dat<<4;
  Lcd1602_Delay1ms(1);
  LCD1602_EN = 1;//高脉冲
  Lcd1602_Delay1ms(5);
  LCD1602_EN = 0;//关闭液晶输出
}


/*液晶初始化*/
void InitLCD1602()
{
  LCD1602WriteCmd(0x32);
  LCD1602WriteCmd(0x28);
  LCD1602WriteCmd(0x0C); //开显示不显示光标
  LCD1602WriteCmd(0x06); //写入字符时字符指针++且光标++
  LCD1602WriteCmd(0x01); //显示清屏
}


/*在LCD1602的坐标(x, y)位置显示str*/
void LcdShowStr(uchar x, uchar y, uchar * str)
{
  uchar addr;

  if (y == 0)
  {
    addr = 0x00 + x; //第一行的x位置显示
  }
  else
  {
    addr = 0x40 + x; //第二行x的位置显示
  }
  LCD1602WriteCmd(addr + 0x80);
  while (*str != '\0')
  {
    LCD1602WriteData(*str++);
  }
}
/*在LCD1602的坐标(x, y)位置显示十进制数*/
void LcdShowNum(uchar x, uchar y, uchar dat)
{
 uchar addr;

  if (y == 0)
  {
    addr = 0x00 + x; //第一行的x位置显示
  }
  else
  {
    addr = 0x40 + x; //第二行x的位置显示
  }
  LCD1602WriteCmd(addr + 0x80);
  LCD1602WriteData(table_num[dat/16]);
  LCD1602WriteData(table_num[dat%16]);  
 }


/*光标控制函数*/
void write_guangbiao(uchar x,uchar y,uchar dat)
{       
    if(y==0)   
        LCD1602WriteCmd(0x80+x);
    else
        LCD1602WriteCmd(0x80+0x40+x);
    if(dat == 1)
        LCD1602WriteCmd(0x0f);     //显示光标并且闪烁   
    else 
        LCD1602WriteCmd(0x0c);   //关闭光标
}
/*屏幕初始化函数*/
void Int_1602_Dis()
{
 LcdShowStr(0,0,"20  -  -       C");
 LcdShowStr(0,1,"    :  :        ");
}

#endif 

/**********************************************************/
#ifndef _naozhong_H_
#define _naozhong_H_

sbit beep=P1^5; 
unsigned char alarm_open;       //闹钟功能标志
unsigned char fen1=0x00,shi1=0x00,fen2=0x00,shi2=0x00;    //两个闹钟变量的定义
unsigned char beep_flag;//闹钟开关标志
bit flag_100ms;
unsigned char alarm_open=0;
/*************闹钟报警函数***************/
void naozhong_alarm()
{
    static unsigned char t,r;
    if(flag_100ms == 1)       //100ms执行一次
    {   
        flag_100ms = 0;
        if(alarm_open == 1) //如果闹钟打开
        {   
            if((miao == 0) && (fen == fen1) && (shi == shi1))
            {       
                beep_flag = 1;  //有报警 打开蜂鸣器响的标志位              
            }
            if(((miao == 0) && (fen == fen2) && (shi == shi2)))
            {
                beep_flag = 1;  //有报警 打开蜂鸣器响的标志位
            }           
            if(beep_flag == 1)  //闹钟以被打开
            {
                    beep = ~beep;      
                    Delay_nms(10);          
            r ++;
            if(r >= 10)
            {
              r = 0;
              t++;
              if(t >= 60)
                 {
                     t = 0;
                     beep_flag = 0;
                     beep = 1;  
                 }
            }
           }
     }  
    }
}

#endif
/******************************************************/
#ifndef _key_H_
#define _key_H_
#include <reg51.h>
#define uchar unsigned char  
#define uint  unsigned int

uchar KeyValue;  //用来存放读取到的键值
uchar mode_flag;   //模式标志
uchar bit_flag;    //位标志
bit flag_200ms;

void Delay_nms(unsigned int c) //延时1ms
{
    unsigned char a,b;
    for (; c>0; c--)
    {
         for (b=199;b>0;b--)
         {
            for(a=1;a>0;a--);
         }      
    }
}

//判断按键是否按下 并得到键值

 void KeyDown()
{
    static uchar key_new;
    KeyValue = 20;                   //按键值还原
    P3 |= 0xff;                     //对应的按键IO口输出为1
    if((P3 & 0xff) != 0xff)     //按键按下
    {
        Delay_nms(1);           //按键消抖动
        if(((P3 & 0xff) != 0xff) && (key_new == 1))
        {                       //确认是按键按下
            key_new = 0;
            switch(P3 & 0xff)
            {
                case 0xfe:  KeyValue = 4;  break;    //得到按键值
                case 0xfd:  KeyValue = 3;  break;    //得到按键值
                case 0xfb:  KeyValue = 2;  break;    //得到按键值
                case 0xf7:  KeyValue = 1;  break;    //得到按键值
            }
        }           
    }
    else 
        key_new = 1;    
}
#endif
/**********************************************************************/
#ifndef _nongli_H_
#define _nongli_H_
#include <intrins.h>

//公历年对应的农历数据,每年三字节,
//格式第一字节BIT7-4 位表示闰月月份,值为0 为无闰月,BIT3-0 对应农历第1-4 月的大小
//第二字节BIT7-0 对应农历第5-12 月大小,第三字节BIT7 表示农历第13 个月大小
//月份对应的位为1 表示本农历月大(30 天),为0 表示小(29 天).
//第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期

//********阳历转换阴历表************************************
code unsigned char year_code[597]={
                    0x04,0xAe,0x53,    //1901 0
                    0x0A,0x57,0x48,    //1902 3
                    0x55,0x26,0xBd,    //1903 6
                    0x0d,0x26,0x50,    //1904 9
                    0x0d,0x95,0x44,    //1905 12
                    0x46,0xAA,0xB9,    //1906 15
                    0x05,0x6A,0x4d,    //1907 18
                    0x09,0xAd,0x42,    //1908 21
                    0x24,0xAe,0xB6,    //1909
                    0x04,0xAe,0x4A,    //1910
                    0x6A,0x4d,0xBe,    //1911
                    0x0A,0x4d,0x52,    //1912
                    0x0d,0x25,0x46,    //1913
                    0x5d,0x52,0xBA,    //1914
                    0x0B,0x54,0x4e,    //1915
                    0x0d,0x6A,0x43,    //1916
                    0x29,0x6d,0x37,    //1917
                    0x09,0x5B,0x4B,    //1918
                    0x74,0x9B,0xC1,    //1919
                    0x04,0x97,0x54,    //1920
                    0x0A,0x4B,0x48,    //1921
                    0x5B,0x25,0xBC,    //1922
                    0x06,0xA5,0x50,    //1923
                    0x06,0xd4,0x45,    //1924
                    0x4A,0xdA,0xB8,    //1925
                    0x02,0xB6,0x4d,    //1926
                    0x09,0x57,0x42,    //1927
                    0x24,0x97,0xB7,    //1928
                    0x04,0x97,0x4A,    //1929
                    0x66,0x4B,0x3e,    //1930
                    0x0d,0x4A,0x51,    //1931
                    0x0e,0xA5,0x46,    //1932
                    0x56,0xd4,0xBA,    //1933
                    0x05,0xAd,0x4e,    //1934
                    0x02,0xB6,0x44,    //1935
                    0x39,0x37,0x38,    //1936
                    0x09,0x2e,0x4B,    //1937
                    0x7C,0x96,0xBf,    //1938
                    0x0C,0x95,0x53,    //1939
                    0x0d,0x4A,0x48,    //1940
                    0x6d,0xA5,0x3B,    //1941
                    0x0B,0x55,0x4f,    //1942
                    0x05,0x6A,0x45,    //1943
                    0x4A,0xAd,0xB9,    //1944
                    0x02,0x5d,0x4d,    //1945
                    0x09,0x2d,0x42,    //1946
                    0x2C,0x95,0xB6,    //1947
                    0x0A,0x95,0x4A,    //1948
                    0x7B,0x4A,0xBd,    //1949
                    0x06,0xCA,0x51,    //1950
                    0x0B,0x55,0x46,    //1951
                    0x55,0x5A,0xBB,    //1952
                    0x04,0xdA,0x4e,    //1953
                    0x0A,0x5B,0x43,    //1954
                    0x35,0x2B,0xB8,    //1955
                    0x05,0x2B,0x4C,    //1956
                    0x8A,0x95,0x3f,    //1957
                    0x0e,0x95,0x52,    //1958
                    0x06,0xAA,0x48,    //1959
                    0x7A,0xd5,0x3C,    //1960
                    0x0A,0xB5,0x4f,    //1961
                    0x04,0xB6,0x45,    //1962
                    0x4A,0x57,0x39,    //1963
                    0x0A,0x57,0x4d,    //1964
                    0x05,0x26,0x42,    //1965
                    0x3e,0x93,0x35,    //1966
                    0x0d,0x95,0x49,    //1967
                    0x75,0xAA,0xBe,    //1968
                    0x05,0x6A,0x51,    //1969
                    0x09,0x6d,0x46,    //1970
                    0x54,0xAe,0xBB,    //1971
                    0x04,0xAd,0x4f,    //1972
                    0x0A,0x4d,0x43,    //1973
                    0x4d,0x26,0xB7,    //1974
                    0x0d,0x25,0x4B,    //1975
                    0x8d,0x52,0xBf,    //1976
                    0x0B,0x54,0x52,    //1977
                    0x0B,0x6A,0x47,    //1978
                    0x69,0x6d,0x3C,    //1979
                    0x09,0x5B,0x50,    //1980
                    0x04,0x9B,0x45,    //1981
                    0x4A,0x4B,0xB9,    //1982
                    0x0A,0x4B,0x4d,    //1983
                    0xAB,0x25,0xC2,    //1984
                    0x06,0xA5,0x54,    //1985
                    0x06,0xd4,0x49,    //1986
                    0x6A,0xdA,0x3d,    //1987
                    0x0A,0xB6,0x51,    //1988
                    0x09,0x37,0x46,    //1989
                    0x54,0x97,0xBB,    //1990
                    0x04,0x97,0x4f,    //1991
                    0x06,0x4B,0x44,    //1992
                    0x36,0xA5,0x37,    //1993
                    0x0e,0xA5,0x4A,    //1994
                    0x86,0xB2,0xBf,    //1995
                    0x05,0xAC,0x53,    //1996
                    0x0A,0xB6,0x47,    //1997
                    0x59,0x36,0xBC,    //1998
                    0x09,0x2e,0x50,    //1999 294
                    0x0C,0x96,0x45,    //2000 297
                    0x4d,0x4A,0xB8,    //2001
                    0x0d,0x4A,0x4C,    //2002
                    0x0d,0xA5,0x41,    //2003
                    0x25,0xAA,0xB6,    //2004
                    0x05,0x6A,0x49,    //2005
                    0x7A,0xAd,0xBd,    //2006
                    0x02,0x5d,0x52,    //2007
                    0x09,0x2d,0x47,    //2008
                    0x5C,0x95,0xBA,    //2009
                    0x0A,0x95,0x4e,    //2010
                    0x0B,0x4A,0x43,    //2011
                    0x4B,0x55,0x37,    //2012
                    0x0A,0xd5,0x4A,    //2013
                    0x95,0x5A,0xBf,    //2014
                    0x04,0xBA,0x53,    //2015
                    0x0A,0x5B,0x48,    //2016
                    0x65,0x2B,0xBC,    //2017
                    0x05,0x2B,0x50,    //2018
                    0x0A,0x93,0x45,    //2019
                    0x47,0x4A,0xB9,    //2020
                    0x06,0xAA,0x4C,    //2021
                    0x0A,0xd5,0x41,    //2022
                    0x24,0xdA,0xB6,    //2023
                    0x04,0xB6,0x4A,    //2024
                    0x69,0x57,0x3d,    //2025
                    0x0A,0x4e,0x51,    //2026
                    0x0d,0x26,0x46,    //2027
                    0x5e,0x93,0x3A,    //2028
                    0x0d,0x53,0x4d,    //2029
                    0x05,0xAA,0x43,    //2030
                    0x36,0xB5,0x37,    //2031
                    0x09,0x6d,0x4B,    //2032
                    0xB4,0xAe,0xBf,    //2033
                    0x04,0xAd,0x53,    //2034
                    0x0A,0x4d,0x48,    //2035
                    0x6d,0x25,0xBC,    //2036
                    0x0d,0x25,0x4f,    //2037
                    0x0d,0x52,0x44,    //2038
                    0x5d,0xAA,0x38,    //2039
                    0x0B,0x5A,0x4C,    //2040
                    0x05,0x6d,0x41,    //2041
                    0x24,0xAd,0xB6,    //2042
                    0x04,0x9B,0x4A,    //2043
                    0x7A,0x4B,0xBe,    //2044
                    0x0A,0x4B,0x51,    //2045
                    0x0A,0xA5,0x46,    //2046
                    0x5B,0x52,0xBA,    //2047
                    0x06,0xd2,0x4e,    //2048
                    0x0A,0xdA,0x42,    //2049
                    0x35,0x5B,0x37,    //2050
                    0x09,0x37,0x4B,    //2051
                    0x84,0x97,0xC1,    //2052
                    0x04,0x97,0x53,    //2053
                    0x06,0x4B,0x48,    //2054
                    0x66,0xA5,0x3C,    //2055
                    0x0e,0xA5,0x4f,    //2056
                    0x06,0xB2,0x44,    //2057
                    0x4A,0xB6,0x38,    //2058
                    0x0A,0xAe,0x4C,    //2059
                    0x09,0x2e,0x42,    //2060
                    0x3C,0x97,0x35,    //2061
                    0x0C,0x96,0x49,    //2062
                    0x7d,0x4A,0xBd,    //2063
                    0x0d,0x4A,0x51,    //2064
                    0x0d,0xA5,0x45,    //2065
                    0x55,0xAA,0xBA,    //2066
                    0x05,0x6A,0x4e,    //2067
                    0x0A,0x6d,0x43,    //2068
                    0x45,0x2e,0xB7,    //2069
                    0x05,0x2d,0x4B,    //2070
                    0x8A,0x95,0xBf,    //2071
                    0x0A,0x95,0x53,    //2072
                    0x0B,0x4A,0x47,    //2073
                    0x6B,0x55,0x3B,    //2074
                    0x0A,0xd5,0x4f,    //2075
                    0x05,0x5A,0x45,    //2076
                    0x4A,0x5d,0x38,    //2077
                    0x0A,0x5B,0x4C,    //2078
                    0x05,0x2B,0x42,    //2079
                    0x3A,0x93,0xB6,    //2080
                    0x06,0x93,0x49,    //2081
                    0x77,0x29,0xBd,    //2082
                    0x06,0xAA,0x51,    //2083
                    0x0A,0xd5,0x46,    //2084
                    0x54,0xdA,0xBA,    //2085
                    0x04,0xB6,0x4e,    //2086
                    0x0A,0x57,0x43,    //2087
                    0x45,0x27,0x38,    //2088
                    0x0d,0x26,0x4A,    //2089
                    0x8e,0x93,0x3e,    //2090
                    0x0d,0x52,0x52,    //2091
                    0x0d,0xAA,0x47,    //2092
                    0x66,0xB5,0x3B,    //2093
                    0x05,0x6d,0x4f,    //2094
                    0x04,0xAe,0x45,    //2095
                    0x4A,0x4e,0xB9,    //2096
                    0x0A,0x4d,0x4C,    //2097
                    0x0d,0x15,0x41,    //2098
                    0x2d,0x92,0xB5,    //2099
};

///月份数据表

code unsigned int day_code[12]={0x00,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3,0x111,0x130,0x14e};

bit c_moon; //世纪
unsigned char year_moon,month_moon,day_moon;

//判断农历月的大月或小月,如果该月为大返回1,为小返回0
bit get_moon_day(unsigned char month_p,unsigned int table_addr)
{
    unsigned char temp;
    switch (month_p){
        case 1:{temp=year_code[table_addr]&0x08;
             if (temp==0)return(0);else return(1);}
        case 2:{temp=year_code[table_addr]&0x04;
             if (temp==0)return(0);else return(1);}
        case 3:{temp=year_code[table_addr]&0x02;
             if (temp==0)return(0);else return(1);}
        case 4:{temp=year_code[table_addr]&0x01;
             if (temp==0)return(0);else return(1);}
        case 5:{temp=year_code[table_addr+1]&0x80;
             if (temp==0) return(0);else return(1);}
        case 6:{temp=year_code[table_addr+1]&0x40;
             if (temp==0)return(0);else return(1);}
        case 7:{temp=year_code[table_addr+1]&0x20;
             if (temp==0)return(0);else return(1);}
        case 8:{temp=year_code[table_addr+1]&0x10;
             if (temp==0)return(0);else return(1);}
        case 9:{temp=year_code[table_addr+1]&0x08;
             if (temp==0)return(0);else return(1);}
        case 10:{temp=year_code[table_addr+1]&0x04;
             if (temp==0)return(0);else return(1);}
        case 11:{temp=year_code[table_addr+1]&0x02;
             if (temp==0)return(0);else return(1);}
        case 12:{temp=year_code[table_addr+1]&0x01;
             if (temp==0)return(0);else return(1);}
        case 13:{temp=year_code[table_addr+2]&0x80;
             if (temp==0)return(0);else return(1);}
    }
    return(0);
}

/*
函数功能:输入BCD阳历数据,输出BCD阴历数据(只允许1901-2099年)
调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun)
如:计算2004年10月16日Conversion(0,0x4,0x10,0x16);
c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世
纪,c_sun=1为19世纪
调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据
*/

void Conversion(bit c,unsigned char year,unsigned char month,unsigned char day)//公历日期
{                         //c=0 为21世纪,c=1 为19世纪 输入输出数据均为BCD数据
    unsigned char temp1,temp2,temp3,month_p;
    unsigned int temp4,table_addr;
    bit flag2,flag_y;
    temp1=year/16;   //BCD->hex 先把数据转换为十六进制
    temp2=year%16;
    year=temp1*10+temp2;
    temp1=month/16;
    temp2=month%16;
    month=temp1*10+temp2;
    temp1=day/16;
    temp2=day%16;
    day=temp1*10+temp2;
    //定位数据表地址
    if(c==0){                   
        table_addr=(year+0x64-1)*0x03; //19..年
    }
    else {
        table_addr=(year-1)*0x03;
    }
    //取当年春节所在的公历月份
    temp1=year_code[table_addr+2]&0x60; 
    temp1=_cror_(temp1,5);
    //取当年春节所在的公历日
    temp2=year_code[table_addr+2]&0x1f; 
    // 计算当年春年离当年元旦的天数,春节只会在公历1月或2月
    if(temp1==0x01){  
        temp3=temp2-1;  
    }  
    else{
        temp3=temp2+0x1f-1;        
    }
    // 计算当年春年离当年元旦的天数完成
    //计算公历日离当年元旦的天数 
     temp4=day_code[month-1]+day-1;

    if ((year%0x04==0)||((year%0x64)&&(year%0x190))) //闰年
    {  
         if((month>0x02))  //月份超过2
             temp4+=1;
    }
    //判断公历日在春节前还是春节后
    if (temp4>=temp3)
    { //公历日在春节后或就是春节当日使用下面代码进行运算
        temp4-=temp3;
        month=0x01;
        month_p=0x01;  //month_p为农历月份指向,公历日在春节前或就是春节当日month_p指向首月
        flag2=get_moon_day(month_p,table_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0
        flag_y=0;
        if(flag2==0)
        temp1=0x1d; //小月29天
        else temp1=0x1e; //大月30天
        temp2=year_code[table_addr]&0xf0;
        temp2=_cror_(temp2,4);  //从数据表中取该年的闰月月份,如为0则该年无闰月
        while(temp4>=temp1){
            temp4-=temp1;
            month_p+=1;
            if(month==temp2)
            {
            flag_y=~flag_y; //闰了该月,月份数值不加
            if(flag_y==0)
            month+=1;
            }
            else month+=1;
            flag2=get_moon_day(month_p,table_addr);
            if(flag2==0)
            temp1=0x1d;
            else 
            temp1=0x1e;
        }
        day=temp4+1;
    }
    else
    {  //公历日在春节前使用下面代码进行运算
        temp3-=temp4;
        if (year==0x00)     //是否在21世纪
        {year=0x63;c=1;}
        else 
        year-=1;
        table_addr-=0x03;
        month=0x0c;
        temp2=year_code[table_addr]&0xf0;  //闰月月份
        temp2=_cror_(temp2,4);
        if (temp2==0)  //无闰月
            month_p=0x0c;     //指向12月
        else
            month_p=0x0d; //   指向13月

         //month_p为月份指向,如果当年有闰月,一年有十三个月,月指向13,无闰月指向12

        flag_y=0;
        flag2=get_moon_day(month_p,table_addr);
        if(flag2==0)
        temp1=0x1d;
        else temp1=0x1e;
        while(temp3>temp1)
        {
            temp3-=temp1;
            month_p-=1;
            if(flag_y==0)
            month-=1;
            if(month==temp2)
            flag_y=~flag_y;
            flag2=get_moon_day(month_p,table_addr);
            if(flag2==0)
            temp1=0x1d;
            else 
            temp1=0x1e;
         }
        day=temp1-temp3+1;
    }
    c_moon=c;                 //HEX->BCD ,运算结束后,把数据转换为BCD数据
    temp1=year/10;
    temp1=_crol_(temp1,4);
    temp2=year%10;
    year_moon=temp1|temp2;
    temp1=month/10;
    temp1=_crol_(temp1,4);
    temp2=month%10;
    month_moon=temp1|temp2;
    temp1=day/10;
    temp1=_crol_(temp1,4);
    temp2=day%10;
    day_moon=temp1|temp2;
}

#endif
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换