找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 9104|回复: 13
收起左侧

求一个PM2.5检测仪的Arduino源程序

[复制链接]
ID:111876 发表于 2016-4-16 08:50 | 显示全部楼层 |阅读模式
    购得夏普GP2Y1010AU0F 传感器,手头有我自己写好引导程序的ATMEGA328P的芯片,有Arduino R3板(可往芯片里写程序),还有1602液晶屏,想用自己搭建的ATMEGA328P最小系统,制作一个PM2.5检测仪,,只差可用的源程序了,在网上没有搜到可用的,请哪位有的朋友,给我传一个,1602屏要用四线制驱动,多谢了!






回复

使用道具 举报

ID:1 发表于 2016-4-16 22:08 | 显示全部楼层
回复

使用道具 举报

ID:111876 发表于 2016-4-17 11:15 | 显示全部楼层
admin 发表于 2016-4-16 22:08
http://www.51hei.com/bbs/dpj-48466-1.html

谢谢版主!但他那个似乎是51单片机的,而我用的是ATMEGA328P的单片机芯片,需要的是Arduino的源程序!
回复

使用道具 举报

ID:111876 发表于 2016-4-30 16:07 | 显示全部楼层
网上找了个源代码,做了一些改写,现在已经成功了,这是我学习单片机一个月以来,做成的第一个小东西。有成就感,高兴啊!哈哈!

20160430_152324.jpg

20160430_150156.jpg

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:111876 发表于 2016-4-30 16:38 | 显示全部楼层
yjx189 发表于 2016-4-30 16:07
网上找了个源代码,做了一些改写,现在已经成功了,这是我学习单片机一个月以来,做成的第一个小东西。有成 ...

谢谢版主先生的在此加分,
回复

使用道具 举报

ID:111876 发表于 2016-4-30 16:48 | 显示全部楼层
本帖最后由 yjx189 于 2016-4-30 16:50 编辑

    下一步打算用手头就有的89C2051单片机,加上1602显示屏,使用坛友送我的SP200S编程器写程序,做一个能测量电感,电容的单片机LC表!这样的LC表还是有一定的使用价值的!
回复

使用道具 举报

