Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MCP342x ssd1306_library
main.cpp@1:c410db7a0673, 2019-12-16 (annotated)
- Committer:
- makermax
- Date:
- Mon Dec 16 14:38:54 2019 +0000
- Revision:
- 1:c410db7a0673
- Parent:
- 0:fcf0df749468
first commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| makermax | 1:c410db7a0673 | 1 | /** | 
| makermax | 1:c410db7a0673 | 2 | ****************************************************************************** | 
| makermax | 1:c410db7a0673 | 3 | * File Name : MAIN.CPP | 
| makermax | 1:c410db7a0673 | 4 | * Description : DEMO CODE FOR THE ION HARDWARE KIT | 
| makermax | 1:c410db7a0673 | 5 | * DATE : DEC 15 2019 | 
| makermax | 1:c410db7a0673 | 6 | ****************************************************************************** | 
| makermax | 1:c410db7a0673 | 7 | ** This notice applies to any and all portions of this file | 
| makermax | 1:c410db7a0673 | 8 | * that are not between comment pairs USER CODE BEGIN and | 
| makermax | 1:c410db7a0673 | 9 | * USER CODE END. Other portions of this file, whether | 
| makermax | 1:c410db7a0673 | 10 | * inserted by the user or by software development tools | 
| makermax | 1:c410db7a0673 | 11 | * are owned by their respective copyright owners. | 
| makermax | 1:c410db7a0673 | 12 | * | 
| makermax | 1:c410db7a0673 | 13 | * COPYRIGHT(c) 2019 MAKERMAX INC. | 
| makermax | 1:c410db7a0673 | 14 | * | 
| makermax | 1:c410db7a0673 | 15 | * Redistribution and use in source and binary forms, with or without modification, | 
| makermax | 1:c410db7a0673 | 16 | * are permitted provided that the following conditions are met: | 
| makermax | 1:c410db7a0673 | 17 | * 1. Redistributions of source code must retain the above copyright notice, | 
| makermax | 1:c410db7a0673 | 18 | * this list of conditions and the following disclaimer. | 
| makermax | 1:c410db7a0673 | 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, | 
| makermax | 1:c410db7a0673 | 20 | * this list of conditions and the following disclaimer in the documentation | 
| makermax | 1:c410db7a0673 | 21 | * and/or other materials provided with the distribution. | 
| makermax | 1:c410db7a0673 | 22 | * 3. Neither the name of MAKERMAX INC. nor the names of its contributors | 
| makermax | 1:c410db7a0673 | 23 | * may be used to endorse or promote products derived from this software | 
| makermax | 1:c410db7a0673 | 24 | * without specific prior written permission. | 
| makermax | 1:c410db7a0673 | 25 | * | 
| makermax | 1:c410db7a0673 | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
| makermax | 1:c410db7a0673 | 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
| makermax | 1:c410db7a0673 | 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| makermax | 1:c410db7a0673 | 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
| makermax | 1:c410db7a0673 | 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
| makermax | 1:c410db7a0673 | 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
| makermax | 1:c410db7a0673 | 32 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
| makermax | 1:c410db7a0673 | 33 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
| makermax | 1:c410db7a0673 | 34 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| makermax | 1:c410db7a0673 | 35 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| makermax | 1:c410db7a0673 | 36 | * | 
| makermax | 1:c410db7a0673 | 37 | ******************************************************************************/ | 
| makermax | 1:c410db7a0673 | 38 | |
| makermax | 0:fcf0df749468 | 39 | |
| makermax | 0:fcf0df749468 | 40 | #include "mbed.h" | 
| makermax | 1:c410db7a0673 | 41 | #include "mcp342x.h" | 
| makermax | 1:c410db7a0673 | 42 | #include "ssd1306.h" | 
| makermax | 1:c410db7a0673 | 43 | |
| makermax | 1:c410db7a0673 | 44 | Serial device(USBTX, USBRX); // tx, rx | 
| makermax | 1:c410db7a0673 | 45 | SSD1306 lcd (I2C_SDA, I2C_SCL); | 
| makermax | 1:c410db7a0673 | 46 | |
| makermax | 1:c410db7a0673 | 47 | int16_t getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) { | 
| makermax | 1:c410db7a0673 | 48 | //Configure channel and trigger. | 
| makermax | 1:c410db7a0673 | 49 | mcp3428->setChannel(ch); | 
| makermax | 1:c410db7a0673 | 50 | mcp3428->setSampleSetting(s); | 
| makermax | 1:c410db7a0673 | 51 | mcp3428->trigger(); | 
| makermax | 1:c410db7a0673 | 52 | |
| makermax | 1:c410db7a0673 | 53 | // polling data | 
| makermax | 1:c410db7a0673 | 54 | MCP342X::Data data; | 
| makermax | 1:c410db7a0673 | 55 | do { | 
| makermax | 1:c410db7a0673 | 56 | wait_us(100000); | 
| makermax | 1:c410db7a0673 | 57 | mcp3428->getData(&data); | 
| makermax | 1:c410db7a0673 | 58 | } while(data.st == MCP342X::DATA_NOT_UPDATED); | 
| makermax | 1:c410db7a0673 | 59 | |
| makermax | 1:c410db7a0673 | 60 | return data.value; | 
| makermax | 1:c410db7a0673 | 61 | } | 
| makermax | 1:c410db7a0673 | 62 | |
| makermax | 1:c410db7a0673 | 63 | #define I2C_SPEED_100KHZ 100000 | 
| makermax | 1:c410db7a0673 | 64 | #define PIN_SERIAL_TX P0_4 | 
| makermax | 1:c410db7a0673 | 65 | #define PIN_SERIAL_RX P0_5 | 
| makermax | 1:c410db7a0673 | 66 | |
| makermax | 1:c410db7a0673 | 67 | DigitalOut myled(LED1); | 
| makermax | 1:c410db7a0673 | 68 | AnalogOut aout(A2); | 
| makermax | 1:c410db7a0673 | 69 | AnalogOut aout2(D13); | 
| makermax | 1:c410db7a0673 | 70 | |
| makermax | 1:c410db7a0673 | 71 | |
| makermax | 1:c410db7a0673 | 72 | |
| makermax | 1:c410db7a0673 | 73 | float voltage=0; //ch3 | 
| makermax | 1:c410db7a0673 | 74 | float chg=0; //ch1 | 
| makermax | 1:c410db7a0673 | 75 | float dchg=0; //ch2 | 
| makermax | 1:c410db7a0673 | 76 | float temp=0; //ch4 | 
| makermax | 1:c410db7a0673 | 77 | float cshunt=0.2; | 
| makermax | 1:c410db7a0673 | 78 | float dshunt=0.2; | 
| makermax | 0:fcf0df749468 | 79 | |
| makermax | 0:fcf0df749468 | 80 | |
| makermax | 1:c410db7a0673 | 81 | int main() { | 
| makermax | 1:c410db7a0673 | 82 | // Instanciate I2C | 
| makermax | 1:c410db7a0673 | 83 | I2C i2c(I2C_SDA, I2C_SCL); | 
| makermax | 1:c410db7a0673 | 84 | i2c.frequency(I2C_SPEED_100KHZ); | 
| makermax | 1:c410db7a0673 | 85 | |
| makermax | 1:c410db7a0673 | 86 | // Serial output for debug. (optional) | 
| makermax | 1:c410db7a0673 | 87 | |
| makermax | 1:c410db7a0673 | 88 | MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_68H); | 
| makermax | 1:c410db7a0673 | 89 | mcp342x.setConversionMode(MCP342X::ONE_SHOT); | 
| makermax | 1:c410db7a0673 | 90 | |
| makermax | 1:c410db7a0673 | 91 | while(1) { | 
| makermax | 1:c410db7a0673 | 92 | aout.write_u16(0); | 
| makermax | 1:c410db7a0673 | 93 | aout2.write_u16(30000); // 0 - 65500 | 
| makermax | 1:c410db7a0673 | 94 | HAL_Delay(100); | 
| makermax | 1:c410db7a0673 | 95 | |
| makermax | 1:c410db7a0673 | 96 | // Supposes that the device is MCP3428, which has 4 channels. | 
| makermax | 1:c410db7a0673 | 97 | const uint8_t CHANNEL_NUM = 4; | 
| makermax | 1:c410db7a0673 | 98 | // Sampling setting. Ch1 is 12-bit, Ch2 is 14-bit, Ch3 is 16-bit, Ch4 is 16-bit. | 
| makermax | 1:c410db7a0673 | 99 | const MCP342X::SampleSetting sampleSetting[CHANNEL_NUM] = | 
| makermax | 1:c410db7a0673 | 100 | {MCP342X::SAMPLE_15HZ_16BIT, MCP342X::SAMPLE_15HZ_16BIT, | 
| makermax | 1:c410db7a0673 | 101 | MCP342X::SAMPLE_15HZ_16BIT, MCP342X::SAMPLE_15HZ_16BIT}; | 
| makermax | 1:c410db7a0673 | 102 | // Data buffer. | 
| makermax | 1:c410db7a0673 | 103 | int16_t data[CHANNEL_NUM]; | 
| makermax | 1:c410db7a0673 | 104 | // Measures each channel. | 
| makermax | 1:c410db7a0673 | 105 | for (int i=0; i < CHANNEL_NUM; i++) { | 
| makermax | 1:c410db7a0673 | 106 | data[i]=getAdcData(&mcp342x, (MCP342X::AdcChannel)i, sampleSetting[i]); | 
| makermax | 1:c410db7a0673 | 107 | } | 
| makermax | 1:c410db7a0673 | 108 | // Prints out the ADC results. | 
| makermax | 1:c410db7a0673 | 109 | device.printf("%d, %d, %d, %d\r\n", data[0], data[1], data[2], data[3]); | 
| makermax | 1:c410db7a0673 | 110 | |
| makermax | 1:c410db7a0673 | 111 | chg=float(data[0]); | 
| makermax | 1:c410db7a0673 | 112 | chg=chg/32767; | 
| makermax | 1:c410db7a0673 | 113 | chg=chg*2.048; | 
| makermax | 1:c410db7a0673 | 114 | chg=chg/cshunt; | 
| makermax | 1:c410db7a0673 | 115 | |
| makermax | 1:c410db7a0673 | 116 | dchg=float(data[1]); | 
| makermax | 1:c410db7a0673 | 117 | dchg=dchg/32767; | 
| makermax | 1:c410db7a0673 | 118 | dchg=dchg*2.048; | 
| makermax | 1:c410db7a0673 | 119 | dchg=dchg/dshunt; | 
| makermax | 1:c410db7a0673 | 120 | |
| makermax | 1:c410db7a0673 | 121 | voltage=float(data[2]); | 
| makermax | 1:c410db7a0673 | 122 | voltage=voltage/32767; | 
| makermax | 1:c410db7a0673 | 123 | voltage=voltage*2.048; | 
| makermax | 1:c410db7a0673 | 124 | voltage=voltage/0.3197; | 
| makermax | 0:fcf0df749468 | 125 | |
| makermax | 0:fcf0df749468 | 126 | |
| makermax | 1:c410db7a0673 | 127 | temp=float(data[3]); | 
| makermax | 1:c410db7a0673 | 128 | temp=temp/32767; | 
| makermax | 1:c410db7a0673 | 129 | temp=temp*2.048; | 
| makermax | 1:c410db7a0673 | 130 | |
| makermax | 1:c410db7a0673 | 131 | lcd.speed (SSD1306::Slow); // set working frequency | 
| makermax | 1:c410db7a0673 | 132 | lcd.init(); // initialize SSD1306 | 
| makermax | 1:c410db7a0673 | 133 | lcd.cls(); // clear frame buffer | 
| makermax | 1:c410db7a0673 | 134 | lcd.locate (2,1); // set text cursor to line 3, column 1 | 
| makermax | 1:c410db7a0673 | 135 | lcd.printf ("ch1=%f",chg); // print to frame buffer | 
| makermax | 1:c410db7a0673 | 136 | lcd.locate (3,1); | 
| makermax | 1:c410db7a0673 | 137 | lcd.printf ("ch2=%f",dchg); | 
| makermax | 1:c410db7a0673 | 138 | lcd.locate (4,1); | 
| makermax | 1:c410db7a0673 | 139 | lcd.printf ("ch3=%f",voltage); | 
| makermax | 1:c410db7a0673 | 140 | lcd.locate (5,1); | 
| makermax | 1:c410db7a0673 | 141 | lcd.printf ("ch4=%f",temp); | 
| makermax | 1:c410db7a0673 | 142 | |
| makermax | 1:c410db7a0673 | 143 | lcd.line ( 6, 12, 114, 12, SSD1306::Normal); // | 
| makermax | 1:c410db7a0673 | 144 | lcd.line (114, 64, 114, 12, SSD1306::Normal); // Surrounds text with | 
| makermax | 1:c410db7a0673 | 145 | lcd.line (114, 62, 6, 62, SSD1306::Normal); // a rectangle | 
| makermax | 1:c410db7a0673 | 146 | lcd.line ( 6, 64, 6, 12, SSD1306::Normal); // | 
| makermax | 1:c410db7a0673 | 147 | //lcd.fill (255, 255); // fills screen outside rectangle | 
| makermax | 1:c410db7a0673 | 148 | lcd.redraw(); // updates actual display transferring frame buffer over I2C bus | 
| makermax | 1:c410db7a0673 | 149 | |
| makermax | 1:c410db7a0673 | 150 | HAL_Delay(5000); | 
| makermax | 1:c410db7a0673 | 151 | aout2.write_u16(0); // 0 - 65500 | 
| makermax | 1:c410db7a0673 | 152 | aout.write_u16(30000); | 
| makermax | 1:c410db7a0673 | 153 | |
| makermax | 1:c410db7a0673 | 154 | |
| makermax | 1:c410db7a0673 | 155 | for (int i=0; i < CHANNEL_NUM; i++) { | 
| makermax | 1:c410db7a0673 | 156 | data[i]=getAdcData(&mcp342x, (MCP342X::AdcChannel)i, sampleSetting[i]); | 
| makermax | 1:c410db7a0673 | 157 | } | 
| makermax | 1:c410db7a0673 | 158 | // Prints out the ADC results. | 
| makermax | 1:c410db7a0673 | 159 | device.printf("%d, %d, %d, %d\r\n", data[0], data[1], data[2], data[3]); | 
| makermax | 0:fcf0df749468 | 160 | |
| makermax | 1:c410db7a0673 | 161 | chg=float(data[0]); | 
| makermax | 1:c410db7a0673 | 162 | chg=chg/32767; | 
| makermax | 1:c410db7a0673 | 163 | chg=chg*2.048; | 
| makermax | 1:c410db7a0673 | 164 | chg=chg/cshunt; | 
| makermax | 1:c410db7a0673 | 165 | |
| makermax | 1:c410db7a0673 | 166 | dchg=float(data[1]); | 
| makermax | 1:c410db7a0673 | 167 | dchg=dchg/32767; | 
| makermax | 1:c410db7a0673 | 168 | dchg=dchg*2.048; | 
| makermax | 1:c410db7a0673 | 169 | dchg=dchg/dshunt; | 
| makermax | 1:c410db7a0673 | 170 | |
| makermax | 1:c410db7a0673 | 171 | voltage=float(data[2]); | 
| makermax | 1:c410db7a0673 | 172 | voltage=voltage/32767; | 
| makermax | 1:c410db7a0673 | 173 | voltage=voltage*2.048; | 
| makermax | 1:c410db7a0673 | 174 | voltage=voltage/0.3197; | 
| makermax | 1:c410db7a0673 | 175 | |
| makermax | 1:c410db7a0673 | 176 | |
| makermax | 1:c410db7a0673 | 177 | temp=float(data[3]); | 
| makermax | 1:c410db7a0673 | 178 | temp=temp/32767; | 
| makermax | 1:c410db7a0673 | 179 | temp=temp*2.048; | 
| makermax | 1:c410db7a0673 | 180 | |
| makermax | 1:c410db7a0673 | 181 | lcd.speed (SSD1306::Slow); // set working frequency | 
| makermax | 1:c410db7a0673 | 182 | lcd.init(); // initialize SSD1306 | 
| makermax | 1:c410db7a0673 | 183 | lcd.cls(); // clear frame buffer | 
| makermax | 1:c410db7a0673 | 184 | lcd.locate (2,1); // set text cursor to line 3, column 1 | 
| makermax | 1:c410db7a0673 | 185 | lcd.printf ("ch1=%f",chg); // print to frame buffer | 
| makermax | 1:c410db7a0673 | 186 | lcd.locate (3,1); | 
| makermax | 1:c410db7a0673 | 187 | lcd.printf ("ch2=%f",dchg); | 
| makermax | 1:c410db7a0673 | 188 | lcd.locate (4,1); | 
| makermax | 1:c410db7a0673 | 189 | lcd.printf ("ch3=%f",voltage); | 
| makermax | 1:c410db7a0673 | 190 | lcd.locate (5,1); | 
| makermax | 1:c410db7a0673 | 191 | lcd.printf ("ch4=%f",temp); | 
| makermax | 1:c410db7a0673 | 192 | |
| makermax | 1:c410db7a0673 | 193 | lcd.line ( 6, 12, 114, 12, SSD1306::Normal); // | 
| makermax | 1:c410db7a0673 | 194 | lcd.line (114, 64, 114, 12, SSD1306::Normal); // Surrounds text with | 
| makermax | 1:c410db7a0673 | 195 | lcd.line (114, 62, 6, 62, SSD1306::Normal); // a rectangle | 
| makermax | 1:c410db7a0673 | 196 | lcd.line ( 6, 64, 6, 12, SSD1306::Normal); // | 
| makermax | 1:c410db7a0673 | 197 | //lcd.fill (255, 255); // fills screen outside rectangle | 
| makermax | 1:c410db7a0673 | 198 | lcd.redraw(); // updates actual display transferring frame buffer over I2C bus | 
| makermax | 1:c410db7a0673 | 199 | HAL_Delay(5000); | 
| makermax | 1:c410db7a0673 | 200 | |
| makermax | 1:c410db7a0673 | 201 | |
| makermax | 1:c410db7a0673 | 202 | |
| makermax | 0:fcf0df749468 | 203 | } | 
| makermax | 0:fcf0df749468 | 204 | } | 
| makermax | 1:c410db7a0673 | 205 | |
| makermax | 1:c410db7a0673 | 206 |