基于Arduino的LCD12864显示温湿度课程设计

                           
2020年04月20日

前言

这是同学在大三下学期的时候做的一个机械电子课程设计,现在分享给有需要的小伙伴,相关资料仅供参考。

Arduino 和 51 单片机 编程方法什么的都差不多,如果你也想学习 51 单片机,请参考我的另外一篇文章,希望这些内容能给你带来一些帮助。

51单片机学习资料及学习方法分享

以下是温湿度显示的资料、实际运行图片、电路图、程序。

【基于Arduino的LCD12864显示温湿度课程设计】

材料:Arduino R3、旋转编码器(可按下)、HT11温湿度传感器、LCD12864

功能:上下移动光标,选择某一选项后显示相应内容。

开始两秒显示欢迎界面,然后给出四个选项。

箭头左边表示选项内容,箭头右边表示按下后显示内容。

1.温度和湿度检测 → 湿度: 15 温度: 35 (实时)
2.秒表显示页面 → 秒表: 015 (实时)
3.进入郭亮主页 → 电话:1234567 Q Q:1234567
4.陕西科技大学 → 地址: 陕西西安

所有文件下载:基于Arduino的LCD12864显示温湿度课程设计.zip

文件中包含:实际运行图片、电路图、程序

一、实际运行图片

二、电路图

三、程序

/***********************************************
1. SPI Interface Inatruction//引脚含义!!
      clockPin --> SCK(EN)
      latchPin --> CS(RS)
      dataPin --> SID(RW)
 2. Connection:           //无用
    1)Turn the BL_ON Switch to the "ON" side;
    2)Turn the PBS_ON Switch to the "SPI" side

Method1:                  //与单片机引脚连接
      LCD                   Arduino
      EN                 Digital Pin 2
      RS                 Digital Pin 7
      RW                 Digital Pin 10
      VCC                     5V
      GND                     GND;

Method2:                  //无用
      LCD                          Arduino
      SCK       clockPin(defined in the "initDriverPin" function)
      CS        latchPin(defined in the "initDriverPin" function)
      SID       dataPin (defined in the "initDriverPin" function)
      VCC                            5V
      GND                           GND
***********************************************/

