|
|
我自己想做一个串口时钟,用串口发送指令返回时间信息。自己就利用其他高手的帖子自己写了一个程序。但是有一个问题自己解决不了。怎么显示24小时制的时间?。
如图所示,我下午15点设置的时间,显示的是3
下面是我在DS3231中写的时间
- //调用DS3231库与i2c驱动库
- #include <DS3231.h>
- #include <Wire.h>
- //初始化ds3231以及所需要的变量
- DS3231 Clock;
- bool Century=false;
- bool h12;
- bool PM;
- byte ADay, AHour, AMinute, ASecond, ABits;
- bool ADy, A12h, Apm;
-
- byte year, month, date, DoW, hour, minute, second;
-
- void setup() {
-
- Wire.begin();
- Clock.setSecond(50);//配置秒
- Clock.setMinute(32);//配置分钟
- Clock.setHour(17); //配置小时(24小时制)
- Clock.setDate(22); //配置日
- Clock.setMonth(9); //配置月
- Clock.setYear(15); //配置年 (仅最后两位)
-
- }
- void loop() {
- }
复制代码
下面是我在ARDUINO中写的读取指令
- #include <DS3231.h>
- #include <Wire.h>
-
-
- //========定义时钟需要的各种变量========
- DS3231 Clock;
- bool Century=false;
- bool h12;
- bool PM;
- byte ADay, AHour, AMinute, ASecond, ABits;
- bool ADy, A12h, Apm;
- int second,minute,hour,date,month,year,val;
-
-
- void setup(void) {
-
- Serial.begin(115200);
-
- }
-
- void ReadDS3231()
- {
- int second,minute,hour,date,month,year,pm;
- second=Clock.getSecond();
- minute=Clock.getMinute();
- hour=Clock.getHour(h12,PM);
- date=Clock.getDate();
- month=Clock.getMonth(Century);
- year=Clock.getYear();
-
-
- Serial.print("20");
- Serial.print(year,DEC);
- Serial.print('-');
- Serial.print(month,DEC);
- Serial.print('-');
- Serial.print(date,DEC);
- Serial.print(' ');
- Serial.print(hour,DEC);
- Serial.print(':');
- Serial.print(minute,DEC);
- Serial.print(':');
- Serial.print(second,DEC);
- Serial.print('\n');
- }
- void loop()
- {
- val=Serial.read();
- if(val=='a')
- ReadDS3231();
- }
复制代码
求各路高手指点迷津 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|