- #pyb.delay(5000)
- SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK
- #DIN =>SPI(1).MOSI 'X8' data flow (Master out, Slave in)
- #CLK =>SPI(1).SCK 'X6' SPI clock
- RST = pyb.Pin('Y10')
- CE = pyb.Pin('Y11')
- DC = pyb.Pin('Y9')
- LIGHT = pyb.Pin('Y12')
- while True:
- u2 = UART(2, 2400)
- pyb.delay(1000)
- #print('kaishi')
- u2.deinit()
- pyb.delay(10)
- if(u2.any()>0):
- W=1
- _dataRead=u2.readall()
- #print('_dataRead=',_dataRead)
- R=0
- while (W>0):
- #print('截取开始')
- T=_dataRead[R]
- if(T==170):
- E=R+1
- F=R+2
- #R=_dataRead[65]
- #print('十位 =',_dataRead[E])
- #print('个位 =',_dataRead[F])
- W=0
- R=R+1
- P=_dataRead[E]
- L=_dataRead[F]
- SHI=P*256+L# 把串口收到的十六进制数据转换成十进制
- SHUCHU=SHI/G*A
- if(SHUCHU<35):
- Quality = 'Excellente'
- print('环境质量: 优','PM2.5=',SHUCHU)
- pyb.LED(1).off()
- pyb.LED(2).on()
- pyb.LED(3).off()
- pyb.LED(4).off()
- elif(35<SHUCHU<75):
- Quality = 'Good'
- print('环境质量: 良好','PM2.5=',SHUCHU)
- pyb.LED(1).off()
- pyb.LED(2).on()
- pyb.LED(3).off()
- pyb.LED(4).off()
- elif(75<SHUCHU<115):
- Quality = 'Slightly-polluted'
- print('环境质量: 轻度污染','PM2.5=',SHUCHU)
- pyb.LED(1).off()
- pyb.LED(2).off()
- pyb.LED(3).on()
- pyb.LED(4).off()
- elif(115<SHUCHU<150):
- Quality = 'Medium pollution'
- print('环境质量: 中度污染','PM2.5=',SHUCHU)
- pyb.LED(1).off()
- pyb.LED(2).off()
- pyb.LED(3).on()
- pyb.LED(4).off()
- elif(150<SHUCHU<250):
- Quality = 'Heavy pollution'
- print('环境质量: 重度污染','PM2.5=',SHUCHU)
- pyb.LED(1).on()
- pyb.LED(2).off()
- pyb.LED(3).off()
- pyb.LED(4).off()
- elif(250<SHUCHU):
- Quality = 'Serious pollution'
- print('环境质量: 严重污染','PM2.5=',SHUCHU)
- pyb.LED(1).on()
- pyb.LED(2).off()
- pyb.LED(3).on()
- pyb.LED(4).off()
- lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
- lcd_5110.lcd_write_string('AQI Level',0,0)
- lcd_5110.lcd_write_string(str(Quality),0,1)
- lcd_5110.lcd_write_string('PM2.5:',0,2)
- lcd_5110.lcd_write_string(str(SHUCHU),0,3)
来源: https://www.cnblogs.com/xxosu/p/8487396.html