ID:117823 发表于 2016-5-1 16:34 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dht11.h>
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6. #include <DS1302.h>

  7. char sensor;
  8. static long timer;
  9. static byte session;
  10. long previousMillis = 0;  
  11. long interval = 1000;

  12. int sensorPin = A3;
  13. int sensorValue = 0;
  14. int sensorPin1 = A7;
  15. int sensorValue1 = 0;
  16. const int buzz=3;
  17. const int  alcoholic=A6;
  18. static int alcoholic_value=200;
  19. char buflcd[16];
  20. dht11 DHT11;
  21. #define DHT11PIN 2
  22. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  23. namespace {

  24. // Set the appropriate digital I/O pin connections. These are the pin
  25. // assignments for the Arduino as well for as the DS1302 chip. See the DS1302
  26. // datasheet:
  27. const int kCePin   = A2;  // Chip Enable
  28. const int kIoPin   = A1;  // Input/Output
  29. const int kSclkPin = A0;  // Serial Clock

  30. // Create a DS1302 object.
  31. DS1302 rtc(kCePin, kIoPin, kSclkPin);
  32. void ds1302_init()
  33. {
  34.   rtc.writeProtect(false);
  35.   rtc.halt(false);
  36. }

  37. String dayAsString(const Time::Day day) {
  38.   switch (day) {
  39.     case Time::kSunday: return "Sunday";
  40.     case Time::kMonday: return "Monday";
  41.     case Time::kTuesday: return "Tuesday";
  42.     case Time::kWednesday: return "Wednesday";
  43.     case Time::kThursday: return "Thursday";
  44.     case Time::kFriday: return "Friday";
  45.     case Time::kSaturday: return "Saturday";
  46.   }
  47.   return "(unknown day)";
  48. }

  49. void printTime() {
  50.   // Get the current time and date from the chip.
  51.   Time t = rtc.time();
  52.   // Format the time and date and insert into the temporary buffer.
  53.   snprintf(buflcd, sizeof(buflcd), "%2d-%d-%d %d:%d:%d   ",
  54.            t.yr-2000, t.mon, t.date,
  55.            t.hr, t.min, t.sec);

  56.   // Print the formatted string to serial so we can see the time.
  57.   lcd.setCursor(0,1);
  58.   lcd.print(buflcd);
  59. }

  60. void edit_time()
  61. {
  62.   /* 串口数据缓存 */
  63. String comdata = "";
  64. int numdata[7] ={0}, j = 0, mark = 0;
  65.   while (Serial.available() > 0)
  66.     {
  67.         comdata += char(Serial.read());
  68.         delay(2);
  69.         mark = 1;
  70.     }
  71.     /* 以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组 */
  72.     if(mark == 1)
  73.     {
  74.         Serial.print("You inputed : ");
  75.         Serial.println(comdata);
  76.         for(int i = 0; i < comdata.length() ; i++)
  77.         {
  78.             if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
  79.             {
  80.                 j++;
  81.             }
  82.             else
  83.             {
  84.                 numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
  85.             }
  86.         }
  87.         if(numdata[0]>2000)
  88.         {
  89.         /* 将转换好的numdata凑成时间格式,写入DS1302 */
  90.         Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], Time::kSaturday );
  91.         rtc.time(t);
  92.         }
  93.         else
  94.         {
  95.           if(numdata[0]>100)
  96.           {
  97.            alcoholic_value= numdata[0];
  98.           }
  99.         }
  100.     }
  101. }

  102. }  // namespace
  103. #if defined(ARDUINO) && ARDUINO >= 100
  104. #define printByte(args)  write(args);
  105. #else
  106. #define printByte(args)  print(args,BYTE);
  107. #endif
  108. uint8_t bell[8]  = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
  109. uint8_t note[8]  = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
  110. uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
  111. uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
  112. uint8_t duck[8]  = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
  113. uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
  114. uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
  115. uint8_t retarrow[8] = {        0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
  116. void lcd_init()
  117. {
  118.   lcd.init();                      // initialize the lcd
  119.   lcd.backlight();
  120.   lcd.createChar(0, bell);
  121.   lcd.createChar(1, note);
  122.   lcd.createChar(2, clock);
  123.   lcd.createChar(3, heart);
  124.   lcd.createChar(4, duck);
  125.   lcd.createChar(5, check);
  126.   lcd.createChar(6, cross);
  127.   lcd.createChar(7, retarrow);
  128. }

  129. void dh11()
  130. {
  131.   int chk = DHT11.read(DHT11PIN);  
  132.   Serial.print("Humidity (%): ");
  133.   Serial.println((int)DHT11.humidity);
  134.   Serial.print("Temperature (oC): ");
  135.   Serial.println((int)DHT11.temperature);
  136.   sensorValue = analogRead(sensorPin);
  137.   Serial.print("Light=" );                       
  138.   Serial.println(sensorValue);
  139.   sensorValue1 = analogRead(sensorPin1);
  140.   Serial.print("alcoholic=" );                       
  141.   Serial.println(sensorValue1);  
  142.   lcd.setCursor(0,0);
  143.   snprintf(buflcd, sizeof(buflcd), "%2dC %2d %3d %3d  ", DHT11.temperature,DHT11.humidity,sensorValue,sensorValue1);
  144.   lcd.print(buflcd);
  145. }
  146. void pin_mode()
  147. {
  148. pinMode(buzz, OUTPUT);
  149. digitalWrite(buzz, LOW);
  150. pinMode(alcoholic, INPUT);
  151. }

  152. void alcoholic_buzz()
  153. {
  154.   sensorValue1 = analogRead(sensorPin1);
  155.   if(sensorValue1>alcoholic_value)
  156.   {
  157.     digitalWrite(buzz, HIGH);
  158.     lcd.setCursor(15, 0);
  159.     lcd.printByte(0);
  160.   }
  161.   else
  162.   {
  163.     digitalWrite(buzz, LOW);
  164.     lcd.setCursor(15, 0);
  165.     lcd.print(" ");
  166.   }
  167. }


  168. void setup()
  169. {
  170.   Serial.begin(115200);
  171.   pin_mode();
  172.   lcd_init();
  173. ds1302_init();
  174. }

  175. void loop()
  176. {
  177.   unsigned long currentMillis = millis();
  178.     if(currentMillis - previousMillis > interval)
  179.    {
  180.    //display temperature and humidity   
  181.     previousMillis = currentMillis;
  182.     dh11();
  183.     alcoholic_buzz();
  184.     printTime();
  185.    }  
  186.   edit_time();
  187. }
复制代码
回复

使用道具 举报

ID:111876 发表于 2016-5-2 10:31 | 显示全部楼层
dadangjia 发表于 2016-5-1 16:34
#include
#include
#include

谢谢,但是I2C的板子很难伺候,我网购了2块I2C转接板,用时一周,把网上能搜到,能问到的所有办法都试过了,结果是1602屏不能正常显示,,最后又返回来使用四线式连接,才解决的问题!!去他地吧I2C!!
回复

使用道具 举报

ID:111876 发表于 2016-5-2 10:36 | 显示全部楼层
楼上的先生,再仔细一看你上传的程序,是一个采用1302的时钟源代码啊!
回复

使用道具 举报

ID:117823 发表于 2016-6-4 01:38 来自手机 | 显示全部楼层
可以用,我用了10个pcf8574 iic模块1602可以正常使用,很简单的。我采用的是ds1302代码,库文件可以在网上找,大多类似。
回复

使用道具 举报

ID:111876 发表于 2016-6-6 07:13 | 显示全部楼层
dadangjia 发表于 2016-6-4 01:38
可以用,我用了10个pcf8574 iic模块1602可以正常使用,很简单的。我采用的是ds1302代码,库文件可以在网上 ...

说的什么啊?
回复

使用道具 举报

ID:180355 发表于 2017-3-22 15:42 | 显示全部楼层
yjx189 发表于 2016-4-30 16:07
网上找了个源代码,做了一些改写,现在已经成功了,这是我学习单片机一个月以来,做成的第一个小东西。有成 ...

你好,求打包
回复

使用道具 举报

ID:193375 发表于 2017-4-25 15:49 | 显示全部楼层
求分享
回复

使用道具 举报

ID:202802 发表于 2017-5-20 11:15 | 显示全部楼层
#include <UTFT.h>


UTFT myGLCD(YYROBOT_TFT144,A2,A1,A5,A4,A3);  // Remember to change the model parameter to suit your display module!
//YYROBOT_TFT144  屏幕型号,不用修改
//SDA----A2
//SCL----A1
//CS-----A5
//RST----A4
//RS----A3
//LED---A0  UTFT库里面设定的,如果需要修改需要修改库文件

extern uint8_t SmallFont[];//原始文件在库文件的DefaultFonts.c中
extern uint8_t BigFont[];//原始文件在库文件的DefaultFonts.c中
extern uint8_t SevenSegNumFont[];//原始文件在库文件的DefaultFonts.c中
const uint8_t tfont16[] =
{

  0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80,0x0F,0x00,0x00,0x00,/*"P",0*/
  0x10,0x04,0x1F,0xFC,0x1F,0x00,0x00,0xFC,0x1F,0x00,0x1F,0xFC,0x10,0x04,0x00,0x00,/*"M",1*/
  0x00,0x00,0x0E,0x0C,0x10,0x14,0x10,0x24,0x10,0x44,0x11,0x84,0x0E,0x0C,0x00,0x00,/*"2",2*/
  0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*".",3*/
  0x00,0x00,0x1F,0x98,0x10,0x84,0x11,0x04,0x11,0x04,0x10,0x88,0x10,0x70,0x00,0x00,/*"5",4*/
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",5*/
  0x08,0x20,0x06,0x20,0x40,0x7E,0x31,0x80,0x04,0x20,0x18,0x40,0x10,0x80,0x13,0xFF,
  0x1C,0x02,0xF3,0x84,0x10,0x60,0x10,0x50,0x14,0x88,0x19,0x04,0x00,0x02,0x00,0x00,/*"浓",6*/
  0x00,0x02,0x00,0x0C,0x3F,0xF1,0x24,0x01,0x24,0x21,0x24,0x32,0x3F,0xAA,0xA4,0xA4,
  0x64,0xA4,0x24,0xA4,0x3F,0xAA,0x24,0x32,0x24,0x01,0x24,0x01,0x20,0x01,0x00,0x00,/*"度",7*/


};

#define PM2_5Serial Serial1 //用于接收PM2.5数据
//本例程使用MEGA2560,有Serial1,若使用UNO,请将这里修改为Serial
//并且把DebugSerial.begin(9600);修改为DebugSerial.begin(2400);
//串口界面通过2400波特率观看数据
#define DebugSerial Serial //用于把数据通过串口界面返回

byte buffer[7] = {
};
int count = 0;
bool bufferComplete = false;

void setup() {
  PM2_5Serial.begin(2400);
  DebugSerial.begin(9600);

  randomSeed(analogRead(0));

  // Setup the LCD
  myGLCD.InitLCD();//初始化液晶
  myGLCD.InitLCD();//初始化两次有利于系统稳定
  myGLCD.InitLCD(PORTRAIT);
  myGLCD.setFont(SmallFont);//设置字体为SmallFont格式

  //ChineseFont Test 16*16自定义汉字测试
  myGLCD.clrScr();
  myGLCD.setFont(SmallFont);  
  myGLCD.print("ug/m3", RIGHT, 100);  //打印

  Show_CH_Font16(0,10,0);
  Show_CH_Font16(16,10,1);
  Show_CH_Font16(32,10,2);
  Show_CH_Font16(48,10,3);
  Show_CH_Font16(64,10,4);



  //SegNum Test
  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(0, 0, 0);

}

void loop() {  
  int ppm;
  int count_sum = 0;
  long sum = 0;
  while (count_sum < 10)
  {
    while (PM2_5Serial.available() > 0) {
      char inChar = (char)PM2_5Serial.read();
      buffer[count] = (byte)inChar;

      //    DebugSerial.print("count=");
      //    DebugSerial.println(count);
      //
      //    DebugSerial.print("buffer[count]=");
      //    DebugSerial.println(buffer[count],HEX);


      if (buffer[count] == 0xAA) {
        count = 0;
        //      DebugSerial.println("start");
      }
      else if (buffer[count] == 0xFF && buffer[0] == 0xAA)
      {
        bufferComplete = true;
        //      DebugSerial.println("bufferComplete = true;");
      }

      count++;
      if (count >= 7)
      {
        count = 0;
      }
    }

    //把接收到的数据转发出来
    if (bufferComplete == true) {
      bufferComplete = false;
      for (int i = 0 ; i < 7 ; i++)
      {
        DebugSerial.print(buffer[i], HEX);
        DebugSerial.print(",");
      }
      DebugSerial.println("");

      //处理数据
      float v_out = 0;
      v_out = (float)((buffer[1] * 256) + buffer[2]) * 5 / 1024;
      DebugSerial.print("v_out = ");
      DebugSerial.println(v_out);
      int ppm = v_out * 1000 * 0.5; //单位为ug/m3
      DebugSerial.print("ppm = ");
      DebugSerial.print(ppm);
      DebugSerial.println("ug/m3");

      if(ppm >= 0)
      {
        count_sum++;
        sum += ppm;
      }
    }
  }

  sum /= 10;
  DebugSerial.println("***********");
  DebugSerial.print("sum = ");
  DebugSerial.print(sum);
  DebugSerial.println("ug/m3");
  DebugSerial.println("***********");

  myGLCD.setFont(SevenSegNumFont);
  myGLCD.printNumI(sum, 0, 40, 4, '0');//显示数值是i,位置x=0,y=40,长度4,空位置填充字符0


  delay(500);

}


void Show_CH_Font16(int x,int y,int FontPos)
{
  char temp,t,t1,k;
  int y0=y;
  int HZnum;

  for(t=0;t<32;t++)//每个16*16的汉字点阵 有32个字节
  {   
    temp=tfont16[t+32*FontPos];                    
    for(t1=0;t1<8;t1++)
    {
      if(temp&0x80)
      {
        myGLCD.setColor(255, 255, 255);//FontColor
        myGLCD.drawPixel(x,y);
      }
      else
      {
        myGLCD.setColor(0, 0, 0);//BackColor
        myGLCD.drawPixel(x,y);
      }

      temp<<=1;
      y++;
      if((y-y0)==16)
      {
        y=y0;
        x++;
        break;
      }

    }   
  }   


}
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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