[活动] 基于K22F的PM2.5+甲醛监测器(5)

[复制链接]
 楼主| Ketose 发表于 2016-1-23 09:15 | 显示全部楼层 |阅读模式
本帖最后由 Ketose 于 2016-1-23 09:18 编辑

测量数据的使用0.96寸的12864显示输出(完结)
1、OLED介绍
0.96’ OLED 显示模块, 分辨率为 128*64,采用SSD1306 驱动 IC,该芯片内部集成 DCDC 升压,仅需 3.3V 供电,即可正常工作。实际上就是由一个SSD1306控制器和一个128X64的有机发光二极管点阵组成。具体更详细的资料大家可以到网上查,我这里不做详细介绍了。
2、模块的接口原理图

在K22F上,使用SPI1来驱动。
3、整个系统原理图

4、OLED驱动部分代码:
  1. /**
  2. ****************************************************************************************
  3. *
  4. * [url=home.php?mod=space&uid=288409]@file[/url] lcd_12864.c
  5. *
  6. * [url=home.php?mod=space&uid=247401]@brief[/url] lcd_12864 driver.
  7. *
  8. * Copyright (C) TChip 2014
  9. *
  10. * $Rev: 1.0 $
  11. *
  12. ****************************************************************************************
  13. */

  14. /**
  15. ****************************************************************************************
  16. * @addtogroup  SPI
  17. * @{
  18. ****************************************************************************************
  19. */

  20. /*
  21. * INCLUDE FILES
  22. ****************************************************************************************
  23. */
  24. #include "fsl_dspi_master_driver.h"
  25. #include "fsl_spi_master_driver.h"
  26. #include "fsl_gpio_hal.h"
  27. #include "fsl_port_hal.h"
  28. #include "fsl_debug_console.h"
  29. #include "delay.h"
  30. #include "oled.h"
  31. #include "oledfont.h"

  32. /*
  33. * MARCO VARIABLE DECLARATION
  34. ****************************************************************************************
  35. */
  36. #define TRANSFER_SIZE               (1)                /*! Transfer size */
  37. #define TRANSFER_BAUDRATE           (1000000U)           /*! Transfer baudrate - 500k */
  38. #define MASTER_TRANSFER_TIMEOUT     (5000U)             /*! Transfer timeout of master - 5s */
  39. /*
  40. * LOCAL VARIABLE DECLARATION
  41. ****************************************************************************************
  42. */
  43. //check spi status
  44. volatile uint8_t        oled_rx_flag = 0;
  45. volatile uint8_t        oled_tx_flag = 0;

  46. //uint8_t i;
  47. uint8_t buffer[32];

  48. //Init the OLED
  49. //static uint8_t                 Init_buffer[] =
  50. //{
  51. //         0xAE ,//--turn off oled panel
  52. //         0x00 ,//---set low column address
  53. //         0x10 ,//---set high column address
  54. //         0x40 ,//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  55. //         0x81 ,//--set contrast control register
  56. //         0xCF , // Set SEG Output Current Brightness
  57. //         0xA1 ,//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  58. //         0xC8 ,//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  59. //         0xA6 ,//--set normal display
  60. //         0xA8 ,//--set multiplex ratio(1 to 64)
  61. //         0x3f ,//--1/64 duty
  62. //         0xD3 ,//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  63. //         0x00 ,//-not offset
  64. //         0xd5 ,//--set display clock divide ratio/oscillator frequency
  65. //         0x80 ,//--set divide ratio, Set Clock as 100 Frames/Sec
  66. //         0xD9 ,//--set pre-charge period
  67. //         0xF1 ,//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  68. //         0xDA ,//--set com pins hardware configuration
  69. //         0x12 ,
  70. //         0xDB ,//--set vcomh
  71. //         0x40 ,//Set VCOM Deselect Level
  72. //         0x20 ,//-Set Page Addressing Mode (0x00/0x01/0x02)
  73. //         0x02 ,//
  74. //         0x8D ,//--set Charge Pump enable/disable
  75. //         0x14 ,//--set(0x10) disable
  76. //         0xA4 ,// Disable Entire Display On (0xa4/0xa5)
  77. //         0xA6 ,// Disable Inverse Display On (0xa6/a7)
  78. //         0xAF ,//--turn on oled panel
  79. //
  80. //         0xAF , /*display ON*/
  81. //};

  82. /*
  83. * GLOBAL VARIABLE DECLARATION
  84. ****************************************************************************************
  85. */


  86. #if (NXP_OLED)
  87. //void DelayUs(uint32_t us)
  88. //{
  89. //  for(int i=us*30;i>0;i--)
  90. //    __NOP();
  91. //}
  92. //void DelayMs(uint32_t ms)
  93. //{
  94. //  for(int i=ms;i>0;i--)
  95. //    DelayUs(1000);
  96. //}
  97. /**
  98. ****************************************************************************************
  99. * [url=home.php?mod=space&uid=247401]@brief[/url] SPI RX CALLBACK FUNCTION.
  100. * @description
  101. *
  102. ****************************************************************************************
  103. */
  104. void oled_read_done(void)
  105. {
  106.     oled_rx_flag = 0;
  107. }

  108. /**
  109. ****************************************************************************************
  110. * [url=home.php?mod=space&uid=247401]@brief[/url] SPI TX CALLBACK FUNCTION.
  111. * @description
  112. *
  113. ****************************************************************************************
  114. */
  115. void oled_write_done(void)
  116. {
  117.     oled_tx_flag = 0;
  118. }

  119. /**
  120. ****************************************************************************************
  121. * @brief Configure the SPI GPIO and set RS  、 RST GPIO output,Init them.
  122. * @description
  123. *
  124. ****************************************************************************************
  125. */
  126. void oled_io_config(void)
  127. {   
  128.     //Init Gpio with a callback,it's necessary
  129.     //set the LCD_RS_PIN an output
  130.     PORT_HAL_SetMuxMode(PORTB,OLED_DC_PIN,kPortMuxAsGpio);
  131.     GPIO_HAL_SetPinDir(OLED_DC,OLED_DC_PIN,kGpioDigitalOutput);
  132.     GPIO_HAL_ClearPinOutput(OLED_DC,OLED_DC_PIN);

  133.     //set the LCD_RST_PIN an output
  134.     //prevent it reset the lcd
  135.     PORT_HAL_SetMuxMode(PORTC,OLED_RST_PIN,kPortMuxAsGpio);
  136.     GPIO_HAL_SetPinDir(OLED_RST,OLED_RST_PIN,kGpioDigitalOutput);
  137.     GPIO_HAL_SetPinOutput(OLED_RST,OLED_RST_PIN);
  138. }


  139. void OLED_Set_Pos(unsigned char x, unsigned char y)
  140. {
  141.     OLED_WR_Byte(0xb0+y,OLED_CMD);
  142.     OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
  143.     OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
  144. }
  145. //开启OLED显示
  146. void OLED_Display_On(void)
  147. {
  148.     OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  149.     OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
  150.     OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
  151. }
  152. //关闭OLED显示
  153. void OLED_Display_Off(void)
  154. {
  155.     OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
  156.     OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
  157.     OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
  158. }
  159. //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
  160. void OLED_Clear(void)
  161. {
  162.     uint8_t i,n;
  163.     for(i=0; i<8; i++)
  164.     {
  165.         OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  166.         OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  167.         OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址
  168.         for(n=0; n<128; n++)
  169.           OLED_WR_Byte(0,OLED_DATA);
  170.     } //更新显示
  171. }

  172. void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
  173. {
  174.     dspi_status_t dspiResult;
  175.     buffer[0] = dat;
  176.     if(cmd)
  177.         OLED_DC_Set();
  178.     else
  179.         OLED_DC_Clr();
  180.     OLED_CS_Clr();
  181.     //spi_write(QN_SPI1, buffer, 1, oled_write_done);
  182.     dspiResult = DSPI_DRV_MasterTransferBlocking(SPI1_IDX,
  183.                                                  NULL,
  184.                                                  buffer,
  185.                                                  NULL,
  186.                                                  1,
  187.                                                  MASTER_TRANSFER_TIMEOUT);
  188.     if(kStatus_DSPI_Success != dspiResult)
  189.       while(1);
  190.     oled_write_done();
  191.     DelayUs(100);

  192.     OLED_CS_Set();
  193.     OLED_DC_Set();

  194. }

  195. //void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
  196. //{
  197. //        //uint8_t i;
  198. //        uint8_t buffer[1];
  199. //        buffer[0] = dat;
  200. //        if(cmd)
  201. //          OLED_DC_Set();
  202. //        else
  203. //          OLED_DC_Clr();
  204. //        OLED_CS_Clr();
  205. //        spi_write(QN_SPI1, buffer, 1, lcd_write_done);
  206. //        delay(100);
  207. //
  208. //        OLED_CS_Set();
  209. //        OLED_DC_Set();
  210. //}


  211. void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr)
  212. {
  213.     unsigned char c=0,i=0;
  214.     c=chr-' ';//得到偏移后的值
  215.     if(x>Max_Column-1) {
  216.         x=0;
  217.         y=y+2;
  218.     }
  219.     if(SIZE ==16)
  220.     {
  221.         OLED_Set_Pos(x,y);
  222.         for(i=0; i<8; i++)
  223.             OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
  224.         OLED_Set_Pos(x,y+1);
  225.         for(i=0; i<8; i++)
  226.             OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
  227.     }
  228.     else {
  229.         OLED_Set_Pos(x,y+1);
  230.         for(i=0; i<6; i++)
  231.             OLED_WR_Byte(F6x8[c][i],OLED_DATA);

  232.     }
  233. }
  234. //m^n函数
  235. uint32_t oled_pow(uint8_t m,uint8_t n)
  236. {
  237.     uint32_t result=1;
  238.     while(n--)result*=m;
  239.     return result;
  240. }
  241. //显示2个数字
  242. //x,y :起点坐标
  243. //len :数字的位数
  244. //size:字体大小
  245. //mode:模式        0,填充模式;1,叠加模式
  246. //num:数值(0~4294967295);
  247. void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size)
  248. {
  249.     uint8_t t,temp;
  250.     uint8_t enshow=0;
  251.     for(t=0; t<len; t++)
  252.     {
  253.         temp=(num/oled_pow(10,len-t-1))%10;
  254.         if(enshow==0&&t<(len-1))
  255.         {
  256.             if(temp==0)
  257.             {
  258.                 OLED_ShowChar(x+(size/2)*t,y,' ');
  259.                 continue;
  260.             } else enshow=1;

  261.         }
  262.         OLED_ShowChar(x+(size/2)*t,y,temp+'0');
  263.     }
  264. }
  265. //显示一个字符号串
  266. void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr)
  267. {
  268.     unsigned char j=0;
  269.     while (chr[j]!='\0')
  270.     {   OLED_ShowChar(x,y,chr[j]);
  271.         x+=8;
  272.         if(x>120) {
  273.             x=0;
  274.             y+=2;
  275.         }
  276.         j++;
  277.     }
  278. }
  279. //显示汉字
  280. void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
  281. {
  282.     uint8_t t,adder=0;
  283.     OLED_Set_Pos(x,y);
  284.     for(t=0; t<16; t++)
  285.     {
  286.         OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
  287.         adder+=1;
  288.     }
  289.     OLED_Set_Pos(x,y+1);
  290.     for(t=0; t<16; t++)
  291.     {
  292.         OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
  293.         adder+=1;
  294.     }
  295. }
  296. /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
  297. void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
  298. {
  299.     unsigned int j=0;
  300.     unsigned char x,y;

  301.     if(y1%8==0) y=y1/8;
  302.     else y=y1/8+1;
  303.     for(y=y0; y<y1; y++)
  304.     {
  305.         OLED_Set_Pos(x0,y);
  306.         for(x=x0; x<x1; x++)
  307.         {
  308.             OLED_WR_Byte(BMP[j++],OLED_DATA);
  309.         }
  310.     }
  311. }


  312. void SPI_Init()
  313. {
  314.         static uint32_t calculatedBaudRate;
  315.         static dspi_master_state_t masterState;
  316.         static dspi_device_t masterDevice;
  317.         static dspi_master_user_config_t masterUserConfig = {
  318.                 .isChipSelectContinuous     = false,
  319.                 .isSckContinuous            = false,
  320.                 .pcsPolarity                = kDspiPcs_ActiveLow,
  321.                 .whichCtar                  = kDspiCtar0,
  322.                 .whichPcs                   = kDspiPcs0
  323.         };
  324.         dspi_status_t dspiResult;
  325.     // Setup the configuration.
  326.     masterDevice.dataBusConfig.bitsPerFrame = 8;
  327.     masterDevice.dataBusConfig.clkPhase     = kDspiClockPhase_FirstEdge;
  328.     masterDevice.dataBusConfig.clkPolarity  = kDspiClockPolarity_ActiveHigh;
  329.     masterDevice.dataBusConfig.direction    = kDspiMsbFirst;
  330.     // Initialize master driver.
  331.     dspiResult = DSPI_DRV_MasterInit(SPI1_IDX,
  332.                                      &masterState,
  333.                                      &masterUserConfig);
  334.         while(dspiResult != kStatus_DSPI_Success);
  335.     // Configure baudrate.
  336.     masterDevice.bitsPerSec = 32;
  337.     dspiResult = DSPI_DRV_MasterConfigureBus(SPI1_IDX,
  338.                                              &masterDevice,
  339.                                              &calculatedBaudRate);
  340.     while(dspiResult != kStatus_DSPI_Success);
  341. }


  342. //初始化SSD1306
  343. void OLED_Init(void)
  344. {
  345.     oled_io_config();
  346.    
  347.     SPI_Init();

  348.     OLED_RST_Set();
  349.     DelayMs(10);
  350.     OLED_RST_Clr();
  351.     DelayMs(10);
  352.     OLED_RST_Set();


  353.     OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
  354.     OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
  355.     OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  356.     OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  357.     OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
  358.     OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
  359.     OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  360.     OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  361.     OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
  362.     OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  363.     OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
  364.     OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  365.     OLED_WR_Byte(0x00,OLED_CMD);//-not offset
  366.     OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
  367.     OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
  368.     OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
  369.     OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  370.     OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
  371.     OLED_WR_Byte(0x12,OLED_CMD);
  372.     OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
  373.     OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
  374.     OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
  375.     OLED_WR_Byte(0x02,OLED_CMD);//
  376.     OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
  377.     OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
  378.     OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
  379.     OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
  380.     OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel

  381.     OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
  382.     OLED_Clear();
  383.     OLED_Set_Pos(0,0);
  384. }

  385. #endif
  386. ///end


