6921浏览
查看: 6921|回复: 2

[讨论] 攀藤G1 LCD12864显示(含中美标准)

[复制链接]
本人新手,借鉴网上诸位大神的代码(不胜感谢),修修补补改改。终于拼凑出一套代码,可以在LCD 12864上显示PM1 PM2.5 PM10中美两种参数了(左为国标,右为美标)。给有需要的玩家参考,也请高手不吝指教!

下一步计划加入时钟,温湿度等传感器,实现翻页显示![mw_shl_code=applescript,true]/**
* PM2.5&温湿度显示
* 激光PM2.5传感器:攀藤 G1 PMS1003
* Arduino MEGA 2560
* LCD 12864 ST7920
* 2017-4-12
*/

#include <U8glib.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11);           //定义G1软串口接口(RX TX)
U8GLIB_ST7920_128X64 u8g(3, 9, 8, U8G_PIN_NONE);//SPI    接线

struct PARAMS {
//中国标准
long P1;//显示PM1.0
long _P1;//读取PM1.0
long P2;//显示PM2.5
long _P2;//读取PM2.5
long P3;//显示PM10
long _P3;//读取PM10
//美国标准
long FP1;//显示PM1.0
long _FP1;//读取PM1.0
long FP2;//显示PM2.5
long _FP2;//读取PM2.5
long FP3;//显示PM10
long _FP3;//读取PM10
} _params;

//G5 相关变量
static unsigned char ucRxBuffer[250];
static unsigned char ucRxCnt = 0;

//循环计数器
unsigned char loopCnt = 0;

void setup() {
  Serial.begin(9600);
  //软串口读取G1
  mySerial.begin(9600);
  delay(500);
  //设置屏幕字体和背景颜色
  u8g.setColorIndex(255);
  u8g.setHiColorByRGB(255, 255, 255);
  delay(800);
}
//中国标准参数部分字体位置 设置
void showPM1(void) {
  int x = 0, y = 0;//PM1.0显示位置
  //画LOGO
  u8g.drawRFrame(x, y, 15, 15, 1);
  u8g.setFont(u8g_font_04b_03b);
  u8g.setPrintPos(x + 3, y + 5);
  u8g.print("pm");
u8g.setPrintPos(x + 6, y + 12);
  u8g.print("1");
  u8g.setFont(u8g_font_timB10);
  u8g.setPrintPos(x + 18, y + 12);
  u8g.print(":");
  //设置双倍大小字体显示
  u8g.setScale2x2();
  u8g.setPrintPos(x / 2 + 11, y / 2 + 7);
  u8g.setFont(u8g_font_orgv01);
  u8g.print(_params.P1);
  u8g.undoScale();
}
void showPM25(void) {
  int x = 0, y = 17;//PM2.5显示位置
  //画LOGO
  u8g.drawRFrame(x, y, 15, 15, 1);
  u8g.setFont(u8g_font_04b_03b);
  u8g.setPrintPos(x + 3, y + 5);
  u8g.print("pm");
u8g.setPrintPos(x + 2, y + 12);
  u8g.print("2.5");
  u8g.setFont(u8g_font_timB10);
  u8g.setPrintPos(x + 18, y + 12);
  u8g.print(":");
  //设置双倍大小字体显示
  u8g.setScale2x2();
  u8g.setPrintPos(x / 2 + 11, y / 2 + 7);
   u8g.setFont(u8g_font_orgv01);
  u8g.print(_params.P2);
  u8g.undoScale();
}
void showPM10(void) {
  int x = 0, y = 34;//PM2.5显示位置
  //画LOGO
  u8g.drawRFrame(x, y, 15, 15, 1);
  u8g.setFont(u8g_font_04b_03b);
  u8g.setPrintPos(x + 3, y + 5);
  u8g.print("pm");
u8g.setPrintPos(x + 4, y + 12);
  u8g.print("10");
  u8g.setFont(u8g_font_timB10);
  u8g.setPrintPos(x + 18, y + 12);
  u8g.print(":");
  //设置双倍大小字体显示
  u8g.setScale2x2();
  u8g.setPrintPos(x / 2 + 11, y / 2 + 7);
   u8g.setFont(u8g_font_orgv01);
  u8g.print(_params.P3);
  u8g.undoScale();
}


