![](/media/cache/profiles/Timo_01_DSC_4754_533x533.jpg.50x50_q85.jpg)
The code is developed for the hardware NUCLEO-L432KC and Digilent Pmod OLEDrgb. The purpose is to make students learn the development process and test that the hardware works fine. For converting this to Mbed OS 6 Adafruit_GFX.h needed to add #include "Stream.h" in beginning of the Adafruit_GFX.h Adafruit_SSD1331.cpp wait_ms(200) replaced with ThisThread::sleep_for(200ms); for all the similar wait_ms lines.
Dependencies: Adafruit-GFX-MbedOS6
main.cpp@3:64fb84822c90, 2020-10-01 (annotated)
- Committer:
- timo_k2
- Date:
- Thu Oct 01 05:28:04 2020 +0000
- Revision:
- 3:64fb84822c90
- Parent:
- 2:29bb1ef0d6cd
- Child:
- 4:b3559f0f6072
The OS6 does not include floating point printf() !!!; Millivolts counted with c++ modulus operator.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timo_k2 | 0:9f57fa937a86 | 1 | /* mbed Microcontroller Library |
timo_k2 | 0:9f57fa937a86 | 2 | * Copyright (c) 2019 ARM Limited |
timo_k2 | 0:9f57fa937a86 | 3 | * SPDX-License-Identifier: Apache-2.0 |
timo_k2 | 0:9f57fa937a86 | 4 | ************************************************************************ |
timo_k2 | 0:9f57fa937a86 | 5 | * |
timo_k2 | 0:9f57fa937a86 | 6 | * Commissioning test for the L432KC and the Pmod small OLED display |
timo_k2 | 0:9f57fa937a86 | 7 | * |
timo_k2 | 0:9f57fa937a86 | 8 | ************************************************************************* |
timo_k2 | 1:de95c9ab922e | 9 | * Description: McLab10_OLEDrgb_L432KC_OS60_tk2 |
timo_k2 | 0:9f57fa937a86 | 10 | * "Test module Pmod Digilent Lextronic" will be displayed on OLEDrgb module |
timo_k2 | 0:9f57fa937a86 | 11 | * with different size and colors..... and later the variable values. |
timo_k2 | 0:9f57fa937a86 | 12 | * |
timo_k2 | 0:9f57fa937a86 | 13 | * Material |
timo_k2 | 0:9f57fa937a86 | 14 | * 1. ST L432KC ( or some other micro controller board with SPI communication ) |
timo_k2 | 0:9f57fa937a86 | 15 | * 2. Digilent Pmod OLEDrgb and the libraries |
timo_k2 | 0:9f57fa937a86 | 16 | * Adafruit_SSD1331_Mbed |
timo_k2 | 0:9f57fa937a86 | 17 | * Adafruit-GFX |
timo_k2 | 0:9f57fa937a86 | 18 | * These libraries can be found with search on the page |
timo_k2 | 0:9f57fa937a86 | 19 | * https://www.mbed.com/en/platform/mbed-os/ |
timo_k2 | 0:9f57fa937a86 | 20 | * Please connect L432KC - Pmod_OLEDrgb with lines: |
timo_k2 | 0:9f57fa937a86 | 21 | * L432KC D13 - OLED 4 SCK hardware defined for the SPI |
timo_k2 | 0:9f57fa937a86 | 22 | * L432KC D11 - OLED 2 MOSI hardware definef for the SPI |
timo_k2 | 0:9f57fa937a86 | 23 | * L432KC D9 - OLED 1 CS or any other free |
timo_k2 | 0:9f57fa937a86 | 24 | * L432KC D10 - OLED 7 DC or any other free |
timo_k2 | 0:9f57fa937a86 | 25 | * L432KC D6 - OLED 8 RES or any other free |
timo_k2 | 0:9f57fa937a86 | 26 | * VCC - OLED 9 VCCEN Enable - VCC |
timo_k2 | 0:9f57fa937a86 | 27 | * VCC - OLED10 PMODEN Pmod Enable - VCC |
timo_k2 | 0:9f57fa937a86 | 28 | * GND - OLED 5 Ground |
timo_k2 | 2:29bb1ef0d6cd | 29 | * VCC - OLED 6 Power supply 3.3 V |
timo_k2 | 2:29bb1ef0d6cd | 30 | * L432KC 3V3 - VCC L432 supplying 3.3 V |
timo_k2 | 2:29bb1ef0d6cd | 31 | * L432KC GND - GND Ground |
timo_k2 | 0:9f57fa937a86 | 32 | * |
timo_k2 | 0:9f57fa937a86 | 33 | ************************************************************************* |
timo_k2 | 0:9f57fa937a86 | 34 | * Compiles OK, works with hardware with operating system revision 6691:cf4f12a |
timo_k2 | 0:9f57fa937a86 | 35 | * Compiles OK, DOESN'T WORK with hardware with op sys revision 7088:3ab72c7 |
timo_k2 | 1:de95c9ab922e | 36 | * Updated for OS6, Compiles OK, works with hardware with os revision OS 7864:890f056 |
timo_k2 | 1:de95c9ab922e | 37 | * Timo Karppinen 29.08.2020 SPDX-License-Identifier: Apache-2.0 |
timo_k2 | 0:9f57fa937a86 | 38 | **************************************************************/ |
timo_k2 | 0:9f57fa937a86 | 39 | |
timo_k2 | 0:9f57fa937a86 | 40 | #include "mbed.h" |
timo_k2 | 0:9f57fa937a86 | 41 | #include "Adafruit_SSD1331.h" // By using the Adafruit SSD1331 library and Adafruit GFX library |
timo_k2 | 1:de95c9ab922e | 42 | #include "Adafruit_GFX.h" // we will get similar code working than in Arduino-boards. |
timo_k2 | 0:9f57fa937a86 | 43 | // There are other SSD1331 libraries, too. |
timo_k2 | 0:9f57fa937a86 | 44 | // https://os.mbed.com/search/?q=ssd1331 |
timo_k2 | 0:9f57fa937a86 | 45 | // The tested are: |
timo_k2 | 1:de95c9ab922e | 46 | // Adafruit_SSD1331_MbedOS6 by Timo Karppinen |
timo_k2 | 1:de95c9ab922e | 47 | // Adafruit-GFX-MbedOS6 by Timo Karppinen |
timo_k2 | 0:9f57fa937a86 | 48 | // PmodOLEDrgb |
timo_k2 | 0:9f57fa937a86 | 49 | Adafruit_SSD1331 OLED(D9, D6, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck |
timo_k2 | 0:9f57fa937a86 | 50 | |
timo_k2 | 3:64fb84822c90 | 51 | |
timo_k2 | 0:9f57fa937a86 | 52 | //DigitalOut LED(LED1); // LED1, LED2, LED3 and LED4 are the D13 PB_3 pin in this board and |
timo_k2 | 1:de95c9ab922e | 53 | //can not be used as a LED because of the SPI |
timo_k2 | 0:9f57fa937a86 | 54 | DigitalOut VCCEN(D3); |
timo_k2 | 0:9f57fa937a86 | 55 | DigitalOut PMODEN(D5); |
timo_k2 | 0:9f57fa937a86 | 56 | |
timo_k2 | 0:9f57fa937a86 | 57 | // Definition of colours on the OLED display |
timo_k2 | 0:9f57fa937a86 | 58 | #define Black 0x0000 |
timo_k2 | 0:9f57fa937a86 | 59 | #define Blue 0x001F |
timo_k2 | 0:9f57fa937a86 | 60 | #define Red 0xF800 |
timo_k2 | 0:9f57fa937a86 | 61 | #define Green 0x07E0 |
timo_k2 | 0:9f57fa937a86 | 62 | #define Cyan 0x07FF |
timo_k2 | 0:9f57fa937a86 | 63 | #define Magenta 0xF81F |
timo_k2 | 0:9f57fa937a86 | 64 | #define Yellow 0xFFE0 |
timo_k2 | 0:9f57fa937a86 | 65 | #define White 0xFFFF |
timo_k2 | 0:9f57fa937a86 | 66 | |
timo_k2 | 3:64fb84822c90 | 67 | float ADCScaledF = 0; // 32 bit floating point |
timo_k2 | 3:64fb84822c90 | 68 | int volt = 0; |
timo_k2 | 3:64fb84822c90 | 69 | int millivolt = 0; |
timo_k2 | 3:64fb84822c90 | 70 | void getADC(); |
timo_k2 | 0:9f57fa937a86 | 71 | |
timo_k2 | 0:9f57fa937a86 | 72 | char Time[50],Date[50]; |
timo_k2 | 0:9f57fa937a86 | 73 | void getTime(); |
timo_k2 | 0:9f57fa937a86 | 74 | int first = 0; |
timo_k2 | 0:9f57fa937a86 | 75 | |
timo_k2 | 0:9f57fa937a86 | 76 | int main() |
timo_k2 | 0:9f57fa937a86 | 77 | { |
timo_k2 | 0:9f57fa937a86 | 78 | // Showing with a LED that program has started |
timo_k2 | 0:9f57fa937a86 | 79 | // LED = 0; |
timo_k2 | 0:9f57fa937a86 | 80 | VCCEN = 1; // if you did not connect VCCEN permanently to Vcc |
timo_k2 | 0:9f57fa937a86 | 81 | PMODEN = 1; // if you did not connect PMODEN permanently to Vcc |
timo_k2 | 0:9f57fa937a86 | 82 | ThisThread::sleep_for(2000ms); |
timo_k2 | 0:9f57fa937a86 | 83 | // LED = 1; |
timo_k2 | 0:9f57fa937a86 | 84 | ThisThread::sleep_for(2000ms); |
timo_k2 | 0:9f57fa937a86 | 85 | // LED = 0; |
timo_k2 | 1:de95c9ab922e | 86 | // Initalize the PmodOLEDrgb, the library includes SPI and there is |
timo_k2 | 1:de95c9ab922e | 87 | // no need to initialize the SPI in the main.cpp |
timo_k2 | 0:9f57fa937a86 | 88 | OLED.begin(); // initialization of display object |
timo_k2 | 0:9f57fa937a86 | 89 | |
timo_k2 | 0:9f57fa937a86 | 90 | OLED.clearScreen(); |
timo_k2 | 0:9f57fa937a86 | 91 | |
timo_k2 | 0:9f57fa937a86 | 92 | while (true) { |
timo_k2 | 0:9f57fa937a86 | 93 | while(first < 3) |
timo_k2 | 0:9f57fa937a86 | 94 | { |
timo_k2 | 0:9f57fa937a86 | 95 | first += 1; |
timo_k2 | 0:9f57fa937a86 | 96 | OLED.fillScreen(Black); // background screen in black |
timo_k2 | 0:9f57fa937a86 | 97 | OLED.setTextColor(Cyan); // colour of text in cyan |
timo_k2 | 0:9f57fa937a86 | 98 | OLED.setCursor(0,0); // cursor is in x=0 and y=15 |
timo_k2 | 0:9f57fa937a86 | 99 | OLED.printf("Test module Pmod"); // display text |
timo_k2 | 0:9f57fa937a86 | 100 | ThisThread::sleep_for(500ms); // wait 500 ms |
timo_k2 | 0:9f57fa937a86 | 101 | OLED.setCursor(0,15); // cursor is in x=0 and y=15 |
timo_k2 | 0:9f57fa937a86 | 102 | OLED.setTextSize(2); // size of text |
timo_k2 | 0:9f57fa937a86 | 103 | OLED.setTextColor(Red); // text in red colour |
timo_k2 | 0:9f57fa937a86 | 104 | OLED.printf("DIGILENT"); // display text |
timo_k2 | 0:9f57fa937a86 | 105 | OLED.setCursor(20,40); // cursor is in x=20 and y=40 |
timo_k2 | 0:9f57fa937a86 | 106 | OLED.setTextSize(1); // size of text |
timo_k2 | 0:9f57fa937a86 | 107 | OLED.setTextColor(Green); // text in green colour |
timo_k2 | 0:9f57fa937a86 | 108 | OLED.printf("LEXTRONIC"); // display text |
timo_k2 | 0:9f57fa937a86 | 109 | OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); //blue line x=1, width-1 and y=60 |
timo_k2 | 0:9f57fa937a86 | 110 | ThisThread::sleep_for(2s); // wait 2 s |
timo_k2 | 0:9f57fa937a86 | 111 | OLED.fillScreen(Black); // background display in black (erase display) |
timo_k2 | 0:9f57fa937a86 | 112 | OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge |
timo_k2 | 0:9f57fa937a86 | 113 | OLED.fillRoundRect(35, 5, 30, 40, 1, White); |
timo_k2 | 0:9f57fa937a86 | 114 | OLED.fillRoundRect(65, 5, 30, 40, 1, Red); |
timo_k2 | 0:9f57fa937a86 | 115 | OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55 |
timo_k2 | 0:9f57fa937a86 | 116 | ThisThread::sleep_for(2s); // wait 2 s |
timo_k2 | 0:9f57fa937a86 | 117 | } |
timo_k2 | 0:9f57fa937a86 | 118 | |
timo_k2 | 0:9f57fa937a86 | 119 | getTime(); |
timo_k2 | 0:9f57fa937a86 | 120 | OLED.clearScreen(); |
timo_k2 | 0:9f57fa937a86 | 121 | OLED.fillScreen(Black); // background screen in black |
timo_k2 | 0:9f57fa937a86 | 122 | OLED.setTextColor(Cyan); // colour of text in cyan |
timo_k2 | 0:9f57fa937a86 | 123 | ThisThread::sleep_for(300ms); |
timo_k2 | 0:9f57fa937a86 | 124 | OLED.setCursor(0,0); // cursor is in x=0 and y=0 |
timo_k2 | 3:64fb84822c90 | 125 | OLED.printf("Rec :%s \n",Time); |
timo_k2 | 0:9f57fa937a86 | 126 | |
timo_k2 | 3:64fb84822c90 | 127 | getADC(); |
timo_k2 | 3:64fb84822c90 | 128 | OLED.printf("VOLT :%d.%03d\r\n", volt, millivolt); |
timo_k2 | 0:9f57fa937a86 | 129 | |
timo_k2 | 3:64fb84822c90 | 130 | if (ADCScaledF > 2.00) |
timo_k2 | 0:9f57fa937a86 | 131 | { |
timo_k2 | 0:9f57fa937a86 | 132 | OLED.setTextColor(Yellow); |
timo_k2 | 0:9f57fa937a86 | 133 | OLED.printf("Be aware of reaching the limit"); |
timo_k2 | 0:9f57fa937a86 | 134 | OLED.fillRoundRect(10, 35, 50, 3 , 1, Yellow); |
timo_k2 | 0:9f57fa937a86 | 135 | } |
timo_k2 | 1:de95c9ab922e | 136 | OLED.setCursor(0,40); // cursor is in x=0 and y=40 |
timo_k2 | 0:9f57fa937a86 | 137 | OLED.printf("Wait a moment !"); |
timo_k2 | 0:9f57fa937a86 | 138 | OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); // blue line x=1 to screen width-1 and y=60 |
timo_k2 | 0:9f57fa937a86 | 139 | printf("printed on OLED"); |
timo_k2 | 0:9f57fa937a86 | 140 | |
timo_k2 | 0:9f57fa937a86 | 141 | ThisThread::sleep_for(3000ms); |
timo_k2 | 0:9f57fa937a86 | 142 | } |
timo_k2 | 0:9f57fa937a86 | 143 | } |
timo_k2 | 0:9f57fa937a86 | 144 | |
timo_k2 | 0:9f57fa937a86 | 145 | void getTime() |
timo_k2 | 0:9f57fa937a86 | 146 | { |
timo_k2 | 0:9f57fa937a86 | 147 | time_t seconds = time(NULL); // https://os.mbed.com/docs/mbed-os/v6.2/apis/time.html |
timo_k2 | 0:9f57fa937a86 | 148 | strftime(Time,40,"%H:%M:%S", localtime(&seconds)); |
timo_k2 | 0:9f57fa937a86 | 149 | strftime(Date,40,"%d-%b-%Y", localtime(&seconds)); |
timo_k2 | 3:64fb84822c90 | 150 | printf("Recorded :%s \r\n",Time); |
timo_k2 | 0:9f57fa937a86 | 151 | } |
timo_k2 | 0:9f57fa937a86 | 152 | |
timo_k2 | 3:64fb84822c90 | 153 | void getADC() |
timo_k2 | 0:9f57fa937a86 | 154 | { |
timo_k2 | 3:64fb84822c90 | 155 | static int count = 200; // variable value like it would be from Analog to Digital Converter |
timo_k2 | 0:9f57fa937a86 | 156 | |
timo_k2 | 3:64fb84822c90 | 157 | count = count + 10; |
timo_k2 | 0:9f57fa937a86 | 158 | if (count > 4095){ |
timo_k2 | 3:64fb84822c90 | 159 | count = 200; |
timo_k2 | 0:9f57fa937a86 | 160 | } |
timo_k2 | 0:9f57fa937a86 | 161 | |
timo_k2 | 3:64fb84822c90 | 162 | ADCScaledF = (float(count))*(float(2.50)/4095); // The value 2.50 is 64 bit double precision floating point of type double. |
timo_k2 | 0:9f57fa937a86 | 163 | // Conversions to 32 bit floating point of type float. |
timo_k2 | 3:64fb84822c90 | 164 | volt = (int)ADCScaledF; // The OS6 does not include floating point printf !!! |
timo_k2 | 3:64fb84822c90 | 165 | millivolt = ((int)(ADCScaledF*1000))% 1000; // Millivolts counted with c++ modulus operator |
timo_k2 | 3:64fb84822c90 | 166 | printf("ADC :%d \r\n",count); |
timo_k2 | 3:64fb84822c90 | 167 | printf("VOLT :%d.%03d\r\n", volt, millivolt); // millivolts with preceeding zeros and three digits |
timo_k2 | 0:9f57fa937a86 | 168 | ThisThread::sleep_for(100ms); |
timo_k2 | 0:9f57fa937a86 | 169 | } |