中秋节做个带月饼的气象站多屏显示-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6258|回复: 0

中秋节做个带月饼的气象站多屏显示

[复制链接]
发表于 2017-10-4 16:44 | 显示全部楼层 |阅读模式
本帖最后由 topdog 于 2020-2-21 00:02 编辑

      今天是丁酉年中秋佳节,首先祝各位网友阖家欢乐,万事如意。     
     器材清单:mcookie电池盒,core,hub,AM2321,oled。
     用到的库文件是AM2321的 https://github.com/wangdong/AM2321 ,但是文件有错误需要修改一下。notepad++打开AM2321.CPP,把AM2321温湿度计I2C地址修改为 #define I2C_ADDR_AM2321 0x5C ,设备ID的地址修改为0x08,本例不用可以不修改,还有就是温湿度的计算,按下图修改。因为从传感器读回的温湿度值是十六进制的,高位左移八位就是乘以256,再低位同
    class AirConditionReader : public DataReader<I2C_ADDR_AM2321, PARAM_AM2321_READ, REG_AM2321_HUMIDITY_MSB,0x04>{
public:
    unsigned int humidity;
    int temperature;
public:
    bool read() {
        if(!readRaw())
            return false;
        humidity     = buf[2] << 8|buf[3];        
        temperature  = buf[4] << 8|buf[5];      
        return true;
    }

};
      
在使用AM2321库时,读取温湿度前要先 ac.read(); 才能读取数据。以湿度为例 u8g2.print(float(ac.humidity)/10,1); 湿度输出除十,保留一位小数。    下面简单介绍一下I2C总线AM2321的工作步骤:

   1,唤醒
        Wire.beginTransmission(I2CADDR); 起来干活了
        Wire.endTransmission();          稍息......
   2,写入读命令
        Wire.beginTransmission(I2CADDR); 打开am2321的i2c地址0x5c
        Wire.write(COMMAND);             写入AM2321的读取命令0x03
        Wire.write(REGADDR);             AM2321温湿度起始寄存器位置0x00 ,设备信息起始位置0x08
        Wire.write(REGCOUNT);            使用寄存器长度温湿度是4个,0x00到0x04
        Wire.endTransmission();          关闭i2c总线,再见。
   3, 等待数据总线响应有个80us的间歇时间
        delayMicroseconds(1600); //>1.5ms
   4,读传回来的数据
        Wire.requestFrom(I2CADDR, 2 + REGCOUNT + 2); 前面的2个字节分别是状态码,数据长度,后面的2个字节是crc16验证码
        int i = 0;
        for (; i < 2 + REGCOUNT; ++i)      
            buf = Wire.read();                       温度高位低位,湿度高位低位,这里是4个字节。buf[2]开始。

        unsigned short crc = 0;                     
        crc  = Wire.read();       CRC低位,
低位在前,读取crc16验证码,
        crc |= Wire.read() << 8;  CRC高位
左移与,形成16位的crc16验证码核对数据正确。

        if (crc == crc16(buf, i))
            return true;         
CRC验证正确输出
        return false;
      }
     5,crc16验证
     主机或传感器可用校验码进行判别接收信息是否正确。由于电子噪声或一些其它干扰,信息在传输过程中有时会发生错误,错误校验码(CRC)可以检验主机或传感器在通讯数据传送过程中的信息是否有误,错误的数据可以放弃(无论是发送还是接收),这样增加了系统的安全和效率。
     第二个用到的库是u8g2,这里强调的是字体样式的大小的查询,https://github.com/olikraus/u8g2/wiki/fntlistmono
     这是第二代oled使用的u8g库文件,@奈何 老师在《Aduino /Genuino 101开发入门》一书中详细介绍,大家可以参阅。
    程序如下:

//U8g2lib字体查询 https://github.com/olikraus/u8g2/wiki/fntlistmono
//AM2321库文件 https://github.com/wangdong/AM2321

#include <Arduino.h>
#include <U8g2lib.h>
#include <AM2321.h>
#include <Wire.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

AM2321 ac;

