test code for hdc1080 humidity sensor
Fork of Nucleo-F303K8-SSD1306_OLED by
main.cpp@2:dc3e84d595c3, 2016-03-21 (annotated)
- Committer:
- joeata2wh
- Date:
- Mon Mar 21 03:42:56 2016 +0000
- Revision:
- 2:dc3e84d595c3
- Parent:
- 1:90d213185462
- Child:
- 3:47148198f5f2
got it working with the adafruit library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joeata2wh | 1:90d213185462 | 1 | /* Example of Reading all the ADC pins PIN and display ot OLED display using |
joeata2wh | 2:dc3e84d595c3 | 2 | Nucleo_F303K8 by Joseph Ellsworth CTO A2WH - Free for all but no warranty, no promises |
joeata2wh | 2:dc3e84d595c3 | 3 | Displays voltage read from first 2 ADC lines on OLED display |
joeata2wh | 2:dc3e84d595c3 | 4 | |
joeata2wh | 2:dc3e84d595c3 | 5 | Used to Drive: |
joeata2wh | 1:90d213185462 | 6 | ebay part http://www.ebay.com/itm/152000005331?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT |
joeata2wh | 1:90d213185462 | 7 | 0-96-I2C-IIC-SPI-Serial-White-OLED-LCD-LED-Display-Module-128X64 |
joeata2wh | 1:90d213185462 | 8 | The Datasheet. https://www.adafruit.com/datasheets/SSD1306.pdf |
joeata2wh | 2:dc3e84d595c3 | 9 | |
joeata2wh | 1:90d213185462 | 10 | Unfortunately this part never turns on using the SSD1308 library. |
joeata2wh | 2:dc3e84d595c3 | 11 | but did find that the https://developer.mbed.org/users/nkhorman/code/Adafruit_GFX/ |
joeata2wh | 2:dc3e84d595c3 | 12 | library works. Unfortunately the Adafruit library doesn't include the scroll functionality. |
joeata2wh | 2:dc3e84d595c3 | 13 | |
joeata2wh | 1:90d213185462 | 14 | Using my I2C address scanner I found that it responds on Added 120 (x78) ,121 (x79) |
joeata2wh | 2:dc3e84d595c3 | 15 | and when the part was unsoldered nothing responded on 120 or 121. According to |
joeata2wh | 1:90d213185462 | 16 | to the page #19 (8.1.5) of the data sheet the I2C address should be 011110 |
joeata2wh | 2:dc3e84d595c3 | 17 | which seems to map correctly to dec=120 hex=79 |
joeata2wh | 2:dc3e84d595c3 | 18 | |
joeata2wh | 1:90d213185462 | 19 | */ |
joeata2wh | 0:fa185766e039 | 20 | |
joeata2wh | 0:fa185766e039 | 21 | #include "mbed.h" |
joeata2wh | 2:dc3e84d595c3 | 22 | //#include "SSD1308.h" |
joeata2wh | 2:dc3e84d595c3 | 23 | #include "Adafruit_SSD1306.h" |
joeata2wh | 0:fa185766e039 | 24 | |
joeata2wh | 1:90d213185462 | 25 | //#include "mbed_logo.h" |
joeata2wh | 2:dc3e84d595c3 | 26 | |
joeata2wh | 2:dc3e84d595c3 | 27 | //Pin Defines for I2C Bus |
joeata2wh | 2:dc3e84d595c3 | 28 | #define D_SDA PB_7 // specific for Nucleo-F303K8 |
joeata2wh | 2:dc3e84d595c3 | 29 | #define D_SCL PB_6 // specific for Nucleo-F303K8 |
joeata2wh | 2:dc3e84d595c3 | 30 | I2C i2c(D_SDA, D_SCL); |
joeata2wh | 2:dc3e84d595c3 | 31 | |
joeata2wh | 2:dc3e84d595c3 | 32 | // Host PC Communication channels |
joeata2wh | 2:dc3e84d595c3 | 33 | Serial pc(USBTX, USBRX); // tx, rx |
joeata2wh | 2:dc3e84d595c3 | 34 | |
joeata2wh | 2:dc3e84d595c3 | 35 | AnalogIn pa0(PA_0); |
joeata2wh | 2:dc3e84d595c3 | 36 | AnalogIn pa1(PA_1); |
joeata2wh | 2:dc3e84d595c3 | 37 | |
joeata2wh | 0:fa185766e039 | 38 | DigitalOut myled(LED1); |
joeata2wh | 0:fa185766e039 | 39 | const float voltMeterARef = 3.3; |
joeata2wh | 0:fa185766e039 | 40 | |
joeata2wh | 2:dc3e84d595c3 | 41 | // an I2C sub-class that provides a constructed default |
joeata2wh | 2:dc3e84d595c3 | 42 | class I2CPreInit : public I2C |
joeata2wh | 2:dc3e84d595c3 | 43 | { |
joeata2wh | 2:dc3e84d595c3 | 44 | public: |
joeata2wh | 2:dc3e84d595c3 | 45 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) { |
joeata2wh | 2:dc3e84d595c3 | 46 | frequency(400000); |
joeata2wh | 2:dc3e84d595c3 | 47 | start(); |
joeata2wh | 2:dc3e84d595c3 | 48 | }; |
joeata2wh | 2:dc3e84d595c3 | 49 | }; |
joeata2wh | 0:fa185766e039 | 50 | |
joeata2wh | 2:dc3e84d595c3 | 51 | |
joeata2wh | 2:dc3e84d595c3 | 52 | I2CPreInit gI2C(D_SDA,D_SCL); |
joeata2wh | 2:dc3e84d595c3 | 53 | Adafruit_SSD1306_I2c gOled2(gI2C,PB_5); |
joeata2wh | 1:90d213185462 | 54 | |
joeata2wh | 0:fa185766e039 | 55 | |
joeata2wh | 2:dc3e84d595c3 | 56 | float readPrint(AnalogIn ain, char *label) |
joeata2wh | 2:dc3e84d595c3 | 57 | { |
joeata2wh | 2:dc3e84d595c3 | 58 | float tval = ain.read(); |
joeata2wh | 2:dc3e84d595c3 | 59 | float volts = tval * voltMeterARef; |
joeata2wh | 2:dc3e84d595c3 | 60 | float perc = tval * 100.0; |
joeata2wh | 2:dc3e84d595c3 | 61 | unsigned short tvalu16 = ain.read_u16 (); |
joeata2wh | 2:dc3e84d595c3 | 62 | printf("adc %s R=%3.3f V=%3.3f%% U16=%u\r\n",label, tval, volts, tvalu16); |
joeata2wh | 2:dc3e84d595c3 | 63 | gOled2.printf("%s=%3.3fV\r\n",label,volts); |
joeata2wh | 2:dc3e84d595c3 | 64 | gOled2.display(); |
joeata2wh | 2:dc3e84d595c3 | 65 | return tval; |
joeata2wh | 2:dc3e84d595c3 | 66 | } |
joeata2wh | 2:dc3e84d595c3 | 67 | |
joeata2wh | 2:dc3e84d595c3 | 68 | |
joeata2wh | 2:dc3e84d595c3 | 69 | int main() |
joeata2wh | 2:dc3e84d595c3 | 70 | { |
joeata2wh | 2:dc3e84d595c3 | 71 | // Display with the Adafruit Library |
joeata2wh | 2:dc3e84d595c3 | 72 | gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height()); |
joeata2wh | 2:dc3e84d595c3 | 73 | wait(5); |
joeata2wh | 2:dc3e84d595c3 | 74 | gOled2.clearDisplay(); |
joeata2wh | 0:fa185766e039 | 75 | while(1) { |
joeata2wh | 2:dc3e84d595c3 | 76 | myled = !myled; |
joeata2wh | 2:dc3e84d595c3 | 77 | gOled2.clearDisplay(); |
joeata2wh | 2:dc3e84d595c3 | 78 | gOled2.setTextCursor(1,1); |
joeata2wh | 2:dc3e84d595c3 | 79 | readPrint(pa0, "PA_0"); |
joeata2wh | 2:dc3e84d595c3 | 80 | wait(0.1); |
joeata2wh | 2:dc3e84d595c3 | 81 | gOled2.setTextCursor(1,10); |
joeata2wh | 2:dc3e84d595c3 | 82 | readPrint(pa1, "PA_1"); |
joeata2wh | 2:dc3e84d595c3 | 83 | wait(3.0); |
joeata2wh | 0:fa185766e039 | 84 | } |
joeata2wh | 2:dc3e84d595c3 | 85 | |
joeata2wh | 2:dc3e84d595c3 | 86 | |
joeata2wh | 0:fa185766e039 | 87 | } |