plantpark 发表于 2012-8-20 23:19:47

为什么我不能通过串口修改时间呢?

太行摄狼 发表于 2012-8-30 21:16:41

高深的3102,买了芯片还没搞,先学习下

939670129 发表于 2012-9-20 09:24:33

不错,挺实用的

ewx_boy 发表于 2012-10-22 17:59:43

为了这个文章,买了个时钟模块~~~

智Tang平 发表于 2012-11-19 16:34:47

再加个温度传感器应该不错

wenhong2966 发表于 2012-11-20 22:08:14

编译的时候怎么出现这个错误啊

提示如下:
error: 'String' does not name a type In function 'void print_time()':
In function 'void setup()':
In function 'void loop()':

TranceMania 发表于 2013-1-3 00:39:12

我稍微改写了下程序做个测试,想让UNO的13脚在晚上8点到12点输出低电平,其余时刻输出高电平。

但在运行过程中发现了个小问题:
串口供电时一切正常,而用电源口供电时本该低电平的时候,13引脚的LED却是无规律的闪烁。

请各位老师指教,谢谢了!

代码:
#include <stdio.h>
#include <string.h>
#include <DS1302.h>

/* 接口定义
CE(DS1302 pin5) -> Arduino D5
IO(DS1302 pin6) -> Arduino D6
SCLK(DS1302 pin7) -> Arduino D7
*/
uint8_t CE_PIN   = 5;
uint8_t IO_PIN   = 6;
uint8_t SCLK_PIN = 7;

/* 日期变量缓存 */
char buf;
char day;
/* 串口数据缓存 */
String comdata = "";
int numdata ={0}, j = 0, mark = 0, k = 0;
/* 创建 DS1302 对象 */
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);


void print_time()
{
   /* 从 DS1302 获取当前时间 */
   Time t = rtc.time();
   /* 将星期从数字转换为名称 */
   memset(day, 0, sizeof(day));
   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;
   }
   /* 将日期代码格式化凑成buf等待输出 */
   snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", day, t.yr, t.mon, t.date, t.hr, t.min, t.sec);
   /* 输出日期到串口 */
   Serial.println(buf);
   k = t.hr;
}


void setup()
{
   Serial.begin(9600);
   rtc.write_protect(false);
   rtc.halt(false);
   pinMode(13,OUTPUT);
}

void loop()
{
   if( k>=19 && k<=23 ) {
   digitalWrite(13,LOW);
   }
   else{
   digitalWrite(13,HIGH);
   }
   /* 当串口有数据的时候,将数据拼接到变量comdata */
   while (Serial.available() > 0)
   {
         comdata += char(Serial.read());
         delay(2);
         mark = 1;
   }
   /* 以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组 */
   if(mark == 1)
   {
         Serial.print("You inputed : ");
         Serial.println(comdata);
         for(int i = 0; i < comdata.length() ; i++)
         {
             if(comdata == ',' || comdata == 0x10 || comdata == 0x13)
             {
               j++;
             }
             else
             {
               numdata = numdata * 10 + (comdata - '0');
             }
         }
         /* 将转换好的numdata凑成时间格式,写入DS1302 */
         Time t(numdata, numdata, numdata, numdata, numdata, numdata, numdata);
         rtc.time(t);
         mark = 0;j=0;
         /* 清空 comdata 变量,以便等待下一次输入 */
         comdata = String("");
         /* 清空 numdata */
         for(int i = 0; i < 7 ; i++) numdata=0;
   }
   
   /* 打印当前时间 */
   print_time();
   delay(1000);
}

luckly 发表于 2013-2-20 15:08:40

我按上面的介绍,试了一下,编译通不过。请指教。

Ansifa 发表于 2013-2-20 16:03:52

luckly 发表于 2013-2-20 15:08 static/image/common/back.gif
我按上面的介绍,试了一下,编译通不过。请指教。

你的库文件没装好吧?或者没放对位置

Ansifa 发表于 2013-2-20 16:04:00

luckly 发表于 2013-2-20 15:08 static/image/common/back.gif
我按上面的介绍,试了一下,编译通不过。请指教。

你的库文件没装好吧?或者没放对位置

luckly 发表于 2013-2-20 16:12:13

