绿林网页 发表于 2012-12-8 23:32:03

我的电子闹钟,由Arduino最小系统、LCD12864中文屏幕、DS1302时钟模块、DHT11温湿...

本帖最后由 弘毅 于 2012-12-18 23:44 编辑

最近利用手头的LCD12864中文屏幕,DS1302时钟模块,DHT11温湿度传感器,自制的Arduino最小系统,芯片为Atmega8 ,开发一个电子闹钟。先上图:
这是正面外观、屏幕显示主界面(左上角的圆圈代表闹铃开启,最下面显示当前温度和湿度)

不做操作10秒后,会自动进入睡眠模式,屏幕背光灯亮度减弱,屏幕随机位置显示时间和温湿度轮换,按任意键唤醒回到主界面。



侧视图:



后视图:





自己做的最小系统,芯片是 Atmega8,之前露过面的哦。





可惜只写了主界面和简化的睡眠模式,Atmega8芯片容量不够了,无法继续写更多功能,只好先来个简化的。



上个视频看看:

http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=92019545_1785094373_aR61SXE9XjWP+Eh0HTWxve0D+/cXuvDogGi0uluhIQtPE1XaapWbYtwO5S3TFqwbrz0xHcZkeP8wkkR5Zatf1jYqYA0QhVc/s.swf

随后,换开发用的 Arduino UNO板子,继续写功能模块







好了,终于都写完了,实现了日期、时间、温度、湿度显示,睡眠模式,日期时间设置,闹铃开启关闭,闹铃时间设置功能。

程序大小


上视频:

http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=92042444_1785094373_PUvgHHQxXTWP+Eh0HTWxve0D+/cXuvDogGi0v1CgIQpPE1XaapWbYtkF5C3SFqwbrz0xHcZkeP8wkkR5Zatf1jYqYA0QhVc/s.swf

缺陷:设计睡眠功能,主要是为了防止长期单点显示对屏幕造成伤害,另外减弱背光也是想省电,但省电结果不是很理想,还是比较费电的。

zzp1383 发表于 2014-9-28 16:48:00

// A1/15 确定按钮; A2/16 下移按钮; A3/17 上移按钮; A4/18 返回按钮;
这句代码中,是如何定义A1-A4的按键端口号的?我仿制了一个,能够正确显示图片,但是貌似干扰,图标、设置内容等乱跑。

幻生幻灭 发表于 2012-12-9 09:07:12

图片失效啦

咯小3虽 发表于 2012-12-9 10:40:54

{:soso_e154:}求图,我也做个学习下!

绿林网页 发表于 2012-12-9 17:31:12

幻生幻灭 发表于 2012-12-9 09:07 static/image/common/back.gif
图片失效啦

我又发了一遍图

绿林网页 发表于 2012-12-9 17:31:46

咯小3虽 发表于 2012-12-9 10:40 static/image/common/back.gif
求图,我也做个学习下!

我又发了一遍图

咯小3虽 发表于 2012-12-9 21:22:01

绿林网页 发表于 2012-12-9 17:31 static/image/common/back.gif
我又发了一遍图

:lol有图了。

绿林网页 发表于 2012-12-10 20:11:15

本帖最后由 绿林网页 于 2012-12-10 20:12 编辑

Atmega8最小系统的代码
#include <dht11.h>
#define DHT11PIN A0
dht11 DHT11;
#include <DS1302.h>
//接口定义
//CE(DS1302 pin5) -> Arduino D5
//IO(DS1302 pin6) -> Arduino D6
//SCLK(DS1302 pin7) -> Arduino D7
DS1302 rtc(5, 6, 7);
Time otime;
#include "LCD12864RSPI.h"
//latchPin = 12;
//clockPin = 10;
//dataPin = 11;
#define AR_SIZE( a ) sizeof( a ) / sizeof( a )

// A1/15 确定按钮; A2/16 下移按钮; A3/17 上移按钮; A4/18 返回按钮;
int clockPin=13; //闹铃
int bledPin=9; //背光灯