5、主程序代码
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. *   of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. *   list of conditions and the following disclaimer in the documentation and/or
  13. *   other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. *   contributors may be used to endorse or promote products derived from this
  17. *   software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. *
  31. * [File Name]     main.c
  32. * [Platform]      FRDM-K22F
  33. * [Project]       K22F_PM25
  34. * [Version]       1.00
  35. * [Author]        Goth
  36. * [Date]          01/19/2016
  37. * [Language]      'C'
  38. * [History]       1.00 - Original Release
  39. *
  40. */

  41. //-----------------------------------------------------------------------
  42. // Standard C/C++ Includes
  43. //-----------------------------------------------------------------------

  44. #include <stdio.h>
  45. #include <math.h>
  46. //-----------------------------------------------------------------------
  47. // KSDK Includes
  48. //-----------------------------------------------------------------------
  49. #include "main.h"
  50. //-----------------------------------------------------------------------
  51. // Application Includes
  52. //-----------------------------------------------------------------------
  53. #include "oled.h"
  54. #include "dht11.h"
  55. #include "gp2y.h"
  56. #include "ch2o.h"
  57. #include "delay.h"
  58. //-----------------------------------------------------------------------
  59. // Function Prototypes
  60. //-----------------------------------------------------------------------
  61. void ReadCH2O(void);
  62. void ReadDHT11(void);
  63. void ReadGP2Y(void);
  64. //-----------------------------------------------------------------------
  65. // Constants
  66. //-----------------------------------------------------------------------

  67. //-----------------------------------------------------------------------
  68. // Typedefs
  69. //-----------------------------------------------------------------------

  70. //-----------------------------------------------------------------------
  71. // Global Variables
  72. //-----------------------------------------------------------------------
  73. extern char **[];
  74. //-----------------------------------------------------------------------
  75. // Macros
  76. //-----------------------------------------------------------------------

  77. //-----------------------------------------------------------------------
  78. // Main Function
  79. //-----------------------------------------------------------------------

  80. int main(void)
  81. {

  82.     // Configure board specific pin muxing
  83.     hardware_init();
  84.         OSA_Init();
  85.     // Initialize UART terminal
  86.     dbg_uart_init();
  87.         
  88.         DelayInit();
  89.         
  90.         DHT11_Init();
  91.         CH2O_Init();
  92.         GP2Y_init();
  93.         OLED_Init();
  94.         OLED_Clear();

  95.     PRINTF("\r\nRunning the K22F_PM25 project.\r\n");
  96.         //Show **
  97.         OLED_DrawBMP(0,0,128,8,(unsigned char*)**);
  98.         DelayMs(1000);
  99.         OLED_Clear();

  100.     for (;;)                                         // Forever loop
  101.     {
  102.                 ReadCH2O();
  103.                 ReadDHT11();
  104.                 ReadGP2Y();
  105.                 DelayUs(1000);
  106.     }
  107. }

  108. void ReadCH2O(void)
  109. {
  110.         uint8_t receiveBuffer[32];
  111.         uint8_t rx;
  112.         
  113.         rx = 0x00;
  114.         for(int i=0;i<9;i++)
  115.                 receiveBuffer[i] = 0;
  116.         // Call received API
  117.         UART_DRV_ReceiveData(UART2_IDX, &rx, 1u);

  118.         // Wait until we receive a character
  119.         while (kStatus_UART_RxBusy == UART_DRV_GetReceiveStatus(UART2_IDX, NULL));
  120.         if(rx == 0xFF)
  121.         {
  122.                 receiveBuffer[0] = rx;
  123.                 for(int i=1;i<9;i++)
  124.                 {
  125.                         // Call received API
  126.                         UART_DRV_ReceiveData(UART2_IDX, &rx, 1u);

  127.                         // Wait until we receive a character
  128.                         while (kStatus_UART_RxBusy == UART_DRV_GetReceiveStatus(UART2_IDX, NULL));
  129.                         receiveBuffer[i] = rx;
  130.                 }
  131.         }
  132.         /*国家标准室内0.08mg/m3 = 833 * 0.08 = 66.64 ppb。*/
  133.         int pm25Value = receiveBuffer[4]*256 + receiveBuffer[5];
  134.         float result = pm25Value / 66.64 * 0.08;
  135.         /*
  136.         int v1 = (int)result;
  137.         int v2 = (int)((result - v1)*1000);
  138.         PRINTF("CH2O: %d.%03d(mg/m3)\r\n",v1,v2);
  139.         */
  140.         sprintf((char*)receiveBuffer,":%1.3f mg/",result);
  141.         OLED_ShowCHinese(0,0,4);//显示汉字甲
  142.         OLED_ShowCHinese(16,0,5);//显示汉字醛
  143.         OLED_ShowString(32,0,receiveBuffer);
  144.         OLED_ShowCHinese(112,0,10);//显示m³
  145. }

  146. void ReadDHT11(void)
  147. {
  148.         uint8_t showBuffer[32];
  149.         uint8_t temp;              
  150.     uint8_t humi;
  151.         DHT11_Read_Data(&temp,&humi);
  152.         
  153.         sprintf((char*)showBuffer,":%3d",temp);
  154.         OLED_ShowCHinese(0,2,6);//显示汉字温
  155.         OLED_ShowCHinese(16,2,8);//显示汉字度
  156.         OLED_ShowString(32,2,showBuffer);
  157.         OLED_ShowCHinese(64,2,9);//显示℃
  158.         
  159.         sprintf((char*)showBuffer,":%3d",humi);
  160.         OLED_ShowCHinese(0,4,7);//显示汉字温
  161.         OLED_ShowCHinese(16,4,8);//显示汉字度
  162.         OLED_ShowString(32,4,showBuffer);
  163.         OLED_ShowCHinese(64,4,11);//显示%
  164. }

  165. void ReadGP2Y(void)
  166. {
  167.         uint8_t showBuffer[32];
  168.         int new_pm25 = 0;
  169.         static int prev_pm25 = 0;
  170.         int last_pm25 = 0;
  171.         
  172.         GP2Y_ON;
  173.         DelayUs(280);
  174.         float adValue = GP2Y_Read();
  175.         DelayUs(40);
  176.         GP2Y_OFF;
  177.         DelayUs(9680);

  178.         
  179.         /*无尘测得电压值为0.6,手册上每0.1mg/m3 电压增加0.5V
  180.          *使用excel记算得出公式:y = 0.2x-0.12          */
  181.         new_pm25 = (0.2f*adValue-0.12f)*1000; //电压-灰尘转换
  182.         /*一阶滤波*/
  183.         last_pm25 = 0.2*new_pm25+0.8*prev_pm25;
  184.         prev_pm25 = new_pm25;
  185.         
  186.         sprintf((char*)showBuffer,"PM25:%3d   ug/",last_pm25);
  187.         OLED_ShowString(0,6,showBuffer);
  188.         OLED_ShowCHinese(112,6,10);//显示m³
  189. }

  190. ////////////////////////////////////////////////////////////////////////////////
  191. // EOF
  192. ////////////////////////////////////////////////////////////////////////////////
6、效果演示

  

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
deviceplugs 发表于 2016-1-23 20:38 | 显示全部楼层
这个甲醛检测器用的是什么型号,价格贵吗?想做一个放在遥控器中
 楼主| Ketose 发表于 2016-1-23 20:51 | 显示全部楼层
deviceplugs 发表于 2016-1-23 20:38
这个甲醛检测器用的是什么型号,价格贵吗?想做一个放在遥控器中

ZE08-CH2O模块,淘宝上100多,有点贵。
justinlin2015 发表于 2016-1-26 16:54 | 显示全部楼层
学习学习了
han0097 发表于 2018-7-14 23:45 | 显示全部楼层
不冷吗?14度
han0097 发表于 2018-7-15 20:47 | 显示全部楼层
程序不全啊,缺少很多函数。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:我最讨厌两种人:一是有种族歧视的; 二是黑人;三是不识数的!

66

主题

3258

帖子

15

粉丝
快速回复 在线客服 返回列表 返回顶部