Ansifa 发表于 2013-2-20 16:04 static/image/common/back.gif
你的库文件没装好吧?或者没放对位置

谢谢,我再试试。我是解压出来直接放到Libraries 里面了。

caodingguang 发表于 2013-3-23 20:41:32

#include <IRremote.h>
#include <IRremoteInt.h>
#include <stdio.h>
#include <string.h>
#include <DS1302.h>

int CE_PIN   = 5;
int IO_PIN   = 6;
int SCLK_PIN = 7;
int RECV_PIN = 11;
int flag=0;
int MQ_count=0;
int SR_count=0;
int FR_count=0;
char buf;
String sms_SR="A huge of human is coming ";
String sms_MQ="A huge of gas is coming";
String sms_FR="A huge of fire is coming";
/* 创建 DS1302 对象 */
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
    Serial.begin(9600);
    rtc.writeProtect(false);
    rtc.halt(false);
    irrecv.enableIRIn();
}

void loop()
{   
if (irrecv.decode(&results))
    {
      if(results.value==16753245)
      {
          delay(1000);
          delay(1000);
          Serial.println("system is on");
          while(1)
            {
                SR501_det();
                MQ2_det();
                FR_det();
                switch(flag)
                  {
                  case 1 : if(SR_count==0)
                              {
                                  SR_count=1;
                                  Add_time();
                                  sms_SR.concat(buf);
                                  gsm_sms(sms_SR);
                               }
                               break;
                   case 2 : if(MQ_count==0)
                              {
                                 MQ_count=1;
                                 Add_time();
                                 sms_MQ.concat(buf);
                                 gsm_sms(sms_MQ);
                              }
                              break;
                   case 3 : if(FR_count==0)
                              {
                              FR_count=1;
                              Add_time();
                              sms_FR.concat(buf);
                              gsm_sms(sms_FR);
                              }
                              break;
                  }
            }
      }
      irrecv.resume();
    }
}

void gsm_sms(String text)
{
    Serial.println("AT");
    delay(100);
    Serial.println("AT+CMGF=1");
    delay(100);
    Serial.println("AT+CMGS=\"618975\"");
    delay(100);
    Serial.print(text);
    Serial.write(0x0D);
    delay(100);
    Serial.write(0x1A);
    Serial.write(0x0D);
    delay(100);
}

void Add_time()
{
   Time t = rtc.getTime();
   snprintf(buf, sizeof(buf), "%02d:%02d:%02d", t.hour, t.min, t.sec);
}

void SR501_det()
{
    int SR501;
    SR501=analogRead(5);
    if(SR501>700)
      {
      //digitalWrite(3,LOW);
      //delay(1000);
      //digitalWrite(3,HIGH);
      flag=1;
      }
}

void MQ2_det()
{
    int MQ2;
    MQ2=analogRead(4);
    if(MQ2>100)
      {
      //digitalWrite(2,LOW);
      //delay(1000);
      //digitalWrite(2,HIGH);
      flag=2;
      }
}

void FR_det()
{
int fire;
fire=analogRead(3);
if(fire<100)
    {
      flag=3;
    }
}

golden_zjy 发表于 2013-5-22 14:51:02

用了一楼的库和代码,输出的秒和分都有问题,不连贯乱跳。60秒分也不加1.请哪位高手帮忙诊断下。:dizzy:
我的环境:UNO+IDE1.0.5
楼上的库文件中的引用WProgram.h已经改成Arduino.h

flashria 发表于 2013-7-30 20:02:33



请问我这是为什么?我就用了一个ds1302+32.768k面包板搭了个电路 请问还需要别的吗?

flashria 发表于 2013-7-30 21:57:23

lionelg 发表于 2012-4-6 19:40 static/image/common/back.gif
有个问题,显示的时间总是Friday 2165-25-44 27:00:24
也没办法设置,可以显示输入,但是时间还是不变,我 ...

您好我测试ds1302也是这个问题 Saturday 2165-25-45 27:165:85一直显示这个时间请问您怎么解决的?

页: 1 2 3 4 [5] 6 7 8 9 10 11
查看完整版本: Arduino学习笔记A8 - Arduino 连接 DS1302时钟模块