#define  Mid_Autumn_width 68
#define  Mid_Autumn_height 64
static const unsigned char Mid_Autumn_bits[] U8X8_PROGMEM = {
0x00,0x00,0x00,0xFF,0xF8,0x07,0x00,0x00,0x00,0x00,0x00,0xC0,0xFF,0xFF,0x0F,0x00,0x00,0x00,0x00,0x00,
0xF0,0x80,0x0F,0x78,0x00,0x00,0x00,0x00,0x00,0x3C,0x40,0x10,0xE0,0x01,0x00,0x00,0x00,0x00,0x03,0x20,
0x2F,0x00,0x06,0x00,0x00,0x00,0xC0,0xF1,0xD1,0x5A,0x7C,0x0C,0x00,0x00,0x00,0x60,0x32,0x91,0x48,0x64,
0x32,0x00,0x00,0x00,0xF0,0x11,0x9F,0xC8,0x47,0x7E,0x00,0x00,0x00,0x18,0x12,0xD1,0x5A,0x44,0xC2,0x00,
0x00,0x00,0x14,0xF2,0xA1,0x2F,0x7C,0x02,0x01,0x00,0x00,0x12,0x00,0x40,0x10,0x00,0x02,0x02,0x00,0x00,
0x91,0x0F,0x80,0x0F,0x80,0x6F,0x06,0x00,0x80,0x7F,0xFC,0x7F,0xF2,0xFF,0xF1,0x0F,0x00,0xC0,0x11,0x80,
0x01,0x00,0x00,0x40,0x1C,0x00,0x40,0x1D,0x80,0x01,0x00,0x00,0xC0,0x15,0x00,0x20,0x11,0x80,0x01,0x00,
0x00,0x40,0x24,0x00,0xD0,0x1F,0x80,0x01,0x00,0x00,0xC0,0x3F,0x00,0x50,0x00,0x80,0x01,0x02,0x00,0x00,
0x50,0x00,0x48,0xC0,0xFF,0xFF,0x07,0x00,0x00,0xD0,0x00,0x48,0x43,0x80,0x01,0x02,0x00,0x00,0xD6,0x00,
0x4C,0x46,0x80,0x01,0x02,0x00,0x00,0xD3,0x00,0xCC,0x44,0x80,0x01,0x02,0x00,0x00,0x99,0x01,0x4C,0x45,
0x80,0x01,0x02,0x00,0x00,0x95,0x03,0xCA,0x47,0x80,0x01,0x02,0x00,0x00,0x9F,0x02,0x0A,0x41,0x80,0x01,
0x02,0x00,0x00,0x84,0x02,0x06,0x41,0x80,0x01,0x02,0x00,0x00,0x04,0x03,0x06,0xC1,0xFF,0xFF,0x03,0x00,
0x00,0x04,0x03,0xF7,0x01,0x80,0x01,0x04,0x18,0x00,0x7E,0x03,0x95,0x04,0x80,0x01,0x0E,0x18,0x00,0x49,
0x07,0xCD,0x1A,0x80,0xC1,0x1F,0x18,0x80,0x5A,0x05,0xCD,0x17,0x80,0xF1,0x03,0x18,0x40,0x9E,0x05,0xA5,
0x14,0x80,0x01,0x03,0x18,0x40,0xAD,0x05,0xFD,0x1E,0x80,0x01,0x03,0x18,0xC0,0xEB,0x05,0xA5,0x14,0x80,
0x01,0x03,0x18,0x42,0xAD,0x05,0xCD,0x11,0x80,0x01,0x33,0x18,0x46,0x9E,0x05,0xD5,0x1B,0x80,0xB9,0x7F,
0x18,0x83,0x5E,0x05,0xA5,0x14,0x80,0x01,0x03,0x18,0x01,0x29,0x05,0x87,0x11,0x80,0x80,0x83,0x98,0x00,
0x2C,0x05,0x26,0x11,0x00,0x80,0xC3,0x58,0x00,0x44,0x07,0x12,0x11,0x00,0x40,0xED,0x38,0x00,0x84,0x02,
0x0A,0x11,0x00,0x40,0x1B,0x3C,0x00,0x84,0x02,0xCA,0x17,0x00,0x20,0x13,0x2C,0x00,0x9F,0x01,0x4C,0x14,
0x00,0x20,0x13,0x64,0x00,0x91,0x01,0x4C,0x14,0x00,0x10,0x03,0x44,0x00,0x91,0x01,0xF8,0x17,0x00,0x00,
0x03,0x46,0x00,0xFF,0x00,0x48,0x10,0x00,0x08,0x03,0xC2,0x00,0x90,0x00,0x48,0x18,0x00,0x00,0x03,0x83,
0xC1,0x50,0x00,0x50,0x10,0x00,0x00,0x83,0x81,0x43,0x50,0x00,0xF0,0x1F,0x00,0x00,0xC3,0x00,0xC7,0x3F,
0x00,0x20,0x11,0x00,0x00,0x63,0x00,0x4E,0x24,0x00,0x40,0x19,0x00,0x00,0x13,0x00,0xDC,0x14,0x00,0xC0,
0x11,0x00,0x00,0x01,0x00,0x00,0x0C,0x00,0x80,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F,0x00,0x00,0x91,0xFF,
0x81,0x0F,0xFC,0x7F,0x06,0x00,0x00,0x12,0x02,0x40,0x10,0x00,0x02,0x02,0x00,0x00,0x14,0xFE,0xA1,0x2B,
0xFC,0x03,0x01,0x00,0x00,0x18,0x12,0xD1,0x5A,0x44,0xC2,0x00,0x00,0x00,0xF0,0x13,0xFF,0xFF,0x47,0x7E,
0x00,0x00,0x00,0x60,0xFE,0x91,0x48,0xFC,0x33,0x00,0x00,0x00,0x80,0x11,0xD0,0x52,0x60,0x0C,0x00,0x00,
0x00,0x00,0x03,0x20,0x2F,0x00,0x06,0x00,0x00,0x00,0x00,0xFC,0x43,0x10,0xFE,0x01,0x00,0x00,0x00,0x00,
0x70,0x84,0x0F,0x79,0x00,0x00,0x00,0x00,0x00,0x80,0x1F,0xC0,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,
0xFF,0x03,0x00,0x00,0x00 };

