找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1372|回复: 1
打印 上一主题 下一主题
收起左侧

单片机数字时钟

[复制链接]
跳转到指定楼层
楼主
ID:324142 发表于 2018-12-10 13:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

基于ds18b20的简单单片机数字时钟
#include <REGX52.H>
#include <stdio.h>
#include "key.h"
#include "EEPROM.h"
#include "LCD1602.h"
#include "DS1302.h"
#include "SPEAKER.H"


bit TimeReadFlag,FlashFlag;

void Delay_ms(unsigned int Cnt);
void SystemInit(void);
void SetAlarm( AlarmTypeDef *Alarm );
void DisTime( RealTimeTypeDef *Time );
void TimeFlash( unsigned char flash );
void SetTime(void);
bit Is_LeapYear(unsigned int year);
unsigned char Get_Week(unsigned int Year,unsigned char Month,unsigned char Date);
unsigned char Get_DateMax(unsigned int Year,unsigned char Month);
unsigned char KeyNum;
unsigned int  KeyUpCnt,KeyDownCnt;

unsigned char code DateMaxTab[13] = {
        0,31,28,31,30,31,30,31,31,30,31,30,31,
};

void main()
{
               
        SystemInit();
        RealTime.Year_H = 20;
       
        while(1)
        {
                if( TimeReadFlag )       
                {
                        GetTime( &RealTime );
                        TimeReadFlag = 0;
                        DisTime( &RealTime );
       
                        //Õûμ㱨ê±
                        if( RealTime.Min==59 && RealTime.Sec>=56 )
                        {
                                RCAP2H = 64614/256;                //500Hz
                                RCAP2L = 64614%256;
                                RCAP1 = 15536;                                //50ms
                                Jiepai = 2;
                                JpCount = 0;
                                Mp = 1;
                                TR1 = 1;
                                TR2 = 1;
                        }
                        else if( RealTime.Min==0 && RealTime.Sec==0 )
                        {
                                RCAP2H = 65075/256;                //1000Hz
                                RCAP2L = 65075%256;
                                RCAP1 = 15536;                                //50ms
                                Jiepai = 10;
                                JpCount = 0;
                                Mp = 1;
                                TR1 = 1;
                                TR2 = 1;
                        }
                }
               
                if( KeyNum==0x13 )
                {
                        while(KeyNum==0x13);
                        SetTime();
                }
        }
}


void Delay_ms(unsigned int Cnt)
{
        unsigned int t;
        while(Cnt--)
        {
                t = 100;
                while(--t);
        }
}


void SystemInit()
{
        TMOD = 0x11;
        TH0 = (65536-10000)/256;
        TL0 = (65536-10000)%256;
        ET0 = 1;
        ET1 = 1;
        ET2 = 1;
        EA  = 1;
        TR0 = 1;
        PT2 = 1;
       
        DS1302_Init();
        LCD_initial();
}


/***********************************
ÏÔê¾ê±¼ä
***********************************/
void DisTime( RealTimeTypeDef *Time )
{
        char cStr[20];
       
        sprintf(cStr,"%04d-%02d-%02d     %1d",Time->Year,(int)Time->Month,(int)Time->Date,(int)Time->Day);
        LCD1602_write_string(0,0,cStr);
        sprintf(cStr,"%02d:%02d:%02d",(int)Time->Hour,(int)Time->Min,(int)Time->Sec);
        LCD1602_write_string(1,4,cStr);
}

/***********************************
ÏÔê¾éá˸
flash 0£o2»éá˸
                        1£oD¡ê±éá˸                        2£o·Öéá˸                        3£oÃëéá˸                                8£oðoÅéá˸
                        4£oÄêéá˸                                5£oÔÂéá˸                        6£oèÕÆúéá˸                        7£oDÇÆúéá˸                       
***********************************/
void TimeFlash( unsigned char flash )
{
        switch( flash )
        {
                case 1:        LCD1602_write_string(1,4,"  ");        break;
                case 2:        LCD1602_write_string(1,7,"  ");        break;
                case 3:        LCD1602_write_string(1,10,"  ");        break;
                case 4:        LCD1602_write_string(0,0,"    ");        break;
                case 5:        LCD1602_write_string(0,5,"  ");        break;
                case 6:        LCD1602_write_string(0,8,"  ");        break;
                case 7:        LCD1602_write_string(0,15," ");        break;
                case 8: LCD1602_write_char(1,6,' ');
                                                LCD1602_write_char(1,9,' ');
                default:break;
        }
}



/****************************************************************************
* Ãû    3Æ£ou8 Is_Leap_Year(u16 year)
* 1|    Äü£oÅD¶ÏêÇ·ñêÇèòÄê
* èë¿ú2Îêy£oyear
* 3ö¿ú2Îêy£o1:êÇèòÄꣻ0:·ÇèòÄê
* Ëμ    Ã÷£o
* μ÷ó÷½·¨£oÎT
****************************************************************************/
bit Is_LeapYear(unsigned int year)
{
        if(year%4==0) //±ØDëÄü±»4Õû3y
        {
                if(year%100==0)
                {
                        if(year%400==0)return 1;//èç1ûòÔ00½áÎ2,»1òaÄü±»400Õû3y
                        else return 0;
                }
                else return 1;
        }
        else return 0;
}

