Hotboards_SpiLcd.cpp - Library for write and control and lcd with spi interfaces and the ST7032 controller. Base on Arduino's Liquid Cristal library

Dependents:   Hotboards_SpiLcd-Hello_World Hotboards_SpiLcd-Writing_In_Diferent_Rows Hotboards_SpiLcd_Scrolling_Text Hotboards_SpiLcd_AutoScroll ... more

Committer:
Hotboards
Date:
Sat Feb 27 20:02:57 2016 +0000
Revision:
2:9673849ef2e9
Parent:
1:b777b6147d99
added mbed documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:1d03652e9f7a 1 /*
Hotboards 1:b777b6147d99 2 Hotboards_SpiLcd.cpp - Library to control and write an lcd with spi interface and ST7032 controller.
Hotboards 0:1d03652e9f7a 3 base on Arduino's Liquid Cristal library
Hotboards 0:1d03652e9f7a 4 Library ported by diego from Hotboards January 16, 2016.
Hotboards 0:1d03652e9f7a 5 and originally cretaed by
Hotboards 0:1d03652e9f7a 6 by David A. Mellis
Hotboards 0:1d03652e9f7a 7 library modified 5 Jul 2009
Hotboards 0:1d03652e9f7a 8 by Limor Fried (http://www.ladyada.net)
Hotboards 0:1d03652e9f7a 9 example added 9 Jul 2009
Hotboards 0:1d03652e9f7a 10 by Tom Igoe
Hotboards 0:1d03652e9f7a 11 modified 22 Nov 2010
Hotboards 0:1d03652e9f7a 12 by Tom Igoe
Hotboards 0:1d03652e9f7a 13 Released into the public domain.
Hotboards 0:1d03652e9f7a 14 */
Hotboards 0:1d03652e9f7a 15
Hotboards 0:1d03652e9f7a 16 #include "Hotboards_SpiLcd.h"
Hotboards 0:1d03652e9f7a 17
Hotboards 0:1d03652e9f7a 18 Hotboards_SpiLcd::Hotboards_SpiLcd( SPI &spi, PinName cs, PinName rs, PinName rst )
Hotboards 0:1d03652e9f7a 19 : _spi(spi), _cs_pin(cs), _rs_pin(rs), _rst_pin(rst)
Hotboards 0:1d03652e9f7a 20 {
Hotboards 0:1d03652e9f7a 21 _cs_pin = 1; /*chip select deactivated*/
Hotboards 0:1d03652e9f7a 22 _rs_pin = 0;
Hotboards 0:1d03652e9f7a 23 _rst_pin = 1; /*reset not active*/
Hotboards 0:1d03652e9f7a 24 }
Hotboards 0:1d03652e9f7a 25
Hotboards 2:9673849ef2e9 26
Hotboards 0:1d03652e9f7a 27 void Hotboards_SpiLcd::init( void )
Hotboards 0:1d03652e9f7a 28 {
Hotboards 0:1d03652e9f7a 29 _rst_pin = 0;
Hotboards 0:1d03652e9f7a 30 wait( 0.002 );
Hotboards 0:1d03652e9f7a 31 _rst_pin = 1;
Hotboards 0:1d03652e9f7a 32 wait( 0.02 );
Hotboards 0:1d03652e9f7a 33 command( 0x30 );/*wakeup*/
Hotboards 0:1d03652e9f7a 34 wait( 0.002 );
Hotboards 0:1d03652e9f7a 35 command( 0x30 );/*wakeup*/
Hotboards 0:1d03652e9f7a 36 command( 0x30 );/*wakeup*/
Hotboards 0:1d03652e9f7a 37
Hotboards 0:1d03652e9f7a 38 // configure default display functions, two lines, 5x8 charcters
Hotboards 0:1d03652e9f7a 39 _displayfunction = HT_SPILCD_8BITMODE | HT_SPILCD_2LINE | HT_SPILCD_5x8DOTS | HT_SPILCD_EXTINST;
Hotboards 0:1d03652e9f7a 40 command( HT_SPILCD_FUNCTIONSET | _displayfunction );
Hotboards 0:1d03652e9f7a 41
Hotboards 0:1d03652e9f7a 42 command( 0x14 );/*internaloscfrequency*/
Hotboards 0:1d03652e9f7a 43 command( 0x56 );/*powercontroll*/
Hotboards 0:1d03652e9f7a 44 command( 0x6d );/*followercontrol*/
Hotboards 0:1d03652e9f7a 45 wait( 0.2 );
Hotboards 0:1d03652e9f7a 46 command( 0x70 );/*constrast*/
Hotboards 0:1d03652e9f7a 47
Hotboards 0:1d03652e9f7a 48 // reenable shift functions disbling extended instructions
Hotboards 0:1d03652e9f7a 49 _displayfunction &= ~HT_SPILCD_EXTINST;
Hotboards 0:1d03652e9f7a 50 command( HT_SPILCD_FUNCTIONSET | _displayfunction );
Hotboards 0:1d03652e9f7a 51
Hotboards 0:1d03652e9f7a 52 // turn the display on with no cursor or blinking default
Hotboards 0:1d03652e9f7a 53 _displaycontrol = HT_SPILCD_DISPLAYON | HT_SPILCD_CURSOROFF | HT_SPILCD_BLINKOFF;
Hotboards 0:1d03652e9f7a 54 display( );
Hotboards 0:1d03652e9f7a 55
Hotboards 0:1d03652e9f7a 56 // Initialize to default text direction (for romance languages)
Hotboards 0:1d03652e9f7a 57 _displaymode = HT_SPILCD_ENTRYLEFT | HT_SPILCD_ENTRYSHIFTDECREMENT;
Hotboards 0:1d03652e9f7a 58 // set the entry mode
Hotboards 0:1d03652e9f7a 59 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 60 clear( );/*clearscreen*/
Hotboards 0:1d03652e9f7a 61 }
Hotboards 0:1d03652e9f7a 62
Hotboards 2:9673849ef2e9 63
Hotboards 0:1d03652e9f7a 64 void Hotboards_SpiLcd::clear( void )
Hotboards 0:1d03652e9f7a 65 {
Hotboards 0:1d03652e9f7a 66 command( HT_SPILCD_CLEARDISPLAY ); // clear display, set cursor position to zero
Hotboards 0:1d03652e9f7a 67 wait( 0.002 ); // this command takes a long time!
Hotboards 0:1d03652e9f7a 68 }
Hotboards 0:1d03652e9f7a 69
Hotboards 2:9673849ef2e9 70
Hotboards 0:1d03652e9f7a 71 void Hotboards_SpiLcd::home( void )
Hotboards 0:1d03652e9f7a 72 {
Hotboards 0:1d03652e9f7a 73 command( HT_SPILCD_RETURNHOME ); // set cursor position to zero
Hotboards 0:1d03652e9f7a 74 wait( 0.002 ); // this command takes a long time!
Hotboards 0:1d03652e9f7a 75 }
Hotboards 0:1d03652e9f7a 76
Hotboards 2:9673849ef2e9 77
Hotboards 0:1d03652e9f7a 78 void Hotboards_SpiLcd::noDisplay( void )
Hotboards 0:1d03652e9f7a 79 {
Hotboards 0:1d03652e9f7a 80 _displaycontrol &= ~HT_SPILCD_DISPLAYON;
Hotboards 0:1d03652e9f7a 81 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 82 }
Hotboards 0:1d03652e9f7a 83
Hotboards 2:9673849ef2e9 84
Hotboards 0:1d03652e9f7a 85 void Hotboards_SpiLcd::display( void )
Hotboards 0:1d03652e9f7a 86 {
Hotboards 0:1d03652e9f7a 87 _displaycontrol |= HT_SPILCD_DISPLAYON;
Hotboards 0:1d03652e9f7a 88 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 89 }
Hotboards 0:1d03652e9f7a 90
Hotboards 2:9673849ef2e9 91
Hotboards 0:1d03652e9f7a 92 void Hotboards_SpiLcd::noBlink( void )
Hotboards 0:1d03652e9f7a 93 {
Hotboards 0:1d03652e9f7a 94 _displaycontrol &= ~HT_SPILCD_BLINKON;
Hotboards 0:1d03652e9f7a 95 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 96 }
Hotboards 0:1d03652e9f7a 97
Hotboards 2:9673849ef2e9 98
Hotboards 0:1d03652e9f7a 99 void Hotboards_SpiLcd::blink( void )
Hotboards 0:1d03652e9f7a 100 {
Hotboards 0:1d03652e9f7a 101 _displaycontrol |= HT_SPILCD_BLINKON;
Hotboards 0:1d03652e9f7a 102 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 103 }
Hotboards 0:1d03652e9f7a 104
Hotboards 0:1d03652e9f7a 105
Hotboards 2:9673849ef2e9 106
Hotboards 0:1d03652e9f7a 107 void Hotboards_SpiLcd::noCursor( void )
Hotboards 0:1d03652e9f7a 108 {
Hotboards 0:1d03652e9f7a 109 _displaycontrol &= ~HT_SPILCD_CURSORON;
Hotboards 0:1d03652e9f7a 110 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 111 }
Hotboards 0:1d03652e9f7a 112
Hotboards 2:9673849ef2e9 113
Hotboards 0:1d03652e9f7a 114 void Hotboards_SpiLcd::cursor( void )
Hotboards 0:1d03652e9f7a 115 {
Hotboards 0:1d03652e9f7a 116 _displaycontrol |= HT_SPILCD_CURSORON;
Hotboards 0:1d03652e9f7a 117 command( HT_SPILCD_DISPLAYCONTROL | _displaycontrol );
Hotboards 0:1d03652e9f7a 118
Hotboards 0:1d03652e9f7a 119 }
Hotboards 0:1d03652e9f7a 120
Hotboards 2:9673849ef2e9 121
Hotboards 0:1d03652e9f7a 122 void Hotboards_SpiLcd::scrollDisplayLeft( void )
Hotboards 0:1d03652e9f7a 123 {
Hotboards 0:1d03652e9f7a 124 command( HT_SPILCD_CURSORSHIFT | HT_SPILCD_DISPLAYMOVE | HT_SPILCD_MOVELEFT );
Hotboards 0:1d03652e9f7a 125 }
Hotboards 0:1d03652e9f7a 126
Hotboards 2:9673849ef2e9 127
Hotboards 0:1d03652e9f7a 128 void Hotboards_SpiLcd::scrollDisplayRight( void )
Hotboards 0:1d03652e9f7a 129 {
Hotboards 0:1d03652e9f7a 130 command( HT_SPILCD_CURSORSHIFT | HT_SPILCD_DISPLAYMOVE | HT_SPILCD_MOVERIGHT );
Hotboards 0:1d03652e9f7a 131 }
Hotboards 0:1d03652e9f7a 132
Hotboards 2:9673849ef2e9 133
Hotboards 0:1d03652e9f7a 134 void Hotboards_SpiLcd::leftToRight( void )
Hotboards 0:1d03652e9f7a 135 {
Hotboards 0:1d03652e9f7a 136 _displaymode |= HT_SPILCD_ENTRYLEFT;
Hotboards 0:1d03652e9f7a 137 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 138 }
Hotboards 0:1d03652e9f7a 139
Hotboards 2:9673849ef2e9 140
Hotboards 0:1d03652e9f7a 141 void Hotboards_SpiLcd::rightToLeft( void )
Hotboards 0:1d03652e9f7a 142 {
Hotboards 0:1d03652e9f7a 143 _displaymode &= ~HT_SPILCD_ENTRYLEFT;
Hotboards 0:1d03652e9f7a 144 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 145 }
Hotboards 0:1d03652e9f7a 146
Hotboards 2:9673849ef2e9 147
Hotboards 0:1d03652e9f7a 148 void Hotboards_SpiLcd::autoscroll( void )
Hotboards 0:1d03652e9f7a 149 {
Hotboards 0:1d03652e9f7a 150 _displaymode |= HT_SPILCD_ENTRYSHIFTINCREMENT;
Hotboards 0:1d03652e9f7a 151 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 152 }
Hotboards 0:1d03652e9f7a 153
Hotboards 2:9673849ef2e9 154
Hotboards 0:1d03652e9f7a 155 void Hotboards_SpiLcd::noAutoscroll( void )
Hotboards 0:1d03652e9f7a 156 {
Hotboards 0:1d03652e9f7a 157 _displaymode &= ~HT_SPILCD_ENTRYSHIFTINCREMENT;
Hotboards 0:1d03652e9f7a 158 command( HT_SPILCD_ENTRYMODESET | _displaymode );
Hotboards 0:1d03652e9f7a 159 }
Hotboards 0:1d03652e9f7a 160
Hotboards 2:9673849ef2e9 161
Hotboards 0:1d03652e9f7a 162 void Hotboards_SpiLcd::setCursor( uint8_t col, uint8_t row )
Hotboards 0:1d03652e9f7a 163 {
Hotboards 0:1d03652e9f7a 164 const uint8_t Buffer[ 2 ]= { 0x00u, 0x40u };
Hotboards 0:1d03652e9f7a 165 uint8_t address;
Hotboards 0:1d03652e9f7a 166
Hotboards 0:1d03652e9f7a 167 if( row > 1 )
Hotboards 0:1d03652e9f7a 168 {
Hotboards 0:1d03652e9f7a 169 row = 1;
Hotboards 0:1d03652e9f7a 170 }
Hotboards 0:1d03652e9f7a 171 address = Buffer[ row ] + col;
Hotboards 0:1d03652e9f7a 172 command( 0x80 | address );
Hotboards 0:1d03652e9f7a 173 }
Hotboards 0:1d03652e9f7a 174
Hotboards 0:1d03652e9f7a 175
Hotboards 0:1d03652e9f7a 176 /*********** mid level commands, for sending data/cmds */
Hotboards 2:9673849ef2e9 177
Hotboards 0:1d03652e9f7a 178 inline void Hotboards_SpiLcd::command( uint8_t value )
Hotboards 0:1d03652e9f7a 179 {
Hotboards 0:1d03652e9f7a 180 _cs_pin = 0;
Hotboards 0:1d03652e9f7a 181 send( value, 0 );
Hotboards 0:1d03652e9f7a 182 _cs_pin = 1;
Hotboards 0:1d03652e9f7a 183 wait( 0.00003 ); // commands need > 27us to settle
Hotboards 0:1d03652e9f7a 184 }
Hotboards 0:1d03652e9f7a 185
Hotboards 0:1d03652e9f7a 186 /*
Hotboards 0:1d03652e9f7a 187 * Write a character to the LCD.
Hotboards 0:1d03652e9f7a 188 */
Hotboards 0:1d03652e9f7a 189 int Hotboards_SpiLcd::_putc(int value)
Hotboards 0:1d03652e9f7a 190 {
Hotboards 0:1d03652e9f7a 191 _cs_pin = 0;
Hotboards 0:1d03652e9f7a 192 send( value, 1 );
Hotboards 0:1d03652e9f7a 193 _cs_pin = 1;
Hotboards 0:1d03652e9f7a 194 wait( 0.00003 ); // commands need > 27us to settle
Hotboards 0:1d03652e9f7a 195 return 1;
Hotboards 0:1d03652e9f7a 196 }
Hotboards 0:1d03652e9f7a 197
Hotboards 0:1d03652e9f7a 198 int Hotboards_SpiLcd::_getc(void)
Hotboards 0:1d03652e9f7a 199 {
Hotboards 0:1d03652e9f7a 200 return -1;
Hotboards 0:1d03652e9f7a 201 }
Hotboards 0:1d03652e9f7a 202
Hotboards 0:1d03652e9f7a 203 /************ low level data pushing commands **********/
Hotboards 0:1d03652e9f7a 204
Hotboards 0:1d03652e9f7a 205 // write either command or data, with automatic 4/8-bit selection
Hotboards 0:1d03652e9f7a 206 void Hotboards_SpiLcd::send( uint8_t value, uint8_t mode )
Hotboards 0:1d03652e9f7a 207 {
Hotboards 0:1d03652e9f7a 208 _rs_pin = mode;
Hotboards 0:1d03652e9f7a 209 _spi.write( value );
Hotboards 0:1d03652e9f7a 210 }