悪の猫 发表于 2014-4-10 15:46:25

关于DS1302

本帖最后由 悪の猫 于 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;
char day;

/* 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);
}

fisherdl 发表于 2014-4-10 16:40:44

线太长了,或者是杜邦线,面包线接触不好。

悪の猫 发表于 2014-4-10 16:45:04

fisherdl 发表于 2014-4-10 16:40 static/image/common/back.gif
线太长了,或者是杜邦线,面包线接触不好。

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

林定祥 发表于 2014-4-10 18:10:10

算法错了吧。

shihaipeng04 发表于 2014-4-10 21:49:03

程序没看明白,好似就是在setup里设置了一下时间,然后loop里循环显示时间。 为什么会变呢?不明白

悪の猫 发表于 2014-4-11 00:27:22

shihaipeng04 发表于 2014-4-10 21:49 static/image/common/back.gif
程序没看明白,好似就是在setup里设置了一下时间,然后loop里循环显示时间。 为什么会变呢?不明白

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

shihaipeng04 发表于 2014-4-11 00:35:12

悪の猫 发表于 2014-4-11 00:27 static/image/common/back.gif
时间变化是DS1302模块..arduino只是读取和设置而已。1302的库也应该是没问题的。

我是指时间从 1:42:49 一下变成 1:23:00 。 每秒变的那是正常的

悪の猫 发表于 2014-4-11 11:06:52

shihaipeng04 发表于 2014-4-11 00:35 static/image/common/back.gif
我是指时间从 1:42:49 一下变成 1:23:00 。 每秒变的那是正常的

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

shihaipeng04 发表于 2014-4-11 15:25:53

悪の猫 发表于 2014-4-11 11:06 static/image/common/back.gif
哦.查来查去要么就是模块问题,要么就是干扰,实在不行换个3231试试- -

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

fisherdl 发表于 2014-4-15 10:36:33

悪の猫 发表于 2014-4-10 16:45 static/image/common/back.gif
20CM的公母头,貌似没有更短的了诶....那上拉能不能解决?

我也遇到过这个问题,碰一下显示就不正常。不信你用线焊上去试试,显示就正常了。
页: [1]
查看完整版本: 关于DS1302