//美国标准
void showFPM1(void) {
  int x = 64, y = 0;//PM1.0显示位置
  //画LOGO
  u8g.drawRFrame(x, y, 15, 15, 1);
  u8g.setFont(u8g_font_04b_03b);
  u8g.setPrintPos(x + 3, y + 5);
  u8g.print("pm");
u8g.setPrintPos(x + 6, y + 12);
  u8g.print("1");
  u8g.setFont(u8g_font_timB10);
  u8g.setPrintPos(x + 18, y + 12);
  u8g.print(":");
  //设置双倍大小字体显示
  u8g.setScale2x2();
  u8g.setPrintPos(x / 2 + 11, y / 2 + 7);
  u8g.setFont(u8g_font_orgv01);
  u8g.print(_params.FP1);
  u8g.undoScale();
}
void showFPM25(void) {
  int x = 64, y = 17;//PM2.5显示位置
  //画LOGO
  u8g.drawRFrame(x, y, 15, 15, 1);
  u8g.setFont(u8g_font_04b_03b);
  u8g.setPrintPos(x + 3, y + 5);
  u8g.print("pm");
u8g.setPrintPos(x + 2, y + 12);
  u8g.print("2.5");
  u8g.setFont(u8g_font_timB10);
  u8g.setPrintPos(x + 18, y + 12);
  u8g.print(":");
  //设置双倍大小字体显示
  u8g.setScale2x2();
  u8g.setPrintPos(x / 2 + 11, y / 2 + 7);
   u8g.setFont(u8g_font_orgv01);
  u8g.print(_params.FP2);
  u8g.undoScale();
}
void showFPM10(void) {
  int x = 64, y = 34;//PM10 显示位置
  //画LOGO
  u8g.drawRFrame(x, y, 15, 15, 1);
  u8g.setFont(u8g_font_04b_03b);
  u8g.setPrintPos(x + 3, y + 5);
  u8g.print("pm");
u8g.setPrintPos(x + 4, y + 12);
  u8g.print("10");
  u8g.setFont(u8g_font_timB10);
  u8g.setPrintPos(x + 18, y + 12);
  u8g.print(":");
  //设置双倍大小字体显示
  u8g.setScale2x2();
  u8g.setPrintPos(x / 2 + 11, y / 2 + 7);
  u8g.setFont(u8g_font_orgv01);
  u8g.print(_params.FP3);
  u8g.undoScale();

}



//画界面
void draw(void) {
  showPM1();
  showPM25();
  showPM10();
  showFPM1();
  showFPM25();
  showFPM10();
}




//获取PM2.5的值
void getPM25(unsigned char ucData) {

  ucRxBuffer[ucRxCnt++] = ucData;

  if (ucRxBuffer[0] != 0x42 && ucRxBuffer[1] != 0x4D)  {
    ucRxCnt = 0;
  }
  if (ucRxCnt > 31) {
_params._P1 = (float)ucRxBuffer[10] * 256 + (float)ucRxBuffer[11];   
_params._P2 = (float)ucRxBuffer[12] * 256 + (float)ucRxBuffer[13];
   _params._P3 = (float)ucRxBuffer[14] * 256 + (float)ucRxBuffer[15]; //中国标准
_params._FP1 = (float)ucRxBuffer[4] * 256 + (float)ucRxBuffer[5];   
_params._FP2 = (float)ucRxBuffer[6] * 256 + (float)ucRxBuffer[7];
   _params._FP3 = (float)ucRxBuffer[8] * 256 + (float)ucRxBuffer[9]; //美国标准
    ucRxCnt = 0;

  return ucRxCnt;
  }
}
//中位值平均滤波,处理PM1.0的值
#define FILTER_N 5
int Filter1() {
int i;
int filter_sum = 0;
int filter_max, filter_min;
int filter_buf[FILTER_N];
for (i = 0; i < FILTER_N; i++) {
filter_buf = _params._P1;
  delay(1);
}
filter_max = filter_buf[0];
filter_min = filter_buf[0];
filter_sum = filter_buf[0];
for (i = FILTER_N - 1; i > 0; i--) {
if (filter_buf > filter_max)
filter_max = filter_buf;
else if (filter_buf < filter_min)
filter_min = filter_buf;
filter_sum = filter_sum + filter_buf;
filter_buf = filter_buf[i - 1];
}
i = FILTER_N - 2;
filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是为了四舍五入
filter_sum = filter_sum / i;
return filter_sum;
}
//中位值平均滤波,处理PM2.5的值
#define FILTER_N 5
int Filter2() {
int i;
int filter_sum = 0;
int filter_max, filter_min;
int filter_buf[FILTER_N];
for (i = 0; i < FILTER_N; i++) {
filter_buf = _params._P2;
  delay(1);
}
filter_max = filter_buf[0];
filter_min = filter_buf[0];
filter_sum = filter_buf[0];
for (i = FILTER_N - 1; i > 0; i--) {
if (filter_buf > filter_max)
filter_max = filter_buf;
else if (filter_buf < filter_min)
filter_min = filter_buf;
filter_sum = filter_sum + filter_buf;
filter_buf = filter_buf[i - 1];
}
i = FILTER_N - 2;
filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是为了四舍五入
filter_sum = filter_sum / i;
return filter_sum;
}
//中位值平均滤波,处理PM10的值
#define FILTER_N 5
int Filter3() {
int i;
int filter_sum = 0;
int filter_max, filter_min;
int filter_buf[FILTER_N];
for (i = 0; i < FILTER_N; i++) {
filter_buf = _params._P3;
  delay(1);
}
filter_max = filter_buf[0];
filter_min = filter_buf[0];
filter_sum = filter_buf[0];
for (i = FILTER_N - 1; i > 0; i--) {
if (filter_buf > filter_max)
filter_max = filter_buf;
else if (filter_buf < filter_min)
filter_min = filter_buf;
filter_sum = filter_sum + filter_buf;
filter_buf = filter_buf[i - 1];
}
i = FILTER_N - 2;
filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是为了四舍五入
filter_sum = filter_sum / i;
return filter_sum;
}

