⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 通过AVR单片机驱动DS1302时钟芯片
💻 C
字号:
#include<iom16v.h>
#include <macros.h>
#include"1302.h"
#include "lcd12864.h"


//顺序:秒,分,时,日,月,星期,年;   格式:BCD
uchar times[7];
//液晶显示数字编码
uchar shuzi[] = {"0123456789"};
//游标位置变量
uchar PP = 0;
//是否处于修改状态标志,1--是,0--否
uchar cflag = 0;
uchar Key4Scan(void);
void ShowTime(void);

/****************主函数****************/
void main(void)
{      
    Reset_DS1302();             //初始化DS1302
    lcd__Init();                 //初始化液晶
    while(1)
    {
        if(!cflag)
        {
            Get_DS1302(times);          //获取时间数据
            ShowTime();                 //转换显示
        }
        
        switch(Key4Scan())
        {
        case 0x01:
                    switch(PP++)      //确定游标地址
                    {
                    case 0: write_com(0x80);break;
                    case 1: write_com(0x83);break;
                    case 2: write_com(0x80);break;
                    case 3: write_com(0x80);break;
                    case 4: write_com(0x80);break;
                    case 5: write_com(0x80);break;
                    case 6: write_com(0x80);break;
                    default:break;
                    }                  
                    write_com(0x0f);   //打开游标
                    if(PP == 7) PP = 0;
                    cflag = 1;          //标志置位
                    break;
        case 0x02:
                    if(cflag)
                    {
                        switch(PP)
                        {
                        case 1:         //年
                                times[6]++;
                                if((times[6]&0x0f) == 0x0a)
                                {
                                    times[6] += 0x06;       
                                }
                                if(times[6] > 0x99)
                                {
                                    times[6] = 0x00;    
                                }
								write_com(0x80);
                                write_data(shuzi[times[6]>>4]);
                                write_data(shuzi[times[6]&0x0f]);
                                write_com(0x80); //把光标停留在这里不动
                                break;
                        case 2:         //月
                                times[4]++;
                                if((times[4]&0x0f) == 0x0a)
                                {
                                    times[4] += 0x06;       
                                }
                                if(times[4] > 0x12)
                                {
                                    times[4] = 0x01;    
                                }
								write_com(0x83);
                                write_data(shuzi[times[4]>>4]);
                                write_data(shuzi[times[4]&0x0f]);
                                write_com(0x83);
                                break;
                         case 3:         //日
                                times[3]++;
                                if((times[3]&0x0f) == 0x0a)
                                {
                                    times[3] += 0x06;       
                                }
                                if(times[3] > 0x31)
                                {
                                    times[3] = 0x01;    
                                }
								write_com(0x85);
                                write_data(shuzi[times[3]>>4]);
                                write_data(shuzi[times[3]&0x0f]);
                                write_com(0x85);
                                break;
                         case 4:         //周
                                times[5]++;
                                if((times[5]&0x0f) == 0x08)
                                {
                                    times[5] = 0x01;       
                                }
                                write_com(0x86);
								write_data(shuzi[times[5]]);
                                write_com(0x86);
                                break;
                         case 5:         //时
                                times[2]++;
                                if((times[2]&0x0f) == 0x0a)
                                {
                                    times[2] += 0x06;       
                                }
                                if(times[2] > 0x23)
                                {
                                    times[2] = 0x00;    
                                }
								write_com(0x90);
                                write_data(shuzi[times[2]>>4]);
                                write_data(shuzi[times[2]&0x0f]);
                                write_com(0x90);
                                break;
                         case 6:         //分
                                times[1]++;
                                if((times[1]&0x0f) == 0x0a)
                                {
                                    times[1] += 0x06;       
                                }
                                if(times[1] > 0x59)
                                {
                                    times[1] = 0x00;    
                                }
								write_com(0x93);
                                write_data(shuzi[times[1]>>4]);
                                write_data(shuzi[times[1]&0x0f]);
                                write_com(0x93);
                                break;
                         case 0:         //秒
                                times[0]++;
                                if((times[0]&0x0f) == 0x0a)
                                {
                                    times[0] += 0x06;       
                                }
                                if(times[0] > 0x59)
                                {
                                    times[0] = 0x00;    
                                }
								write_com(0x95);
                                write_data(shuzi[times[0]>>4]);
                                write_data(shuzi[times[0]&0x0f]);
                                write_com(0x95);
                                break;
                        default:
                                break;                            
                                
                        }
                    }
                    break;
        case 0x03:
                    if(cflag)
                    {
                        cflag = 0;
                        PP = 0;
                       write_com(0x0c);   //关闭游标
                    }
                    break;
        case 0x04:
                    if(cflag)
                    {
                        cflag = 0;
                        PP = 0;
                        write_com(0x0c);    //关闭游标
                        Set_DS1302(times);
                    }
                    break; 
        default:
                    break;                    
        }
    }
}
/*******************************************
函数名称:ShowTime
功    能:将DS1302的时间转换成10进制显示
参    数:无
返回值  :无
********************************************/    
void ShowTime(void)
{
    uchar h1[14];       //第1行显示数据
    uchar h2[8];        //第2行显示数据
    
    h1[0] = shuzi[2];
    h1[1] = shuzi[0];
    h1[2] = shuzi[times[6]>>4];     //年
    h1[3] = shuzi[times[6]&0x0f];
    h1[4] = 0x2d;       //"-"
    h1[5] = shuzi[times[4]>>4];     //月
    h1[6] = shuzi[times[4]&0x0f];
    h1[7] = 0x2d;       //"-"
    h1[8] = shuzi[times[3]>>4];     //日
    h1[9] = shuzi[times[3]&0x0f];
    h1[10] = 0x20;      //" "
    h1[11] = 0x2a;      //"*"
    h1[12] = shuzi[times[5]];       //周
    h1[13] = 0x2a;      //"*"
    write_string(h1);           //在第一行显示
   
    h2[0] = shuzi[times[2]>>4];     //时
    h2[1] = shuzi[times[2]&0x0f];
    h2[2] = 0x3a;       //":"
    h2[3] = shuzi[times[1]>>4];     //分
    h2[4] = shuzi[times[1]&0x0f];
    h2[5] = 0x3a;       //":"
    h2[6] = shuzi[times[0]>>4];     //秒
    h2[7] = shuzi[times[0]&0x0f];
    write_string(h2);           //在第二行显示
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -