刚刚开始玩,1602+ds1302++lm35,依葫芦画瓢写了程序,目前能显示时间、温度,但是想通过扩展板的按键更改时间,应该如何实现。
- #include <LiquidCrystal.h>
- #include <stdio.h>
- #include <string.h>
- #include <DS1302.h>
- LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
- uint8_t CE_PIN = 2;
- uint8_t IO_PIN = 3;
- uint8_t SCLK_PIN = 11;
- char buf[50];
- char day[10];
- String comdata = "";
- int numdata[7] ={0}, j = 0, mark = 0;
- DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
- int potPin = 4;
- float temperature = 0;
- long val=0;
- void print_time()
- {
-
- Time t = rtc.time();
-
- memset(day, 0, sizeof(day));
- switch (t.day)
- {
- case 1: strcpy(day, "Sun"); break;
- case 2: strcpy(day, "Mon"); break;
- case 3: strcpy(day, "Tue"); break;
- case 4: strcpy(day, "Wed"); break;
- case 5: strcpy(day, "Thu"); break;
- case 6: strcpy(day, "Fri"); break;
- case 7: strcpy(day, "Sat"); break;
- }
-
- snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d ", day, t.yr, t.mon, t.date);
-
- lcd.clear();
- lcd.begin(16, 2);
- lcd.setCursor(0,0);
- lcd.print(buf);
- snprintf(buf, sizeof(buf), "%02d:%02d:%02d", t.hr, t.min, t.sec);
- lcd.setCursor(0, 1);
- lcd.print(buf);
- }
- void setup()
- {
- Serial.begin(9600);
- rtc.write_protect(false);
- rtc.halt(false);
- }
- void loop()
- {
-
- while (Serial.available() > 0)
- {
- comdata += char(Serial.read());
- delay(2);
- mark = 1;
- }
-
- if(mark == 1)
- {
- Serial.print("You inputed : ");
- Serial.println(comdata);
- for(int i = 0; i < comdata.length() ; i++)
- {
- if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
- {
- j++;
- }
- else
- {
- numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
- }
- }
-
- Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
- rtc.time(t);
- mark = 0;j=0;
-
- comdata = String("");
-
- for(int i = 0; i < 7 ; i++) numdata[i]=0;
- }
-
-
- print_time();
-
- val = analogRead(potPin);
- temperature = (val*0.0048828125*1000);
- lcd.setCursor(10, 1) ;
- lcd.print((long)temperature / 10);
- lcd.print(".");
- lcd.print( (long)temperature % 10);
- lcd.print((char)223);
- lcd.print("C");
-
- delay(1000);
-
- }
复制代码 |