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.
Fork of LcdWindow by
ks0108_8bit.cpp@9:2fe93daa2106, 2011-02-22 (annotated)
- Committer:
- hlipka
- Date:
- Tue Feb 22 22:57:44 2011 +0000
- Revision:
- 9:2fe93daa2106
- Parent:
- 3:e5d5e2fe4bf6
fixed semaphore handling - should now be really thread safe (can be called from interrupts)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hlipka | 0:ae5037e3d6e0 | 1 | /* |
| hlipka | 0:ae5037e3d6e0 | 2 | * mbed LCDWindow library |
| hlipka | 0:ae5037e3d6e0 | 3 | * Copyright (c) 2010 Hendrik Lipka |
| hlipka | 0:ae5037e3d6e0 | 4 | * |
| hlipka | 0:ae5037e3d6e0 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| hlipka | 0:ae5037e3d6e0 | 6 | * of this software and associated documentation files (the "Software"), to deal |
| hlipka | 0:ae5037e3d6e0 | 7 | * in the Software without restriction, including without limitation the rights |
| hlipka | 0:ae5037e3d6e0 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| hlipka | 0:ae5037e3d6e0 | 9 | * copies of the Software, and to permit persons to whom the Software is |
| hlipka | 0:ae5037e3d6e0 | 10 | * furnished to do so, subject to the following conditions: |
| hlipka | 0:ae5037e3d6e0 | 11 | * |
| hlipka | 0:ae5037e3d6e0 | 12 | * The above copyright notice and this permission notice shall be included in |
| hlipka | 0:ae5037e3d6e0 | 13 | * all copies or substantial portions of the Software. |
| hlipka | 0:ae5037e3d6e0 | 14 | * |
| hlipka | 0:ae5037e3d6e0 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| hlipka | 0:ae5037e3d6e0 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| hlipka | 0:ae5037e3d6e0 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| hlipka | 0:ae5037e3d6e0 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| hlipka | 0:ae5037e3d6e0 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| hlipka | 0:ae5037e3d6e0 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| hlipka | 0:ae5037e3d6e0 | 21 | * THE SOFTWARE. |
| hlipka | 0:ae5037e3d6e0 | 22 | */ |
| hlipka | 0:ae5037e3d6e0 | 23 | |
| hlipka | 0:ae5037e3d6e0 | 24 | #include "ks0108_8bit.h" |
| hlipka | 0:ae5037e3d6e0 | 25 | |
| hlipka | 0:ae5037e3d6e0 | 26 | #include "BusOut.h" |
| hlipka | 0:ae5037e3d6e0 | 27 | #include "DigitalOut.h" |
| hlipka | 0:ae5037e3d6e0 | 28 | #include "wait_api.h" |
| hlipka | 0:ae5037e3d6e0 | 29 | #include "font.h" |
| hlipka | 0:ae5037e3d6e0 | 30 | |
| hlipka | 0:ae5037e3d6e0 | 31 | #define ENABLE 1 |
| hlipka | 0:ae5037e3d6e0 | 32 | |
| hlipka | 2:5ac5bab7daaf | 33 | void KS0108LCD8bit::writeText(const unsigned int column, const unsigned int row, const char text[]) { |
| hlipka | 0:ae5037e3d6e0 | 34 | int i=0; |
| hlipka | 0:ae5037e3d6e0 | 35 | while (text[i]!=0) { |
| hlipka | 2:5ac5bab7daaf | 36 | character(column+i, row,text[i]); |
| hlipka | 0:ae5037e3d6e0 | 37 | i++; |
| hlipka | 0:ae5037e3d6e0 | 38 | } |
| hlipka | 0:ae5037e3d6e0 | 39 | } |
| hlipka | 0:ae5037e3d6e0 | 40 | |
| hlipka | 0:ae5037e3d6e0 | 41 | void KS0108LCD8bit::clear() { |
| hlipka | 9:2fe93daa2106 | 42 | if (!_guard->take()) |
| hlipka | 9:2fe93daa2106 | 43 | return; |
| hlipka | 0:ae5037e3d6e0 | 44 | clearHalf(_left); |
| hlipka | 0:ae5037e3d6e0 | 45 | if (NULL!=_right) |
| hlipka | 0:ae5037e3d6e0 | 46 | clearHalf(_right); |
| hlipka | 3:e5d5e2fe4bf6 | 47 | _guard->release(); |
| hlipka | 0:ae5037e3d6e0 | 48 | } |
| hlipka | 0:ae5037e3d6e0 | 49 | |
| hlipka | 0:ae5037e3d6e0 | 50 | void KS0108LCD8bit::clearHalf(DigitalOut* cs) { |
| hlipka | 0:ae5037e3d6e0 | 51 | for (int x=0;x<8;x++) |
| hlipka | 0:ae5037e3d6e0 | 52 | { |
| hlipka | 2:5ac5bab7daaf | 53 | sendCmd(0xb8|x,cs); |
| hlipka | 2:5ac5bab7daaf | 54 | wait_us(1); |
| hlipka | 0:ae5037e3d6e0 | 55 | for (int y=0;y<64;y++) |
| hlipka | 0:ae5037e3d6e0 | 56 | { |
| hlipka | 0:ae5037e3d6e0 | 57 | sendCmd(0x40|y,cs); |
| hlipka | 0:ae5037e3d6e0 | 58 | wait_us(1); |
| hlipka | 0:ae5037e3d6e0 | 59 | sendData(0,cs); |
| hlipka | 0:ae5037e3d6e0 | 60 | wait_us(1); |
| hlipka | 0:ae5037e3d6e0 | 61 | } |
| hlipka | 0:ae5037e3d6e0 | 62 | } |
| hlipka | 0:ae5037e3d6e0 | 63 | } |
| hlipka | 0:ae5037e3d6e0 | 64 | |
| hlipka | 2:5ac5bab7daaf | 65 | void KS0108LCD8bit::character(int column, int row, int c){ |
| hlipka | 0:ae5037e3d6e0 | 66 | DigitalOut* cs=NULL; |
| hlipka | 2:5ac5bab7daaf | 67 | int icolumn=column; |
| hlipka | 2:5ac5bab7daaf | 68 | if (icolumn>7) |
| hlipka | 0:ae5037e3d6e0 | 69 | { |
| hlipka | 0:ae5037e3d6e0 | 70 | cs=_right; |
| hlipka | 2:5ac5bab7daaf | 71 | icolumn-=8; |
| hlipka | 0:ae5037e3d6e0 | 72 | } |
| hlipka | 0:ae5037e3d6e0 | 73 | else |
| hlipka | 0:ae5037e3d6e0 | 74 | { |
| hlipka | 0:ae5037e3d6e0 | 75 | cs=_left; |
| hlipka | 0:ae5037e3d6e0 | 76 | } |
| hlipka | 0:ae5037e3d6e0 | 77 | if (NULL==cs) |
| hlipka | 0:ae5037e3d6e0 | 78 | return; |
| hlipka | 0:ae5037e3d6e0 | 79 | |
| hlipka | 9:2fe93daa2106 | 80 | if (!_guard->take()) |
| hlipka | 9:2fe93daa2106 | 81 | return; |
| hlipka | 2:5ac5bab7daaf | 82 | sendCmd(0xb8|row,cs); // set x page |
| hlipka | 0:ae5037e3d6e0 | 83 | |
| hlipka | 2:5ac5bab7daaf | 84 | unsigned int y=icolumn*8; |
| hlipka | 0:ae5037e3d6e0 | 85 | sendCmd(0x40|y,cs); // set start line |
| hlipka | 0:ae5037e3d6e0 | 86 | |
| hlipka | 0:ae5037e3d6e0 | 87 | // send character data |
| hlipka | 0:ae5037e3d6e0 | 88 | for (int i=0;i<8;i++) |
| hlipka | 0:ae5037e3d6e0 | 89 | { |
| hlipka | 0:ae5037e3d6e0 | 90 | sendData(font_data[c][i],cs); |
| hlipka | 0:ae5037e3d6e0 | 91 | } |
| hlipka | 0:ae5037e3d6e0 | 92 | |
| hlipka | 3:e5d5e2fe4bf6 | 93 | _guard->release(); |
| hlipka | 0:ae5037e3d6e0 | 94 | } |
| hlipka | 0:ae5037e3d6e0 | 95 | |
| hlipka | 0:ae5037e3d6e0 | 96 | KS0108LCD8bit::KS0108LCD8bit |
| hlipka | 2:5ac5bab7daaf | 97 | (const unsigned int columns, const unsigned int rows, BusOut *data, const PinName enable, const PinName rs, const PinName leftCS, const PinName rightCS) |
| hlipka | 2:5ac5bab7daaf | 98 | :TextLCDBase(columns, rows) { |
| hlipka | 0:ae5037e3d6e0 | 99 | _data=data; |
| hlipka | 0:ae5037e3d6e0 | 100 | _rs=new DigitalOut(rs); |
| hlipka | 0:ae5037e3d6e0 | 101 | _enable=new DigitalOut(enable); |
| hlipka | 0:ae5037e3d6e0 | 102 | _left=new DigitalOut(leftCS); |
| hlipka | 0:ae5037e3d6e0 | 103 | _left->write(1-ENABLE); |
| hlipka | 0:ae5037e3d6e0 | 104 | if (NC!=rightCS) |
| hlipka | 0:ae5037e3d6e0 | 105 | { |
| hlipka | 0:ae5037e3d6e0 | 106 | _right=new DigitalOut(rightCS); |
| hlipka | 0:ae5037e3d6e0 | 107 | _right->write(1-ENABLE); |
| hlipka | 0:ae5037e3d6e0 | 108 | } |
| hlipka | 0:ae5037e3d6e0 | 109 | else |
| hlipka | 0:ae5037e3d6e0 | 110 | _right=NULL; |
| hlipka | 0:ae5037e3d6e0 | 111 | _enable->write(0); |
| hlipka | 0:ae5037e3d6e0 | 112 | wait_ms(80); |
| hlipka | 0:ae5037e3d6e0 | 113 | } |
| hlipka | 0:ae5037e3d6e0 | 114 | |
| hlipka | 0:ae5037e3d6e0 | 115 | void KS0108LCD8bit::init() { |
| hlipka | 0:ae5037e3d6e0 | 116 | sendCmd(0x3f, _left); |
| hlipka | 0:ae5037e3d6e0 | 117 | wait_ms(10); |
| hlipka | 0:ae5037e3d6e0 | 118 | sendCmd(0xc0, _left); |
| hlipka | 0:ae5037e3d6e0 | 119 | |
| hlipka | 0:ae5037e3d6e0 | 120 | if (NULL!=_right) |
| hlipka | 0:ae5037e3d6e0 | 121 | { |
| hlipka | 0:ae5037e3d6e0 | 122 | sendCmd(0x3f, _right); |
| hlipka | 0:ae5037e3d6e0 | 123 | wait_ms(10); |
| hlipka | 0:ae5037e3d6e0 | 124 | sendCmd(0xc0, _right); |
| hlipka | 0:ae5037e3d6e0 | 125 | } |
| hlipka | 0:ae5037e3d6e0 | 126 | wait_ms(50); |
| hlipka | 0:ae5037e3d6e0 | 127 | clear(); |
| hlipka | 0:ae5037e3d6e0 | 128 | } |
| hlipka | 0:ae5037e3d6e0 | 129 | |
| hlipka | 1:65f72ed914fa | 130 | void KS0108LCD8bit::sendCmd(const unsigned char cmd, DigitalOut *cs) { |
| hlipka | 0:ae5037e3d6e0 | 131 | _rs->write(0); |
| hlipka | 0:ae5037e3d6e0 | 132 | wait_us(1); |
| hlipka | 0:ae5037e3d6e0 | 133 | sendByte(cmd, cs); |
| hlipka | 0:ae5037e3d6e0 | 134 | wait_us(10); |
| hlipka | 0:ae5037e3d6e0 | 135 | } |
| hlipka | 0:ae5037e3d6e0 | 136 | |
| hlipka | 1:65f72ed914fa | 137 | void KS0108LCD8bit::sendData(const unsigned char cmd, DigitalOut *cs) { |
| hlipka | 0:ae5037e3d6e0 | 138 | _rs->write(1); |
| hlipka | 0:ae5037e3d6e0 | 139 | wait_us(1); |
| hlipka | 0:ae5037e3d6e0 | 140 | sendByte(cmd, cs); |
| hlipka | 0:ae5037e3d6e0 | 141 | wait_us(10); |
| hlipka | 0:ae5037e3d6e0 | 142 | } |
| hlipka | 0:ae5037e3d6e0 | 143 | |
| hlipka | 1:65f72ed914fa | 144 | void KS0108LCD8bit::sendByte(const unsigned char byte, DigitalOut *cs) { |
| hlipka | 0:ae5037e3d6e0 | 145 | // display reads flags with rising flank of E |
| hlipka | 0:ae5037e3d6e0 | 146 | _enable->write(0); |
| hlipka | 0:ae5037e3d6e0 | 147 | cs->write(ENABLE); |
| hlipka | 0:ae5037e3d6e0 | 148 | _data->write(byte); |
| hlipka | 0:ae5037e3d6e0 | 149 | |
| hlipka | 0:ae5037e3d6e0 | 150 | wait_us(2); |
| hlipka | 0:ae5037e3d6e0 | 151 | _enable->write(1); |
| hlipka | 0:ae5037e3d6e0 | 152 | |
| hlipka | 0:ae5037e3d6e0 | 153 | // display reads data with falling flank of E |
| hlipka | 0:ae5037e3d6e0 | 154 | wait_us(2); |
| hlipka | 0:ae5037e3d6e0 | 155 | _enable->write(0); |
| hlipka | 0:ae5037e3d6e0 | 156 | |
| hlipka | 0:ae5037e3d6e0 | 157 | wait_us(30); |
| hlipka | 0:ae5037e3d6e0 | 158 | cs->write(1-ENABLE); |
| hlipka | 0:ae5037e3d6e0 | 159 | } |
