libraries / Visuino_LiquidCrystal / Visuino_LiquidCrystal_SR2W.cppon commit Added link to project report (97a3ba0)
   1// ---------------------------------------------------------------------------
   2// Created/Adapted by Bill Perry 2012-03-16
   3// Copyright 2012 - Under creative commons license 3.0:
   4//        Attribution-ShareAlike CC BY-SA
   5//
   6// This software is furnished "as is", without technical support, and with no 
   7// warranty, express or implied, as to its usefulness for any purpose.
   8//
   9// Thread Safe: No
  10// Extendable: Yes
  11//
  12// @file LiquidCrystal_SR2W.cpp
  13// Connects a hd44780 LCD using 2 pins from the Arduino, via an 8-bit 
  14// ShiftRegister (SR2W from now on).
  15// 
  16// @brief 
  17// This is a port of the ShiftRegLCD library from raron and ported to the
  18// LCD library.
  19//
  20//
  21// See the corresponding SR2W header file for full details.
  22//
  23// History
  24// 2012.03.29  bperrybap - Fixed incorrect use of 5x10 for default font 
  25//                         (now matches original LQ library)
  26//                         Fixed typo in SR2W mask define names
  27//                         changed default backlight state to on
  28// 2012.03.16  bperrybap - created/modified from SR sources to create SR2W
  29// @author B. Perry - bperrybap@opensource.billsworld.billandterrie.com
  30// ---------------------------------------------------------------------------
  31
  32#include "Visuino_LiquidCrystal_SR2W.h"
  33
  34// CONSTRUCTORS
  35// ---------------------------------------------------------------------------
  36// Assuming 1 line 8 pixel high font
  37LiquidCrystal_SR2W::LiquidCrystal_SR2W (uint8_t srdata, uint8_t srclock, t_backlighPol blpol)
  38{
  39        init ( srdata, srclock, blpol, 1, 0 );
  40}
  41
  42
  43// PRIVATE METHODS
  44// ---------------------------------------------------------------------------
  45
  46//
  47// init
  48void LiquidCrystal_SR2W::init(uint8_t srdata, uint8_t srclock, t_backlighPol blpol, uint8_t lines, uint8_t font)
  49{
  50        _srDataRegister = fio_pinToOutputRegister(srdata);
  51        _srDataMask = fio_pinToBit(srdata);
  52        _srClockRegister = fio_pinToOutputRegister(srclock);
  53        _srClockMask = fio_pinToBit(srclock);
  54   
  55        _blPolarity = blpol;
  56   
  57        _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  58   
  59        backlight(); // set default backlight state to on
  60}
  61
  62//
  63// loadSR
  64void LiquidCrystal_SR2W::loadSR(uint8_t val)
  65{
  66        // Clear to keep Enable LOW while clocking in new bits
  67        fio_shiftOut(_srDataRegister, _srDataMask, _srClockRegister, _srClockMask);
  68   
  69   
  70        // clock out SR data byte
  71        fio_shiftOut(_srDataRegister, _srDataMask, _srClockRegister, _srClockMask, val, MSBFIRST);
  72   
  73        
  74        // strobe LCD enable which can now be toggled by the data line
  75        ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  76        {
  77                fio_digitalWrite_HIGH(_srDataRegister, _srDataMask);
  78                waitUsec (1);         // enable pulse must be >450ns               
  79                fio_digitalWrite_SWITCHTO(_srDataRegister, _srDataMask, LOW);
  80        } // end critical section
  81}
  82
  83// PUBLIC METHODS
  84// ---------------------------------------------------------------------------
  85
  86
  87/************ low level data pushing commands **********/
  88//
  89// send
  90void LiquidCrystal_SR2W::send(uint8_t value, uint8_t mode)
  91{
  92        uint8_t myMode = ( mode == DATA ) ? SR2W_RS_MASK : 0; 
  93   
  94        myMode = myMode | SR2W_EN_MASK | _blMask;
  95
  96        if ( mode != FOUR_BITS )
  97        {
  98                loadSR(myMode | ((value >> 1) & SR2W_DATA_MASK)); // upper nibble
  99        }
 100
 101        loadSR(myMode | ((value << 3) & SR2W_DATA_MASK)); // lower nibble
 102   
 103        /*
 104         * Don't call waitUsec()
 105         * do our own delay optmization since this code is so fast it needs some added delay
 106         * even on slower AVRs.
 107         */
 108#if (F_CPU <= 16000000)
 109        delayMicroseconds ( 10 );      // commands & data writes need > 37us to complete
 110#else
 111        delayMicroseconds ( 37 );      // commands & data writes need > 37us to complete
 112#endif
 113}
 114
 115//
 116// setBacklight
 117void LiquidCrystal_SR2W::setBacklight ( uint8_t value ) 
 118{ 
 119        // Check for polarity to configure mask accordingly
 120        // ----------------------------------------------------------
 121        if  ( ((_blPolarity == POSITIVE) && (value > 0)) || 
 122        ((_blPolarity == NEGATIVE ) && ( value == 0 )) )
 123        {
 124                _blMask = SR2W_BL_MASK;
 125        }
 126        else 
 127        {
 128                _blMask = 0;
 129        }
 130   
 131        // send dummy data of blMask to set BL pin
 132        // Note: loadSR() will strobe the data line trying to pulse EN
 133        // but E will not strobe because the EN output bit is not set.
 134        loadSR(_blMask); 
 135}