An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01 MOD by Zak

Dependents:   mbed_XBus_Test

Fork of ACM1602NI by Takuo WATANABE

Committer:
takuo
Date:
Mon May 13 13:00:04 2013 +0000
Revision:
2:6a201d126c69
Parent:
1:84527dcd3fcc
Child:
3:bac80efb323e
Fixed the software description

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takuo 2:6a201d126c69 1 /* An mbed text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01
takuo 1:84527dcd3fcc 2 * Copyright 2013, wtakuo
takuo 1:84527dcd3fcc 3 *
takuo 1:84527dcd3fcc 4 * Licensed under the Apache License, Version 2.0 (the "License");
takuo 1:84527dcd3fcc 5 * you may not use this file except in compliance with the License.
takuo 1:84527dcd3fcc 6 * You may obtain a copy of the License at
takuo 1:84527dcd3fcc 7 *
takuo 1:84527dcd3fcc 8 * http://www.apache.org/licenses/LICENSE-2.0
takuo 1:84527dcd3fcc 9 *
takuo 1:84527dcd3fcc 10 * Unless required by applicable law or agreed to in writing, software
takuo 1:84527dcd3fcc 11 * distributed under the License is distributed on an "AS IS" BASIS,
takuo 1:84527dcd3fcc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
takuo 1:84527dcd3fcc 13 * See the License for the specific language governing permissions and
takuo 1:84527dcd3fcc 14 * limitations under the License.
takuo 1:84527dcd3fcc 15 */
takuo 1:84527dcd3fcc 16
takuo 0:aac87cbfc229 17 #include "mbed.h"
takuo 0:aac87cbfc229 18 #include "ACM1602NI.h"
takuo 0:aac87cbfc229 19
takuo 0:aac87cbfc229 20 #define _i2cSUCCESS 0
takuo 0:aac87cbfc229 21 #define _i2cFAILURE 1
takuo 0:aac87cbfc229 22
takuo 0:aac87cbfc229 23 ACM1602NI::ACM1602NI(I2C &i2c): _i2c(i2c) {
takuo 0:aac87cbfc229 24 init();
takuo 0:aac87cbfc229 25 }
takuo 0:aac87cbfc229 26
takuo 0:aac87cbfc229 27 void ACM1602NI::init() {
takuo 0:aac87cbfc229 28 writeCommand(0x01);
takuo 0:aac87cbfc229 29 wait_ms(i2c_command_wait_ms);
takuo 0:aac87cbfc229 30 writeCommand(0x38);
takuo 0:aac87cbfc229 31 wait_ms(i2c_command_wait_ms);
takuo 0:aac87cbfc229 32 writeCommand(0x0f);
takuo 0:aac87cbfc229 33 wait_ms(i2c_command_wait_ms);
takuo 0:aac87cbfc229 34 writeCommand(0x06);
takuo 0:aac87cbfc229 35 wait_ms(i2c_command_wait_ms);
takuo 0:aac87cbfc229 36 locate(0, 0);
takuo 0:aac87cbfc229 37 }
takuo 0:aac87cbfc229 38
takuo 0:aac87cbfc229 39 int ACM1602NI::writeBytes(const char *data, int length, bool repeated) {
takuo 0:aac87cbfc229 40 wait_us(i2c_bit_wait_us);
takuo 0:aac87cbfc229 41 _i2c.start();
takuo 0:aac87cbfc229 42 for (int i = 0; i < length; i++) {
takuo 0:aac87cbfc229 43 wait_us(i2c_bit_wait_us);
takuo 0:aac87cbfc229 44 if (_i2c.write(data[i]) != 1) {
takuo 0:aac87cbfc229 45 wait_us(i2c_bit_wait_us);
takuo 0:aac87cbfc229 46 _i2c.stop();
takuo 0:aac87cbfc229 47 return _i2cFAILURE;
takuo 0:aac87cbfc229 48 }
takuo 0:aac87cbfc229 49 }
takuo 0:aac87cbfc229 50 if (!repeated) {
takuo 0:aac87cbfc229 51 wait_us(i2c_bit_wait_us);
takuo 0:aac87cbfc229 52 _i2c.stop();
takuo 0:aac87cbfc229 53 }
takuo 0:aac87cbfc229 54 return _i2cSUCCESS;
takuo 0:aac87cbfc229 55 }
takuo 0:aac87cbfc229 56
takuo 0:aac87cbfc229 57 void ACM1602NI::character(int column, int row, int c) {
takuo 0:aac87cbfc229 58 int a = address(column, row);
takuo 0:aac87cbfc229 59 writeCommand(a);
takuo 0:aac87cbfc229 60 writeData(c);
takuo 0:aac87cbfc229 61 }
takuo 0:aac87cbfc229 62
takuo 0:aac87cbfc229 63 void ACM1602NI::cls() {
takuo 0:aac87cbfc229 64 writeCommand(0x01);
takuo 0:aac87cbfc229 65 wait_ms(i2c_command_wait_ms);
takuo 0:aac87cbfc229 66 locate(0, 0);
takuo 0:aac87cbfc229 67 }
takuo 0:aac87cbfc229 68
takuo 0:aac87cbfc229 69 void ACM1602NI::locate(int column, int row) {
takuo 0:aac87cbfc229 70 _column = column;
takuo 0:aac87cbfc229 71 _row = row;
takuo 0:aac87cbfc229 72 }
takuo 0:aac87cbfc229 73
takuo 0:aac87cbfc229 74 int ACM1602NI::_putc(int value) {
takuo 0:aac87cbfc229 75 if (value == '\n') {
takuo 0:aac87cbfc229 76 _column = 0;
takuo 0:aac87cbfc229 77 _row = (_row + 1) % rows();
takuo 0:aac87cbfc229 78 }
takuo 0:aac87cbfc229 79 else {
takuo 0:aac87cbfc229 80 character(_column, _row, value);
takuo 0:aac87cbfc229 81 _column++;
takuo 0:aac87cbfc229 82 if (_column >= columns()) {
takuo 0:aac87cbfc229 83 _column = 0;
takuo 0:aac87cbfc229 84 _row = (_row + 1) % rows();
takuo 0:aac87cbfc229 85 }
takuo 0:aac87cbfc229 86 }
takuo 0:aac87cbfc229 87 return value;
takuo 0:aac87cbfc229 88 }
takuo 0:aac87cbfc229 89
takuo 0:aac87cbfc229 90 int ACM1602NI::_getc() {
takuo 0:aac87cbfc229 91 return -1;
takuo 0:aac87cbfc229 92 }
takuo 0:aac87cbfc229 93
takuo 0:aac87cbfc229 94 void ACM1602NI::writeCommand(int command) {
takuo 0:aac87cbfc229 95 char bs[3] = { i2c_addr, 0x00, command };
takuo 0:aac87cbfc229 96 writeBytes(bs, 3);
takuo 0:aac87cbfc229 97 }
takuo 0:aac87cbfc229 98
takuo 0:aac87cbfc229 99 void ACM1602NI::writeData(int data) {
takuo 0:aac87cbfc229 100 char bs[3] = { i2c_addr, 0x80, data };
takuo 0:aac87cbfc229 101 writeBytes(bs, 3);
takuo 0:aac87cbfc229 102 }
takuo 0:aac87cbfc229 103
takuo 0:aac87cbfc229 104 int ACM1602NI::address(int column, int row) {
takuo 0:aac87cbfc229 105 return 0x80 + row * 0x40 + column;
takuo 0:aac87cbfc229 106 }
takuo 0:aac87cbfc229 107
takuo 0:aac87cbfc229 108 int ACM1602NI::columns() {
takuo 0:aac87cbfc229 109 return display_columns;
takuo 0:aac87cbfc229 110 }
takuo 0:aac87cbfc229 111
takuo 0:aac87cbfc229 112 int ACM1602NI::rows() {
takuo 0:aac87cbfc229 113 return display_rows;
takuo 0:aac87cbfc229 114 }
takuo 0:aac87cbfc229 115