本帖最后由 死磕单片机 于 2015-10-31 23:11 编辑
这个是网上看到的程程序自己用IIC的1602显示,上传代码后时间不走,一直是2000-00-00;00-00-00,请大家帮帮忙,小弟谢谢大家了。
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <stdio.h>
#include <string.h>
#include <DS1302.h>
//#define beta 4090 // from your thermistors datasheet
#define resistance 10
//LiquidCrystal lcd(12,11,5,4,3,2);
/* Set the appropriate digital I/O pin connections */
uint8_t CE_PIN = 6;
uint8_t IO_PIN = 7;
uint8_t SCLK_PIN = 8;
/* Create buffers */
char buf[20];
char buf2[20];
char day[10];
/* Create a DS1302 object */
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
char *print_data()
{
/* Get the current time and date from the chip */
Time t = rtc.time();
/* Name the day of the week */
memset(day, 0, sizeof(day)); /* clear day buffer */
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;
}
/* Format the time and date and insert into the temporary buffer */
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d",
day,
t.yr, t.mon, t.date);
/* Print the formatted string to serial so we can see the time */
return(buf);
}
char *print_time(){
Time t=rtc.time();
snprintf(buf2,sizeof(buf2),"%02d:%02d:%02d ",t.hr,t.min,t.sec);
return(buf2);
}
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.begin(16,2);
Serial.begin(9600);
rtc.write_protect(false);
rtc.halt(false);
Time t(2014, 2, 8, 14, 50, 40 , 7);
rtc.time(t);
}
void loop(){
delay(1000);
lcd.clear();
lcd.print(print_data());
lcd.setCursor(0,1);
lcd.print(print_time());
Serial.println(print_data());
Serial.println(print_time()); |