环境:
Arduino nao 板子 + ds1302 时钟模块 + 温湿度传感器 + LCD12864中文屏幕
功能:显示年月日、星期、时间、温度、湿度
代码如下:- #include "LCD12864RSPI.h"
- #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
- #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[7] ={
- 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[9];
- 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[zlen];
- /* __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[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
- {
- j++;
- }
- else
- {
- numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
- }
- }
- /* 将转换好的numdata凑成时间格式,写入DS1302 */
- Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
- rtc.time(t);
- mark = 0;
- j=0;
- /* 清空 comdata 变量,以便等待下一次输入 */
- comdata = String("");
- /* 清空 numdata */
- for(int i = 0; i < 7 ; i++) numdata[i]=0;
- }
- }
复制代码 代码尚未写完,现在出现情况,arduino总是自动复位,或者卡死,求教问题所在!
|