2wireLCD
2-wire-LCD-Interface¶
This is a 2-wire-LCD-interface, but not I2C.
- Advantage:
- You can use all pins to connect your LCD.
- You don't need especially (I2C)IC.
- high Refresh-Rate for the Display.(any MHz I2C only 400kHz)
- here is the programm:
#include "mbed.h" #include "lcd_2wire.h" lcd_2wire dis(p29,p30); AnalogIn input(p20); DigitalOut myled(LED1); float wert; char c[3]; int main() { dis.lcd_init(); dis.lcd_goto_xy(0,0); dis.lcd_write_str("********************"); dis.lcd_goto_xy(7,1); dis.lcd_write_str("Hallo"); dis.lcd_goto_xy(7,2); dis.lcd_write_str("MBED!"); dis.lcd_goto_xy(0,3); dis.lcd_write_str("********************"); wait(2); while (1) { wert = input.read(); sprintf(c,"%.3f",wert); dis.lcd_clear(); dis.lcd_goto_xy(7,1); dis.lcd_write_str("Analogwert:"); dis.lcd_goto_xy(7,2); dis.lcd_write_str(c); wait_ms(1); myled = 1; wait(0.1); myled = 0; wait(0.1); } }
- here is the necessary library:
#include "mbed.h" #include "lcd_2wire.h" lcd_2wire::lcd_2wire (PinName pinc, PinName pind ): _pinc(pinc), _pind(pind) { _pinc = 0;//clock signal _pind = 0;// data signal } //----------------------------- void lcd_2wire::LCD_SET_CLK(void) { _pinc = 1; } //--------------------------------- void lcd_2wire::LCD_CLR_CLK(void) { _pinc = 0; } //---------------------------------- void lcd_2wire::LCD_SET_DATA(void) { _pind = 1; } //---------------------------------- void lcd_2wire::LCD_CLR_DATA(void) { _pind = 0; } //---------------------------------- int LCD_ROW_TABLE[4] = { //Display row addresses 0x80, 0xC0, 0x80 + LCD_COLS, 0xC0 + LCD_COLS }; //---------------------------------------- void lcd_2wire::lcd_delay(void) { wait_us(100); } //------------------------------------------- void lcd_2wire::lcd_init_shift_reg(int mode) { //Clear shift register, set E and RS int n; LCD_CLR_DATA(); //clear data signal lcd_delay(); //clear all outputs of shift register for (n=7; n; n--) { LCD_SET_CLK(); lcd_delay(); LCD_CLR_CLK(); } //Set high level for E at Q7 LCD_SET_DATA(); lcd_delay(); LCD_SET_CLK(); lcd_delay(); LCD_CLR_CLK(); //Set level for RS at Q6 if (mode == LCD_DATA_MODE) { LCD_SET_DATA(); } else { LCD_CLR_DATA(); } lcd_delay(); LCD_SET_CLK(); lcd_delay(); LCD_CLR_CLK(); } //---------------------------------------------- void lcd_2wire::lcd_shift_nibble(int data) { //Clock in 4 bits of data int n, mask= 0x08; for (n =4; n; n--) { //do this for 4 databits if (data & mask) { // set or reset data signal LCD_SET_DATA(); } else { LCD_CLR_DATA(); } LCD_SET_CLK(); lcd_delay(); LCD_CLR_CLK(); mask >>= 1; // shift right } LCD_SET_CLK(); lcd_delay(); LCD_CLR_CLK(); } //------------------------------------------------- void lcd_2wire::lcd_shift_byte(int data , int mode) { lcd_init_shift_reg(mode); lcd_shift_nibble(data >> 4); LCD_SET_DATA(); lcd_delay(); if (mode != LCD_INIT_MODE) { lcd_init_shift_reg(mode); lcd_shift_nibble(data); LCD_SET_DATA(); lcd_delay(); } LCD_CLR_DATA(); lcd_delay(); } //------------------------------------------------- void lcd_2wire::lcd_write_command(int command) { lcd_shift_byte(command,LCD_CMD_MODE); } //--------------------------------------------------- void lcd_2wire::lcd_write_data(int data) { lcd_shift_byte(data,LCD_DATA_MODE); } //---------------------------------------------- //Functions flags - Bit 0 0: 5x7 Dots 1:5x10 Dots // Bit 1 0: 1 Row 1: 2 Rows // Bit 2 0: 4-Bit Intf 1: 8-Bit Intf void lcd_2wire::lcd_function_mode(int function_mode) { function_mode =((function_mode & 7) << 2) + 0x20; lcd_write_command(function_mode); } //------------------------------------------------- void lcd_2wire::lcd_display_mode(int display_mode) { display_mode =(display_mode & 7) + 0x08; lcd_write_command(display_mode); } //------------------------------------------------- void lcd_2wire::lcd_entry_mode(int entry_mode) { entry_mode =(entry_mode & 3) + 0x04; lcd_write_command(entry_mode); } //------------------------------------------------- void lcd_2wire::lcd_clear(void) { lcd_write_command(LCD_CLR); } //-------------------------------------------------- void lcd_2wire::lcd_home(void) { lcd_write_command(LCD_HOME); } //-------------------------------------------------- void lcd_2wire::lcd_goto_xy(int xpos, int ypos) { if (xpos < LCD_COLS && ypos <LCD_ROWS) { lcd_write_command(LCD_ROW_TABLE[ypos] + xpos); } } //--------------------------------------------------- void lcd_2wire::lcd_write_char(int chr) { lcd_write_data(chr); } //-------------------------------------------------- void lcd_2wire::lcd_write_char_xy(int x, int y, int chr) { lcd_goto_xy(x,y); lcd_write_data(chr); } //------------------------------------------------ void lcd_2wire::lcd_write_str(const char *sp) { while (*sp) { lcd_write_data(*sp++); } } //---------------------------------------------------- void lcd_2wire::lcd_write_str_xy(int x, int y, const char *sp) { lcd_goto_xy(x,y); lcd_write_data(*sp++); } //---------------------------------------------------- void lcd_2wire::lcd_init(void) { LCD_CLR_CLK(); LCD_CLR_DATA(); wait_ms(LCD_POWER_UP_DELAY_MS); //Startinitialisation lcd_shift_byte(LCD_FUNCTION_8BIT_1LINE,LCD_INIT_MODE); wait_ms(LCD_INIT_DELAY_MS); lcd_shift_byte(LCD_FUNCTION_8BIT_1LINE,LCD_INIT_MODE); wait_ms(LCD_INIT_DELAY_MS); lcd_shift_byte(LCD_FUNCTION_8BIT_1LINE,LCD_INIT_MODE); wait_ms(LCD_INIT_DELAY_MS); lcd_shift_byte(LCD_FUNCTION_4BIT_1LINE,LCD_INIT_MODE); wait_ms(LCD_INIT_DELAY_MS); //Display is in 4 bit I/O mode now #if (LCD_ROWS > 1) lcd_write_command(LCD_FUNCTION_4BIT_2LINE); #else lcd_write_command(LCD_FUNCTION_4BIT_1LINE); #endif wait_ms(LCD_INIT_DELAY_MS); lcd_write_command(LCD_DISP_ON); wait_ms(LCD_INIT_DELAY_MS); lcd_write_command(LCD_CLR); lcd_write_command(LCD_HOME); wait_ms(LCD_INIT_DELAY_MS); }
- and the header
#ifndef MBED_LCD_2WIRE_H #define MBED_LCD_2WIRE_H #include "mbed.h" class lcd_2wire { public: lcd_2wire(PinName pinc, PinName pind); #define LCD_CMD_MODE 0 #define LCD_DATA_MODE 1 #define LCD_INIT_MODE 2 #define LCD_CLR 1 #define LCD_HOME 2 #define LCD_DISP_ON 0x0C #define LCD_FUNCTION_4BIT_1LINE 0x20 #define LCD_FUNCTION_4BIT_2LINE 0x28 #define LCD_FUNCTION_8BIT_1LINE 0x30 #define LCD_FUNCTION_8BIT_2LINE 0x38 #define LCD_COLS 20 #define LCD_ROWS 4 #define LCD_POWER_UP_DELAY_MS 50 #define LCD_INIT_DELAY_MS 5 void lcd_init(void); void lcd_clear(); void lcd_home(void); void lcd_goto_xy(int xpos, int ypos); void lcd_write_char(int chr); void lcd_write_char_xy(int x, int y, int chr); void lcd_write_str(const char *sp); void lcd_write_str_xy(int x,int y,const char *sp); private: DigitalOut _pinc; DigitalOut _pind; void LCD_SET_CLK(); void LCD_CLR_CLK(); void LCD_SET_DATA(); void LCD_CLR_DATA(); void lcd_delay(); void lcd_init_shift_reg(int mode); void lcd_shift_nibble(int data); void lcd_shift_byte(int data, int mode); void lcd_write_command(int command); void lcd_write_data(int data); void lcd_function_mode(int function_mode); void lcd_display_mode(int display_mode); void lcd_entry_mode(int entry_mode); }; #endif
- to the end any photos