;我是新手,不好意思请教大侠们
我有一个温度芯片+1602的LCD + 1302时钟芯片
做个一个时间和温度显示的实验,
实验结果是程序能运行只是有一些问题
为什么我的程序在加了1302后 4秒才能读到一次时间?而且整的程序都被拖慢了?
程序代码:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//
#include <stdio.h>
#include <string.h>
#include <DS1302.h>
#define uchar unsigned char
#define uint unsigned int
/* Set the appropriate digital I/O pin connections */
uint8_t CE_PIN = 5;
uint8_t IO_PIN = 6;
uint8_t SCLK_PIN = 7;
char buf[30];
char buf1[30];
char day[10];
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2
int timeH;
int timeM;
int timeS;
int T[1];
int T[0];
int T[2];
float temp1;
int T[20];
float D[20];
void LCD();
void TEMP();
void LED();
//-----------------初始化设置----------------------
void setup()
{
lcd.init(); // 初始化LCD // Print a message to the LCD.
lcd.backlight(); //背光控制
pinMode( 13 , OUTPUT);
pinMode( 2 , OUTPUT);
pinMode( 1 , INPUT);
T[1] = 0;
T[0] = 0;
D[4]=100;
//Serial.begin(9600);//
/* 初始化一个新的芯片通过关闭写保护和清理
时钟停止标志。这些方法并不需要一直被称为。看到DS1302
数据表的细节。 */
// rtc.write_protect(false);
// rtc.halt(false);
/*设置一个新的时间对象设置日期和时间 */
/* Tuesday, May 19, 2009 at 21:16:37. */
// ime t(2013,9,28,22,48,37,1);
/* 写入时间和日期到芯片 */
// rtc.time(t);
delay(1000);
}
//---------------------主程序-------------------------
void loop()
{
//----------------------------------------温度计算
T[0]++;
if (T[0]>500)
{
TEMP();
}
//----------------------------------------LCD显示
T[1]++;
if (T[1]>1000)
{
LCD();
}
//----------------------------------------LED显示
T[2]++;
if (T[2]>50)
{
LED();
}
//----------------------------------------系统扫描显示0.5秒闪烁
if (T[1] == 500) {digitalWrite( 13 , HIGH );}
if (T[1] == 1000){digitalWrite( 13 , LOW ); }
//---------------------------------------时钟读取
D[6]++;
if (D[6]>50)
{
print_time();
D[6]=0;
}
delay(1);
}
//--------------------------LCDxianshi
void LCD()
{
D[7]++;
if (D[7]<=5)
{
lcd.init(); // initialize the lcd // Print a message to the LCD.
lcd.backlight();
lcd.print(buf);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(buf1);
// lcd.print(day);
// lcd.print(" ");
// lcd.print(t.yr);
// lcd.print(" ");
// lcd.print(t.mon);
// lcd.print(" ");
// lcd.print(t.date);
// lcd.setCursor(0,1);
// lcd.print(t.hr);
// lcd.print(" ");
// lcd.print(t.min);
// lcd.print(" ");
// lcd.print(t.sec);
}
else
{
lcd.init(); // initialize the lcd // Print a message to the LCD.
lcd.backlight();
lcd.print("Max");
lcd.print(D[3],1);
lcd.print(",Min");
lcd.print(D[4],1);
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.print(temp1,1);
lcd.print(" C");
}
if (D[7]>10)
{
D[7] = 0;
}
T[1] = 0;
}
void TEMP()
{
float A;
float V1 = analogRead(A0);
D[0] = D[0] + 1 ;
D[1] = D[1] + V1;
V1 = D[1] / D[0];
if (D[0]>50)
{
D[0] = D[0] - 1;
D[1] = D[1] - V1;
}
temp1 = V1*500/1023;
if (D[3]<temp1)
{
D[3]=temp1;
}
if (D[4]>temp1)
{
D[4] = temp1;
}
T[0] = 0;
}
void LED()
{
if (digitalRead( 1)){digitalWrite( 2 , HIGH );}
else {digitalWrite( 2 , LOW );}
T[2] = 0;
}
//-------------------时间子程序--------------------
void print_time()
{
/* 从芯片读取当前的时间和日期 */
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, "Sun");
break;
case 2:
strcpy(day, "Mon");
break;
case 3:
strcpy(day, "Tues");
break;
case 4:
strcpy(day, "Wednes");
break;
case 5:
strcpy(day, "Thurs");
break;
case 6:
strcpy(day, "Fri");
break;
case 7:
strcpy(day, "Satur");
break;
}
/* Format the time and date and insert into the temporary buffer */
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d ",day,t.yr, t.mon, t.date);
snprintf(buf1, sizeof(buf1), "%02d:%02d:%02d",t.hr, t.min, t.sec);
/* Print the formatted string to serial so we can see the time */
// Serial.println(buf);
}
|