#define humid_width 35
#define humid_height 48
static const unsigned char humid_bits[] U8X8_PROGMEM = {
   0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x80,
   0x0f, 0x00, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00, 0xe0, 0x1d, 0x00,
   0x00, 0x00, 0xe0, 0x38, 0x00, 0x00, 0x00, 0xf0, 0x78, 0x00, 0x00, 0x00,
   0x78, 0xf0, 0x00, 0x00, 0x00, 0x38, 0xe0, 0x00, 0x00, 0x00, 0x1c, 0xe0,
   0x01, 0x00, 0x00, 0x1e, 0xc0, 0x03, 0x00, 0x00, 0x0f, 0x80, 0x03, 0x00,
   0x00, 0x07, 0x00, 0x07, 0x00, 0x80, 0x07, 0x00, 0x0f, 0x00, 0xc0, 0x03,
   0x00, 0x0e, 0x00, 0xc0, 0x01, 0x00, 0x1c, 0x00, 0xe0, 0x01, 0x00, 0x3c,
   0x00, 0xe0, 0x00, 0x00, 0x38, 0x00, 0x70, 0x00, 0x00, 0x78, 0x00, 0x78,
   0x00, 0x00, 0x70, 0x00, 0x38, 0x00, 0x00, 0xe0, 0x00, 0x38, 0x00, 0x00,
   0xe0, 0x00, 0x1c, 0x00, 0x00, 0xc0, 0x01, 0x1c, 0x00, 0x00, 0xc0, 0x01,
   0x0e, 0x00, 0x00, 0xc0, 0x03, 0x0e, 0x00, 0x00, 0x80, 0x03, 0x0e, 0x00,
   0x00, 0x80, 0x03, 0x07, 0x00, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00, 0x00,
   0x07, 0x07, 0x00, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00, 0x00, 0x07, 0x07,
   0x00, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00,
   0x00, 0x07, 0x07, 0x00, 0x00, 0x00, 0x07, 0x0e, 0x00, 0x00, 0x80, 0x03,
   0x0e, 0x00, 0x00, 0x80, 0x03, 0x1e, 0x00, 0x00, 0xc0, 0x03, 0x1c, 0x00,
   0x00, 0xc0, 0x01, 0x3c, 0x00, 0x00, 0xe0, 0x01, 0x78, 0x00, 0x00, 0xf0,
   0x00, 0xf0, 0x00, 0x00, 0x78, 0x00, 0xe0, 0x01, 0x00, 0x3c, 0x00, 0xc0,
   0x07, 0x00, 0x3f, 0x00, 0x80, 0x1f, 0xc0, 0x0f, 0x00, 0x00, 0xff, 0xff,
   0x07, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00 };