//⊙℃%:
unsigned char str2[]={
0x20,0x20,0xA1,0xD1,0xa1,0xe6,0xa3,0xa5,0xA3,0xBA};
//    周一周二周三周四周五周六周日设置日期时间闹铃开启关闭
unsigned char str4[]={
0x20,0x20,0x20,0x20,0xD6,0xDC,0xD2,0xBB,0xD6,0xDC,0xB6,0xFE,0xD6,0xDC,0xC8,0xFD,0xD6,0xDC,0xCB,0xC4,0xD6,0xDC,0xCE,0xE5,0xD6,0xDC,0xC1,0xF9,0xD6,0xDC,0xC8,0xD5,0xC9,0xE8,0xD6,0xC3,0xC8,0xD5,0xC6,0xDA,0xCA,0xB1,0xBC,0xE4,0xC4,0xD6,0xC1,0xE5,0xBF,0xAA,0xC6,0xF4,0xB9,0xD8,0xB1,0xD5};

int mode=1;//0 睡眠; 1 正常; 2 设置;
int sx=0; //0 主菜单 1 日期 2 时间 3 闹铃
int Isclock=1; //闹铃是否开启
int light=10; //背光亮度

void setup()
{
Serial.begin(9600);
rtc.write_protect(false);
rtc.halt(false);
LCDA.Initialise();
delay(100);
LCDA.CLEAR();
for(int i=15;i<19;i++) {
    pinMode(i,INPUT);
}
pinMode(bledPin,OUTPUT);
digitalWrite(bledPin,HIGH);
digitalWrite(clockPin,LOW);
// Time t(2012,12,8,13,53,0,6);
//rtc.time(t);
delay(1000);
}

void loop()
{
switch(mode)
{
case 0:
    s_ls();
    break;
case 2:
    break;
default :
    s_nr();
    break;   
}
}
void s_nr()
{
LCDA.CLEAR();
delay(10);
digitalWrite(bledPin,HIGH);
int js=0;
while(1)
{
    // c_clock();
    s_fdate();
    delay(300);
    js++;
    if(js>50)
    {
      mode=0;
      break;
    }
}
}
void s_ls()
{
analogWrite(bledPin,light);
int tr=0,tc=0;
while(1)
{
    //if(c_clock()>0){
    //   mode=1;
    //   break;
    // }
    if( c_btn()>0){
      mode=1;
      break;
    }
    LCDA.CLEAR();
    delay(10);
    tr= (int)random(4);
    tc=(int)random(5);
    s_time(tr,tc);
    delay(5000);
}
}
int c_btn()
{
int r=0;
for(int i=15;i<19;i++) {
    if(digitalRead(i)==HIGH){
      delay(10);
      if(digitalRead(i)==HIGH)
      {
      r=i-14;
      break;
      }
    }
}
return r;
}
//int c_clock()
//{
//return 0;
//}
void s_fdate()
{
LCDA.DisplayString(0,0,(unsigned char *)str2,sizeof(str2));
char tca1;
otime = rtc.time();   
snprintf(tca1, sizeof(tca1), "%04d-%02d-%02d%s",otime.yr, otime.mon,otime.date,str4);
LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
char tca2;
snprintf(tca2, sizeof(tca2), "%02d:%02d:%02d",otime.hr, otime.min,otime.sec);
LCDA.DisplayString(2,2,(unsigned char *)tca2,sizeof(tca2));
s_wsd(3,1);
}
void s_time(int r,int c)
{
char tca2;
otime = rtc.time();
snprintf(tca2, sizeof(tca2), "%02d:%02d",otime.hr, otime.min);
LCDA.DisplayString(r,c,(unsigned char *)tca2,sizeof(tca2));
}
void s_wsd(int r,int c)
{
char tws;
DHT11.read(DHT11PIN);
snprintf(tws, sizeof(tws), "%2d%.2s%2d%.2s",(DHT11.temperature-2),str2,DHT11.humidity,str2);
LCDA.DisplayString(r,c,(unsigned char *)tws,sizeof(tws));
}

绿林网页 发表于 2012-12-11 20:03:10

Arduino uno 的全部代码
#include <dht11.h>
#define DHT11PIN A0
dht11 DHT11;
#include <DS1302.h>
//接口定义
//CE(DS1302 pin5) -> Arduino D5
//IO(DS1302 pin6) -> Arduino D6
//SCLK(DS1302 pin7) -> Arduino D7
DS1302 rtc(5, 6, 7);
Time otime;
#include "LCD12864RSPI.h"
//latchPin = 12;
//clockPin = 10;
//dataPin = 11;
#define AR_SIZE( a ) sizeof( a ) / sizeof( a )

// A1/15 确定按钮; A2/16 下移按钮; A3/17 上移按钮; A4/18 返回按钮;
int clockledPin=13; //闹铃闪灯
int clockaudioPin=3;//闹铃声音
int bledPin=9; //背光灯

