![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Temp and Pressure
main.cpp@0:b4b35a558b4d, 2021-02-21 (annotated)
- Committer:
- srdjansimic
- Date:
- Sun Feb 21 02:07:19 2021 +0000
- Revision:
- 0:b4b35a558b4d
- Child:
- 3:ec7bc6955531
Init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
srdjansimic | 0:b4b35a558b4d | 1 | /* mbed Microcontroller Library |
srdjansimic | 0:b4b35a558b4d | 2 | * Copyright (c) 2019 ARM Limited |
srdjansimic | 0:b4b35a558b4d | 3 | * SPDX-License-Identifier: Apache-2.0 |
srdjansimic | 0:b4b35a558b4d | 4 | */ |
srdjansimic | 0:b4b35a558b4d | 5 | |
srdjansimic | 0:b4b35a558b4d | 6 | #include "mbed.h" |
srdjansimic | 0:b4b35a558b4d | 7 | |
srdjansimic | 0:b4b35a558b4d | 8 | #include <stdio.h> |
srdjansimic | 0:b4b35a558b4d | 9 | #include "BMP180.h" |
srdjansimic | 0:b4b35a558b4d | 10 | |
srdjansimic | 0:b4b35a558b4d | 11 | I2C i2c(I2C_SDA, I2C_SCL); |
srdjansimic | 0:b4b35a558b4d | 12 | BMP180 bmp180(&i2c); |
srdjansimic | 0:b4b35a558b4d | 13 | |
srdjansimic | 0:b4b35a558b4d | 14 | // Blinking rate in milliseconds |
srdjansimic | 0:b4b35a558b4d | 15 | #define BLINKING_RATE_MS 2000 |
srdjansimic | 0:b4b35a558b4d | 16 | |
srdjansimic | 0:b4b35a558b4d | 17 | DigitalOut my_led(LED1); |
srdjansimic | 0:b4b35a558b4d | 18 | DigitalOut pin_out(D8); |
srdjansimic | 0:b4b35a558b4d | 19 | InterruptIn my_button(USER_BUTTON); |
srdjansimic | 0:b4b35a558b4d | 20 | PwmOut my_pwm(D9); |
srdjansimic | 0:b4b35a558b4d | 21 | AnalogIn adc_temp(ADC_TEMP); |
srdjansimic | 0:b4b35a558b4d | 22 | |
srdjansimic | 0:b4b35a558b4d | 23 | |
srdjansimic | 0:b4b35a558b4d | 24 | void pressed() |
srdjansimic | 0:b4b35a558b4d | 25 | { |
srdjansimic | 0:b4b35a558b4d | 26 | printf("SDA %d\r\n", I2C_SDA); |
srdjansimic | 0:b4b35a558b4d | 27 | printf("SDA %d\r\n", I2C_SCL); |
srdjansimic | 0:b4b35a558b4d | 28 | if (my_pwm.read() < 1.0) { |
srdjansimic | 0:b4b35a558b4d | 29 | my_pwm.write(my_pwm.read() + 0.1); |
srdjansimic | 0:b4b35a558b4d | 30 | if (my_pwm.read() > 1.0) { |
srdjansimic | 0:b4b35a558b4d | 31 | my_pwm.write(1.0); |
srdjansimic | 0:b4b35a558b4d | 32 | printf("pwm set to 100 %\n"); |
srdjansimic | 0:b4b35a558b4d | 33 | } else { |
srdjansimic | 0:b4b35a558b4d | 34 | printf("pwm set to %.2f %%\n", my_pwm.read() * 100); |
srdjansimic | 0:b4b35a558b4d | 35 | } |
srdjansimic | 0:b4b35a558b4d | 36 | } else { |
srdjansimic | 0:b4b35a558b4d | 37 | my_pwm.write(0); |
srdjansimic | 0:b4b35a558b4d | 38 | printf("pwm set to 0 %\n"); |
srdjansimic | 0:b4b35a558b4d | 39 | } |
srdjansimic | 0:b4b35a558b4d | 40 | wait(0.1); |
srdjansimic | 0:b4b35a558b4d | 41 | return; |
srdjansimic | 0:b4b35a558b4d | 42 | } |
srdjansimic | 0:b4b35a558b4d | 43 | |
srdjansimic | 0:b4b35a558b4d | 44 | void BMP180_GetAll (int oss) |
srdjansimic | 0:b4b35a558b4d | 45 | { |
srdjansimic | 0:b4b35a558b4d | 46 | bmp180.startTemperature(); |
srdjansimic | 0:b4b35a558b4d | 47 | wait_ms(5); // Wait for conversion to complete |
srdjansimic | 0:b4b35a558b4d | 48 | float temp; |
srdjansimic | 0:b4b35a558b4d | 49 | if(bmp180.getTemperature(&temp) != 0) { |
srdjansimic | 0:b4b35a558b4d | 50 | printf("Error getting temperature\n"); |
srdjansimic | 0:b4b35a558b4d | 51 | return; |
srdjansimic | 0:b4b35a558b4d | 52 | } |
srdjansimic | 0:b4b35a558b4d | 53 | |
srdjansimic | 0:b4b35a558b4d | 54 | // bmp180.startPressure(BMP180::ULTRA_LOW_POWER); |
srdjansimic | 0:b4b35a558b4d | 55 | bmp180.startPressure(BMP180::STANDARD); |
srdjansimic | 0:b4b35a558b4d | 56 | wait_ms(10); // Wait for conversion to complete |
srdjansimic | 0:b4b35a558b4d | 57 | int pressure; |
srdjansimic | 0:b4b35a558b4d | 58 | if(bmp180.getPressure(&pressure) != 0) { |
srdjansimic | 0:b4b35a558b4d | 59 | printf("Error getting pressure\n"); |
srdjansimic | 0:b4b35a558b4d | 60 | return; |
srdjansimic | 0:b4b35a558b4d | 61 | } |
srdjansimic | 0:b4b35a558b4d | 62 | |
srdjansimic | 0:b4b35a558b4d | 63 | float altitude; |
srdjansimic | 0:b4b35a558b4d | 64 | altitude = 44330*(1-(pow(((float)pressure/101325.0), 0.19029495718))); |
srdjansimic | 0:b4b35a558b4d | 65 | |
srdjansimic | 0:b4b35a558b4d | 66 | printf("Pressure = %d Pa | Temperature = %.2f C | Altitude = %f\n", pressure, temp, altitude); |
srdjansimic | 0:b4b35a558b4d | 67 | } |
srdjansimic | 0:b4b35a558b4d | 68 | |
srdjansimic | 0:b4b35a558b4d | 69 | int main() |
srdjansimic | 0:b4b35a558b4d | 70 | { |
srdjansimic | 0:b4b35a558b4d | 71 | while(1) { |
srdjansimic | 0:b4b35a558b4d | 72 | bmp180.reset(); |
srdjansimic | 0:b4b35a558b4d | 73 | if (bmp180.init() != 0) { |
srdjansimic | 0:b4b35a558b4d | 74 | printf("Error communicating with BMP180\n"); |
srdjansimic | 0:b4b35a558b4d | 75 | } else { |
srdjansimic | 0:b4b35a558b4d | 76 | printf("Initialized BMP180\n"); |
srdjansimic | 0:b4b35a558b4d | 77 | break; |
srdjansimic | 0:b4b35a558b4d | 78 | } |
srdjansimic | 0:b4b35a558b4d | 79 | wait(1); |
srdjansimic | 0:b4b35a558b4d | 80 | } |
srdjansimic | 0:b4b35a558b4d | 81 | |
srdjansimic | 0:b4b35a558b4d | 82 | // Initialise the digital pin LED1 as an output |
srdjansimic | 0:b4b35a558b4d | 83 | printf("Blinking rate %d\r\n", BLINKING_RATE_MS); |
srdjansimic | 0:b4b35a558b4d | 84 | my_pwm.period_ms(10); |
srdjansimic | 0:b4b35a558b4d | 85 | my_pwm.write(1.0); |
srdjansimic | 0:b4b35a558b4d | 86 | float fPower = my_pwm.read() * 100L; |
srdjansimic | 0:b4b35a558b4d | 87 | printf("pwm set to %.2f\r\n", fPower); |
srdjansimic | 0:b4b35a558b4d | 88 | |
srdjansimic | 0:b4b35a558b4d | 89 | float f_Temperature; |
srdjansimic | 0:b4b35a558b4d | 90 | |
srdjansimic | 0:b4b35a558b4d | 91 | // Set button |
srdjansimic | 0:b4b35a558b4d | 92 | my_button.rise(&pressed); |
srdjansimic | 0:b4b35a558b4d | 93 | |
srdjansimic | 0:b4b35a558b4d | 94 | while (true) { |
srdjansimic | 0:b4b35a558b4d | 95 | BMP180_GetAll(0); |
srdjansimic | 0:b4b35a558b4d | 96 | |
srdjansimic | 0:b4b35a558b4d | 97 | f_Temperature = (adc_temp.read()*100); |
srdjansimic | 0:b4b35a558b4d | 98 | printf("ADC Temp = %f \r\n", f_Temperature); |
srdjansimic | 0:b4b35a558b4d | 99 | |
srdjansimic | 0:b4b35a558b4d | 100 | |
srdjansimic | 0:b4b35a558b4d | 101 | my_led = !my_led; |
srdjansimic | 0:b4b35a558b4d | 102 | pin_out = !pin_out; |
srdjansimic | 0:b4b35a558b4d | 103 | printf("Blinking rate %d\r\n", my_led); |
srdjansimic | 0:b4b35a558b4d | 104 | wait(BLINKING_RATE_MS / 1000); |
srdjansimic | 0:b4b35a558b4d | 105 | } |
srdjansimic | 0:b4b35a558b4d | 106 | } |