#include "LCD12864RSPI.h"  //12864SPI头文件
#include <MsTimer2.h>      //定时器库的头文件
#include <dht11.h>         //传感器头文件
dht11 DHT11;               //定义相等
#define DHT11PIN 8         //传感器out引脚定义
/*------------------------------------------------------------------------------
; DFrobot bitmap - size 128x64 pixels, black/white image
------------------------------------------------------------------------------*/ 
void scankey();                       //定义编码器按键动作扫描执行、时间传感显示刷新程序
void display();                       //分页显示(二级目录显示)
void location();                      //光标位置显示更正
void timerefresh();                   //时间显示刷新
void shouyerefresh();                 //首页显示刷新
void sensorrefresh();                 //传感器显示刷新
unsigned char logo[]={                //图片取模软件生成欢迎界面
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xE0,0x1F,
      0xF0,0x3F,0xFE,0x03,0xFF,0x07,0xFF,0xF0,
      0x04,0x00,0x0F,0x00,0x0F,0xFF,0xF0,0x7F,
      0xFC,0x3F,0xFF,0x0F,0xFF,0x87,0xFF,0xF0,
      0x04,0x00,0x07,0x00,0x0F,0xF0,0x38,0xFE,
      0x0E,0x3E,0x07,0x9F,0xC1,0xC0,0x7E,0x00,
      0x04,0x00,0x07,0x00,0x0F,0xF0,0x19,0xFC,
      0x07,0x3E,0x03,0x9F,0x80,0xE0,0x7E,0x00,
      0x04,0x00,0x07,0x1F,0xFF,0xF0,0x39,0xF8,
      0x03,0x3E,0x03,0xBF,0x00,0x60,0x7E,0x00,
      0x04,0x7F,0xC7,0x0F,0xFF,0xFF,0xF1,0xF8,
      0x03,0x3F,0xFF,0x3F,0x00,0x30,0x7E,0x00,
      0x04,0x7F,0xC7,0x00,0x0F,0xFF,0xF1,0xF8,
      0x01,0x3F,0xFF,0x3F,0x00,0x30,0x7E,0x00,
      0x04,0x7F,0xC7,0x00,0x0F,0xF0,0x39,0xF8,
      0x03,0x3E,0x07,0xBF,0x00,0x30,0x7E,0x00,
      0x04,0x7F,0xC7,0x00,0x0F,0xF0,0x19,0xF8,
      0x03,0x3E,0x03,0xBF,0x00,0x30,0x7E,0x00,
      0x04,0x00,0x07,0x1F,0xFF,0xF0,0x19,0xF8,
      0x07,0x3E,0x03,0x9F,0x80,0x60,0x7E,0x00,
      0x04,0x00,0x07,0x1F,0xFF,0xF0,0x18,0xFC,
      0x0E,0x3E,0x07,0x9F,0xC0,0xE0,0x7E,0x00,
      0x04,0x00,0x07,0x1F,0xFB,0xF0,0x18,0xFF,
      0xBC,0x3F,0xFF,0x0F,0xF3,0xC0,0x3E,0x00,
      0x04,0x00,0x0F,0x1F,0xF8,0xF0,0x18,0x3F,
      0xF0,0x0F,0xFC,0x03,0xFF,0x00,0x1E,0x00,
      0x07,0xFF,0xFF,0xFF,0xF8,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x07,0xC1,0xC0,0x00,0x07,0xC0,
      0x00,0x3F,0xC0,0x00,0x00,0x00,0x00,0x00,
      0x01,0xFE,0x1F,0xC7,0xE0,0x3E,0x1F,0xE0,
      0x7E,0x3F,0xC0,0x1F,0x07,0xE0,0x71,0xC0,
      0x03,0xFF,0x3C,0x0F,0xF0,0xFF,0x3D,0xE1,
      0xFE,0x3F,0x00,0x7E,0x1F,0xE0,0x71,0xC0,
      0x03,0x8F,0xB8,0x0E,0x71,0xE7,0x38,0xC3,
      0xC7,0x0E,0x00,0xF0,0x3C,0x70,0xF1,0xC0,
      0x03,0x0F,0xB8,0x1C,0x63,0xC3,0xB1,0xC7,
      0x83,0x06,0x00,0xE0,0x78,0x30,0xFB,0xC0,
      0x03,0x07,0xB8,0x1C,0xE3,0x83,0xB3,0x87,
      0x07,0x06,0x01,0xC0,0x70,0x70,0xFF,0xE0,
      0x03,0x0F,0x3F,0x1F,0xC7,0x07,0x3F,0xCE,
      0x07,0x06,0x01,0xC0,0xE0,0x71,0xFF,0xE0,
      0x03,0x1F,0x3F,0x1F,0x87,0x07,0x3F,0xEE,
      0x0E,0x06,0x01,0xC0,0xE0,0xE1,0xDE,0x60,
      0x03,0x3E,0x18,0x1F,0xC7,0x0F,0x38,0xEE,
      0x0E,0x06,0x01,0xC0,0xE0,0xE1,0x9E,0x60,
      0x03,0xF8,0x18,0x1F,0xF7,0x9E,0x31,0xCF,
      0x1C,0x06,0x00,0xFC,0xF1,0xC3,0x9C,0x70,
      0x03,0xF0,0x18,0x1C,0x73,0xFC,0x3B,0xC7,
      0xF8,0x06,0x1C,0xFC,0x7F,0x83,0x9C,0x70,
      0x03,0xC0,0x18,0x1C,0x03,0xF0,0x3F,0x07,
      0xF0,0x04,0x1C,0x3C,0x7F,0x03,0x00,0x60,
      0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};


int i = 0;              //定义页码数值
int Left = 12;          //左转引脚定义
int Right = 13;         //右转引脚定义
int Key = 11;           //按键引脚定义
unsigned char j = 0;    //编码器动作标志位
unsigned char k = 0;    //按键动作标志位
unsigned int zzz = 0;   //进入中断(每20ms)次数
unsigned int m = 0;     //时间变量(每1s加1)
unsigned int n = 0;     //按键动作时光标不显示1次
unsigned int o = 0;     //启动时光标更正1次

