エレキジャックweb mbed入門 mbed RMCS鉄道模型自動制御システム 課題10です。単線ですが、ポイントを一つ設置して2列車を交互運転できるレイアウトです3アクティブ光センサ、1ポイントを設置しています。コンパクトに小型アルミトランクに組み込みました。

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD0420.cpp Source File

TextLCD0420.cpp

00001 /* mbed TextLCD Library
00002  * Copyright (c) 2007-2009 sford
00003  * Released under the MIT License: http://mbed.org/license/mit
00004  */
00005  // for 04row*20line LCD
00006  
00007 #include "TextLCD0420.h"
00008 
00009 #include "mbed.h"
00010 #include "error.h"
00011 
00012 using namespace mbed;
00013 
00014 /*
00015  * useful info found at http://www.a-netz.de/lcd.en.php
00016  *
00017  *
00018  * Initialisation
00019  * ==============
00020  *
00021  * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
00022  *
00023  * - wait approximately 15 ms so the display is ready to execute commands
00024  * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). 
00025  * - 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.
00026  * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
00027  * - Execute the "clear display" command
00028  *
00029  * Timing
00030  * ======
00031  *
00032  * Nearly all commands transmitted to the display need 40us for execution. 
00033  * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" 
00034  * These commands need 1.64ms for execution. These timings are valid for all displays working with an 
00035  * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you 
00036  * can use the busy flag to test if the display is ready to accept the next command.
00037  * 
00038  * _e is kept high apart from calling clock
00039  * _rw is kept 0 (write) apart from actions that uyse it differently
00040  * _rs is set by the data/command writes
00041  */
00042 
00043 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, 
00044     PinName d2, PinName d3, int columns, int rows) : _rw(rw), _rs(rs), 
00045     _e(e), _d(d0, d1, d2, d3), _columns(columns), _rows(rows) {
00046 
00047    
00048     // Mon, 27 Apr 2009 23:32:34 +0200
00049     // Kevin Konradt:
00050     // When using a LCD with 1 row x 16 characters
00051     // instead of 2x16, try changing _columns to 8.
00052     // (display seems to split the 16 characters into 
00053     // 2 virtual rows with 8 characters each.)
00054 
00055     _rw = 0;
00056     _e  = 1;
00057     _rs = 0; // command mode
00058 
00059     // Should theoretically wait 15ms, but most things will be powered up pre-reset
00060     // so i'll disable that for the minute. If implemented, could wait 15ms post reset
00061     // instead
00062     // wait(0.015); 
00063         
00064     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00065     for(int i=0; i<3; i++) {
00066         writeNibble(0x3);
00067         wait(0.00164);      // this command takes 1.64ms, so wait for it
00068     }
00069     writeNibble(0x2); // 4-bit mode
00070             
00071     writeCommand(0x28);    // Function set 001 BW N F - -  
00072     writeCommand(0x0C);
00073     writeCommand(0x6);  //  Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00074     
00075     cls();
00076 }
00077 
00078 int TextLCD::_putc(int value) {
00079     if(value == '\n') {
00080         newline();
00081     } else {
00082         writeData(value);
00083     }
00084     return value;
00085 }
00086 
00087 int TextLCD::_getc() {
00088     return 0;
00089 }
00090 
00091 void TextLCD::newline() {
00092     _column = 0;
00093     _row++;
00094     if(_row >= _rows) {
00095         _row = 0;
00096     }
00097     locate(_column, _row); 
00098 }
00099 
00100 void TextLCD::locate(int column, int row) {
00101     int address;
00102     if(column < 0 || column >= _columns || row < 0 || row >= _rows) {
00103         error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
00104         return;
00105     }
00106     
00107     _row = row;  
00108     _column = column;
00109 //  02*20 =>   int address = 0x80 + (_row * 40) + _column; // memory starts at 0x80, and is 40 chars long per row 
00110 
00111 
00112       if(_row == 0 ){
00113         address = 0x80+_column;
00114       }
00115       else if(_row == 1 ){
00116         address = 0x80+ (0x40) + _column;
00117       }
00118       else if(_row == 2){
00119         address = 0x80+(0x14)+_column;
00120       }
00121       else if(_row == 3){
00122         address = 0x80+(0x54)+_column;
00123       }
00124 
00125       writeCommand(address);            
00126 }
00127 
00128 void TextLCD::cls() {
00129     writeCommand(0x01); // Clear Display
00130     wait(0.00164f);        // This command takes 1.64 ms
00131       locate(0, 0);
00132 }
00133 
00134 void TextLCD::reset() {
00135     cls();
00136 }
00137 
00138 void TextLCD::clock() {
00139     wait(0.000040f);
00140     _e = 0;
00141     wait(0.000040f);  // most instructions take 40us
00142     _e = 1;    
00143 }
00144 
00145 void TextLCD::writeNibble(int value) {
00146     _d = value;
00147     clock();
00148 }
00149 
00150 void TextLCD::writeByte(int value) {
00151     writeNibble(value >> 4);
00152     writeNibble(value >> 0);
00153 }
00154 
00155 void TextLCD::writeCommand(int command) {
00156     _rs = 0;
00157     writeByte(command);
00158 }
00159 
00160 void TextLCD::writeData(int data) {
00161     _rs = 1;
00162     writeByte(data);
00163     _column++;
00164     if(_column >= _columns) {
00165         newline();
00166     } 
00167 }