更加优雅的51单片机时钟(DS1302时钟)

首先是DS1302设置时钟的代码,与其他不同,选择不用数组。将每个功能都写个函数。

#include <REGX52.H>
sbit SCLK = P3 ^ 6;
sbit IO = P3 ^ 4;
sbit CE = P3 ^ 5;
#define SECOND 0x80
#define MIN 0x82
#define HOUR 0x84
#define DATE 0x86
#define MOUTH 0x88
#define DAY 0x8a
#define YEAR 0x8c
void set(unsigned char command, unsigned char time)
{
    unsigned char i;
    CE = 1;
    for (i = 0; i < 8; i++)
    {
        IO = command & (0x01 << i);
        SCLK = 1;
        SCLK = 0;
    }
    for (i = 0; i < 8; i++)
    {
        IO = time & (0x01 << i);
        SCLK = 1;
        SCLK = 0;
    }
    CE = 0;
}
unsigned char get(unsigned char command)
{
    unsigned char i, time = 0x00;
    CE = 1;
    command |= 0x01;
    for (i = 0; i < 8; i++)
    {
        IO = command & (0x01 << i);
        SCLK = 0;
        SCLK = 1;
    }
    for (i = 0; i < 8; i++)
    {
        SCLK = 1;
        SCLK = 0;
        if (IO)
        {
            time = time | (0x01 << i);
        }
    }
    CE = 0;
    IO = 0;
    return time / 16 * 10 + time % 16;
}
void DS1302Initial()
{
    CE = 0;
    SCLK = 0;
    set(0x8E, 0X00);
}
void setSeconds(unsigned char date)
{
    set(SECOND, date);
}
void setMinutes(unsigned char date)
{
    set(MIN, date);
}
void setHour(unsigned char date)
{
    set(HOUR, date);
}
void setDate(unsigned char date)
{
    set(DATE, date);
}
void setMonth(unsigned char date)
{
    set(MOUTH, date);
}
void setDay(unsigned char date)
{
    set(DAY, date);
}
void setYear(unsigned char date)
{
    set(YEAR, date);
}

unsigned char getSeconds()
{
    get(SECOND);
}
unsigned char getMinutes()
{
    get(MIN);
}
unsigned char getHour()
{
    get(HOUR);
}
unsigned char getDate()
{
    get(DATE);
}
unsigned char getMonth()
{
    get(MOUTH);
}
unsigned char getDay()
{
    get(DAY);
}
unsigned char getYear()
{
    get(YEAR);
}

然后是主代码