#define temperature_width 18
#define temperature_height 47
static const unsigned char temperature_bits[] U8X8_PROGMEM = {
   0xc0, 0x0f, 0x00, 0xe0, 0x1f, 0x00, 0x70, 0x38, 0x00, 0x30, 0x30, 0x00,
   0x30, 0x30, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x00,
   0x30, 0x30, 0x00, 0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00, 0x30, 0x30, 0x00,
   0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00, 0x30, 0x30, 0x00, 0x30, 0x30, 0x00,
   0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00, 0x30, 0x30, 0x00, 0xb0, 0x37, 0x00,
   0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00, 0x30, 0x30, 0x00, 0xb0, 0x37, 0x00,
   0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00,
   0xb0, 0x37, 0x00, 0xb0, 0x37, 0x00, 0x98, 0x67, 0x00, 0x8c, 0xc7, 0x00,
   0xc6, 0x8f, 0x01, 0xe2, 0x1f, 0x01, 0xf3, 0x3f, 0x03, 0xf3, 0x3f, 0x03,
   0xf3, 0x3f, 0x03, 0xf3, 0x3f, 0x03, 0xf3, 0x3f, 0x03, 0xf3, 0x3f, 0x03,
   0xf3, 0x3f, 0x03, 0xe6, 0x9f, 0x01, 0xc6, 0x8f, 0x01, 0x0c, 0xc0, 0x00,
   0x38, 0x70, 0x00, 0xf0, 0x3f, 0x00, 0xc0, 0x0f, 0x00 };

void drawMidAutumn(void) {   
  u8g2.drawXBMP( 30,0,  Mid_Autumn_width, Mid_Autumn_height,  Mid_Autumn_bits);
}

void drawTemp(void) {  
  u8g2.setFont(u8g2_font_amstrad_cpc_extended_8u );
  u8g2.setCursor(0, 10);
  u8g2.print("TEMPERATURE  1/2");

  u8g2.setFont(u8g2_font_profont29_mn);

  u8g2.setCursor(0, 50);
  ac.read();   
  u8g2.print(float(ac.temperature)/10,1);
  u8g2.setFont(u8g2_font_pxplusibmvga8_m_all);
  u8g2.drawStr(70, 50,"C");

  // 温度标志
  u8g2.drawCircle(67, 40, 2);
  
  // 温度计图形
  u8g2.drawXBMP(100, 16, temperature_width, temperature_height, temperature_bits);
  
}

void drawHumid(void) {   
  u8g2.setFont(u8g2_font_amstrad_cpc_extended_8u );
  u8g2.setCursor(0,10);
  u8g2.print("HUMIDITY     2/2");

  u8g2.setFont(u8g2_font_profont29_mn);
  u8g2.setCursor(0, 50);
  ac.read();   
  u8g2.print(float(ac.humidity)/10,1);
   u8g2.setFont(u8g2_font_pxplusibmvga8_m_all);
  u8g2.drawStr(70, 50,"%");

  //湿度水滴
  u8g2.drawXBMP( 92, 16, humid_width, humid_height, humid_bits);
}

uint8_t draw_state = 0;
void draw() {
  u8g2.clearBuffer();  
  switch(draw_state ) {
    case 0: drawMidAutumn(); break;   
    case 1: drawTemp(); break;
    case 2: drawHumid(); break;
  }
   u8g2.sendBuffer();
}

void setup(void) {  
  Serial.begin(9600);
  u8g2.begin();
}

void loop(void) {
  //三画面循环
  delay(200);   
  u8g2.firstPage();
  do {
    draw();
  } while( u8g2.nextPage() );
   // 三画面切换
  draw_state++;  
  if ( draw_state > 3  )
    draw_state = 0;
  // 经验值根据爱好设定每屏滞留时间
  delay(2000);
}



  效果图:
3.JPG

4.JPG

5.JPG

AM2321美科科技称Tem_Hum   库文件: https://git.microduino.cn/hardwa ... 07_m_Sensor_Tem_Hum 我还写了一版作为附件。











mcookie_u8g2lib_Tem_hum.rar

2.27 KB, 下载次数: 14

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 02:51 , Processed in 0.079405 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表