/****************************************************************************
* Ãû    3Æ£ou8 Get_Week(u16 Year,u16 Month,u16 Date)
* 1|    Äü£oóÃ2ìàÕ£¨Zeller£©1«ê½¼ÆËãDÇÆú¼¸       

        w=y+[y/4]+[c/4]-2c+[26£¨m+1£©/10]+d-1

        è»oów¶Ô7è¡óà¡£
        1«ê½ÖDμÄ·ûoÅo¬òåèçÏ£¬w£oDÇÆú£»c£oêà¼í£»y£oÄ꣨á½Î»êy£©£»m£oÔ£¨m′óóúμèóú3£¬D¡
        óúμèóú14£¬¼′Ôú2ìàÕ1«ê½ÖD£¬Ä3ÄêμÄ1¡¢2ÔÂòa¿′×÷éÏò»ÄêμÄ13¡¢14ÔÂà′¼ÆË㣬±èèç2003Äê1ÔÂ1
        èÕòa¿′×÷2002ÄêμÄ13ÔÂ1èÕà′¼ÆË㣩£»d£oèÕ£»[ ]′ú±íè¡Õû£¬¼′Ö»òaÕûêy2¿·Ö¡£

* èë¿ú2Îêy£oYear:Äê; Month:ÔÂ; Date:èÕ¡£
* 3ö¿ú2Îêy£oDÇÆú¼¸ 1~6′ú±íDÇÆúò»~DÇÆúáù£¬0′ú±íDÇÆúìì
* Ëμ    Ã÷£o
* μ÷ó÷½·¨£oÎT
****************************************************************************/
unsigned char Get_Week(unsigned int Year,unsigned char Month,unsigned char Date)
{
        int  W,Y,C,M,D;
        unsigned char day;
       
        C = Year/100;
        Y = Year%100;
        M = Month;
        if(M<3){M+=12;Y--;}
        D = Date;
        W = Y + Y/4 + C/4 - 2*C + 26*(M+1)/10 + D - 1;
        while(W<0)        W += 7;
        day = W%7;
        if(day==0) day = 7;
        return day;
}

unsigned char Get_DateMax(unsigned int Year,unsigned char Month)
{
        unsigned char DateMax = 0;
       
        DateMax = DateMaxTab[ Month ];
        if( Month==2 )
        {
                if( Is_LeapYear( Year ) )        DateMax++;
        }
       
        return DateMax;
}

void SetTime(void)
{
        unsigned char tmpIndex=0,tmp[14];
        unsigned char temp;
        DisTime( &RealTime );
       
        LCD_write_cmd(0x0F);
        LCD1602_SetCursors(0,0);
       
        while( tmpIndex<14 )
        {
                if( KeyNum==0x13 )
                {
                        while(KeyNum==0x13);
                        break;
                }
                if( KeyNum<='9' && KeyNum >='0')// num
                {
                        tmp[ tmpIndex ] = KeyNum-'0';
                        LCD_write_data( KeyNum );
                        tmpIndex++;
                        switch( tmpIndex )
                        {
                                case 4:                LCD1602_SetCursors( 0,5 );break;
                                case 6:                LCD1602_SetCursors( 0,8 );break;
                                case 8:                LCD1602_SetCursors( 1,4 );break;
                                case 10:        LCD1602_SetCursors( 1,7 );break;
                                case 12:        LCD1602_SetCursors( 1,10 );break;
                                default:        break;
                        }
                        while( KeyNum!=0xff );
                }
        }
        if(tmpIndex == 14)
        {
                RealTime.Year = tmp[0]*1000+tmp[1]*100+tmp[2]*10+tmp[3];
                if((tmp[4]*10+tmp[5])<=12 && (tmp[4]*10+tmp[5])>=1)
                        RealTime.Month = tmp[4]*10+tmp[5];
                temp = DateMaxTab[RealTime.Month] +Get_DateMax(RealTime.Year,RealTime.Month);
                if((tmp[6]*10+tmp[7])<=temp && (tmp[6]*10+tmp[7])>=1)   
                RealTime.Date =  tmp[6]*10+tmp[7];
                if((tmp[8]*10+tmp[9])<24 && (tmp[8]*10+tmp[9])>=0)               
                        RealTime.Hour =  tmp[8]*10+tmp[9];
                if((tmp[10]*10+tmp[11])<60 && (tmp[10]*10+tmp[11])>=0)
                        RealTime.Min =  tmp[10]*10+tmp[11];
                if((tmp[12]*10+tmp[13])<60 && (tmp[12]*10+tmp[13])>=0)
                        RealTime.Sec =  tmp[12]*10+tmp[13];       
                RealTime.Day = Get_Week( RealTime.Year , RealTime.Month , RealTime.Date );
                WriteTime( &RealTime );
                RealTime.Year_H = RealTime.Year/100;
               
    }
        LCD_write_cmd(0x0C);
}


DS1302.zip

370.71 KB, 下载次数: 4, 下载积分: 黑币 -5

基于ds1802的简单数字时钟

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2018-12-10 17:32 | 只看该作者
补全原理图或者详细说明一下电路连接即可获得100+黑币
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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