#include <REGX52.H>
#include "LCD1602.h"
#include "DS_1302.h"
#include "onKey.h"
#include "timer0.h"
#include "Delay.h"
unsigned char time, MODE, sel;
unsigned char temp[6];
unsigned char flashFlag;
void timer0() interrupt 1
{
    static unsigned int count;
    TL0 = 0xAE; // 设置定时初值
    TH0 = 0xFB; // 设置定时初值
    count++;
    if (count >= 500)
    {
        count = 0;
        flashFlag = !flashFlag;
    }
}
void showTime()
{
    LCD_ShowNum(1, 1, getYear(), 2);
    LCD_ShowNum(1, 4, getMonth(), 2);
    LCD_ShowNum(1, 7, getDate(), 2);

    LCD_ShowNum(2, 1, getHour(), 2);
    LCD_ShowNum(2, 4, getMinutes(), 2);
    LCD_ShowNum(2, 7, getSeconds(), 2);
}
void selectionAndSetTime()
{

    if (onKey() == 2)
    {
        sel++;
        sel = sel % 6;
    }
    if (onKey() == 3)
    {
        temp[sel]++;
        if (temp[0] > 99)
            temp[0] = 0;
        if (temp[1] > 12)
            temp[1] = 1;
        if (temp[1] % 2 != 0)
        {
            if (temp[2] > 31)
            {
                temp[2] = 0;
            }
        }
        if (temp[1] != 2 && temp[1] % 2 == 0) //
        {
            if (temp[2] > 30)
            {
                temp[2] = 0;
            }
        }
        if (temp[1] == 2) // 二月
        {
            if (temp[0] % 4 == 0)
            {
                if (temp[2] > 29)
                {
                    temp[2] = 0;
                }
            }
            else
            {
                if (temp[2] > 28)
                {
                    temp[2] = 0;
                }
            }
        }
        if (temp[3] > 24)
            temp[3] = 0;
        if (temp[4] > 60)
            temp[4] = 0;
        if (temp[5] > 60)
            temp[5] = 0;
    }
    if (onKey() == 4)
    {
        temp[sel]--;
        if (temp[0] > 200)
            temp[0] = 99;
        if (temp[1] > 200) // month
            temp[1] = 12;
        if (temp[1] % 2 != 0) // date
        {
            if (temp[2] < 1)
            {
                temp[2] = 31;
            }
        }
        if (temp[1] != 2 && temp[1] % 2 == 0) // min date
        {
            if (temp[2] < 1)
            {
                temp[2] = 30;
            }
        }
        if (temp[1] == 2) // 二月
        {
            if (temp[0] % 4 == 0)
            {
                if (temp[2] > 200)
                {
                    temp[2] = 29;
                }
            }
            else
            {
                if (temp[2] > 200)
                {
                    temp[2] = 28;
                }
            }
        }
        if (temp[3] > 200) // second  min hour
            temp[3] = 24;
        if (temp[4] > 200)
            temp[4] = 60;
        if (temp[5] > 200)
            temp[5] = 60;
    }

    // // flash
    if (sel == 0)
    {
        LCD_ShowNum(2, 7, temp[5], 2);

        if (sel == 0 && flashFlag == 1)
            LCD_ShowString(1, 1, "  ");
        else
            LCD_ShowNum(1, 1, temp[0], 2);
    }

    if (sel == 1)
    {
        LCD_ShowNum(1, 1, temp[0], 2);

        if (sel == 1 && flashFlag == 1)
            LCD_ShowString(1, 4, "  ");
        else
            LCD_ShowNum(1, 4, temp[1], 2);
    }

    if (sel == 2)
    {
        LCD_ShowNum(1, 4, temp[1], 2);

        if (sel == 2 && flashFlag == 1)
            LCD_ShowString(1, 7, "  ");
        else
            LCD_ShowNum(1, 7, temp[2], 2);
    }

    if (sel == 3)
    {
        LCD_ShowNum(1, 7, temp[2], 2);

        if (sel == 3 && flashFlag == 1)
            LCD_ShowString(2, 1, "  ");
        else
            LCD_ShowNum(2, 1, temp[3], 2);
    }
    if (sel == 4)
    {
        LCD_ShowNum(2, 1, temp[3], 2); // mend vanish

        if (sel == 4 && flashFlag == 1)
            LCD_ShowString(2, 4, "  ");
        else
            LCD_ShowNum(2, 4, temp[4], 2);
    }
    if (sel == 5)
    {
        LCD_ShowNum(2, 4, temp[4], 2);

        if (sel == 5 && flashFlag == 1)
            LCD_ShowString(2, 7, "  ");
        else
            LCD_ShowNum(2, 7, temp[5], 2);
    }
}
void save()
{
    setYear(temp[0]/10*16+temp[0]%10);
    setMonth(temp[1]/10*16+temp[1]%10);
    setDate(temp[2]/10*16+temp[2]%10);

    setHour(temp[3]/10*16+temp[3]%10);
    setMinutes(temp[4]/10*16+temp[4]%10);
    setSeconds(temp[5]/10*16+temp[5]%10);
    sel = 0;
}
void main()
{
    DS1302Initial();
    LCD_Init();
    timerInitial();
    LCD_ShowString(2, 1, "  :  :");
    setHour(0x23);
    setMinutes(0x59);
    setSeconds(0x57);

    LCD_ShowString(1, 1, "  -  -");
    setYear(0x25);
    setMonth(0x02);
    setDate(0x030);
    while (1)
    {
        if (onKey() == 1)
        {
            if (MODE == 0)
            {
                // temp[] = {getYear(), getMonth(), getDate(), getHour(), getMinutes(), getSeconds()};
                int i;
                for ( i = 0; i < 6; i++)
                {
                   temp[i]=0;
                }
                
                temp[0] = getYear();
                temp[1] = getMonth();
                temp[2] = getDate();
                temp[3] = getHour();
                temp[4] = getMinutes();
                temp[5] = getSeconds();
                Delay(10);
                MODE = 1;
            }
            else
            {
                save();
                MODE = 0;
            }
        }

        switch (MODE)
        {
        case 0:
            showTime();
            break;
        case 1:
            selectionAndSetTime();
            break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值