绿林网页 发表于 2012-8-25 18:29:57

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

环境:
Arduino nao 板子 + ds1302 时钟模块 + 温湿度传感器 + LCD12864中文屏幕

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

代码如下:#include "LCD12864RSPI.h"
#define AR_SIZE( a ) sizeof( a ) / sizeof( a )
#include <stdio.h>
#include <string.h>
#include <DS1302.h>
#include <dht11.h>
#define DHT11PIN A0

unsigned char yearunit[]={
0xc4,0xea,0x20,0x20,0xd4,0xc2,0x20,0x20,0xc8,0xd5};
unsigned char weekunit[]={
0xd0,0xc7,0xc6,0xda};
unsigned char wdunit[]={
0xa1,0xe6};
unsigned char sdunit[]={
0xa3,0xa5};

/* 接口定义
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;




dht11 DHT11;

float wd=0;
float sd=0;

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

void setup()
{
Serial.begin(9600);
rtc.write_protect(false);
rtc.halt(false);
LCDA.Initialise(); // 屏幕初始化
delay(100);
LCDA.CLEAR();//清屏
delay(100);
LCDA.DisplayString(0,2,yearunit,AR_SIZE(yearunit));
delay(100);
LCDA.DisplayString(1,6,weekunit,AR_SIZE(weekunit));
delay(100);
LCDA.DisplayString(3,2,wdunit,AR_SIZE(wdunit));   
delay(100);
LCDA.DisplayString(3,6,sdunit,AR_SIZE(sdunit));
delay(3000);
}

void loop()
{
update_time();
delay(100);
Time otime = rtc.time();
delay(100);
shownum(0,0,4,0,otime.yr);
delay(100);
shownum(0,3,2,0,otime.mon);
delay(100);
shownum(0,5,2,0,otime.date);   
delay(100);
char timestr;
   snprintf(timestr, sizeof(timestr), "%02d:%02d:%02d",otime.hr, otime.min, otime.sec);
   LCDA.DisplayString(1,0,(unsigned char *)timestr,sizeof(timestr));
   delay(100);
wsd();
delay(100);   
shownum(3,0,4,1,wd);
delay(100);
shownum(3,4,4,1,sd);
delay(1000);
}

void shownum(int x,int y,int zlen,int xlen,long num){
char diststr;
/*__val:输入数据,__width:总共的数据宽度,__prec:小数后面位数,__s:传入的指针,即缓冲区返回值:即__s   */
dtostrf(num,zlen,xlen,diststr);
LCDA.DisplayString(x,y,(unsigned char *)diststr,sizeof(diststr));
}
void wsd()
{
int chk = DHT11.read(DHT11PIN);
wd=(float)DHT11.temperature-2;
sd=(float)DHT11.humidity;
}
void update_time()
{
/* 当串口有数据的时候,将数据拼接到变量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;
}
}
代码尚未写完,现在出现情况,arduino总是自动复位,或者卡死,求教问题所在!

zhangdeyue1 发表于 2012-9-20 14:38:15

有库函数没?
页: [1]
查看完整版本: 时钟模块+温湿度模块+12864,系统一直自动复位是怎么回事