极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13726|回复: 1

时钟模块+温湿度模块+12864,系统一直自动复位是怎么回事

[复制链接]
发表于 2012-8-25 18:29:57 | 显示全部楼层 |阅读模式
环境:
Arduino nao 板子 + ds1302 时钟模块 + 温湿度传感器 + LCD12864中文屏幕

功能:显示年月日、星期、时间、温度、湿度

代码如下:
  1. #include "LCD12864RSPI.h"
  2. #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <DS1302.h>
  6. #include <dht11.h>
  7. #define DHT11PIN A0

  8. unsigned char yearunit[]={
  9.   0xc4,0xea,0x20,0x20,0xd4,0xc2,0x20,0x20,0xc8,0xd5};
  10. unsigned char weekunit[]={
  11.   0xd0,0xc7,0xc6,0xda};
  12. unsigned char wdunit[]={
  13.   0xa1,0xe6};
  14. unsigned char sdunit[]={
  15.   0xa3,0xa5};

  16. /* 接口定义
  17. CE(DS1302 pin5) -> Arduino D5
  18. IO(DS1302 pin6) -> Arduino D6
  19. SCLK(DS1302 pin7) -> Arduino D7
  20. */
  21. uint8_t CE_PIN   = 5;
  22. uint8_t IO_PIN   = 6;
  23. uint8_t SCLK_PIN = 7;




  24. dht11 DHT11;

  25. float wd=0;
  26. float sd=0;

  27. /* 串口数据缓存 */
  28. String comdata = "";
  29. int numdata[7] ={
  30.   0}
  31. , j = 0, mark = 0;
  32. /* 创建 DS1302 对象 */
  33. DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);

  34. void setup()
  35. {
  36.   Serial.begin(9600);
  37.   rtc.write_protect(false);
  38.   rtc.halt(false);
  39.   LCDA.Initialise(); // 屏幕初始化
  40.   delay(100);
  41.   LCDA.CLEAR();//清屏
  42.   delay(100);
  43.   LCDA.DisplayString(0,2,yearunit,AR_SIZE(yearunit));
  44.   delay(100);
  45.   LCDA.DisplayString(1,6,weekunit,AR_SIZE(weekunit));
  46.   delay(100);
  47.   LCDA.DisplayString(3,2,wdunit,AR_SIZE(wdunit));   
  48.   delay(100);
  49.   LCDA.DisplayString(3,6,sdunit,AR_SIZE(sdunit));
  50.   delay(3000);
  51. }

  52. void loop()
  53. {
  54.   update_time();
  55.   delay(100);
  56.   Time otime = rtc.time();
  57.   delay(100);
  58.   shownum(0,0,4,0,otime.yr);
  59.   delay(100);
  60. shownum(0,3,2,0,otime.mon);
  61.   delay(100);
  62. shownum(0,5,2,0,otime.date);   
  63.   delay(100);
  64. char timestr[9];  
  65.    snprintf(timestr, sizeof(timestr), "%02d:%02d:%02d",otime.hr, otime.min, otime.sec);
  66.    LCDA.DisplayString(1,0,(unsigned char *)timestr,sizeof(timestr));
  67.    delay(100);
  68.   wsd();
  69.   delay(100);   
  70.   shownum(3,0,4,1,wd);
  71.   delay(100);
  72.   shownum(3,4,4,1,sd);
  73.   delay(1000);
  74. }

  75. void shownum(int x,int y,int zlen,int xlen,long num){
  76.   char diststr[zlen];
  77.   /*  __val:输入数据,__width:总共的数据宽度,__prec:小数后面位数,__s:传入的指针,即缓冲区返回值:即__s   */
  78.   dtostrf(num,zlen,xlen,diststr);
  79.   LCDA.DisplayString(x,y,(unsigned char *)diststr,sizeof(diststr));
  80. }
  81. void wsd()
  82. {
  83.   int chk = DHT11.read(DHT11PIN);
  84.   wd=(float)DHT11.temperature-2;
  85.   sd=(float)DHT11.humidity;
  86. }
  87. void update_time()
  88. {
  89.   /* 当串口有数据的时候,将数据拼接到变量comdata */
  90.   while (Serial.available() > 0)
  91.   {
  92.     comdata += char(Serial.read());
  93.     delay(2);
  94.     mark = 1;
  95.   }
  96.   /* 以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组 */
  97.   if(mark == 1)
  98.   {
  99.     Serial.print("You inputed : ");
  100.     Serial.println(comdata);
  101.     for(int i = 0; i < comdata.length() ; i++)
  102.     {
  103.       if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
  104.       {
  105.         j++;
  106.       }
  107.       else
  108.       {
  109.         numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
  110.       }
  111.     }
  112.     /* 将转换好的numdata凑成时间格式,写入DS1302 */
  113.     Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
  114.     rtc.time(t);
  115.     mark = 0;
  116.     j=0;
  117.     /* 清空 comdata 变量,以便等待下一次输入 */
  118.     comdata = String("");
  119.     /* 清空 numdata */
  120.     for(int i = 0; i < 7 ; i++) numdata[i]=0;
  121.   }
  122. }
复制代码
代码尚未写完,现在出现情况,arduino总是自动复位,或者卡死,求教问题所在!

回复

使用道具 举报

发表于 2012-9-20 14:38:15 | 显示全部楼层
有库函数没?
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-3 19:11 , Processed in 0.038907 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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