I've been trying to interface my 16x2 lcd display, from arduino starter kit, without the LiquidCrystal library for a while... well.. I already made my own "library" and code for 4b interface, that I tried on ATMega2560 and it worked. But whenever I try to use it on my Arduino UNO, I can't even get past the initialization. I've tried a lot of things, even starting from scratch.
Could it be something with the ports? I have connected the LCD pins DB7-DB4 to digital pins 7-4, E to 3, RS to 2 (PortD according to arduino port manipulation page), which shouldn't be a problem (?). (Helloworld and all the other lcd example codes work)
Has anyone tried interfacing lcd with arduino uno without library and succeded? I know there are a lot of topics on this if you google for it, but none of them worked. I wrote the program from scratch several times.
And for anyone asking: No, i CAN'T use the LiquidCrystal library. It's something that I've been asked to do in C. Even though I can't use C properly...
#define DB7 7
#define DB6 6
#define DB5 5
#define DB4 4
#define Enable 3
#define RS 2
#define LCD_out PORTD
/* cbi sbi */
#define sbi(k,b) k|=(1<<b)
#define cbi(k,b) k&=~(1>>b)
void LCD_send (uint8_t lcd_reg);
void LCD_init_4b();
void LCD_char (char lcd_data);
uint8_t i;
char name[] ="name xyz";
char smth[] ="insert smth ";
int main(void) {
DDRD = 0b11111100; // RX, TX 0
LCD_init_4b;
for (i=0; name[i]!=0; i++) {
LCD_char(name[i]);
}
LCD_send(0xC0); //2nd line
_delay_ms(3);
LCD_send(0);
for (i=0; smth[i]!=0; i++) {
LCD_char(smth[i]);
}
}
void LCD_send(uint8_t lcd_reg) {
LCD_out = lcd_reg;
sbi(LCD_out,Enable);
_delay_us(1);
cbi(LCD_out,Enable);
_delay_us(40);
}
/* 4b interface init */
void LCD_init_4b() {
for (uint8_t lcd_i=0; lcd_i<40; lcd_i++) {
_delay_ms(16);
}
LCD_send(1<<DB5); // First setup according to docs
LCD_send(1<<DB5); // Twice
LCD_send(1<<DB7); // 2line mode, 5x7
_delay_us(60);
LCD_send(0);
LCD_send((1<<DB7)|(1<<DB6)|(1<<DB5)); //Display,cursor,blink ON
_delay_us(60);
LCD_send(0);
LCD_send(1<<DB4);
_delay_ms(3);
LCD_send(0);
LCD_send((1<<DB6)|(1<<DB5)); // increment, shift off
_delay_ms(3);
LCD_send(0);
LCD_send(0x80); // first line
_delay_ms(3);
LCD_send(0);
}
/* Char print */
void LCD_char (char lcd_data) {
LCD_send(( (int) lcd_data & 0xF0) | (1<<RS));
_delay_us(50);
LCD_send((( (int) lcd_data << 4) & 0xF0) | (1<<RS));
_delay_us(50);
}
This is my last program that I used (and worked on ATMega2560).. I tried to make it as simple as possible, yet it failed.
Some delays are unnecessary. It's been a month and I have no idea, what am I doing wrong.