1 前言
我们常用的时钟,基本的功能有修改,闹钟功能.当然还有定时器的功能,
上一次,我实现了DS1302的基本的写入与读取功能,并且将显示的结果显示到了显示屏上面.
这一次,我将在上一次的基础上面进行功能的加入修改闹钟的功能.
2 功能逻辑
自定义一个Buff[]数组,用于存放修改的数据.闹钟也需要修改数据,当闹钟中修改的数据和从DS1302收到的数据相同的时候,蜂鸣器响.
实现的逻辑十分简单,但是,实现出功能却没有想象中的那么简单.
3 具体实现
我的设计是三种模式
3.1第一种模式是显示时间
if(mode == 1)
{
clock_out(); //将数据读出来
clock_Show(); //将数据显示在屏幕上面
}
3.2第二种模式是修改
if(mode == 2) //暂停修改
{
if(key == 7){Delay_ms(10);Change++;} //S7左移
if(key == 8){Delay_ms(10);Change--;} //S8右移
if(Change >= 100){Change = 0;}
if(Change >= 6){Change = 6;}
if(Change == 3)
{
if(key == 6){Delay_ms(10);Buff[3]++;Buff[6]++;} //S5加
if(key == 5){Delay_ms(10);Buff[3]--;Buff[6]--;} //S6减
}
else if(Change == 6)
{
if(key == 6){Delay_ms(10);Buff[3]++;Buff[6]++;} //S5加
if(key == 5){Delay_ms(10);Buff[3]--;Buff[6]--;} //S6减
}
else
{
if(key == 6){Delay_ms(10);Buff[Change]++;} //S5加
if(key == 5){Delay_ms(10);Buff[Change]--;} //S6减
}
if(Change == 0) //对于秒的溢出处理
{
if(Buff[Change] > 60 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] > 100){Buff[Change] = 60;}
}
if(Change == 1) //对于分的溢出处理
{
if(Buff[Change] > 60 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] > 100){Buff[Change] = 60;}
}
if(Change == 2) //对于时的溢出处理
{
if(Buff[Change] > 24 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] > 100){Buff[Change] = 24;}
}
if(Change == 3) //对于日的溢出处理
{
if(Buff[4]==1|Buff[4]==3|Buff[4]==5|Buff[4]==7|Buff[4]==8|Buff[4]==10|Buff[4]==12)
{
if(Buff[Change] >31 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] >100){Buff[Change] = 31;}
}
if(Buff[4]==4|Buff[4]==6|Buff[4]==9|Buff[4]==11)
{
if(Buff[Change] >30 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] >100){Buff[Change] = 30;}
}
if(Buff[4] == 2) //对于闰年的处理
{
if(Buff[5] % 4 == 0)
{
if(Buff[Change] >29 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] >100){Buff[Change] = 29;}
}
else
{
if(Buff[Change] >28 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] >100){Buff[Change] = 28;}
}
}
if(Buff[6] > 7 & Buff[6] < 100){Buff[6] = 1;}
if(Buff[6] < 1 | Buff[6] > 100){Buff[6] = 7;}
}
if(Change == 4) //对于月的溢出处理
{
if(Buff[Change] >12 & Buff[Change] < 100){Buff[Change] = 0;}
if(Buff[Change] >100){Buff[Change] = 12;}
}
if(Change == 5) //对于年的溢出处理
{
if(Buff[Change] >99 & Buff[Change] < 200){Buff[Change] = 0;}
if(Buff[Change] >200){Buff[Change] = 99;}
}
if(Change == 6) //对于星期几的溢出处理
{
if(Buff[Change] > 7 & Buff[Change] < 100){Buff[Change] = 1;}
if(Buff[Change] < 1 | Buff[Change] > 100){Buff[Change] = 7;}
if(Buff[4]==1|Buff[4]==3|Buff[4]==5|Buff[4]==7|Buff[4]==8|Buff[4]==10|Buff[4]==12)
{
if(Buff[4] >31 & Buff[4] < 100){Buff[4] = 0;}
if(Buff[4] >100){Buff[4] = 31;}
}
if(Buff[4]==4|Buff[4]==6|Buff[4]==9|Buff[4]==11)
{
if(Buff[4] >30 & Buff[4] < 100){Buff[4] = 0;}
if(Buff[4] >100){Buff[4] = 30;}
}
if(Buff[4] == 2) //对于闰年的处理
{
if(Buff[5] % 4 == 0)
{
if(Buff[4] >29 & Buff[4] < 100){Buff[4] = 0;}
if(Buff[4] >100){Buff[4] = 29;}
}
else
{
if(Buff[4] >28 & Buff[4] < 100){Buff[4] = 0;}
if(Buff[4] >100){Buff[4] = 28;}
}
}
}
DS1302_WriteByte(0x8E,0x00);
TimE[Change] = Buff[Change]/10*16+Buff[Change]%10;
clock_in(); //将改变的缓冲区写入
DS1302_WriteByte(0x8E,0x80);
clock_Show();
Delay_ms(500);
if(Change == 0){OLED_ShowString(3,12," ");}
if(Change == 1){OLED_ShowString(3,9," ");}
if(Change