|
发表于 2020-2-2 12:58:12
|
显示全部楼层
本帖最后由 慕名而来 于 2020-2-2 13:29 编辑
其实玩时钟,按键部分占的比重不小,下面说说我的思路(程序在我以前的帖子里都有贴过),菜鸟编程漏洞很多希望编程的高手指点。
关于按键部分涉及以下这些:
1.正常走时显示函数:
例如:OLED0.96屏&RX8025
- //====时间显示函数==================================
- void dasply_time()
- {
- read_8025time(&time); //读取实时时间time
- PutGB8_16(1,0,9,HEX_D(time[6])/10);
- PutGB8_16(1,0,17,HEX_D(time[6])%10); //显示年
- PutGB16_16(1,0,25,"年");
- PutGB8_16(1,0,45,HEX_D(time[5])/10);
- PutGB8_16(1,0,53,HEX_D(time[5])%10); //显示月
- PutGB16_16(1,0,65,"月");
- PutGB8_16(1,0,85,HEX_D(time[4])/10);
- PutGB8_16(1,0,93,HEX_D(time[4])%10); //显示日
- PutGB16_16(1,0,105,"日");
- PutGB16_32(1,2,16,HEX_D(time[2])/10);
- PutGB16_32(1,2,32,HEX_D(time[2])%10); //显示时
- PutGB16_32(1,2,80,HEX_D(time[1])/10);
- PutGB16_32(1,2,96,HEX_D(time[1])%10); //显示分
- PutGB16_16(1,6,90,"周");
- set_week(); //显示星期
- }
复制代码
2.调整数据显示函数:根据选择按键的键值r调整相关项
- <p>//====时间调整显示函数===============================
- void set_time(uchar c)
- {</p><p> switch(c)
- {
- case 0x01:
- {
- time[6]=read_8025(0x06);//读年
- PutGB8_16(0,0,9,HEX_D(time[6])/10);
- PutGB8_16(0,0,17,HEX_D(time[6])%10); //显示年
- PutGB16_16(1,0,25,"年");
- Adjustment(time[6],6);//调整年
- }
- break;
- case 0x02:
- {
- time[5]=read_8025(0x05);//读月
- PutGB8_16(0,0,45,HEX_D(time[5])/10);
- PutGB8_16(0,0,53,HEX_D(time[5])%10); //显示月
- PutGB16_16(1,0,65,"月");
- Adjustment(time[5],5);//调整月
- }
- break;
- case 0x03:
- {
- time[4]=read_8025(0x04);//读日
- PutGB8_16(0,0,85,HEX_D(time[4])/10);
- PutGB8_16(0,0,93,HEX_D(time[4])%10); //显示日
- PutGB16_16(1,0,105,"日");
- Adjustment(time[4],4);//调整日
- }
- break;
- case 0x04:
- {
- time[2]=read_8025(0x02);//读时
- PutGB16_32(0,2,16,HEX_D(time[2])/10);
- PutGB16_32(0,2,32,HEX_D(time[2])%10); //显示时
- Adjustment(time[2],2);//调整时
- }
- break;
- case 0x05:
- {
- time[1]=read_8025(0x01);//读分
- PutGB16_32(0,2,80,HEX_D(time[1])/10);
- PutGB16_32(0,2,96,HEX_D(time[1])%10); //显示分
- Adjustment(time[1],1);//调整分
- }
- break;
- }
- write_8025(0x00,0x00); //秒归0开始运行
- }</p>
复制代码
3.配合调整显示函数还要用到数据范围限制函数:
- //-------时间数据调节范围设定函数---------------------
- //仅调整1-分、2-时、3-星期、4-日、5-月、6-年、共6个数据
- uchar data_range(signed char date,uchar i)//定义有符号变量适应数值<0的判断
- {
- switch(i)
- {
- case 0x01: {
- if(date>59) date=0;
- if(date<0) date=59;
- }
- break;
- case 0x02: {
- if(date>23) date=0;
- if(date<0) date=23;
- }
- break;
- case 0x03: {
- if(date>06) date=0;
- if(date<0) date=06;
- }
- break;
- case 0x04: {
- if(date>31) date=1;
- if(date<1) date=31;
- }
- break;
- case 0x05: {
- if(date>12) date=1;
- if(date<1) date=12;
- }
- break;
- case 0x06: {
- if(date>99) date=19;
- if(date<19) date=99;
- }
- break;
- }
- return(date);
- }
复制代码
4.调整项选择按键函数:返回键值r
- //------设置按键函数--------------------------------
- uchar Choice_set()
- {
- if(N1==0) //选择键
- {
- Delay_1ms(25);
- if(N1==0)
- {
- r++;
- if(r>5)
- {
- r=0;
- }
- while(!N1);
- }
- }
- return(r);
- }
复制代码
加减按键函数:
- //-------按键加减调整函数---------------------------------
- void Adjustment(signed char da,uchar i)//定义有符号变量适应数值<0的判断
- {
- signed char dat;
- dat=HEX_D(da);
- if(N2==0)
- {
- Delay_1ms(25);
- if(N2==0)
- { //如果按动上调键
- dat++; //数字加
- dat=data_range(dat,i);
- while(!N2);
- }
- }
- if(N3 == 0)
- {
- Delay_1ms(25);
- if(N3 == 0)
- { //如果按动下调键
- dat--; //数字减
- dat=data_range(dat,i);
- while(!N3);
- }
- }
- write_8025(i,D_HEX(dat));
- }
复制代码
这些函数通过主函数调用的方法是:先设一个公共变量r
void main(void)
{
******
******
r = 0;//初始化这个变量
while(1)
{
//程序开始就调用这个按键选择函数查看变量r的状态
Choice_set();
//接下来根据r的状态来选择需要运行的函数
if(r != 0) //调整键按下时进入调时状态
{
//如果r的状态有变化则执行调试模式
set_time(r); //调用调试函数
//进入set_time()函数后将r值赋值给 switch(c)中的c,再根据c显示需要调整项的数据
//例如:c=1则执行下面这段代码:
//case 0x01:{time[6]=read_8025(0x06);//读年 PutGB8_16(0,0,9,HEX_D(time[6])/10);******}
//此函数在调整数据时又要调用数据范围限制函数来控制数据的变化范围
}
//如果r=0没有变化或者已经调试完成被清零了就执行正常的走时函数
else if(r == 0) //走时
dasply_time();
}
}
|
打赏
-
查看全部打赏
|