Displaytronic (Xiamen Zettler)'s I2C text LCD driver
Dependents: IsuProject_LPC1768 robottt
ACM1602.cpp@0:f81621685b23, 2014-03-19 (annotated)
- Committer:
- kayekss
- Date:
- Wed Mar 19 01:08:18 2014 +0000
- Revision:
- 0:f81621685b23
Displaytronic (Xiamen Zettler)'s I2C text LCD driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kayekss | 0:f81621685b23 | 1 | // ==================================================== Mar 19 2014, kayeks == |
kayekss | 0:f81621685b23 | 2 | // ACM1602.cpp |
kayekss | 0:f81621685b23 | 3 | // =========================================================================== |
kayekss | 0:f81621685b23 | 4 | // Displaytronic (Xiamen Zettler)'s I2C text LCD driver |
kayekss | 0:f81621685b23 | 5 | |
kayekss | 0:f81621685b23 | 6 | #include <stdlib.h> |
kayekss | 0:f81621685b23 | 7 | #include "mbed.h" |
kayekss | 0:f81621685b23 | 8 | #include "ACM1602.h" |
kayekss | 0:f81621685b23 | 9 | |
kayekss | 0:f81621685b23 | 10 | /** Constructor of class ACM1602. */ |
kayekss | 0:f81621685b23 | 11 | ACM1602::ACM1602(PinName sda, PinName scl, uint8_t address) |
kayekss | 0:f81621685b23 | 12 | : |
kayekss | 0:f81621685b23 | 13 | i2c(sda, scl) |
kayekss | 0:f81621685b23 | 14 | { |
kayekss | 0:f81621685b23 | 15 | i2c.frequency(100000); |
kayekss | 0:f81621685b23 | 16 | this->address = address & 0xfe; |
kayekss | 0:f81621685b23 | 17 | } |
kayekss | 0:f81621685b23 | 18 | |
kayekss | 0:f81621685b23 | 19 | /** Initialize LCD. */ |
kayekss | 0:f81621685b23 | 20 | void ACM1602::init() { |
kayekss | 0:f81621685b23 | 21 | cls(); |
kayekss | 0:f81621685b23 | 22 | command(0x38); // 8-bit mode, double-line |
kayekss | 0:f81621685b23 | 23 | command(0x0c); // Display on, cursor off |
kayekss | 0:f81621685b23 | 24 | command(0x06); // Enable DDRAM address increment |
kayekss | 0:f81621685b23 | 25 | } |
kayekss | 0:f81621685b23 | 26 | |
kayekss | 0:f81621685b23 | 27 | /** Clear display and reset DDRAM address. */ |
kayekss | 0:f81621685b23 | 28 | void ACM1602::cls() { |
kayekss | 0:f81621685b23 | 29 | command(0x01); |
kayekss | 0:f81621685b23 | 30 | wait(0.01); |
kayekss | 0:f81621685b23 | 31 | memset(lineBuffer[0], ' ', 16); |
kayekss | 0:f81621685b23 | 32 | memset(lineBuffer[1], ' ', 16); |
kayekss | 0:f81621685b23 | 33 | this->col = 0; |
kayekss | 0:f81621685b23 | 34 | this->row = 0; |
kayekss | 0:f81621685b23 | 35 | } |
kayekss | 0:f81621685b23 | 36 | |
kayekss | 0:f81621685b23 | 37 | /** Locate the cursor position. */ |
kayekss | 0:f81621685b23 | 38 | void ACM1602::locate(int col, int row) { |
kayekss | 0:f81621685b23 | 39 | if (col < 0 || col > 15 || row < 0 || row > 1) { |
kayekss | 0:f81621685b23 | 40 | return; |
kayekss | 0:f81621685b23 | 41 | } |
kayekss | 0:f81621685b23 | 42 | command(0x80 | (row << 6) | col); |
kayekss | 0:f81621685b23 | 43 | this->col = col; |
kayekss | 0:f81621685b23 | 44 | this->row = row; |
kayekss | 0:f81621685b23 | 45 | } |
kayekss | 0:f81621685b23 | 46 | |
kayekss | 0:f81621685b23 | 47 | /** Implementation of putc from Stream class. */ |
kayekss | 0:f81621685b23 | 48 | int ACM1602::_putc(int c) { |
kayekss | 0:f81621685b23 | 49 | switch (c) { |
kayekss | 0:f81621685b23 | 50 | case '\r': |
kayekss | 0:f81621685b23 | 51 | this->col = 0; |
kayekss | 0:f81621685b23 | 52 | locate(this->col, this->row); |
kayekss | 0:f81621685b23 | 53 | break; |
kayekss | 0:f81621685b23 | 54 | case '\n': |
kayekss | 0:f81621685b23 | 55 | if (this->row == 1) { |
kayekss | 0:f81621685b23 | 56 | shiftUp(); |
kayekss | 0:f81621685b23 | 57 | } else { |
kayekss | 0:f81621685b23 | 58 | locate(this->col, ++this->row); |
kayekss | 0:f81621685b23 | 59 | } |
kayekss | 0:f81621685b23 | 60 | break; |
kayekss | 0:f81621685b23 | 61 | default: |
kayekss | 0:f81621685b23 | 62 | if (this->col > 15) { |
kayekss | 0:f81621685b23 | 63 | shiftUp(); |
kayekss | 0:f81621685b23 | 64 | this->col = 0; |
kayekss | 0:f81621685b23 | 65 | this->row = 1; |
kayekss | 0:f81621685b23 | 66 | locate(this->col, this->row); |
kayekss | 0:f81621685b23 | 67 | } |
kayekss | 0:f81621685b23 | 68 | data(c); |
kayekss | 0:f81621685b23 | 69 | lineBuffer[this->row][this->col] = c; |
kayekss | 0:f81621685b23 | 70 | this->col++; |
kayekss | 0:f81621685b23 | 71 | break; |
kayekss | 0:f81621685b23 | 72 | } |
kayekss | 0:f81621685b23 | 73 | return 0; |
kayekss | 0:f81621685b23 | 74 | } |
kayekss | 0:f81621685b23 | 75 | |
kayekss | 0:f81621685b23 | 76 | /** Implementation of getc from Stream class. */ |
kayekss | 0:f81621685b23 | 77 | int ACM1602::_getc() { |
kayekss | 0:f81621685b23 | 78 | return EOF; |
kayekss | 0:f81621685b23 | 79 | } |
kayekss | 0:f81621685b23 | 80 | |
kayekss | 0:f81621685b23 | 81 | /** Shift up the lower line for scroll. */ |
kayekss | 0:f81621685b23 | 82 | void ACM1602::shiftUp() { |
kayekss | 0:f81621685b23 | 83 | memcpy(lineBuffer[0], lineBuffer[1], 16); |
kayekss | 0:f81621685b23 | 84 | memset(lineBuffer[1], ' ', 16); |
kayekss | 0:f81621685b23 | 85 | |
kayekss | 0:f81621685b23 | 86 | locate(0, 0); |
kayekss | 0:f81621685b23 | 87 | for (uint8_t i = 0; i < 16; i++) { |
kayekss | 0:f81621685b23 | 88 | data(lineBuffer[0][i]); |
kayekss | 0:f81621685b23 | 89 | } |
kayekss | 0:f81621685b23 | 90 | locate(0, 1); |
kayekss | 0:f81621685b23 | 91 | for (uint8_t i = 0; i < 16; i++) { |
kayekss | 0:f81621685b23 | 92 | data(' '); |
kayekss | 0:f81621685b23 | 93 | } |
kayekss | 0:f81621685b23 | 94 | locate(0, 1); |
kayekss | 0:f81621685b23 | 95 | } |
kayekss | 0:f81621685b23 | 96 | |
kayekss | 0:f81621685b23 | 97 | /** Send a command byte to LCD. */ |
kayekss | 0:f81621685b23 | 98 | void ACM1602::command(uint8_t b) { |
kayekss | 0:f81621685b23 | 99 | wait_us(25); i2c.start(); |
kayekss | 0:f81621685b23 | 100 | wait_us(25); i2c.write(this->address); |
kayekss | 0:f81621685b23 | 101 | wait_us(25); i2c.write(0x00); |
kayekss | 0:f81621685b23 | 102 | wait_us(25); i2c.write(b); |
kayekss | 0:f81621685b23 | 103 | wait_us(25); i2c.stop(); |
kayekss | 0:f81621685b23 | 104 | wait_us(40); |
kayekss | 0:f81621685b23 | 105 | } |
kayekss | 0:f81621685b23 | 106 | |
kayekss | 0:f81621685b23 | 107 | /** Send a data byte to LCD. */ |
kayekss | 0:f81621685b23 | 108 | void ACM1602::data(uint8_t b) { |
kayekss | 0:f81621685b23 | 109 | wait_us(25); i2c.start(); |
kayekss | 0:f81621685b23 | 110 | wait_us(25); i2c.write(this->address); |
kayekss | 0:f81621685b23 | 111 | wait_us(25); i2c.write(0x80); |
kayekss | 0:f81621685b23 | 112 | wait_us(25); i2c.write(b); |
kayekss | 0:f81621685b23 | 113 | wait_us(25); i2c.stop(); |
kayekss | 0:f81621685b23 | 114 | wait_us(40); |
kayekss | 0:f81621685b23 | 115 | } |