Synergia AVR 0.1

lib/lcd/lcd.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2010 Tymon Tobolski
00003  
00004 Permission is hereby granted, free of charge, to any person obtaining
00005 a copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be
00013 included in all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00018 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00019 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00020 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00021 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 */
00023 
00024 #include <util/delay.h>
00025 #include <avr/pgmspace.h>
00026 #include <stdlib.h>
00027 #include "lcd.h"
00028 
00029 #ifndef LCD_RS
00030         #error "LCD_RS not defined"
00031 #endif
00032 
00033 #ifndef LCD_E
00034         #error "LCD_E not defined"
00035 #endif
00036 
00037 #ifndef LCD_D4
00038         #error "LCD_D4 not defined"
00039 #endif
00040 
00041 #ifndef LCD_D5
00042         #error "LCD_D5 not defined"
00043 #endif
00044 
00045 #ifndef LCD_D6
00046         #error "LCD_D6 not defined"
00047 #endif
00048 
00049 #ifndef LCD_D7
00050         #error "LCD_D7 not defined"
00051 #endif
00052 
00053 #ifndef LCD_DP
00054         #error "LCD_DP not defined"
00055 #endif
00056 
00057 #ifndef LCD_CP
00058         #error "LCD_CP not defined"
00059 #endif
00060 
00061 #ifndef LCD_DDR
00062         #error "LCD_DDR not defined"
00063 #endif
00064 
00065 #ifndef LCD_CDR
00066         #error "LCD_CDR not defined"
00067 #endif
00068 
00069 
00070 LCD::LCD(){
00071         _delay_ms(15);
00072         LCD_DP=0x00;
00073         LCD_CP=0x00;
00074         LCD_DDR|=1<<LCD_D7|1<<LCD_D6|1<<LCD_D5|1<<LCD_D4;
00075         LCD_CDR|=1<<LCD_E|1<<LCD_RS;
00076     //--------- Write 0x03 -----------
00077         LCD_DP|=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4;
00078         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00079         _delay_ms(1);
00080         LCD_CP&=~(1<<LCD_E);
00081         _delay_ms(5);
00082         //--------- Write 0x03 -----------
00083         LCD_DP|=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4;
00084         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00085         _delay_ms(1);
00086         LCD_CP&=~(1<<LCD_E);
00087         _delay_ms(1);
00088         //--------- Write 0x03 ------------
00089         LCD_DP|=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4;
00090         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00091         _delay_ms(1);
00092         LCD_CP&=~(1<<LCD_E);
00093         _delay_ms(1);
00094         //--------- Enable Four Bit Mode ----------
00095         LCD_DP&=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|0<<LCD_D4; //Write 0x2
00096         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00097         _delay_ms(1);
00098         LCD_CP&=~(1<<LCD_E);
00099         _delay_ms(1);
00100    //---------- Set Interface Length ----------
00101         LCD_DP|=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|0<<LCD_D4; //Write 0x2 - 4 bits
00102         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00103         _delay_ms(1);
00104         LCD_CP&=~(1<<LCD_E);
00105         _delay_ms(1);
00106         LCD_DP|=1<<LCD_D7|0<<LCD_D6|0<<LCD_D5|0<<LCD_D4; //Write 0x8 - 2 lines, 5x7
00107         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00108         _delay_ms(1);
00109         LCD_CP&=~(1<<LCD_E);
00110         _delay_ms(1);
00111    //---------- Turn off the Display ----------
00112         LCD_DP&=0<<LCD_D7|0<<LCD_D6|0<<LCD_D5|0<<LCD_D4; //Write 0x0 
00113         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00114         _delay_ms(1);
00115         LCD_CP&=~(1<<LCD_E);
00116         _delay_ms(1);
00117         LCD_DP|=1<<LCD_D7|0<<LCD_D6|0<<LCD_D5|0<<LCD_D4; //Write 0x8 
00118         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00119         _delay_ms(1);
00120         LCD_CP&=~(1<<LCD_E);
00121         _delay_ms(1);
00122    //------------ Clear the Display -----------
00123         LCD_DP&=0<<LCD_D7|0<<LCD_D6|0<<LCD_D5|0<<LCD_D4; //Write 0x0 
00124         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00125         _delay_ms(1);
00126         LCD_CP&=~(1<<LCD_E);
00127         _delay_ms(1);
00128         LCD_DP|=0<<LCD_D7|0<<LCD_D6|0<<LCD_D5|1<<LCD_D4; //Write 0x1
00129         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00130         _delay_ms(1);
00131         LCD_CP&=~(1<<LCD_E);
00132         _delay_ms(1);
00133    //-------- Set Cursor Move Direction --------
00134         LCD_DP&=0<<LCD_D7|0<<LCD_D6|0<<LCD_D5|0<<LCD_D4; //Write 0x0 
00135         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00136         _delay_ms(1);
00137         LCD_CP&=~(1<<LCD_E);
00138         _delay_ms(1);
00139         LCD_DP|=0<<LCD_D7|1<<LCD_D6|1<<LCD_D5|0<<LCD_D4; //Write 0x6 - Increment the Cursor
00140         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00141         _delay_ms(1);
00142         LCD_CP&=~(1<<LCD_E);
00143         _delay_ms(1);
00144    //---------- Enable Display/Cursor ----------
00145         LCD_DP&=0<<LCD_D7|0<<LCD_D6|0<<LCD_D5|0<<LCD_D4; //Write 0x0 
00146         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00147         _delay_ms(1);
00148         LCD_CP&=~(1<<LCD_E);
00149         _delay_ms(1);
00150         LCD_DP|=1<<LCD_D7|1<<LCD_D6|1<<LCD_D5|1<<LCD_D4; //Write 0xF - Display on, cursor on, blink on 
00151         LCD_CP|=1<<LCD_E|0<<LCD_RS;             
00152         _delay_ms(1);
00153         LCD_CP&=~(1<<LCD_E);
00154         _delay_ms(1);
00155 }
00156         
00157 void LCD::cmd(unsigned char c){
00158         //4 MSB bits
00159         LCD_DP=((c>>2)&0b00111100);
00160         LCD_CP|=1<<LCD_E;               
00161         _delay_ms(1);
00162         LCD_CP&=~(1<<LCD_E);
00163         _delay_ms(1);
00164         //4 LSB bits
00165         LCD_DP=((c<<2)&0b00111100);
00166         LCD_CP|=1<<LCD_E;               
00167         _delay_ms(1);
00168         LCD_CP&=~(1<<LCD_E);
00169         _delay_ms(1);
00170 }
00171 
00172 void LCD::clear(){
00173         cmd(0x01);
00174 }
00175 
00176 void LCD::gotoxy(unsigned char x, unsigned char y){
00177         // remap lines into proper order
00178         switch(y){
00179                 case 0:  cmd(1<<7 | (0x00 + x)); break;
00180                 case 1:  cmd(1<<7 | (0x40 + x)); break;
00181                 case 2:  cmd(1<<7 | (0x14 + x)); break;
00182                 case 3:  cmd(1<<7 | (0x54 + x)); break;
00183                 default: cmd(1<<7 | (0x00 + x));
00184         }
00185 }
00186 
00187 void LCD::define(const unsigned char * dfn, unsigned char code){
00188         unsigned char a = (code << 3) | 0x40;
00189         for(int i=0; i<8; i++){
00190                 cmd(a++);
00191 //              *this << (unsigned char)*(dfn+i);
00192                 *this << (unsigned char)pgm_read_byte(dfn+i);
00193         }
00194 }
00195 
00196 void LCD::shift(char n){
00197         if(n > 0) while(n--) cmd(0x14); // shift right
00198         else while(n++) cmd(0x10); // shift left
00199 }
00200 
00201 void LCD::cursorOn(){
00202         cmd(0x0E);
00203 }
00204 
00205 void LCD::cursorOff(){
00206         cmd(0x0C);
00207 }
00208 
00209 void LCD::cursorBlink(){
00210         cmd(0x0F);
00211 }
00212 
00213 void LCD::blank(){
00214         cmd(0x08);
00215 }
00216 
00217 void LCD::visible(){
00218         cmd(0x0C);
00219 }
00220 
00221 LCD & LCD::operator<<(const unsigned char c){
00222         LCD_DP=((c>>2)&0b00111100);
00223         LCD_CP|=1<<LCD_RS|1<<LCD_E;
00224         _delay_ms(1);
00225         LCD_CP&=0<<LCD_E|0<<LCD_RS;     
00226         _delay_ms(1);
00227     //4 LSB bits
00228         LCD_DP=((c<<2)&0b00111100);
00229         LCD_CP|=1<<LCD_RS|1<<LCD_E;
00230         _delay_ms(1);
00231         LCD_CP&=0<<LCD_E|0<<LCD_RS;     
00232         _delay_ms(1);
00233         return *this;
00234 }
00235 
00236 LCD & LCD::operator<<(const char c){
00237         *this << (unsigned char)c;
00238         return *this;
00239 }
00240 
00241 LCD & LCD::operator<<(const char * string){
00242         *this << (char*)string;
00243         return *this;
00244 }
00245 
00246 
00247 LCD & LCD::operator<<(char* string){
00248         while(*string != '\0') *this << *string++;
00249         return *this;
00250 }
00251 
00252 LCD & LCD::operator<<(const int number){
00253         *this << (long) number;
00254         return *this;
00255 }
00256 
00257 LCD & LCD::operator<<(const unsigned int number){
00258         *this << (long) number;
00259         return *this;
00260 }
00261 
00262 LCD & LCD::operator<<(const long number){
00263         char str[10];
00264         itoa(number, str, 10);
00265         *this << str;
00266         return *this;
00267 }
 All Classes Files Functions Variables Defines