max32630

Dependencies:   OLED USBDevice max32630fthr

Fork of FTHR_OLED by Greg Steiert

Committer:
feilang
Date:
Wed Sep 27 04:41:40 2017 +0000
Revision:
11:bf8283c27606
Parent:
10:1f5e78dd4a7c
Child:
12:53ca22948a98
add Error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:60a522ae2e35 1 #include "mbed.h"
switches 2:57500e991166 2 #include "max32630fthr.h"
switches 8:1d48f139567d 3 #include "Adafruit_SSD1306.h"
switches 1:6923b075c8d7 4 #include "USBSerial.h"
switches 0:60a522ae2e35 5
switches 6:684c51f32c1d 6 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
switches 0:60a522ae2e35 7
feilang 11:bf8283c27606 8 I2C oledI2C(P3_4, P3_5); // SDA, SCL
switches 8:1d48f139567d 9
switches 1:6923b075c8d7 10 // Hardware serial port over DAPLink
switches 2:57500e991166 11 Serial daplink(P2_1, P2_0);
switches 1:6923b075c8d7 12
feilang 11:bf8283c27606 13 // serial port for pm1003
feilang 11:bf8283c27606 14 Serial pmSerial(P3_1, P3_0);
feilang 11:bf8283c27606 15 //for 595 drive LED bar
feilang 11:bf8283c27606 16 DigitalOut ledbarClk(P5_3);
feilang 11:bf8283c27606 17 DigitalOut ledbardat(P5_4);
feilang 11:bf8283c27606 18 DigitalOut ledbarlatch(P5_5);
feilang 11:bf8283c27606 19 void shirftDate(unsigned int date);
feilang 11:bf8283c27606 20 int airBufferPrcess(unsigned int a,unsigned int b);
switches 9:ecdad18f61c2 21
switches 9:ecdad18f61c2 22 /* Analog inputs 0 and 1 have internal dividers to allow measuring 5V signals
switches 9:ecdad18f61c2 23 * The dividers are selected by using inputs AIN_5 and AIN_5 respectively.
switches 9:ecdad18f61c2 24 * The full scale range for AIN0-3 is 1.2V
switches 9:ecdad18f61c2 25 * The full scale range for AIN4-5 is 6.0V
switches 9:ecdad18f61c2 26 */
feilang 11:bf8283c27606 27 AnalogIn fireAir(AIN_4);
feilang 11:bf8283c27606 28 AnalogIn alcohol(AIN_5);
switches 8:1d48f139567d 29
switches 0:60a522ae2e35 30 // main() runs in its own thread in the OS
switches 0:60a522ae2e35 31 // (note the calls to Thread::wait below for delays)
switches 0:60a522ae2e35 32 int main()
switches 0:60a522ae2e35 33 {
feilang 11:bf8283c27606 34 char incomeByte[32];
feilang 11:bf8283c27606 35 unsigned int pm25;
feilang 11:bf8283c27606 36 unsigned int pm1;
feilang 11:bf8283c27606 37 unsigned int pm10;
switches 2:57500e991166 38
feilang 11:bf8283c27606 39 unsigned int airDisplayDate;
switches 0:60a522ae2e35 40
feilang 11:bf8283c27606 41 daplink.printf("start \r\n");
feilang 11:bf8283c27606 42 pmSerial.baud(9600);
switches 10:1f5e78dd4a7c 43
switches 10:1f5e78dd4a7c 44 Thread::wait(50); // Give the supplies time to settle before initializing the display
feilang 11:bf8283c27606 45 Adafruit_SSD1306_I2c OLED(oledI2C);
feilang 11:bf8283c27606 46 //OLED.printf("%ux%u OLED Display\r\n", OLED.width(), OLED.height());
feilang 11:bf8283c27606 47 //OLED.printf("HelloWorld \r");
feilang 11:bf8283c27606 48 //OLED.display();
switches 8:1d48f139567d 49
feilang 11:bf8283c27606 50 daplink.printf("OLED init over \r\n");
switches 1:6923b075c8d7 51 while(1) {
switches 8:1d48f139567d 52 Thread::wait(250);
feilang 11:bf8283c27606 53
feilang 11:bf8283c27606 54 while(pmSerial.getc() !=0x42){}
feilang 11:bf8283c27606 55 if(pmSerial.getc() == 0x4d){
feilang 11:bf8283c27606 56 for(int i = 2; i < 32; i++){
feilang 11:bf8283c27606 57 incomeByte[i] = pmSerial.getc();
feilang 11:bf8283c27606 58 }
feilang 11:bf8283c27606 59
feilang 11:bf8283c27606 60 unsigned int calcsum = 0;
feilang 11:bf8283c27606 61 incomeByte[0] = 0x42;
feilang 11:bf8283c27606 62 incomeByte[1] = 0x4d;
feilang 11:bf8283c27606 63 unsigned int exptsum = (incomeByte[30]<<8) + incomeByte[31];
feilang 11:bf8283c27606 64 for(int i = 0; i < 30; i++){
feilang 11:bf8283c27606 65 calcsum += incomeByte[i];
feilang 11:bf8283c27606 66 daplink.printf("income[%d],%d\n",i,incomeByte[i]);
feilang 11:bf8283c27606 67 }
feilang 11:bf8283c27606 68 if( calcsum == exptsum)
feilang 11:bf8283c27606 69 {
feilang 11:bf8283c27606 70 daplink.printf("check ok \n");
feilang 11:bf8283c27606 71 pm1 = incomeByte[10] + incomeByte[11];
feilang 11:bf8283c27606 72 pm25 = incomeByte[12] + incomeByte[13];
feilang 11:bf8283c27606 73 pm10 = incomeByte[14] + incomeByte[15];
feilang 11:bf8283c27606 74
feilang 11:bf8283c27606 75 daplink.printf("pm1:%d\n",pm1);
feilang 11:bf8283c27606 76 daplink.printf("pm2.5:%d\n",pm25);
feilang 11:bf8283c27606 77 daplink.printf("pm10:%d\n",pm10);
feilang 11:bf8283c27606 78
feilang 11:bf8283c27606 79 }
feilang 11:bf8283c27606 80
switches 8:1d48f139567d 81 }
feilang 11:bf8283c27606 82
feilang 11:bf8283c27606 83 Thread::wait(10);
feilang 11:bf8283c27606 84 OLED.clearDisplay();
feilang 11:bf8283c27606 85 OLED.setTextCursor(0,0);
feilang 11:bf8283c27606 86 OLED.printf("MAX32630FTHR OLED\n");
feilang 11:bf8283c27606 87 OLED.printf("fireAir: %1.2f\n", (6.0f * fireAir) ); // fireAir inputs 4
feilang 11:bf8283c27606 88 OLED.printf("alcohol: %1.2f\n", (1.2f * alcohol) ); // alcohol inputs 5
feilang 11:bf8283c27606 89 OLED.printf("PM2.5: %d \n", pm25 ); // alcohol inputs 5
feilang 11:bf8283c27606 90 OLED.display();
feilang 11:bf8283c27606 91 airDisplayDate = airBufferPrcess( fireAir, alcohol );
feilang 11:bf8283c27606 92 shirftDate(airDisplayDate);
feilang 11:bf8283c27606 93
feilang 11:bf8283c27606 94 while((pm25 > 100)||(fireAir > 0x08)||(alcohol > 0x08)){
feilang 11:bf8283c27606 95
feilang 11:bf8283c27606 96 OLED.clearDisplay();
feilang 11:bf8283c27606 97 Thread::wait(500);
feilang 11:bf8283c27606 98 OLED.printf("ERROR");
feilang 11:bf8283c27606 99 OLED.display();
feilang 11:bf8283c27606 100 Thread::wait(500);
feilang 11:bf8283c27606 101 }
feilang 11:bf8283c27606 102
feilang 11:bf8283c27606 103
switches 0:60a522ae2e35 104 }
switches 0:60a522ae2e35 105 }
switches 0:60a522ae2e35 106
feilang 11:bf8283c27606 107
feilang 11:bf8283c27606 108
feilang 11:bf8283c27606 109
feilang 11:bf8283c27606 110
feilang 11:bf8283c27606 111 /*传入LB数据,共24位取低位24*/
feilang 11:bf8283c27606 112 /*低20位用于两个LED bar*/
feilang 11:bf8283c27606 113 void shirftDate(unsigned int date){
feilang 11:bf8283c27606 114 for( int i = 0; i < 20; i++ ){
feilang 11:bf8283c27606 115 if( date & 0x200 ){
feilang 11:bf8283c27606 116 ledbardat = 1;
feilang 11:bf8283c27606 117
feilang 11:bf8283c27606 118 }else{
feilang 11:bf8283c27606 119 ledbardat = 0;
feilang 11:bf8283c27606 120 }
feilang 11:bf8283c27606 121
feilang 11:bf8283c27606 122 ledbarClk = 1;
feilang 11:bf8283c27606 123 ledbarClk = 0;
feilang 11:bf8283c27606 124 date <<= 1;
feilang 11:bf8283c27606 125 }
feilang 11:bf8283c27606 126 /*补充4位空白*/
feilang 11:bf8283c27606 127 for(int i=4;i>0;i--){
feilang 11:bf8283c27606 128 ledbardat = 0;
feilang 11:bf8283c27606 129 ledbarClk = 1;
feilang 11:bf8283c27606 130 ledbarClk = 0;
feilang 11:bf8283c27606 131 }
feilang 11:bf8283c27606 132
feilang 11:bf8283c27606 133 ledbarlatch = 1;
feilang 11:bf8283c27606 134
feilang 11:bf8283c27606 135 }
feilang 11:bf8283c27606 136 //空气模拟数据处理,a,b分别表示 CO与acohol;
feilang 11:bf8283c27606 137 //采用对数级浓度增加对应一位,
feilang 11:bf8283c27606 138 int airBufferPrcess(unsigned int a,unsigned int b)
feilang 11:bf8283c27606 139 {
feilang 11:bf8283c27606 140 unsigned int _ProcessDate[3]={1,1,0};
feilang 11:bf8283c27606 141 for(int i=0;i<10;i++){
feilang 11:bf8283c27606 142 a >>= 1;
feilang 11:bf8283c27606 143 b >>= 1;
feilang 11:bf8283c27606 144 if(a){
feilang 11:bf8283c27606 145 _ProcessDate[0] <<= 1;
feilang 11:bf8283c27606 146 _ProcessDate[0]++;
feilang 11:bf8283c27606 147 }
feilang 11:bf8283c27606 148 if(b){
feilang 11:bf8283c27606 149 _ProcessDate[1] <<= 1;
feilang 11:bf8283c27606 150 _ProcessDate[1]++;
feilang 11:bf8283c27606 151 }
feilang 11:bf8283c27606 152 _ProcessDate[2] = ( _ProcessDate[0] << 10);
feilang 11:bf8283c27606 153 _ProcessDate[2] += _ProcessDate[1];
feilang 11:bf8283c27606 154 }
feilang 11:bf8283c27606 155
feilang 11:bf8283c27606 156 return _ProcessDate[2];
feilang 11:bf8283c27606 157 }