极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12504|回复: 2

新手求助关于DS1302的问题,请大家帮忙看看。

[复制链接]
发表于 2015-10-31 23:06:34 | 显示全部楼层 |阅读模式
本帖最后由 死磕单片机 于 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());

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-11-2 16:19:38 | 显示全部楼层
编译没有错误的话,看看时序?是否进入计数?
回复 支持 反对

使用道具 举报

发表于 2016-4-19 11:41:36 | 显示全部楼层
这是因为你的arduino单片机的时钟信号与DS1302模块的时钟信号没建立连接。把DS1302的CLK_PIN与arduino板子上的SCL_PIN连接在一起,你再试试!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 21:19 , Processed in 0.038771 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表