//美国标准数据过滤
#define FILTER_N 5
int Filter4() {
int i;
int filter_sum = 0;
int filter_max, filter_min;
int filter_buf[FILTER_N];
for (i = 0; i < FILTER_N; i++) {
filter_buf = _params._FP1;
  delay(1);
}
filter_max = filter_buf[0];
filter_min = filter_buf[0];
filter_sum = filter_buf[0];
for (i = FILTER_N - 1; i > 0; i--) {
if (filter_buf > filter_max)
filter_max = filter_buf;
else if (filter_buf < filter_min)
filter_min = filter_buf;
filter_sum = filter_sum + filter_buf;
filter_buf = filter_buf[i - 1];
}
i = FILTER_N - 2;
filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是为了四舍五入
filter_sum = filter_sum / i;
return filter_sum;
}
//中位值平均滤波,处理PM2.5的值
#define FILTER_N 5
int Filter5() {
int i;
int filter_sum = 0;
int filter_max, filter_min;
int filter_buf[FILTER_N];
for (i = 0; i < FILTER_N; i++) {
filter_buf = _params._FP2;
  delay(1);
}
filter_max = filter_buf[0];
filter_min = filter_buf[0];
filter_sum = filter_buf[0];
for (i = FILTER_N - 1; i > 0; i--) {
if (filter_buf > filter_max)
filter_max = filter_buf;
else if (filter_buf < filter_min)
filter_min = filter_buf;
filter_sum = filter_sum + filter_buf;
filter_buf = filter_buf[i - 1];
}
i = FILTER_N - 2;
filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是为了四舍五入
filter_sum = filter_sum / i;
return filter_sum;
}
//中位值平均滤波,处理PM10的值
#define FILTER_N 5
int Filter6() {
int i;
int filter_sum = 0;
int filter_max, filter_min;
int filter_buf[FILTER_N];
for (i = 0; i < FILTER_N; i++) {
filter_buf = _params._FP3;
  delay(1);
}
filter_max = filter_buf[0];
filter_min = filter_buf[0];
filter_sum = filter_buf[0];
for (i = FILTER_N - 1; i > 0; i--) {
if (filter_buf > filter_max)
filter_max = filter_buf;
else if (filter_buf < filter_min)
filter_min = filter_buf;
filter_sum = filter_sum + filter_buf;
filter_buf = filter_buf[i - 1];
}
i = FILTER_N - 2;
filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是为了四舍五入
filter_sum = filter_sum / i;
return filter_sum;
}
void loop() {

  while (mySerial.available())   {
    getPM25(mySerial.read());
  }
   _params.P1 = Filter1();
    _params.P2 = Filter2();
     _params.P3 = Filter3();
     _params.FP1 = Filter4();
    _params.FP2 = Filter5();
     _params.FP3 = Filter6();


  u8g.firstPage();
  do {
    draw();
  } while ( u8g.nextPage());
}
[/mw_shl_code]

卫生间抽烟半小时后空气质量

卫生间抽烟半小时后空气质量

书房正常空气质量

书房正常空气质量

wwwyyy317  见习技师
 楼主|

发表于 2017-4-13 20:20:35

:loveliness:
回复

使用道具 举报

hnyzcj  版主

发表于 2017-4-17 10:29:39

不错的
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail