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.
I2CTextLCD.cpp
- Committer:
- RodColeman
- Date:
- 2011-08-18
- Revision:
- 0:9d621b44ea24
- Child:
- 1:e60cbc407d73
File content as of revision 0:9d621b44ea24:
/* mbed I2CTextLCD Library
* Copyright (c) 2007-2009 sford
* Copyright (c) 2010 Wim De Roeve changed to work with I2C PCF8575
* Released under the MIT License: http://mbed.org/license/mit
*/
#include "I2CTextLCD.h"
#include "mbed.h"
#include "error.h"
using namespace mbed;
/*
* useful info found at http://www.a-netz.de/lcd.en.php
*
*
* Initialisation
* ==============
*
* After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
*
* - wait approximately 15 ms so the display is ready to execute commands
* - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
* - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command.
* - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
* - Execute the "clear display" command
*
* Timing
* ======
*
* Nearly all commands transmitted to the display need 40us for execution.
* Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
* These commands need 1.64ms for execution. These timings are valid for all displays working with an
* internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
* can use the busy flag to test if the display is ready to accept the next command.
*
*/
I2CTextLCD::I2CTextLCD(PinName sda, PinName scl, int i2cAddress , int columns, int rows,
bool backlight) : _i2c(sda, scl) {
_i2cAddress = i2cAddress;
_columns = columns;
_rows = rows;
_backlight=backlight;
_i2c.frequency(70000);
// Should theoretically wait 15ms, but most things will be powered up pre-reset
// so i'll disable that for the minute. If implemented, could wait 15ms post reset
// instead
// Winstar 20x4 WH2004-NYG- needs 40ms after VDD> 4,5V
wait(0.05);
// send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
for (int i=0; i<3; i++) {
writeCommand(0x3); //send 3 times mode
wait(0.00164); // this command takes 1.64ms, so wait for it
}
writeCommand(0x2); // 4-bit mode
writeCommand(0x2); // 4-bit mode
writeCommand(0x28); // Function set 001 BW N F - -
wait(0.001);
writeCommand(0x28); // Function set 001 BW N F
wait(0.001);
writeCommand(0x0C); // turns display ON, no cursor. Use 0x0E for cursor ON.
/* 0x28 also works for Winstar WEH002004ALPP5N00000 OLED display. 0x29= westEuro fon table, 0x2A = UK/Russian
*/
// Added RC 2011-8-11
writeCommand(0x01); // clear display RAM all to 00
wait(0.007); // 6.2ms specified for OLED display to recover from RAM clear
writeCommand(0x06); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
cls();
}
int I2CTextLCD::_putc(int value) {
if (value == '\n') {
newline();
} else {
writeData(value);
}
return value;
}
int I2CTextLCD::_getc() {
return 0;
}
/* void I2CTextLCD::backlight(bool status) {
_backlight=status;
if (_backlight)
writeI2CByte(BACKLIGHT_ON | E1_ON);
else
writeI2CByte(E1_ON);
}
*/
void I2CTextLCD::newline() {
_column = 0;
_row++;
if (_row >= _rows) {
_row = 0;
}
locate(_column, _row);
}
void I2CTextLCD::locate(int column, int row) {
if (column < 0 || column >= _columns || row < 0 || row >= _rows) {
error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
return;
}
_row = row;
_column = column;
int address;
if (row==0){
address = 0x80+_column;
}
else if (row==1){
address= 0xc0+_column;
}
else if (row==2){
address=0x94+_column;
}
else if(row==3){
address=0xd4+_column;
}
/*
int address = 0x80 + (_row * 40) + _column; // memory starts at 0x00, and is 40 chars long per row
Set bit 7 also, to signify ADDRESS SET mode.
// pc_LCD.traceOut("locate %dx%d\r\n", column, row);
*/
writeCommand(address); // must set bit 7 to indicate address SET command
wait(0.004); // takes 1.5ms on Winstar LCD 20x4
}
void I2CTextLCD::cls() {
writeCommand(0x01); // Clear Display
wait(0.007f); // This command takes 1.64 ms (LCD), 6.2ms for Winstar OLED
locate(0, 0);
}
void I2CTextLCD::reset() {
cls();
}
void I2CTextLCD::writeByte(int c, bool rs) {
// void writeByte(char c, bool rs){
char cmd[2]; //led = 1;
cmd[0]=c>>2; // upper nibble
cmd[0]=cmd[0] & 0x3c;
cmd[1]=c<<2; // lower nibble
cmd[1]=cmd[1] & 0x3c;
if (rs) {
cmd[0]=cmd[0] | 0x01;
cmd[1]=cmd[1] | 0x01;
}
_i2c.write( _i2cAddress,cmd,1); // E=0, write upper nibble
wait_us(1);
cmd[0]=cmd[0]|0x02; //E=1
_i2c.write( _i2cAddress, cmd,1);
wait_us(1);
cmd[0]=cmd[0]&0x3d; //E=0, RS preserved in bit 0
_i2c.write( _i2cAddress, cmd,1);
wait_us(1);
cmd[0]=cmd[1]; //E=0, write lower nibble
wait_us(1);
cmd[0]=cmd[0]|0x02; //E=1
_i2c.write( _i2cAddress, cmd,1);
wait_us(1);
cmd[0]=cmd[0]&0x3d; //E=0, RS preserved in bit 0
_i2c.write( _i2cAddress, cmd,1);
_i2c.write ( _i2cAddress,cmd,1);
// led = 0;
wait_us(1);
}
//void I2CTextLCD::writeByte(int data, bool rs) {
/*
writeNibble(data >> 4 , rs);
writeNibble(data >> 0 , rs);
RC 2011-8-11. change to d4..d7 - PCF8574 ports P2 .. P5
*/
//data = data>>2;
//data = data & 0x3c;
//writeNibble(data , rs);
//data = data<<2;
//data = data & 0x3c;
//writeNibble(data , rs);
void I2CTextLCD::writeCommand(int command) {
// RS = 0;
writeByte(command,false);
}
void I2CTextLCD::writeData(int data) {
//RS = 1
writeByte(data,true);
_column++;
if (_column >= _columns) {
newline();
}
}
/* void I2CTextLCD::writeI2CByte(int data) {
char cmd[2];
cmd[0] = (data & 0xFF);
cmd[1] = (data >> 8);
_i2c.write(_i2cAddress, cmd, 2);
*/