Test code attempting to drive a Ebay SSD1306 128X65 LCD from STM F303K8. The chip responds my I2C address scanner but the LCD never turns on or displays anything.
Dependencies: SSD1308_128x64_I2C mbed Adafruit_GFX
Fork of Nucleo-F303K8-ADC-TEST by
Update of sample using Adafruit library to drive a generic Ebay SSD1306 OLED display. On Nucleo-F303K8 it will display reading of first two ADC lines once every couple seconds. Should work with other CPU boards.
main.cpp@3:47148198f5f2, 2016-03-30 (annotated)
- Committer:
- joeata2wh
- Date:
- Wed Mar 30 14:49:21 2016 +0000
- Revision:
- 3:47148198f5f2
- Parent:
- 2:dc3e84d595c3
update to use MIT license
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 | 3:47148198f5f2 | 2 | Nucleo_F303K8. Displays voltage read from first 2 ADC lines on OLED display |
joeata2wh | 2:dc3e84d595c3 | 3 | |
joeata2wh | 3:47148198f5f2 | 4 | By Joseph Ellsworth CTO of A2WH |
joeata2wh | 3:47148198f5f2 | 5 | Take a look at A2WH.com Producing Water from Air using Solar Energy |
joeata2wh | 3:47148198f5f2 | 6 | March-2016 License: https://developer.mbed.org/handbook/MIT-Licence |
joeata2wh | 3:47148198f5f2 | 7 | Please contact us http://a2wh.com for help with custom design projects. |
joeata2wh | 3:47148198f5f2 | 8 | |
joeata2wh | 3:47148198f5f2 | 9 | |
joeata2wh | 2:dc3e84d595c3 | 10 | Used to Drive: |
joeata2wh | 1:90d213185462 | 11 | ebay part http://www.ebay.com/itm/152000005331?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT |
joeata2wh | 1:90d213185462 | 12 | 0-96-I2C-IIC-SPI-Serial-White-OLED-LCD-LED-Display-Module-128X64 |
joeata2wh | 1:90d213185462 | 13 | The Datasheet. https://www.adafruit.com/datasheets/SSD1306.pdf |
joeata2wh | 2:dc3e84d595c3 | 14 | |
joeata2wh | 1:90d213185462 | 15 | Unfortunately this part never turns on using the SSD1308 library. |
joeata2wh | 2:dc3e84d595c3 | 16 | but did find that the https://developer.mbed.org/users/nkhorman/code/Adafruit_GFX/ |
joeata2wh | 2:dc3e84d595c3 | 17 | library works. Unfortunately the Adafruit library doesn't include the scroll functionality. |
joeata2wh | 2:dc3e84d595c3 | 18 | |
joeata2wh | 1:90d213185462 | 19 | Using my I2C address scanner I found that it responds on Added 120 (x78) ,121 (x79) |
joeata2wh | 2:dc3e84d595c3 | 20 | and when the part was unsoldered nothing responded on 120 or 121. According to |
joeata2wh | 1:90d213185462 | 21 | to the page #19 (8.1.5) of the data sheet the I2C address should be 011110 |
joeata2wh | 2:dc3e84d595c3 | 22 | which seems to map correctly to dec=120 hex=79 |
joeata2wh | 2:dc3e84d595c3 | 23 | |
joeata2wh | 1:90d213185462 | 24 | */ |
joeata2wh | 0:fa185766e039 | 25 | |
joeata2wh | 0:fa185766e039 | 26 | #include "mbed.h" |
joeata2wh | 2:dc3e84d595c3 | 27 | //#include "SSD1308.h" |
joeata2wh | 2:dc3e84d595c3 | 28 | #include "Adafruit_SSD1306.h" |
joeata2wh | 0:fa185766e039 | 29 | |
joeata2wh | 1:90d213185462 | 30 | //#include "mbed_logo.h" |
joeata2wh | 2:dc3e84d595c3 | 31 | |
joeata2wh | 2:dc3e84d595c3 | 32 | //Pin Defines for I2C Bus |
joeata2wh | 2:dc3e84d595c3 | 33 | #define D_SDA PB_7 // specific for Nucleo-F303K8 |
joeata2wh | 2:dc3e84d595c3 | 34 | #define D_SCL PB_6 // specific for Nucleo-F303K8 |
joeata2wh | 2:dc3e84d595c3 | 35 | I2C i2c(D_SDA, D_SCL); |
joeata2wh | 2:dc3e84d595c3 | 36 | |
joeata2wh | 2:dc3e84d595c3 | 37 | // Host PC Communication channels |
joeata2wh | 2:dc3e84d595c3 | 38 | Serial pc(USBTX, USBRX); // tx, rx |
joeata2wh | 2:dc3e84d595c3 | 39 | |
joeata2wh | 2:dc3e84d595c3 | 40 | AnalogIn pa0(PA_0); |
joeata2wh | 2:dc3e84d595c3 | 41 | AnalogIn pa1(PA_1); |
joeata2wh | 3:47148198f5f2 | 42 | PwmOut fan1(D11); |
joeata2wh | 3:47148198f5f2 | 43 | //PwmOut fan2(D12); |
joeata2wh | 3:47148198f5f2 | 44 | //PwmOut fan3(PA_2); |
joeata2wh | 3:47148198f5f2 | 45 | DigitalOut fan2x(D12); |
joeata2wh | 2:dc3e84d595c3 | 46 | |
joeata2wh | 0:fa185766e039 | 47 | DigitalOut myled(LED1); |
joeata2wh | 0:fa185766e039 | 48 | const float voltMeterARef = 3.3; |
joeata2wh | 0:fa185766e039 | 49 | |
joeata2wh | 2:dc3e84d595c3 | 50 | // an I2C sub-class that provides a constructed default |
joeata2wh | 2:dc3e84d595c3 | 51 | class I2CPreInit : public I2C |
joeata2wh | 2:dc3e84d595c3 | 52 | { |
joeata2wh | 2:dc3e84d595c3 | 53 | public: |
joeata2wh | 2:dc3e84d595c3 | 54 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) { |
joeata2wh | 2:dc3e84d595c3 | 55 | frequency(400000); |
joeata2wh | 2:dc3e84d595c3 | 56 | start(); |
joeata2wh | 2:dc3e84d595c3 | 57 | }; |
joeata2wh | 2:dc3e84d595c3 | 58 | }; |
joeata2wh | 0:fa185766e039 | 59 | |
joeata2wh | 2:dc3e84d595c3 | 60 | |
joeata2wh | 2:dc3e84d595c3 | 61 | I2CPreInit gI2C(D_SDA,D_SCL); |
joeata2wh | 2:dc3e84d595c3 | 62 | Adafruit_SSD1306_I2c gOled2(gI2C,PB_5); |
joeata2wh | 1:90d213185462 | 63 | |
joeata2wh | 0:fa185766e039 | 64 | |
joeata2wh | 2:dc3e84d595c3 | 65 | float readPrint(AnalogIn ain, char *label) |
joeata2wh | 2:dc3e84d595c3 | 66 | { |
joeata2wh | 2:dc3e84d595c3 | 67 | float tval = ain.read(); |
joeata2wh | 2:dc3e84d595c3 | 68 | float volts = tval * voltMeterARef; |
joeata2wh | 3:47148198f5f2 | 69 | float perc = tval * 100.0f; |
joeata2wh | 2:dc3e84d595c3 | 70 | unsigned short tvalu16 = ain.read_u16 (); |
joeata2wh | 3:47148198f5f2 | 71 | pc.printf("adc %s R=%3.3f V=%3.3f U16=%u\r\n",label, tval, volts, tvalu16); |
joeata2wh | 2:dc3e84d595c3 | 72 | gOled2.printf("%s=%3.3fV\r\n",label,volts); |
joeata2wh | 2:dc3e84d595c3 | 73 | gOled2.display(); |
joeata2wh | 2:dc3e84d595c3 | 74 | return tval; |
joeata2wh | 2:dc3e84d595c3 | 75 | } |
joeata2wh | 2:dc3e84d595c3 | 76 | |
joeata2wh | 2:dc3e84d595c3 | 77 | |
joeata2wh | 2:dc3e84d595c3 | 78 | int main() |
joeata2wh | 2:dc3e84d595c3 | 79 | { |
joeata2wh | 3:47148198f5f2 | 80 | pc.baud(9600); |
joeata2wh | 2:dc3e84d595c3 | 81 | // Display with the Adafruit Library |
joeata2wh | 2:dc3e84d595c3 | 82 | gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height()); |
joeata2wh | 2:dc3e84d595c3 | 83 | wait(5); |
joeata2wh | 2:dc3e84d595c3 | 84 | gOled2.clearDisplay(); |
joeata2wh | 0:fa185766e039 | 85 | while(1) { |
joeata2wh | 2:dc3e84d595c3 | 86 | myled = !myled; |
joeata2wh | 2:dc3e84d595c3 | 87 | gOled2.clearDisplay(); |
joeata2wh | 2:dc3e84d595c3 | 88 | gOled2.setTextCursor(1,1); |
joeata2wh | 2:dc3e84d595c3 | 89 | readPrint(pa0, "PA_0"); |
joeata2wh | 2:dc3e84d595c3 | 90 | wait(0.1); |
joeata2wh | 2:dc3e84d595c3 | 91 | gOled2.setTextCursor(1,10); |
joeata2wh | 2:dc3e84d595c3 | 92 | readPrint(pa1, "PA_1"); |
joeata2wh | 3:47148198f5f2 | 93 | |
joeata2wh | 3:47148198f5f2 | 94 | float pwmpw = 0.3; //0.0005; //0.09; // 0.1001; |
joeata2wh | 3:47148198f5f2 | 95 | float pwmpp = 3.0; //0.00001; //0.0001; // 0.01001; |
joeata2wh | 3:47148198f5f2 | 96 | //fan2.period_ms(); |
joeata2wh | 3:47148198f5f2 | 97 | //fan2.pulsewidth_ms(200); |
joeata2wh | 3:47148198f5f2 | 98 | |
joeata2wh | 3:47148198f5f2 | 99 | //float pwmpw = 0.1; //0.0005; //0.09; // 0.1001; |
joeata2wh | 3:47148198f5f2 | 100 | //float pwmpp = 1.0; //0.00001; //0.0001; // 0.01001; |
joeata2wh | 3:47148198f5f2 | 101 | //fan2.pulsewidth(0.1); |
joeata2wh | 3:47148198f5f2 | 102 | //fan2.period(1.0 / 10.0); |
joeata2wh | 3:47148198f5f2 | 103 | //fan2.pulsewidth(2.0); |
joeata2wh | 3:47148198f5f2 | 104 | //fan2.period(20); |
joeata2wh | 3:47148198f5f2 | 105 | float pwmduty = 0.1f; |
joeata2wh | 3:47148198f5f2 | 106 | float unit_time = 10.0; |
joeata2wh | 3:47148198f5f2 | 107 | float total_period = 0.1; //0.7; //3.5; //0.8; |
joeata2wh | 3:47148198f5f2 | 108 | float num_cycle = (unit_time / total_period) + 1; |
joeata2wh | 3:47148198f5f2 | 109 | |
joeata2wh | 3:47148198f5f2 | 110 | while (pwmduty <= 1.1f) { |
joeata2wh | 3:47148198f5f2 | 111 | float on_time = total_period * pwmduty; |
joeata2wh | 3:47148198f5f2 | 112 | float off_time = total_period - on_time; |
joeata2wh | 3:47148198f5f2 | 113 | //gOled2.clearDisplay(); |
joeata2wh | 3:47148198f5f2 | 114 | gOled2.setTextCursor(1,20); |
joeata2wh | 3:47148198f5f2 | 115 | wait(0.1); |
joeata2wh | 3:47148198f5f2 | 116 | gOled2.printf("pwm=%3.3f\r\n", pwmduty); |
joeata2wh | 3:47148198f5f2 | 117 | wait(0.1); |
joeata2wh | 3:47148198f5f2 | 118 | gOled2.display(); |
joeata2wh | 3:47148198f5f2 | 119 | wait(0.1); |
joeata2wh | 3:47148198f5f2 | 120 | gOled2.display(); |
joeata2wh | 3:47148198f5f2 | 121 | wait(0.1); |
joeata2wh | 3:47148198f5f2 | 122 | pc.printf("pwm=%3.3f\r\n", pwmduty); |
joeata2wh | 3:47148198f5f2 | 123 | //wait(3.0); |
joeata2wh | 3:47148198f5f2 | 124 | fan2x = 1; |
joeata2wh | 3:47148198f5f2 | 125 | wait(0.15); |
joeata2wh | 3:47148198f5f2 | 126 | for (int x=0; x<= num_cycle; x++) { |
joeata2wh | 3:47148198f5f2 | 127 | |
joeata2wh | 3:47148198f5f2 | 128 | fan2x = 1; |
joeata2wh | 3:47148198f5f2 | 129 | wait(on_time); |
joeata2wh | 3:47148198f5f2 | 130 | fan2x = 0; |
joeata2wh | 3:47148198f5f2 | 131 | wait(off_time); |
joeata2wh | 3:47148198f5f2 | 132 | // accelerate to get the motor turning |
joeata2wh | 3:47148198f5f2 | 133 | // then slow down to the current speed; |
joeata2wh | 3:47148198f5f2 | 134 | //fan1 = 0.0; |
joeata2wh | 3:47148198f5f2 | 135 | //fan2 = 0.0; |
joeata2wh | 3:47148198f5f2 | 136 | //fan3 = 0.0; |
joeata2wh | 3:47148198f5f2 | 137 | //wait(0.02); |
joeata2wh | 3:47148198f5f2 | 138 | //fan1 = pwmduty; |
joeata2wh | 3:47148198f5f2 | 139 | //fan2 = pwmduty; |
joeata2wh | 3:47148198f5f2 | 140 | //fan3 = pwmduty; |
joeata2wh | 3:47148198f5f2 | 141 | //fan2 = pwmduty; |
joeata2wh | 3:47148198f5f2 | 142 | //fan2.period(pwmduty/100); |
joeata2wh | 3:47148198f5f2 | 143 | //fan2.period(pwmduty); |
joeata2wh | 3:47148198f5f2 | 144 | |
joeata2wh | 3:47148198f5f2 | 145 | } |
joeata2wh | 3:47148198f5f2 | 146 | pwmduty+= 0.1f; |
joeata2wh | 3:47148198f5f2 | 147 | } |
joeata2wh | 3:47148198f5f2 | 148 | //fan1 = 0.0; |
joeata2wh | 3:47148198f5f2 | 149 | //fan2 = 0.0; |
joeata2wh | 3:47148198f5f2 | 150 | //fan3 = 0.0; |
joeata2wh | 2:dc3e84d595c3 | 151 | wait(3.0); |
joeata2wh | 0:fa185766e039 | 152 | } |
joeata2wh | 0:fa185766e039 | 153 | } |