//首页目录显示内容
unsigned char shouye1[]={49,46,0xCE,0xC2,0xB6,0xC8,0xBA,0xCD,0xCA,0xAA,0xB6,0xC8,0xBC,0xEC,0xB2,0xE2};  //显示:[1.温度和湿度检测]
unsigned char shouye2[]={50,46,0xC3,0xEB,0xB1,0xED,0xCF,0xD4,0xCA,0xBE,0xD2,0xB3,0xC3,0xE6};            //显示:[2.秒表显示页面]
unsigned char shouye3[]={51,46,0xBD,0xF8,0xC8,0xEB,0xB9,0xF9,0xC1,0xC1,0xD6,0xF7,0xD2,0xB3};            //显示:[3.进入郭亮主页]
unsigned char shouye4[]={52,46,0xC9,0xC2,0xCE,0xF7,0xBF,0xC6,0xBC,0xBC,0xB4,0xF3,0xD1,0xA7};            //显示:[4.陕西科技大学]

//目录一进入后的内容
unsigned char sensor11[] = {0xCA,0xAA,0xB6,0xC8,58,32,48,48,37};            //[湿度: 湿度值]高两位保存湿度值
unsigned char sensor12[] = {0xCE,0xC2,0xB6,0xC8,58,32,48,48,0xA1,0xE6};     //[温度: 温度值]高两位保存温度值

//目录二进入后的内容
unsigned char sec21[] = {0xC3,0xEB,0xB1,0xED,58,32,48,48,48};              //[秒表: 读秒]48是ASCII码0的值,用于保存时间变量(个十百三位)

//目录三进入后的内容
unsigned char jinru31[]={0xB5,0xE7,0xBB,0xB0,58,49,50,51,52,53,54,55};     //显示[电话:1234567]
unsigned char jinru32[]={81,32,32,81,58,49,50,51,52,53,54,55};             //显示[Q Q:1234567]

//目录四进入后的内容
unsigned char jinru41[]={0xB5,0xD8,0xD6,0xB7,58,32,0xC9,0xC2,0xCE,0xF7,0xCE,0xF7,0xB0,0xB2};    //显示[地址: 陕西西安]

void setup()
{
  pinMode(Left, INPUT);           //Left(左转)定义输入
  pinMode(Right, INPUT);          //Right(右转)定义输入
  pinMode(Key, INPUT);            //Key(按键)定义输入
  digitalWrite(Left,HIGH);        //设置高电平
  digitalWrite(Right,HIGH);       //设置高电平
  digitalWrite(Key,HIGH);         //设置高电平
  
  LCDA.initDriverPin(2,7,10);     //定义LCD引脚
  LCDA.Initialise();              //LCD初始化
  delay(100);                     //等待100ms
  LCDA.DrawFullScreen(logo);      //显示logo图形
  delay(2000); 
  Serial.begin(9600);             //通过串口发送数据波特率9600
  MsTimer2::set(20, scankey);     //设置中断,每20ms进入一次中断服务程序
}

void loop()
{  
  LCDA.CLEAR();                   //清屏
  delay(100);                     //延时
  shouyerefresh();
   while(1)
   {
      delay(1000);
      MsTimer2::stop();                    //停止计时
      int chk = DHT11.read(DHT11PIN);      //从传感器读取一次数值 
      MsTimer2::start();                   //开始计时
      
      Serial.print("Humidity (%): ");
      Serial.println((float)DHT11.humidity, 2);
     
      Serial.print("Temperature (oC): ");
      Serial.println((float)DHT11.temperature, 2);
     
      sensor11[7] = 48+(char)DHT11.humidity%10;         //湿度值个位
      sensor11[6] = 48+((char)DHT11.humidity/10)%10;    //湿度值十位
      sensor12[7] = 48+(char)DHT11.temperature%10;      //温度值个位
      sensor12[6] = 48+((char)DHT11.temperature/10)%10; //温度值十位
      
      if(o==0)
      {
       location();
       o = 1;
      }  
   }
}
   
