Programme d'une sonde de température DHT 11 associée à de la sauvegarde de données sur clé USB et à l'affichage de ces données sur afficheur LCD
Dependencies: FatFileSystemCpp mbed
TextLCD.cpp@0:ed0b4e66d2ad, 2015-06-02 (annotated)
- Committer:
- Fanta025
- Date:
- Tue Jun 02 14:19:54 2015 +0000
- Revision:
- 0:ed0b4e66d2ad
Programme d'une sonde de temp?rature DHT 11 associ?e ? de la sauvegarde de donn?es sur USB et un affichage sur ?cran LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Fanta025 | 0:ed0b4e66d2ad | 1 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
Fanta025 | 0:ed0b4e66d2ad | 2 | * Copyright (c) 2007-2010, sford, http://mbed.org |
Fanta025 | 0:ed0b4e66d2ad | 3 | * |
Fanta025 | 0:ed0b4e66d2ad | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Fanta025 | 0:ed0b4e66d2ad | 5 | * of this software and associated documentation files (the "Software"), to deal |
Fanta025 | 0:ed0b4e66d2ad | 6 | * in the Software without restriction, including without limitation the rights |
Fanta025 | 0:ed0b4e66d2ad | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Fanta025 | 0:ed0b4e66d2ad | 8 | * copies of the Software, and to permit persons to whom the Software is |
Fanta025 | 0:ed0b4e66d2ad | 9 | * furnished to do so, subject to the following conditions: |
Fanta025 | 0:ed0b4e66d2ad | 10 | * |
Fanta025 | 0:ed0b4e66d2ad | 11 | * The above copyright notice and this permission notice shall be included in |
Fanta025 | 0:ed0b4e66d2ad | 12 | * all copies or substantial portions of the Software. |
Fanta025 | 0:ed0b4e66d2ad | 13 | * |
Fanta025 | 0:ed0b4e66d2ad | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Fanta025 | 0:ed0b4e66d2ad | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Fanta025 | 0:ed0b4e66d2ad | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Fanta025 | 0:ed0b4e66d2ad | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Fanta025 | 0:ed0b4e66d2ad | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Fanta025 | 0:ed0b4e66d2ad | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Fanta025 | 0:ed0b4e66d2ad | 20 | * THE SOFTWARE. |
Fanta025 | 0:ed0b4e66d2ad | 21 | */ |
Fanta025 | 0:ed0b4e66d2ad | 22 | |
Fanta025 | 0:ed0b4e66d2ad | 23 | #include "TextLCD.h" |
Fanta025 | 0:ed0b4e66d2ad | 24 | #include "mbed.h" |
Fanta025 | 0:ed0b4e66d2ad | 25 | |
Fanta025 | 0:ed0b4e66d2ad | 26 | TextLCD::TextLCD(PinName rs, PinName e, PinName d0, PinName d1, |
Fanta025 | 0:ed0b4e66d2ad | 27 | PinName d2, PinName d3, LCDType type) : _rs(rs), |
Fanta025 | 0:ed0b4e66d2ad | 28 | _e(e), _d(d0, d1, d2, d3), |
Fanta025 | 0:ed0b4e66d2ad | 29 | _type(type) { |
Fanta025 | 0:ed0b4e66d2ad | 30 | |
Fanta025 | 0:ed0b4e66d2ad | 31 | _e = 0; |
Fanta025 | 0:ed0b4e66d2ad | 32 | _rs = 0; // command mode |
Fanta025 | 0:ed0b4e66d2ad | 33 | |
Fanta025 | 0:ed0b4e66d2ad | 34 | wait(0.050); // Wait 15ms to ensure powered up |
Fanta025 | 0:ed0b4e66d2ad | 35 | |
Fanta025 | 0:ed0b4e66d2ad | 36 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
Fanta025 | 0:ed0b4e66d2ad | 37 | for (int i=0; i<3; i++) { |
Fanta025 | 0:ed0b4e66d2ad | 38 | writeByte(0x30); |
Fanta025 | 0:ed0b4e66d2ad | 39 | wait(0.002); // this command takes 1.64ms, so wait for it |
Fanta025 | 0:ed0b4e66d2ad | 40 | } |
Fanta025 | 0:ed0b4e66d2ad | 41 | writeByte(0x2); |
Fanta025 | 0:ed0b4e66d2ad | 42 | wait(0.002); |
Fanta025 | 0:ed0b4e66d2ad | 43 | writeCommand(0x29); |
Fanta025 | 0:ed0b4e66d2ad | 44 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 45 | writeCommand(0x14); |
Fanta025 | 0:ed0b4e66d2ad | 46 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 47 | writeCommand(0x55); |
Fanta025 | 0:ed0b4e66d2ad | 48 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 49 | writeCommand(0x6D); |
Fanta025 | 0:ed0b4e66d2ad | 50 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 51 | writeCommand(0x78); |
Fanta025 | 0:ed0b4e66d2ad | 52 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 53 | writeCommand(0x28); |
Fanta025 | 0:ed0b4e66d2ad | 54 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 55 | writeCommand(0x0C); |
Fanta025 | 0:ed0b4e66d2ad | 56 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 57 | writeCommand(0x01); |
Fanta025 | 0:ed0b4e66d2ad | 58 | wait_ms(2); |
Fanta025 | 0:ed0b4e66d2ad | 59 | writeCommand(0x06); |
Fanta025 | 0:ed0b4e66d2ad | 60 | wait(0.001f); |
Fanta025 | 0:ed0b4e66d2ad | 61 | } |
Fanta025 | 0:ed0b4e66d2ad | 62 | |
Fanta025 | 0:ed0b4e66d2ad | 63 | void TextLCD::character(int column, int row, int c) { |
Fanta025 | 0:ed0b4e66d2ad | 64 | int a = address(column, row); |
Fanta025 | 0:ed0b4e66d2ad | 65 | writeCommand(a); |
Fanta025 | 0:ed0b4e66d2ad | 66 | writeData(c); |
Fanta025 | 0:ed0b4e66d2ad | 67 | } |
Fanta025 | 0:ed0b4e66d2ad | 68 | |
Fanta025 | 0:ed0b4e66d2ad | 69 | void TextLCD::cls() { |
Fanta025 | 0:ed0b4e66d2ad | 70 | writeCommand(0x01); // cls, and set cursor to 0 |
Fanta025 | 0:ed0b4e66d2ad | 71 | wait(0.00164f); // This command takes 1.64 ms |
Fanta025 | 0:ed0b4e66d2ad | 72 | locate(0, 0); |
Fanta025 | 0:ed0b4e66d2ad | 73 | } |
Fanta025 | 0:ed0b4e66d2ad | 74 | |
Fanta025 | 0:ed0b4e66d2ad | 75 | void TextLCD::locate(int column, int row) { |
Fanta025 | 0:ed0b4e66d2ad | 76 | _column = column; |
Fanta025 | 0:ed0b4e66d2ad | 77 | _row = row; |
Fanta025 | 0:ed0b4e66d2ad | 78 | } |
Fanta025 | 0:ed0b4e66d2ad | 79 | |
Fanta025 | 0:ed0b4e66d2ad | 80 | int TextLCD::_putc(int value) { |
Fanta025 | 0:ed0b4e66d2ad | 81 | if (value == '\n') { |
Fanta025 | 0:ed0b4e66d2ad | 82 | _column = 0; |
Fanta025 | 0:ed0b4e66d2ad | 83 | _row++; |
Fanta025 | 0:ed0b4e66d2ad | 84 | if (_row >= rows()) { |
Fanta025 | 0:ed0b4e66d2ad | 85 | _row = 0; |
Fanta025 | 0:ed0b4e66d2ad | 86 | } |
Fanta025 | 0:ed0b4e66d2ad | 87 | } else { |
Fanta025 | 0:ed0b4e66d2ad | 88 | character(_column, _row, value); |
Fanta025 | 0:ed0b4e66d2ad | 89 | _column++; |
Fanta025 | 0:ed0b4e66d2ad | 90 | if (_column >= columns()) { |
Fanta025 | 0:ed0b4e66d2ad | 91 | _column = 0; |
Fanta025 | 0:ed0b4e66d2ad | 92 | _row++; |
Fanta025 | 0:ed0b4e66d2ad | 93 | if (_row >= rows()) { |
Fanta025 | 0:ed0b4e66d2ad | 94 | _row = 0; |
Fanta025 | 0:ed0b4e66d2ad | 95 | } |
Fanta025 | 0:ed0b4e66d2ad | 96 | } |
Fanta025 | 0:ed0b4e66d2ad | 97 | } |
Fanta025 | 0:ed0b4e66d2ad | 98 | return value; |
Fanta025 | 0:ed0b4e66d2ad | 99 | } |
Fanta025 | 0:ed0b4e66d2ad | 100 | |
Fanta025 | 0:ed0b4e66d2ad | 101 | int TextLCD::_getc() { |
Fanta025 | 0:ed0b4e66d2ad | 102 | return -1; |
Fanta025 | 0:ed0b4e66d2ad | 103 | } |
Fanta025 | 0:ed0b4e66d2ad | 104 | |
Fanta025 | 0:ed0b4e66d2ad | 105 | void TextLCD::writeByte(int value) { |
Fanta025 | 0:ed0b4e66d2ad | 106 | _d = value >> 4; |
Fanta025 | 0:ed0b4e66d2ad | 107 | wait(0.000040f); // most instructions take 40us |
Fanta025 | 0:ed0b4e66d2ad | 108 | _e = 0; |
Fanta025 | 0:ed0b4e66d2ad | 109 | wait(0.000040f); |
Fanta025 | 0:ed0b4e66d2ad | 110 | _e = 1; |
Fanta025 | 0:ed0b4e66d2ad | 111 | _d = value >> 0; |
Fanta025 | 0:ed0b4e66d2ad | 112 | wait(0.000040f); |
Fanta025 | 0:ed0b4e66d2ad | 113 | _e = 0; |
Fanta025 | 0:ed0b4e66d2ad | 114 | wait(0.000040f); // most instructions take 40us |
Fanta025 | 0:ed0b4e66d2ad | 115 | _e = 1; |
Fanta025 | 0:ed0b4e66d2ad | 116 | } |
Fanta025 | 0:ed0b4e66d2ad | 117 | |
Fanta025 | 0:ed0b4e66d2ad | 118 | void TextLCD::writeCommand(int command) { |
Fanta025 | 0:ed0b4e66d2ad | 119 | _rs = 0; |
Fanta025 | 0:ed0b4e66d2ad | 120 | writeByte(command); |
Fanta025 | 0:ed0b4e66d2ad | 121 | } |
Fanta025 | 0:ed0b4e66d2ad | 122 | |
Fanta025 | 0:ed0b4e66d2ad | 123 | void TextLCD::writeData(int data) { |
Fanta025 | 0:ed0b4e66d2ad | 124 | _rs = 1; |
Fanta025 | 0:ed0b4e66d2ad | 125 | writeByte(data); |
Fanta025 | 0:ed0b4e66d2ad | 126 | } |
Fanta025 | 0:ed0b4e66d2ad | 127 | |
Fanta025 | 0:ed0b4e66d2ad | 128 | int TextLCD::address(int column, int row) { |
Fanta025 | 0:ed0b4e66d2ad | 129 | switch (_type) { |
Fanta025 | 0:ed0b4e66d2ad | 130 | case LCD20x4: |
Fanta025 | 0:ed0b4e66d2ad | 131 | switch (row) { |
Fanta025 | 0:ed0b4e66d2ad | 132 | case 0: |
Fanta025 | 0:ed0b4e66d2ad | 133 | return 0x80 + column; |
Fanta025 | 0:ed0b4e66d2ad | 134 | case 1: |
Fanta025 | 0:ed0b4e66d2ad | 135 | return 0xc0 + column; |
Fanta025 | 0:ed0b4e66d2ad | 136 | case 2: |
Fanta025 | 0:ed0b4e66d2ad | 137 | return 0x94 + column; |
Fanta025 | 0:ed0b4e66d2ad | 138 | case 3: |
Fanta025 | 0:ed0b4e66d2ad | 139 | return 0xd4 + column; |
Fanta025 | 0:ed0b4e66d2ad | 140 | } |
Fanta025 | 0:ed0b4e66d2ad | 141 | case LCD16x2B: |
Fanta025 | 0:ed0b4e66d2ad | 142 | return 0x80 + (row * 40) + column; |
Fanta025 | 0:ed0b4e66d2ad | 143 | case LCD16x2: |
Fanta025 | 0:ed0b4e66d2ad | 144 | case LCD20x2: |
Fanta025 | 0:ed0b4e66d2ad | 145 | default: |
Fanta025 | 0:ed0b4e66d2ad | 146 | return 0x80 + (row * 0x40) + column; |
Fanta025 | 0:ed0b4e66d2ad | 147 | } |
Fanta025 | 0:ed0b4e66d2ad | 148 | } |
Fanta025 | 0:ed0b4e66d2ad | 149 | |
Fanta025 | 0:ed0b4e66d2ad | 150 | int TextLCD::columns() { |
Fanta025 | 0:ed0b4e66d2ad | 151 | switch (_type) { |
Fanta025 | 0:ed0b4e66d2ad | 152 | case LCD20x4: |
Fanta025 | 0:ed0b4e66d2ad | 153 | case LCD20x2: |
Fanta025 | 0:ed0b4e66d2ad | 154 | return 20; |
Fanta025 | 0:ed0b4e66d2ad | 155 | case LCD16x2: |
Fanta025 | 0:ed0b4e66d2ad | 156 | case LCD16x2B: |
Fanta025 | 0:ed0b4e66d2ad | 157 | default: |
Fanta025 | 0:ed0b4e66d2ad | 158 | return 16; |
Fanta025 | 0:ed0b4e66d2ad | 159 | } |
Fanta025 | 0:ed0b4e66d2ad | 160 | } |
Fanta025 | 0:ed0b4e66d2ad | 161 | |
Fanta025 | 0:ed0b4e66d2ad | 162 | int TextLCD::rows() { |
Fanta025 | 0:ed0b4e66d2ad | 163 | switch (_type) { |
Fanta025 | 0:ed0b4e66d2ad | 164 | case LCD20x4: |
Fanta025 | 0:ed0b4e66d2ad | 165 | return 4; |
Fanta025 | 0:ed0b4e66d2ad | 166 | case LCD16x2: |
Fanta025 | 0:ed0b4e66d2ad | 167 | case LCD16x2B: |
Fanta025 | 0:ed0b4e66d2ad | 168 | case LCD20x2: |
Fanta025 | 0:ed0b4e66d2ad | 169 | default: |
Fanta025 | 0:ed0b4e66d2ad | 170 | return 2; |
Fanta025 | 0:ed0b4e66d2ad | 171 | } |
Fanta025 | 0:ed0b4e66d2ad | 172 | } |