//⊙℃%:→▔年月日点分
unsigned char str2[]={
0x20,0x20,0xA1,0xD1,0xA1,0xE6,0xA3,0xA5,0xA3,0xBA,0xA1,0xFA,0xA9,0xA5,0xC4,0xEA,0xD4,0xC2,0xC8,0xD5,0xB5,0xE3,0xB7,0xD6};
//    周一周二周三周四周五周六周日设置日期时间闹铃
unsigned char str4[]={
0x20,0x20,0x20,0x20,0xD6,0xDC,0xD2,0xBB,0xD6,0xDC,0xB6,0xFE,0xD6,0xDC,0xC8,0xFD,0xD6,0xDC,0xCB,0xC4,0xD6,0xDC,0xCE,0xE5,0xD6,0xDC,0xC1,0xF9,0xD6,0xDC,0xC8,0xD5,0xC9,0xE8,0xD6,0xC3,0xC8,0xD5,0xC6,0xDA,0xCA,0xB1,0xBC,0xE4,0xC4,0xD6,0xC1,0xE5};
//关闭开启
unsigned char cstr4[]={
0xB9,0xD8,0xB1,0xD5,0xBF,0xAA,0xC6,0xF4};
int mode=1;//0 睡眠; 1 正常; 2 设置;
int sx=0; //0 主菜单 1 日期 2 时间 3 闹铃
int Isclock=1; //闹铃是否开启
int cl_hr=6; //闹铃小时
int cl_min=30; //闹铃分钟
int light=10; //背光亮度
int zr=0,zc=0;
void setup()
{
Serial.begin(9600);
rtc.write_protect(false);
rtc.halt(false);
LCDA.Initialise();
delay(100);
LCDA.CLEAR();
for(int i=15;i<19;i++) {
    pinMode(i,INPUT);
}
pinMode(bledPin,OUTPUT);
pinMode(clockaudioPin,OUTPUT);
pinMode(clockledPin,OUTPUT);
digitalWrite(bledPin,HIGH);
digitalWrite(clockaudioPin,LOW);
digitalWrite(clockledPin,LOW);
delay(1000);
}

