Rikard, When I run an LCD in 4 bit mode, I always ground the 4 bits on the LCD that I'm not using, (ie.lcd bits 0-3). The reasoning here is that if these pins float, you're likely to get something other than what you want during initialization because you have to put the unit in 4 bit mode during initialization. Also, make sure you're actually putting it in 4 bit mode. On your code below, the 4 bit mode and the 8 bit mode commands are the same. Although the 8 bit commands are commented out, make sure it's correct for 4 bit mode. I would check, but I don't have the command sheet at hand. Let us know what you find out what the problem was. Regards, Jim ----- Original Message ----- From: "Rikard Bosnjakovic" To: "Microcontroller discussion list - Public." Sent: Monday, March 30, 2009 9:37 AM Subject: [EE] Problems with LCD-display in 4-bit mode >I bought some cheap HD44780-compatible LCD-displays on ebay to have > something to play with (never used them before). The seller supplied > some tutorials that I'm working with. I get the display to work on my > breadboard when using 8-bit mode (controlled by a PIC 16F628A), but I > cannot make it work using 4-bit mode. When I use 4-bit, D7-D4 is > connected to RB3-RB0. When 8-bit, D7-D0 is connected to RB7-RB0. > > I've switched both the PIC and LCD to eliminate these sources of > errors, so I'm probably doing something wrong in my code. Nothing else > than the LCD-display and my Wisp-programmer is connected to the PIC. > > Here's the code: > > #include > > __CONFIG(INTIO & WDTDIS & PWRTDIS & MCLREN & UNPROTECT & BORDIS & LVPDIS); > > #define RS RA2 > #define RW RA3 > #define E RA4 > #define D4 RB0 > #define D5 RB1 > #define D6 RB2 > #define D7 RB3 > > void lcd_wait(void) { > unsigned char i; > unsigned int j; > > for (i = 0; i < 5; i++) {} > for (j = 0; j < 6000; j++) {} > } > > void init_delay(void) { > unsigned int i; > > for (i = 0; i < 23456; i++) {} > } > > void init_MCU(void) { > TRISA = TRISB = 0; // All outputs > PORTA = PORTB = 0; // Clear garbage > OSCF = 1; // 4MHz INTOSC > CMCON = 0b00000111; // Disable comparators > VRCON = 0; // Disable voltage > reference-module > T1CON = 0; // Disable Timer1 > CCP1CON = 0; // Disable CCP > } > > void lcd_cmd(unsigned char cmd) { > // According to the tutorial, only D7-D4 is used when 4-bit mode > // is selected. RB3-RB0 goes to D7-D4 so we'll mask and shift > // the commands. > PORTB = (cmd & 0xF0) >> 4; > E = 1; > lcd_wait(); > E = 0; > > PORTB = cmd & 0x0F; > E = 1; > lcd_wait(); > E = 0; > } > > void lcd_cmd_8bit(unsigned char cmd) { > PORTB = cmd; > E = 1; > lcd_wait(); > E = 0; > } > > void lcd_putc(char letter) { > RW = 0; > RS = 1; > lcd_cmd(letter); > } > > void lcd_print(char *str) { > char *p = str; > while (*p) > lcd_putc(*p++); > } > > void main(void) { > char snase[] = "ABC"; > init_MCU(); > init_delay(); > > // RA1 is a LED (for debugging) > RA1 = 1; > > // This works fine in 8-bit, but not in 4-bit > lcd_cmd(0b00111000); // Set 4-bit transfer, 2 lines > lcd_cmd(0b00001111); // Display on, cursor on, blink, > underline > // lcd_cmd_8bit(0b00111000); // Set 4-bit transfer, 2 lines > // lcd_cmd_8bit(0b00001111); // Display on, cursor on, > blink, underline > > lcd_print(snase); > > RA1 = 0; > while(1) { > NOP(); > } > } > > What happens in the 4-bit mode is that the display responds to my > commands, but instead of printing three letters (ABC) I get like > "OOOOOO\\\\\\NNNNNN". That is, each letter is repeated and deformed. > > But - even weirder - is that this result is not 100% reproducible. > Sometimes the display turns on and cursor blinks *before* the command > is sent, and when this happens no garbled text is printed. This is the > reason I tried to change displays, but the behaviour is similiar on > everyone. 8-bit mode works fine for everyone. > > Can anyone shed some light on this? Is my lcd_cmd() erronious? > > > -- > - Rikard - http://bos.hack.org/cv/ > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist