![]() | LCD Library 1.2.1 LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library. |
00001 // ---------------------------------------------------------------------------00002 // Created/Adapted by Bill Perry 2012-03-1600003 // Copyright 2012 - Under creative commons license 3.0:00004 // Attribution-ShareAlike CC BY-SA00005 //00006 // This software is furnished "as is", without technical support, and with no 00007 // warranty, express or implied, as to its usefulness for any purpose.00008 //00009 // Thread Safe: No00010 // Extendable: Yes00011 //00012 // @file LiquidCrystal_SR2W.cpp00013 // Connects a hd44780 LCD using 2 pins from the Arduino, via an 8-bit 00014 // ShiftRegister (SR2W from now on).00015 // 00016 // @brief 00017 // This is a port of the ShiftRegLCD library from raron and ported to the00018 // LCD library.00019 //00020 //00021 // See the corresponding SR2W header file for full details.00022 //00023 // History00024 // 2012.03.29 bperrybap - Fixed incorrect use of 5x10 for default font 00025 // (now matches original LQ library)00026 // Fixed typo in SR2W mask define names00027 // changed default backlight state to on00028 // 2012.03.16 bperrybap - created/modified from SR sources to create SR2W00029 // @author B. Perry - bperrybap@opensource.billsworld.billandterrie.com00030 // ---------------------------------------------------------------------------00031 00032 #include "LiquidCrystal_SR2W.h"00033 00034 // CONSTRUCTORS00035 // ---------------------------------------------------------------------------00036 // Assuming 1 line 8 pixel high font00037LiquidCrystal_SR2W::LiquidCrystal_SR2W (uint8_t srdata, uint8_t srclock, t_backlighPol blpol) 00038 { 00039 init ( srdata, srclock, blpol, 1, 0 ); 00040 } 00041 00042 00043 // PRIVATE METHODS00044 // ---------------------------------------------------------------------------00045 00046 //00047 // init00048 void LiquidCrystal_SR2W::init(uint8_t srdata, uint8_t srclock, t_backlighPol blpol, uint8_t lines, uint8_t font) 00049 { 00050 _srDataRegister = fio_pinToOutputRegister(srdata); 00051 _srDataMask = fio_pinToBit(srdata); 00052 _srClockRegister = fio_pinToOutputRegister(srclock); 00053 _srClockMask = fio_pinToBit(srclock); 00054 00055 _blPolarity = blpol; 00056 00057 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; 00058 00059 backlight(); // set default backlight state to on00060 } 00061 00062 //00063 // loadSR00064 void LiquidCrystal_SR2W::loadSR(uint8_t val) 00065 { 00066 // Clear to keep Enable LOW while clocking in new bits00067 fio_shiftOut(_srDataRegister, _srDataMask, _srClockRegister, _srClockMask); 00068 00069 00070 // clock out SR data byte00071 fio_shiftOut(_srDataRegister, _srDataMask, _srClockRegister, _srClockMask, val, MSBFIRST); 00072 00073 00074 // strobe LCD enable which can now be toggled by the data line00075 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 00076 { 00077 fio_digitalWrite_HIGH(_srDataRegister, _srDataMask); 00078 waitUsec (1); // enable pulse must be >450ns 00079 fio_digitalWrite_SWITCHTO(_srDataRegister, _srDataMask, LOW); 00080 } // end critical section00081 } 00082 00083 // PUBLIC METHODS00084 // ---------------------------------------------------------------------------00085 00086 00087 /************ low level data pushing commands **********/00088 //00089 // send00090voidLiquidCrystal_SR2W::send(uint8_t value, uint8_t mode) 00091 { 00092 uint8_t myMode = ( mode == DATA ) ? SR2W_RS_MASK : 0; 00093 00094 myMode = myMode | SR2W_EN_MASK | _blMask; 00095 00096 if ( mode != FOUR_BITS ) 00097 { 00098 loadSR(myMode | ((value >> 1) & SR2W_DATA_MASK)); // upper nibble00099 } 00100 00101 loadSR(myMode | ((value << 3) & SR2W_DATA_MASK)); // lower nibble00102 00103 /*00104 * Don't call waitUsec()00105 * do our own delay optmization since this code is so fast it needs some added delay00106 * even on slower AVRs.00107 */00108 #if (F_CPU <= 16000000)00109 delayMicroseconds ( 10 ); // commands & data writes need > 37us to complete00110 #else00111 delayMicroseconds ( 37 ); // commands & data writes need > 37us to complete00112 #endif00113 } 00114 00115 //00116 // setBacklight00117voidLiquidCrystal_SR2W::setBacklight ( uint8_t value ) 00118 { 00119 // Check for polarity to configure mask accordingly00120 // ----------------------------------------------------------00121 if ( ((_blPolarity == POSITIVE) && (value > 0)) || 00122 ((_blPolarity == NEGATIVE ) && ( value == 0 )) ) 00123 { 00124 _blMask = SR2W_BL_MASK; 00125 } 00126 else00127 { 00128 _blMask = 0; 00129 } 00130 00131 // send dummy data of blMask to set BL pin00132 // Note: loadSR() will strobe the data line trying to pulse EN00133 // but E will not strobe because the EN output bit is not set.00134 loadSR(_blMask); 00135 }