void loop()
{
switch(mode)
{
case 0:
    s_ls();
    break;
case 2:
    s_set();
    break;
default :
    s_nr();
    break;   
}
}
void s_set()
{
switch(sx)
{
case 1:
    s_m1();
    break;
case 2:
    s_m2();
    break;
case 3:
    s_m3();
    break;
default :
    s_m0();
    break;   
}
}
void s_m0()
{
LCDA.CLEAR();
delay(10);
LCDA.DisplayString(0,0,(unsigned char *)(str4),sizeof(str4));
LCDA.DisplayString(1,3,(unsigned char *)(str4),sizeof(str4));
LCDA.DisplayString(2,3,(unsigned char *)(str4),sizeof(str4));
LCDA.DisplayString(3,3,(unsigned char *)(str4),sizeof(str4));
zr=1;
zc=2;
int btn=0;
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));
while(1)
{
    btn=c_btn();
    if(btn==1)    {
      sx=zr;
      break;
    }
    else if(btn== 2){
      m_down();
    }
    else if(btn==3){
      m_up();
    }
    else if(btn==4){
      mode=1;   
      break;
    }
}
}
void s_m1()
{
LCDA.CLEAR();
delay(10);
LCDA.DisplayString(0,0,(unsigned char *)(str4),sizeof(str4));
char tca1;
otime = rtc.time();   
int yr=otime.yr;
int mon=otime.mon;
int date=otime.date;
int day=otime.day;
snprintf(tca1, sizeof(tca1), "%04d%.2s%02d%.2s%02d%.4s",yr,str2, mon,str2,date,str4);
LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
zr=2;
zc=1;
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));
int btn=0;
while(1)
{
    btn=c_btn();
    if(btn==1){   
      otime = rtc.time();
      Time t(yr,mon,date,otime.hr,otime.min,otime.sec,day);
      rtc.time(t);
      m_right(2,1,7);
    }
    else if(btn== 2){
      switch(zc)
      {
      case 1:
      yr++;
      if(yr>2100)yr=2000;
      break;
      case 3:
      mon++;
      if(mon>12)mon=1;
      break;
      case 5:
      date++;
      if(date>31)date=1;
      break;
      case 7:
      day++;
      if(day>7)day=1;
      break;
      }
      snprintf(tca1, sizeof(tca1), "%04d%.2s%02d%.2s%02d%.4s",yr,str2, mon,str2,date,str4);
      LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
    }
    else if(btn==3){
      switch(zc)
      {
      case 1:
      yr--;
      if(yr<2000)yr=2100;
      break;
      case 3:
      mon--;
      if(mon<1)mon=12;
      break;
      case 5:
      date--;
      if(date<1)date=31;
      break;
      case 7:
      day--;
      if(day<1)day=7;
      break;
      }
      snprintf(tca1, sizeof(tca1), "%04d%.2s%02d%.2s%02d%.4s",yr,str2, mon,str2,date,str4);
      LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
    }
    else if(btn==4){
      sx=0;
      break;
    }
}
}
void s_m2()
{
LCDA.CLEAR();
delay(10);
LCDA.DisplayString(0,0,(unsigned char *)(str4),sizeof(str4));
otime = rtc.time();   
int hr=otime.hr;
int minu=otime.min;
char tca2;
snprintf(tca2, sizeof(tca2), "%02d%.2s%02d%.2s",otime.hr,str2, otime.min,str2);
LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
zr=2;
zc=2;
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));
int btn=0;
while(1)
{
    btn=c_btn();
    if(btn==1){   
      otime = rtc.time();
      Time t(otime.yr,otime.mon,otime.date,hr,minu,0,otime.day);
      rtc.time(t);
      m_right(2,2,4);
    }
    else if(btn== 2){
      switch(zc)
      {
      case 2:
      hr++;
      if(hr>23)hr=0;
      break;
      case 4:
      minu++;
      if(minu>59)minu=0;
      break;   
      }
      snprintf(tca2, sizeof(tca2), "%02d%.2s%02d%.2s",hr,str2, minu,str2);
      LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
    }
    else if(btn==3){
      switch(zc)
      {
      case 2:
      hr--;
      if(hr<0)hr=23;
      break;
      case 4:
      minu--;
      if(minu<0)minu=59;
      break;   
      }
      snprintf(tca2, sizeof(tca2), "%02d%.2s%02d%.2s",hr,str2, minu,str2);
      LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
    }
    else if(btn==4){
      sx=0;
      break;
    }
}
}
void s_m3()
{
LCDA.CLEAR();
delay(10);
LCDA.DisplayString(0,0,(unsigned char *)(str4),sizeof(str4));
char tca2;
snprintf(tca2, sizeof(tca2), "%.4s%02d%.2s%02d%.2s",cstr4,cl_hr,str2,cl_min,str2);
LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
zr=2;
zc=2;
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));
int btn=0;
while(1)
{
    btn=c_btn();
    if(btn==1){
      m_right(2,2,6);
    }
    else if(btn== 2){
      switch(zc)
      {
      case 2:
      Isclock++;
      if(Isclock>1)Isclock=0;
      break;
      case 4:
      cl_hr++;
      if(cl_hr>23)cl_hr=0;
      break;
      case 6:
      cl_min++;
      if(cl_min>59)cl_min=0;
      break;   
      }
      snprintf(tca2, sizeof(tca2), "%.4s%02d%.2s%02d%.2s",cstr4,cl_hr,str2,cl_min,str2);
      LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
    }
    else if(btn==3){
      switch(zc)
      {
      case 2:
      Isclock--;
      if(Isclock<0)Isclock=1;
      break;
      case 4:
      cl_hr--;
      if(cl_hr<0)cl_hr=23;
      break;
      case 6:
      cl_min--;
      if(cl_min<0)cl_min=59;
      break;   
      }
      snprintf(tca2, sizeof(tca2), "%.4s%02d%.2s%02d%.2s",cstr4,cl_hr,str2,cl_min,str2);
      LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
    }
    else if(btn==4){
      sx=0;
      break;
    }
}
}
void s_nr()
{
LCDA.CLEAR();
delay(10);
digitalWrite(bledPin,HIGH);
int js=0,btn=0;
while(1)
{
    c_clock();
    btn=c_btn();
    if( btn==1){
      mode=2;
      break;
    }
    s_fdate();
    delay(100);
    js++;
    if(js>50)
    {
      mode=0;
      break;
    }
}
}
void s_ls()
{
analogWrite(bledPin,light);
int tr=0,tc=0,js=0;
while(1)
{
    if(c_clock()>0){
      mode=1;
      break;
    }
    if( c_btn()>0){
      mode=1;
      break;
    }
    if(js==20)
    {
      LCDA.CLEAR();
      delay(10);
      tr= (int)random(4);
      tc=(int)random(5);
      s_time(tr,tc);
    }
    else if(js==50)
    {
      s_wsd(tr,tc);
      js=0;
    }
    delay(100);
    js++;
}
}
void m_up()
{
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));      
zr--;
if(zr<1){
    zr=3;      
}
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));
delay(10);
}
void m_down(){
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));      
zr++;
if(zr>3){
    zr=1;      
}
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));
delay(10);
}
void m_right(int s,int sn,int en){
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));      
zc+=s;
if(zc>en){
    zc=sn;      
}
LCDA.DisplayString(zr,zc,(unsigned char *)(str2),sizeof(str2));
delay(10);
}
int c_btn()
{
for(int i=15;i<19;i++) {   
    if(digitalRead(i)==HIGH){
      delay(100);
      if(digitalRead(i)==HIGH)
      {      
      return (i-14);
      }
    }
}
return 0;
}
int c_clock()
{
int r=0;
if(Isclock==1)
{
    if((cl_hr==otime.hr)&&(cl_min== otime.min))
    {
      r=1;
      digitalWrite(clockledPin,HIGH);
      tone(clockaudioPin,1209,100);
      tone(clockaudioPin,697,100);
   // tone(clockaudioPin,1336,100);
   // tone(clockaudioPin,697,100);
      delay(10);
    }
    else
    {
      digitalWrite(clockledPin,LOW);
      digitalWrite(clockaudioPin,LOW);
    }
}
return r;
}
void s_fdate()
{
LCDA.DisplayString(0,0,(unsigned char *)str2,sizeof(str2));
char tca1;
otime = rtc.time();   
snprintf(tca1, sizeof(tca1), "%04d-%02d-%02d%s",otime.yr, otime.mon,otime.date,str4);
LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
char tca2;
snprintf(tca2, sizeof(tca2), "%02d:%02d:%02d",otime.hr, otime.min,otime.sec);
LCDA.DisplayString(2,2,(unsigned char *)tca2,sizeof(tca2));
s_wsd(3,1);
}
void s_time(int r,int c)
{
char tca2;
otime = rtc.time();
snprintf(tca2, sizeof(tca2), "%02d:%02d",otime.hr, otime.min);
LCDA.DisplayString(r,c,(unsigned char *)tca2,sizeof(tca2));
}
void s_wsd(int r,int c)
{
char tws;
DHT11.read(DHT11PIN);
snprintf(tws, sizeof(tws), "%2d%.2s%2d%.2s",(DHT11.temperature-2),str2,DHT11.humidity,str2);
LCDA.DisplayString(r,c,(unsigned char *)tws,sizeof(tws));
}