void scankey()                     //编码器动作扫描
{
      zzz++;
      if(k==1)
      {
        
        if(n == 1)
        {
        display();
        LCDA.WriteCommand(0x0C);   //光标不显示
        n = 0;
        } 
      }
     if(j == 1)
     {
        j = 0;
        shouyerefresh();
        location();
     }  
     if(zzz == 50)
     {
        sec21[8] = 48+m%10;         //时间的个位
        sec21[7] = 48+(m/10)%10;    //时间的十位
        sec21[6] = 48+(m/100)%10;   //时间的百位
        zzz = 0;
        m++;
        if(k==1&&j==0&&i==0)
        {
          sensorrefresh();
        }
  
        if(k==1&&j==0&&i==1)
        {
          timerefresh();
         }
        if(k==0)
        {
          location();
        }
    }
    if(digitalRead(Left) == 0)            //如果编码器有动作
    {
    delay(3);                             //延时机械消抖(2ms)
    if(digitalRead(Left) == 0)            //如果消抖后确认有动作
    {
      j = 1;
      if(digitalRead(Right) == 1)         //如果为右转
      {
        if(k == 1)
        {
          k = 0;
        }
        else 
        {
          i++;                            //下一页
          if(i == 4)      
          {
            i = 0;
          }
        }
      }
      else                                //如果为左转
      {
        if(k == 1)
        {
            k = 0;
        }
        else 
        {
          i--;                            //上一页
          if(i < 0)
          {
            i = 3;
          }
        }
      }  
    }

    while(digitalRead(Left) == 0);        //左转到位退出中断
   }

  if(digitalRead(Key) == 0)               //如果按键有动作
  {
    delay(20);
    if(digitalRead(Key) ==0)              //延时机械消抖
    {
      k = 1;
      j = 0;
      n = 1; 
      LCDA.WriteCommand(0x0C);  
    }
    while(digitalRead(Key) == 0);          //当按键放开
  }
  Serial.println(i);                       //发送给上位机,检视数据
}

void display()
{
  LCDA.CLEAR();          
  delay(100);
  if(i == 0)
  {
      LCDA.DisplayString(1,0,sensor11,10);        //从第一行第零个字开始显示16位字符
      delay(10);                                  //延时
      LCDA.DisplayString(2,0,sensor12,10);        //从第二行第零个字开始显示16位字符
      delay(10);     
  }
  if(i == 1)
  {
       LCDA.DisplayString(1,0,sec21,9);          //从第一行第零个字开始显示16位字符
       delay(10);                                //延时
        
  }
  if(i == 2)
  {
       LCDA.DisplayString(1,0,jinru31,12);        //从第零行第零个字开始显示16位字符
       delay(10);                                 //延时
       LCDA.DisplayString(2,0,jinru32,12);
       delay(10);
        
  }
  if(i == 3)
  {
       LCDA.DisplayString(0,0,jinru41,14);        //从第零行第零个字开始显示16位字符
       delay(10);                                 //延时   
  }
}

void location()
{ 
         if(i == 0)                  
        {
          LCDA.WriteCommand(0x80);            //写入第零行第零个位置  
          LCDA.WriteCommand(0x0F);            //开光标
        }
        else if(i == 1)
        {
          LCDA.WriteCommand(0x90);     
          LCDA.WriteCommand(0x0F);
        }
        else if(i == 2)
        {
          LCDA.WriteCommand(0x88);
          LCDA.WriteCommand(0x0F);
        }
        else if(i == 3)
        {
          LCDA.WriteCommand(0x98);
          LCDA.WriteCommand(0x0F);
        }
}

void shouyerefresh()
{
  LCDA.CLEAR();          
  delay(100);
  LCDA.DisplayString(0,0,shouye1,16);        //从第零行第零个字开始显示16位字符
  delay(10);                                 //延时
  LCDA.DisplayString(1,0,shouye2,14);
  delay(10);
  LCDA.DisplayString(2,0,shouye3,14);
  delay(10);
  LCDA.DisplayString(3,0,shouye4,14);
  delay(10);
}

void sensorrefresh()
{
    LCDA.DisplayString(1,0,sensor11,10);
    delay(10);
    LCDA.DisplayString(2,0,sensor12,10);
    delay(10);
}

void timerefresh()
{
  LCDA.DisplayString(1,0,sec21,9);
  delay(10);
}
  

冷场莹