Hi Everyone.
I've spent the better part of the day trying to add a read data function to my LCD unit. Everything connection wise is fine with data displaying correctly but due to factors in the environment i sometimes get interference that causes the screen to go ape and display garbage and you need to reset to get the display back. I was hoping i could use the controller to read the data back and if a problem is found it could re initialize the display. I haven't found much on the topic since most tutorials just recommend connecting RW to ground and keep it in write mode. The test function i tried was to read the character back and compare with what should be there and if different to display the read character straight after it. My basic approach was to just duplicate my send character function with it reading but since its in 4 bit mode i'm assuming i would need to read the 4 high bits and then the 4 low bits. The result i get from every read is 0? I anyone has successfully done this could you perhaps spot the error in the code. Ive gone through it over and over but still im missing something.Ive confirmed the RW pin is in fact pulled high by using an LED.
int LCD_PROOFREAD(uint8_t ch){
uint8_t backread=0;
LCDsendCommand(0b00010000); //move cursor to the left
LCDR|=(1<<LCD_E) | (1<<LCD_RW) | (1<<LCD_RS); //Set the DDR as outputs for control bits
LDDR&=~(1<<LCD_D7);
LDDR&=~(1<<LCD_D6);
LDDR&=~(1<<LCD_D5);
LDDR&=~(1<<LCD_D4); //Set the DDR for the data bits to 0 to act as inputs
LCP|=(1<<LCD_RS); //Pull the RS pin high for reading Data not command
LCP|=(1<<LCD_RW); //Pull the RW pin high to read data instead of writing
LCP|=(1<<LCD_E); //Pull the enable pin high
delay_ms(1); //Wait 1ms for data to settle
backread|=(LDP&0b11110000); //Read the port with bit masking bits 4,5,6 and 7 because they are the 4 high bits.
delay_ms(1);
LCP&=~(1<<LCD_E); //Pull the enable bit low again
delay_ms(1);
LCP|=(1<<LCD_RS); //Pull the RS pin high because we are working with data
LCP|=(1<<LCD_RW); //Pull the RW pin high because we are reading not writing
LCP|=(1<<LCD_E); //Pull the enable pin high
_delay_ms(1); //Wait for the data to settle
backread|=((LDP&0b11110000)>>4); //We read the Port with a bitmask just to read pin 4-7. We bit shift the bits 4 to the right to get the 4 low bits.
_delay_ms(1);
LCP&=~(1<<LCD_E); //We now pull the enable pin low
if (ch!=backread) { //Check if the character is the same
LCDsendChar(backread,0); //If no match then display the character read next to the one it read
}
return(0);
}