A concept design laboratory prototype for measuring ambient light value. The high levels typically mean that there is high rates of UVA and UVB radiation as well. A warning of this is displayed on a small screen. NXP FRDM-K64F, Digilent Pmod modules Pmod OLEDrgb and Pmod ALS. Tested as well on ST L432KC.
Dependencies: Adafruit_SSD1331_Mbed Adafruit-GFX
main.cpp@0:87339e72a5eb, 2020-04-19 (annotated)
- Committer:
- timo_k2
- Date:
- Sun Apr 19 16:06:25 2020 +0000
- Revision:
- 0:87339e72a5eb
- Child:
- 1:fde67a85d481
Pmod_OLED_ALS is made for testing that the SPI communication works with NXP FRDM_K64F, Digilent Pmod modules Pmod OLEDrgb and Pmod ALS. It works.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timo_k2 | 0:87339e72a5eb | 1 | /* mbed Microcontroller Library |
timo_k2 | 0:87339e72a5eb | 2 | * Copyright (c) 2019 ARM Limited |
timo_k2 | 0:87339e72a5eb | 3 | * SPDX-License-Identifier: Apache-2.0 |
timo_k2 | 0:87339e72a5eb | 4 | */ |
timo_k2 | 0:87339e72a5eb | 5 | |
timo_k2 | 0:87339e72a5eb | 6 | #include "mbed.h" |
timo_k2 | 0:87339e72a5eb | 7 | #include "Adafruit_SSD1331.h" |
timo_k2 | 0:87339e72a5eb | 8 | #include "Adafruit_GFX.h" |
timo_k2 | 0:87339e72a5eb | 9 | |
timo_k2 | 0:87339e72a5eb | 10 | |
timo_k2 | 0:87339e72a5eb | 11 | // PmodOLEDrgb |
timo_k2 | 0:87339e72a5eb | 12 | Adafruit_SSD1331 OLED(D8, D7, D10, D11, NC, D13); // cs, res, dc, mosi, (nc), sck |
timo_k2 | 0:87339e72a5eb | 13 | |
timo_k2 | 0:87339e72a5eb | 14 | DigitalOut LED(LED2); |
timo_k2 | 0:87339e72a5eb | 15 | DigitalOut VCCEN(D6); |
timo_k2 | 0:87339e72a5eb | 16 | DigitalOut PMODEN(D5); |
timo_k2 | 0:87339e72a5eb | 17 | |
timo_k2 | 0:87339e72a5eb | 18 | // Definition of colors on the OLED display |
timo_k2 | 0:87339e72a5eb | 19 | #define Black 0x0000 |
timo_k2 | 0:87339e72a5eb | 20 | #define Blue 0x001F |
timo_k2 | 0:87339e72a5eb | 21 | #define Red 0xF800 |
timo_k2 | 0:87339e72a5eb | 22 | #define Green 0x07E0 |
timo_k2 | 0:87339e72a5eb | 23 | #define Cyan 0x07FF |
timo_k2 | 0:87339e72a5eb | 24 | #define Magenta 0xF81F |
timo_k2 | 0:87339e72a5eb | 25 | #define Yellow 0xFFE0 |
timo_k2 | 0:87339e72a5eb | 26 | #define White 0xFFFF |
timo_k2 | 0:87339e72a5eb | 27 | |
timo_k2 | 0:87339e72a5eb | 28 | // PmodALS |
timo_k2 | 0:87339e72a5eb | 29 | SPI spi(D11, D12, D13); // mosi, miso, sck |
timo_k2 | 0:87339e72a5eb | 30 | |
timo_k2 | 0:87339e72a5eb | 31 | |
timo_k2 | 0:87339e72a5eb | 32 | DigitalOut alsCS(D4); // chip select for sensor SPI communication |
timo_k2 | 0:87339e72a5eb | 33 | char alsByte0 = 0; // 8 bit data from sensor board, char is the unsigned 8 bit |
timo_k2 | 0:87339e72a5eb | 34 | char alsByte1 = 0; // 8 bit data from sensor board |
timo_k2 | 0:87339e72a5eb | 35 | char alsByteSh0 = 0; |
timo_k2 | 0:87339e72a5eb | 36 | char alsByteSh1 = 0; |
timo_k2 | 0:87339e72a5eb | 37 | char als8bit = 0; |
timo_k2 | 0:87339e72a5eb | 38 | unsigned short alsRaw = 0; // unsigned 16 bit |
timo_k2 | 0:87339e72a5eb | 39 | float alsScaledF = 0; // 32 bit floating point |
timo_k2 | 0:87339e72a5eb | 40 | void getALS(); |
timo_k2 | 0:87339e72a5eb | 41 | |
timo_k2 | 0:87339e72a5eb | 42 | |
timo_k2 | 0:87339e72a5eb | 43 | char Time[50],Date[50]; |
timo_k2 | 0:87339e72a5eb | 44 | void getTime(); |
timo_k2 | 0:87339e72a5eb | 45 | int first(0); |
timo_k2 | 0:87339e72a5eb | 46 | |
timo_k2 | 0:87339e72a5eb | 47 | int main() |
timo_k2 | 0:87339e72a5eb | 48 | { |
timo_k2 | 0:87339e72a5eb | 49 | // Showing with a LED that program has started |
timo_k2 | 0:87339e72a5eb | 50 | LED = 0; |
timo_k2 | 0:87339e72a5eb | 51 | VCCEN = 1; // if you did not connect VCCEN permanently to Vcc |
timo_k2 | 0:87339e72a5eb | 52 | PMODEN = 1; // if you did not connect PMODEN permanently to Vcc |
timo_k2 | 0:87339e72a5eb | 53 | wait_ms(2000); |
timo_k2 | 0:87339e72a5eb | 54 | LED = 1; |
timo_k2 | 0:87339e72a5eb | 55 | wait_ms(2000); |
timo_k2 | 0:87339e72a5eb | 56 | LED = 0; |
timo_k2 | 0:87339e72a5eb | 57 | // Initalize the PmodOLEDrgb |
timo_k2 | 0:87339e72a5eb | 58 | OLED.begin(); // initialization of display objcet |
timo_k2 | 0:87339e72a5eb | 59 | |
timo_k2 | 0:87339e72a5eb | 60 | // SPI for the ALS |
timo_k2 | 0:87339e72a5eb | 61 | alsCS = 1; |
timo_k2 | 0:87339e72a5eb | 62 | // Setup the spi for 8 bit data, high steady state clock, |
timo_k2 | 0:87339e72a5eb | 63 | // second edge capture, with a 12MHz clock rate |
timo_k2 | 0:87339e72a5eb | 64 | spi.format(8,0); |
timo_k2 | 0:87339e72a5eb | 65 | spi.frequency(12000000); |
timo_k2 | 0:87339e72a5eb | 66 | |
timo_k2 | 0:87339e72a5eb | 67 | while (true) { |
timo_k2 | 0:87339e72a5eb | 68 | while(first < 3) |
timo_k2 | 0:87339e72a5eb | 69 | { |
timo_k2 | 0:87339e72a5eb | 70 | first += 1; |
timo_k2 | 0:87339e72a5eb | 71 | OLED.fillScreen(Black); // background screen in black |
timo_k2 | 0:87339e72a5eb | 72 | OLED.setTextColor(Cyan); // color of text in cyan |
timo_k2 | 0:87339e72a5eb | 73 | OLED.setCursor(0,0); // cursor is in x=0 and y=15 |
timo_k2 | 0:87339e72a5eb | 74 | OLED.printf("Test module Pmod"); // display text |
timo_k2 | 0:87339e72a5eb | 75 | wait_ms(500); // wait 500 ms |
timo_k2 | 0:87339e72a5eb | 76 | OLED.setCursor(0,15); // cursor is in x=0 and y=15 |
timo_k2 | 0:87339e72a5eb | 77 | OLED.setTextSize(2); // size of text |
timo_k2 | 0:87339e72a5eb | 78 | OLED.setTextColor(Red); // text in red color |
timo_k2 | 0:87339e72a5eb | 79 | OLED.printf("DIGILENT"); // display text |
timo_k2 | 0:87339e72a5eb | 80 | OLED.setCursor(20,40); // cursor is in x=20 and y=40 |
timo_k2 | 0:87339e72a5eb | 81 | OLED.setTextSize(1); // size of text |
timo_k2 | 0:87339e72a5eb | 82 | OLED.setTextColor(Green); // text in green color |
timo_k2 | 0:87339e72a5eb | 83 | OLED.printf("LEXTRONIC"); // display text |
timo_k2 | 0:87339e72a5eb | 84 | OLED.drawFastHLine(1, 60, OLED.width()-1, Blue); // blue line x=1 to screen width-1 and y=60 |
timo_k2 | 0:87339e72a5eb | 85 | wait_ms(2000); // wait 2 s |
timo_k2 | 0:87339e72a5eb | 86 | OLED.fillScreen(Black); // background display in black (erase display) |
timo_k2 | 0:87339e72a5eb | 87 | OLED.fillRoundRect(5, 5, 30, 40, 1, Blue); // French flag bleu blanc rouge |
timo_k2 | 0:87339e72a5eb | 88 | OLED.fillRoundRect(35, 5, 30, 40, 1, White); |
timo_k2 | 0:87339e72a5eb | 89 | OLED.fillRoundRect(65, 5, 30, 40, 1, Red); |
timo_k2 | 0:87339e72a5eb | 90 | OLED.fillCircle(90, 55, 5, Yellow); // yellow circle with radius=5 in x=90 and y=55 |
timo_k2 | 0:87339e72a5eb | 91 | wait_ms(2000); // wait 2 s |
timo_k2 | 0:87339e72a5eb | 92 | } |
timo_k2 | 0:87339e72a5eb | 93 | |
timo_k2 | 0:87339e72a5eb | 94 | getTime(); |
timo_k2 | 0:87339e72a5eb | 95 | OLED.fillScreen(Black); // background screen in black |
timo_k2 | 0:87339e72a5eb | 96 | OLED.setTextColor(Cyan); // color of text in cyan |
timo_k2 | 0:87339e72a5eb | 97 | wait_ms(300); |
timo_k2 | 0:87339e72a5eb | 98 | OLED.setCursor(0,0); // cursor is in x=0 and y=0 |
timo_k2 | 0:87339e72a5eb | 99 | OLED.printf("Rec '%s' \r\n",Time); |
timo_k2 | 0:87339e72a5eb | 100 | |
timo_k2 | 0:87339e72a5eb | 101 | getALS(); |
timo_k2 | 0:87339e72a5eb | 102 | OLED.printf("LUX = '%0.1f' \r\n",alsScaledF); |
timo_k2 | 0:87339e72a5eb | 103 | |
timo_k2 | 0:87339e72a5eb | 104 | if (alsScaledF > 100) |
timo_k2 | 0:87339e72a5eb | 105 | { |
timo_k2 | 0:87339e72a5eb | 106 | OLED.setTextColor(Yellow); |
timo_k2 | 0:87339e72a5eb | 107 | OLED.printf("Be aware of high UV radiation!"); |
timo_k2 | 0:87339e72a5eb | 108 | OLED.fillRoundRect(10, 35, 50, 3 , 1, Yellow); |
timo_k2 | 0:87339e72a5eb | 109 | } |
timo_k2 | 0:87339e72a5eb | 110 | OLED.setCursor(0,40); // cursor is in x=0 and y=0 |
timo_k2 | 0:87339e72a5eb | 111 | OLED.printf("Test completed"); |
timo_k2 | 0:87339e72a5eb | 112 | printf("printed on OLED"); |
timo_k2 | 0:87339e72a5eb | 113 | |
timo_k2 | 0:87339e72a5eb | 114 | wait(1); |
timo_k2 | 0:87339e72a5eb | 115 | } |
timo_k2 | 0:87339e72a5eb | 116 | } |
timo_k2 | 0:87339e72a5eb | 117 | |
timo_k2 | 0:87339e72a5eb | 118 | void getTime() |
timo_k2 | 0:87339e72a5eb | 119 | { |
timo_k2 | 0:87339e72a5eb | 120 | time_t seconds = time(NULL); |
timo_k2 | 0:87339e72a5eb | 121 | strftime(Time,40,"%H:%M:%S", localtime(&seconds)); |
timo_k2 | 0:87339e72a5eb | 122 | strftime(Date,40,"%d-%b-%Y", localtime(&seconds)); |
timo_k2 | 0:87339e72a5eb | 123 | printf("Recorded '%s' \r\n",Time); |
timo_k2 | 0:87339e72a5eb | 124 | } |
timo_k2 | 0:87339e72a5eb | 125 | |
timo_k2 | 0:87339e72a5eb | 126 | void getALS() |
timo_k2 | 0:87339e72a5eb | 127 | { |
timo_k2 | 0:87339e72a5eb | 128 | alsCS = 0; |
timo_k2 | 0:87339e72a5eb | 129 | |
timo_k2 | 0:87339e72a5eb | 130 | alsByte0 = spi.write(0x00); |
timo_k2 | 0:87339e72a5eb | 131 | alsByte1 = spi.write(0x00); |
timo_k2 | 0:87339e72a5eb | 132 | |
timo_k2 | 0:87339e72a5eb | 133 | alsCS = 1; |
timo_k2 | 0:87339e72a5eb | 134 | |
timo_k2 | 0:87339e72a5eb | 135 | alsByteSh0 = alsByte0 << 4; |
timo_k2 | 0:87339e72a5eb | 136 | alsByteSh1 = alsByte1 >> 4; |
timo_k2 | 0:87339e72a5eb | 137 | |
timo_k2 | 0:87339e72a5eb | 138 | als8bit =( alsByteSh0 | alsByteSh1 ); |
timo_k2 | 0:87339e72a5eb | 139 | |
timo_k2 | 0:87339e72a5eb | 140 | alsRaw = als8bit; // |
timo_k2 | 0:87339e72a5eb | 141 | alsScaledF = (float(alsRaw))*(float(6.68)); // The value 6.68 is 64 bit double precision floating point of type double. |
timo_k2 | 0:87339e72a5eb | 142 | // Conversions to 32 bit floating point of type float. |
timo_k2 | 0:87339e72a5eb | 143 | printf("Ambient light raw 8 bit 0...255 = '%d' \r\n",alsRaw); |
timo_k2 | 0:87339e72a5eb | 144 | printf("Ambient light scaled to LUX = '%0.1f' \r\n",alsScaledF); |
timo_k2 | 0:87339e72a5eb | 145 | wait_ms(100); |
timo_k2 | 0:87339e72a5eb | 146 | } |
timo_k2 | 0:87339e72a5eb | 147 |