极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12291|回复: 9

关于DS1302

[复制链接]
发表于 2014-4-10 15:46:25 | 显示全部楼层 |阅读模式
本帖最后由 悪の猫 于 2014-4-10 18:52 编辑

我用mega2560,5,6,7分别接DS1302的SCLK,IO,CE,但是时间显示

我是根据  Arduino学习笔记A8 - Arduino 连接 DS1302时钟模块  中提供的1.0可用库文件及自带例子弄的。
设定37秒,确实过了23S 分的那个数据会变,但是数字显示完全不对。
是不是需要上拉电阻?能不能内部上拉(似乎有说只有input才能内部上拉)?


/*
Example sketch for interfacing with the DS1302 timekeeping chip.

Copyright (c) 2009, Matt Sparks
All rights reserved.

http://quadpoint.org/projects/arduino-ds1302
*/
#include <stdio.h>
#include <string.h>
#include <DS1302.h>

/* Set the appropriate digital I/O pin connections */
uint8_t CE_PIN   = 5;
uint8_t IO_PIN   = 6;
uint8_t SCLK_PIN = 7;

/* Create buffers */
char buf[50];
char day[10];

/* Create a DS1302 object */
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);


void print_time()
{
  /* 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, "Sunday");
      break;
    case 2:
      strcpy(day, "Monday");
      break;
    case 3:
      strcpy(day, "Tuesday");
      break;
    case 4:
      strcpy(day, "Wednesday");
      break;
    case 5:
      strcpy(day, "Thursday");
      break;
    case 6:
      strcpy(day, "Friday");
      break;
    case 7:
      strcpy(day, "Saturday");
      break;
  }

  /* Format the time and date and insert into the temporary buffer */
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day,
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  /* Print the formatted string to serial so we can see the time */
  Serial.println(buf);
}


void setup()
{
  Serial.begin(9600);

  /* Initialize a new chip by turning off write protection and clearing the
     clock halt flag. These methods needn't always be called. See the DS1302
     datasheet for details. */
  rtc.write_protect(false);
  rtc.halt(false);

  /* Make a new time object to set the date and time */
  /*   Tuesday, May 19, 2009 at 21:16:37.            */
  Time t(2009, 5, 19, 21, 16, 37, 3);

  /* Set the time and date on the chip */
  rtc.time(t);
}


/* Loop and print the time every second */
void loop()
{
  print_time();
  delay(1000);
}

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2014-4-10 16:40:44 | 显示全部楼层
线太长了,或者是杜邦线,面包线接触不好。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-10 16:45:04 | 显示全部楼层
fisherdl 发表于 2014-4-10 16:40
线太长了,或者是杜邦线,面包线接触不好。

20CM的公母头,貌似没有更短的了诶....那上拉能不能解决?
回复 支持 反对

使用道具 举报

发表于 2014-4-10 18:10:10 | 显示全部楼层
算法错了吧。
回复 支持 反对

使用道具 举报

发表于 2014-4-10 21:49:03 | 显示全部楼层
程序没看明白,好似就是在setup里设置了一下时间,然后loop里循环显示时间。 为什么会变呢?不明白
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-11 00:27:22 | 显示全部楼层
shihaipeng04 发表于 2014-4-10 21:49
程序没看明白,好似就是在setup里设置了一下时间,然后loop里循环显示时间。 为什么会变呢?不明白

时间变化是DS1302模块..arduino只是读取和设置而已。1302的库也应该是没问题的。
回复 支持 反对

使用道具 举报

发表于 2014-4-11 00:35:12 | 显示全部楼层
悪の猫 发表于 2014-4-11 00:27
时间变化是DS1302模块..arduino只是读取和设置而已。1302的库也应该是没问题的。

我是指时间从 1:42:49 一下变成 1:23:00 。 每秒变的那是正常的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-11 11:06:52 | 显示全部楼层
shihaipeng04 发表于 2014-4-11 00:35
我是指时间从 1:42:49 一下变成 1:23:00 。 每秒变的那是正常的

哦.查来查去要么就是模块问题,要么就是干扰,实在不行换个3231试试- -
回复 支持 反对

使用道具 举报

发表于 2014-4-11 15:25:53 | 显示全部楼层
悪の猫 发表于 2014-4-11 11:06
哦.查来查去要么就是模块问题,要么就是干扰,实在不行换个3231试试- -

1307我有一个,很不准,半个月快了2个小时,  3231刚买的试了试 10天慢了2秒,应该还算不错的成绩。价格都差不多
回复 支持 反对

使用道具 举报

发表于 2014-4-15 10:36:33 | 显示全部楼层
悪の猫 发表于 2014-4-10 16:45
20CM的公母头,貌似没有更短的了诶....那上拉能不能解决?

我也遇到过这个问题,碰一下显示就不正常。不信你用线焊上去试试,显示就正常了。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 00:59 , Processed in 0.036692 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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