Nucleo_L432_OLED_ADC

Dependencies:   Adafruit_GFX mbed

main.cpp

Committer:
pythonworld
Date:
2016-10-27
Revision:
4:573208af69ba
Parent:
3:85bd3daf17ff
Child:
5:e4f8eaef10c6

File content as of revision 4:573208af69ba:

#include "mbed.h"
#include "Adafruit_SSD1306.h"
//------------------------------------
// Hyperterminal configuration
// 9600 bauds, 8-bit data, no parity
//------------------------------------

// SSD1306 OLED AND Nucleo L432KC
// 2016.10.23
// 2016.10.23 changed
#define offset_voltage 1570
#define sensitivity 330
Serial pc(SERIAL_TX, SERIAL_RX);

DigitalOut myled(LED3);
AnalogIn adc1(A0);
AnalogIn adc2(A1);
AnalogIn adc3(A5);

#define DO A4
#define DI A6
#define CS D9
#define DC D11
#define RST D7

class SPIPreInit : public SPI
{
public:
    SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk) {
        format(8,3);
        frequency(2000000);
    };
};

SPIPreInit mySpi(DI,NC,DO);
Adafruit_SSD1306_Spi oled(mySpi,DC,RST,CS,64,128);



int main()
{
    int x=0;


    oled.clearDisplay();
    oled.setTextSize(2);
    while(1) {
        x++;
        oled.printf("X %2.2lf g\r\n",double(adc1.read()*3300-offset_voltage)/sensitivity);
        oled.printf("Y %2.2lf g\r\n",double(adc2.read()*3300-offset_voltage)/sensitivity);
        oled.printf("Z %2.2lf g\r\n",double(adc3.read()*3300-offset_voltage)/sensitivity);

        oled.display();
        wait(0.5);

        pc.printf("X %2.3f V\r\n", adc1.read()*3300/1000);
        pc.printf("Y %2.3f V\r\n", adc2.read()*3300/1000);
        pc.printf("Z %2.3f V\r\n", adc3.read()*3300/1000);

//  myled = !myled;
        if(x>0) {
            oled.setTextCursor(0,0);
            oled.clearDisplay();
            x=0;
        }
    }
}