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