求优化!

绿林网页 发表于 2012-12-13 19:58:57

图片怎么老是出错不显示呢?

弘毅 发表于 2012-12-18 23:33:05

本帖最后由 弘毅 于 2012-12-18 23:43 编辑

绿林网页 发表于 2012-12-13 19:58 static/image/common/back.gif
图片怎么老是出错不显示呢?

图片你是上传到图片空间,然后引用链接重新调整大小的吧,因为我看了一下后台服务器记录。。。显示是主动删除了。。。是不是因为图片你img代码引用后,显示了2次。。所以你删除了上传的那一部分。上传的删除的。。引用的也自然没了。

直接使用图片上传。。然后插入图片就好。目前上传的图片,因为论坛编辑器限制,没法调整大小=.=

{:soso_e113:} 我从你空间。。把图片重新转过来了,嘿嘿

绿林网页 发表于 2012-12-19 07:57:04

弘毅 发表于 2012-12-18 23:33 static/image/common/back.gif
图片你是上传到图片空间,然后引用链接重新调整大小的吧,因为我看了一下后台服务器记录。。。显示是主 ...

谢谢站长!

davidce 发表于 2013-4-18 08:35:06

霸气的闹钟,用oled 屏幕会比较省电

fangtaonj 发表于 2013-4-24 22:05:43

好东西!做记号备用!

FoieDEEEE_仲敬 发表于 2013-4-29 22:30:40

改用TFT做显示耗电更少而且更漂亮

Turnnyюя 发表于 2013-5-11 14:41:02

感谢楼主 学习中
页: [1] 2 3 4
查看完整版本: 我的电子闹钟,由Arduino最小系统、LCD12864中文屏幕、DS1302时钟模块、DHT11温湿...