bme280, nRF51822 internal temperature, oled ssd1306 128x64. Короче сборный тест.

Dependencies:   mbed SSD1306_128x64_I2C BLE_API nRF51822 Buzzer

Committer:
mamont090671
Date:
Sat Nov 30 09:03:25 2019 +0000
Revision:
4:606e79b9a9ce
Parent:
2:4700f7e71737
bme280, nRF5188, ble400, ssd1306_i2c_128x64, buzzer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mamont090671 2:4700f7e71737 1 #include "mbed.h"
mamont090671 2:4700f7e71737 2 #include "SSD1306.h"
mamont090671 2:4700f7e71737 3 #include "bme280.h"
mamont090671 2:4700f7e71737 4 #include "stdlib.h"
mamont090671 2:4700f7e71737 5 #include "mbed_logo.h"
mamont090671 4:606e79b9a9ce 6 #include "mammoth.h"
mamont090671 2:4700f7e71737 7 //nrf51822 temperature
mamont090671 2:4700f7e71737 8 #include "nrf.h"
mamont090671 2:4700f7e71737 9 #include "nrf_temp.h"
mamont090671 4:606e79b9a9ce 10 //buzzer test
mamont090671 4:606e79b9a9ce 11 #include "beep.h"
mamont090671 2:4700f7e71737 12
mamont090671 2:4700f7e71737 13 //Присваиваем пины для i2c
mamont090671 2:4700f7e71737 14 #define D_SDA P0_0
mamont090671 2:4700f7e71737 15 #define D_SCL P0_1
mamont090671 2:4700f7e71737 16
mamont090671 2:4700f7e71737 17 //Функции обработки событий кнопок
mamont090671 2:4700f7e71737 18 void triggerfall_1(); //Button1 falling interrupt function
mamont090671 2:4700f7e71737 19 void triggerrise_1(); //Button1 rising interrupt function
mamont090671 2:4700f7e71737 20 void triggerfall_2(); //Button2 falling interrupt function
mamont090671 2:4700f7e71737 21 void triggerrise_2(); //Button2 rising interrupt function
mamont090671 2:4700f7e71737 22 //глобальные переменные для bme280
mamont090671 2:4700f7e71737 23 float t; //температура градусы цельсия
mamont090671 2:4700f7e71737 24 float pr; //давление мм ртутного столба
mamont090671 2:4700f7e71737 25 int h; //влажность %
mamont090671 4:606e79b9a9ce 26
mamont090671 4:606e79b9a9ce 27 int flag;
mamont090671 4:606e79b9a9ce 28
mamont090671 4:606e79b9a9ce 29 //buzzer test
mamont090671 4:606e79b9a9ce 30 Beep buzzer(P0_2);
mamont090671 4:606e79b9a9ce 31
mamont090671 2:4700f7e71737 32 //адрес дисплея
mamont090671 2:4700f7e71737 33 uint8_t address = 0x78;
mamont090671 2:4700f7e71737 34 //инициализация i2c
mamont090671 2:4700f7e71737 35 I2C i2c(D_SDA, D_SCL);
mamont090671 2:4700f7e71737 36
mamont090671 2:4700f7e71737 37 SSD1306 oled(i2c, address);
mamont090671 2:4700f7e71737 38 BME280 bme280;
mamont090671 2:4700f7e71737 39
mamont090671 2:4700f7e71737 40 //Initiate IO
mamont090671 2:4700f7e71737 41 DigitalOut led0(LED1);
mamont090671 2:4700f7e71737 42 DigitalOut led1(LED2);
mamont090671 2:4700f7e71737 43 DigitalOut led2(P0_20);
mamont090671 2:4700f7e71737 44 DigitalOut led3(P0_21);
mamont090671 2:4700f7e71737 45 DigitalOut led4(P0_22, 0);
mamont090671 2:4700f7e71737 46 DigitalIn sw1(BUTTON1);
mamont090671 2:4700f7e71737 47 DigitalIn sw2(BUTTON2);
mamont090671 2:4700f7e71737 48
mamont090671 2:4700f7e71737 49 //Initiate input interrupts
mamont090671 2:4700f7e71737 50 InterruptIn sw1Press(BUTTON1);
mamont090671 2:4700f7e71737 51 InterruptIn sw2Press(BUTTON2);
franni 0:3b87797f65fc 52
mamont090671 2:4700f7e71737 53 //bme280
mamont090671 2:4700f7e71737 54 void i2cWrite(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size, uint8_t repeated_start)
mamont090671 2:4700f7e71737 55 {
mamont090671 2:4700f7e71737 56 // mbed uses 8-bit addresses, always confusing.
mamont090671 2:4700f7e71737 57 i2c.write(i2c_address<<1,(const char *)p_data,data_size,repeated_start);
mamont090671 2:4700f7e71737 58 }
mamont090671 2:4700f7e71737 59
mamont090671 2:4700f7e71737 60 void i2cRead(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size)
mamont090671 2:4700f7e71737 61 {
mamont090671 2:4700f7e71737 62 // mbed uses 8-bit addresses, always confusing.
mamont090671 2:4700f7e71737 63 i2c.read(i2c_address<<1,(char *)p_data,data_size);
mamont090671 2:4700f7e71737 64 }
mamont090671 2:4700f7e71737 65
mamont090671 2:4700f7e71737 66 //ssd1306
mamont090671 2:4700f7e71737 67 void Start_page()
mamont090671 2:4700f7e71737 68 {
mamont090671 2:4700f7e71737 69 int i=0;
mamont090671 2:4700f7e71737 70 oled.writeString(1, 3, "Fight Robot");
mamont090671 2:4700f7e71737 71 while(i<100) {
mamont090671 2:4700f7e71737 72 oled.writeProgressBar(20,40,i);
mamont090671 2:4700f7e71737 73 i++;
mamont090671 2:4700f7e71737 74 wait_ms(10);
mamont090671 2:4700f7e71737 75 }
mamont090671 2:4700f7e71737 76 i = 0;
mamont090671 4:606e79b9a9ce 77 // oled.writeBitmap((uint8_t*) mbed_logo);
mamont090671 4:606e79b9a9ce 78 oled.writeBitmap((uint8_t*) mammoth);
mamont090671 4:606e79b9a9ce 79 while(i<100) {
mamont090671 2:4700f7e71737 80 i++;
mamont090671 2:4700f7e71737 81 wait_ms(10);
mamont090671 2:4700f7e71737 82 }
mamont090671 2:4700f7e71737 83 }
mamont090671 2:4700f7e71737 84 void Display_Meteo(float t, float pr, int h)
mamont090671 2:4700f7e71737 85 {
mamont090671 2:4700f7e71737 86 // sprintf(buf, "%f", t);
mamont090671 2:4700f7e71737 87 oled.writeString(1, 2, "T: ");
mamont090671 2:4700f7e71737 88 oled.printf("%.2f",t);
mamont090671 2:4700f7e71737 89 oled.printf("%s"," C");
mamont090671 2:4700f7e71737 90 oled.writeString(3, 2, "P: ");
mamont090671 2:4700f7e71737 91 oled.printf("%.1f",pr);
mamont090671 2:4700f7e71737 92 oled.printf("%s"," mmHg");
mamont090671 2:4700f7e71737 93 oled.writeString(5, 2, "H: ");
mamont090671 2:4700f7e71737 94 oled.printf("%d",h);
mamont090671 2:4700f7e71737 95 oled.printf("%s"," %");
mamont090671 2:4700f7e71737 96 }
mamont090671 2:4700f7e71737 97
mamont090671 2:4700f7e71737 98 int main()
mamont090671 2:4700f7e71737 99 {
mamont090671 4:606e79b9a9ce 100 flag = 0;
mamont090671 2:4700f7e71737 101 //nrf51822 temperature
mamont090671 2:4700f7e71737 102 float temp;
mamont090671 2:4700f7e71737 103 nrf_temp_init();
mamont090671 4:606e79b9a9ce 104
mamont090671 2:4700f7e71737 105 //Initialise LED output
mamont090671 2:4700f7e71737 106 led0=0;
mamont090671 2:4700f7e71737 107 led1=0;
mamont090671 2:4700f7e71737 108 led2=0;
mamont090671 2:4700f7e71737 109 led3=0;
mamont090671 4:606e79b9a9ce 110
mamont090671 2:4700f7e71737 111 //Set falling and rising edge to apppropriate interrup function
mamont090671 2:4700f7e71737 112 sw1Press.fall(&triggerfall_1);
mamont090671 2:4700f7e71737 113 sw1Press.rise(&triggerrise_1);
mamont090671 2:4700f7e71737 114 sw2Press.fall(&triggerfall_2);
mamont090671 2:4700f7e71737 115 sw2Press.rise(&triggerrise_2);
mamont090671 2:4700f7e71737 116
mamont090671 2:4700f7e71737 117 bme280.begin(BME280_I2C_ADDRESS1);
mamont090671 2:4700f7e71737 118 // Configure for test purposes.
mamont090671 2:4700f7e71737 119 bme280.writeConfigRegister(BME280_STANDBY_500_US,BME280_FILTER_OFF,0);
mamont090671 2:4700f7e71737 120 bme280.writeControlRegisters(BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_MODE_NORMAL);
mamont090671 2:4700f7e71737 121 Start_page();
mamont090671 2:4700f7e71737 122 oled.clearDisplay();
mamont090671 2:4700f7e71737 123
mamont090671 2:4700f7e71737 124 while(1) {
mamont090671 2:4700f7e71737 125 //nrf51822 temperature
mamont090671 2:4700f7e71737 126 NRF_TEMP->TASKS_START = 1; /* Start the temperature measurement */
mamont090671 4:606e79b9a9ce 127 while ((NRF_TEMP->EVENTS_DATARDY & TEMP_INTENSET_DATARDY_Msk) != (TEMP_INTENSET_DATARDY_Set << TEMP_INTENSET_DATARDY_Pos)) {}
mamont090671 2:4700f7e71737 128 NRF_TEMP->EVENTS_DATARDY = 0;
mamont090671 2:4700f7e71737 129 temp = (nrf_temp_read()/4);
mamont090671 2:4700f7e71737 130 NRF_TEMP->TASKS_STOP = 1; /* Stop the temperature measurement */
mamont090671 4:606e79b9a9ce 131
mamont090671 2:4700f7e71737 132 led4=!led4;
mamont090671 2:4700f7e71737 133 bme280.read();
mamont090671 2:4700f7e71737 134 t = bme280.temperature();
mamont090671 2:4700f7e71737 135 float p = bme280.pressure()/100;
mamont090671 2:4700f7e71737 136 pr = p*0.750062;
mamont090671 2:4700f7e71737 137 h = bme280.humidity();
mamont090671 4:606e79b9a9ce 138 if(flag == 0) {
mamont090671 4:606e79b9a9ce 139 Display_Meteo(t, pr, h);
franni 0:3b87797f65fc 140
mamont090671 4:606e79b9a9ce 141 oled.writeString(7, 2, "T_nRF: ");
mamont090671 4:606e79b9a9ce 142 oled.printf("%.1f",temp);
mamont090671 4:606e79b9a9ce 143 }
mamont090671 2:4700f7e71737 144 wait_ms(1000);
mamont090671 2:4700f7e71737 145 }
mamont090671 2:4700f7e71737 146 }
mamont090671 2:4700f7e71737 147 //Button1 falling interrupt function
mamont090671 2:4700f7e71737 148 void triggerfall_1()
mamont090671 2:4700f7e71737 149 {
mamont090671 2:4700f7e71737 150 //Toggle LED1
mamont090671 2:4700f7e71737 151 led0=!led0;
mamont090671 4:606e79b9a9ce 152 if(flag == 0) {
mamont090671 4:606e79b9a9ce 153 oled.clearDisplay();
mamont090671 4:606e79b9a9ce 154 flag = 1;
mamont090671 4:606e79b9a9ce 155 wait_ms(10);
mamont090671 4:606e79b9a9ce 156 oled.writeBitmap((uint8_t*) mammoth);
mamont090671 4:606e79b9a9ce 157 }
mamont090671 2:4700f7e71737 158 }
mamont090671 4:606e79b9a9ce 159
mamont090671 2:4700f7e71737 160 //Button1 rising interrupt function
mamont090671 2:4700f7e71737 161 void triggerrise_1()
mamont090671 2:4700f7e71737 162 {
mamont090671 2:4700f7e71737 163 //Toggle LED2
mamont090671 2:4700f7e71737 164 led1=!led1;
mamont090671 4:606e79b9a9ce 165 //buzzer test
mamont090671 4:606e79b9a9ce 166 buzzer.beep(1000,0.5);
mamont090671 4:606e79b9a9ce 167 // oled.writeBitmap((uint8_t*) mammoth);
mamont090671 2:4700f7e71737 168 }
mamont090671 2:4700f7e71737 169 //Button1 falling interrupt function
mamont090671 2:4700f7e71737 170 void triggerfall_2()
mamont090671 2:4700f7e71737 171 {
mamont090671 2:4700f7e71737 172 //Toggle LED3
mamont090671 2:4700f7e71737 173 led2=!led2;
mamont090671 4:606e79b9a9ce 174 if(flag == 1) {
mamont090671 4:606e79b9a9ce 175 oled.clearDisplay();
mamont090671 4:606e79b9a9ce 176 flag = 0;
mamont090671 4:606e79b9a9ce 177 }
mamont090671 2:4700f7e71737 178 }
mamont090671 4:606e79b9a9ce 179
mamont090671 2:4700f7e71737 180 //Button1 rising interrupt function
mamont090671 2:4700f7e71737 181 void triggerrise_2()
mamont090671 2:4700f7e71737 182 {
mamont090671 2:4700f7e71737 183 //Toggle LED4
mamont090671 2:4700f7e71737 184 led3=!led3;
mamont090671 2:4700f7e71737 185 }