|
Controlling numeric LCD isn't so tricky as it may look like. O course you can find numbers of LCD libraries. One of more universal you can find in AVRLIB library for WinAVR AVR GCC compiler. Main disadvantage of such universal libraries that they compile all functions even if you don't want them. This way huge libraries occupy more program memory than you would like to. One way is to write simple routines specified only to your design. My simple LCD library include only 8 bit mode. Each pin, connected to LCD, can be configured separately. For instance:
#define LCD_RS 0 //define MCU pin connected to LCD RS #define LCD_RW 1 //define MCU pin connected to LCD R/W #define LCD_E 2 //define MCU pin connected to LCD E #define LCD_D0 0 //define MCU pin connected to LCD D0 #define LCD_D1 1 //define MCU pin connected to LCD D1 #define LCD_D2 2 //define MCU pin connected to LCD D1 #define LCD_D3 3 //define MCU pin connected to LCD D2 #define LCD_D4 4 //define MCU pin connected to LCD D3 #define LCD_D5 5 //define MCU pin connected to LCD D4 #define LCD_D6 6 //define MCU pin connected to LCD D5 #define LCD_D7 7 //define MCU pin connected to LCD D6 #define LDP PORTD //define MCU port connected to LCD data pins #define LCP PORTB //define MCU port connected to LCD control pins #define LDDR DDRD //define MCU direction register for port connected to LCD data pins #define LCDR DDRB //define MCU direction register for port connected to LCD control pins I have written several LCD controlling functions that may be used more frequently: void LCDsendChar(uint8_t); //forms data ready to send to 74HC164 void LCDsendCommand(uint8_t); //forms data ready to send to 74HC164 void LCDinit(void); //Initializes LCD void LCDclr(void); //Clears LCD void LCDhome(void); //LCD cursor home void LCDstring(uint8_t*, uint8_t); //Outputs string to LCD void LCDGotoXY(uint8_t, uint8_t); //Cursor to X Y position void CopyStringtoLCD(const uint8_t*, uint8_t, uint8_t);//copies flash string to LCD at x,y void LCDdefinechar(const uint8_t *,uint8_t);//write char to LCD CGRAM void LCDshiftRight(uint8_t); //shift by n characters Right void LCDshiftLeft(uint8_t); //shift by n characters Left void LCDcursorOn(void); //Underline cursor ON void LCDcursorOnBlink(void); //Underline blinking cursor ON void LCDcursorOFF(void); //Cursor OFF void LCDblank(void); //LCD blank but not cleared void LCDvisible(void); //LCD visible void LCDcursorLeft(uint8_t); //Shift cursor left by n void LCDcursorRight(uint8_t); //Shif cursor right by n Using this LCD library I have wrote small demo demonstrating how this works. I have constructed small circuit in Proteus simulator to test it.  //------------Main program------------ #include <avr/io.h> #include <avr/pgmspace.h> #include <util/delay.h> #include "lcd_lib.h" //Strings stored in AVR Flash memory const uint8_t welcomeln1[] PROGMEM="AVR LCD DEMO\0"; const uint8_t curhome[] PROGMEM="LCD cursor Home\0"; const uint8_t customsymbol1[] PROGMEM="Define symbol\0"; const uint8_t customsymbol2[] PROGMEM="at CGRAM addr=0\0"; const uint8_t customsymbolout[] PROGMEM="Output symbol\0"; const uint8_t shift[] PROGMEM="<-Shift->\0"; const uint8_t shiftdemo[] PROGMEM="Shift DEMO\0"; const uint8_t lcdblank[] PROGMEM="LCD blank DEMO\0"; const uint8_t lcdanimation[] PROGMEM="LCD animation DEMO\0"; // custom LCD characters const uint8_t backslash[] PROGMEM= { 0b00000000,//back slash 0b00010000, 0b00001000, 0b00000100, 0b00000010, 0b00000001, 0b00000000, 0b00000000 }; //delay 1s void delay1s(void) { uint8_t i; for(i=0;i<100;i++) { _delay_ms(10); } } //demonstration of shift void demoshift(void) { LCDclr(); CopyStringtoLCD(shift, 3, 0); delay1s(); CopyStringtoLCD(welcomeln1, 3, 1); for(uint8_t i=0;i<5;i++) { delay1s(); LCDshiftLeft(1); delay1s(); LCDshiftRight(1); } } //demostration of blank void demolcdblank(void) { LCDclr(); CopyStringtoLCD(lcdblank, 0, 0); for(uint8_t i=0;i<5;i++) { LCDblank(); delay1s(); LCDvisible(); delay1s(); } } //demonstration of animation void demoanimation(void) { LCDclr(); CopyStringtoLCD(customsymbol1, 0, 0); CopyStringtoLCD(customsymbol2, 0, 1); delay1s(); LCDdefinechar(backslash,0); LCDclr(); CopyStringtoLCD(customsymbolout, 0, 0); LCDGotoXY(8, 1); LCDsendChar(0); delay1s(); LCDclr(); CopyStringtoLCD(lcdanimation, 0, 0); for(uint8_t i=0;i<3;i++) { LCDGotoXY(8, 1); LCDsendChar(0); delay1s(); LCDGotoXY(8, 1); LCDsendChar('-'); delay1s(); LCDGotoXY(8, 1); LCDsendChar('/'); delay1s(); LCDGotoXY(8, 1); LCDsendChar('|'); delay1s(); LCDGotoXY(8, 1); LCDsendChar(0); delay1s(); LCDGotoXY(8, 1); LCDsendChar('-'); delay1s(); LCDGotoXY(8, 1); LCDsendChar('/'); delay1s(); LCDGotoXY(8, 1); LCDsendChar('|'); delay1s(); } } int main(void) { LCDinit();//init LCD 8 bit, dual line, cursor right LCDclr();//clears LCD CopyStringtoLCD(curhome, 0, 0);//Cursor home and display message delay1s(); LCDclr();//clears LCD LCDhome();//cursonr home while(1)//loop demos { demoshift(); demolcdblank(); demoanimation(); } return 0; } //-----------end of main program---------- All working files you can download here
Related Items:
|