[AVR] How to get current position of the cursor in LCD of Arduino.

Status
Not open for further replies.

Madhurakshi B S

Newbie level 6
Joined
Mar 15, 2021
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
3
Activity points
153
Good Morning Sir/Madam,

I am using 16x2 LCD and 4x4 keypad in my project. I want use arrow keys in my project hence i want to the current position of the lcd cursor. Once the current position of lcd is know it is easy to function the arrow keys. In "keypad Arduino" library available there is a function called ' lcd.setCursor' and ' lcd.cursor ' in which the former sets the cursor position and later blinks the cursor but do not provide the current position of the cursor.

If there is any such function in Arduino lcd or Arduino keypad please let me know.

I have used the following function to know the current position of the cursor on lcd. please let me know whether the following code is correct or not.

Code:
const int numrow = 2;
const int numcol = 16;

int thisrow = 0; // count taken for row
int thiscol = 0; // count taken for col
bool k;


void cursorlcd();{
char key = keypad.getKey();
k = (isalpha (key) || isdigit(key));
if (k == TRUE);
{
for (int  thisRow = 0; thisRow < numRows; thisRow++) {
      // loop over the rows:
      for (int thisCol = 0; thisCol < numCols; thisCol++) {
        // set the cursor position:
       return thisrow;
       return thiscol;
      }
      }
      }
}

with regards
Madhurakshi B S
 
Last edited by a moderator:

Hi,

Often libraries don't use display readback functions. For details you need to read your library documentation.

It is not needed to read back.
After initialisation the cursor is set at home position. With every character you send the cursor moves (if correctly set up).
Since you know how many characters you send .... you know the cursor position.
The cursor may be visible or not, but makes no difference.

You also have the option to set the cursor position. One usually use this to write text starting at a known display position (cursor).

Klaus
 

Good Noon Sir,

I want to use arrow keys in the keypad. Without knowing the position cursor how to use Up, Down, Right and left keys. Say example the cursor is in 2nd row at ith column when Up key is pressed how will the cursor get to know had it has to come to 1st row to the same ith column.

Madhurakshi B S
 

Hi,

From the beginning you (the software) know the cursor position. Use variables in your software to keep this information up to date.
If you want the display to show the cursor at position X, Y, then just write it to the display. No need for reading the cursor position.

Klaus
 
The 44780, Hitachi (now Renesas) controller is the industry standard controller.

From this writeup -


It might be possible to modify the library such that whenever a write to display occurs
you save the address used by the write instruction to the display. That way you would
be able to keep track of it as Klaus states.

Another method is to create a buffer in ram of the display. And whenever you write to display
you check if buffer has the data, character for character. If not you update display and buffer,'
only the characters that have changed. If buffer already does have desired data, like a display
in a loop constantly being written to with unchanging data, then you do nothing. This has advantage
that no display artifacts are created when display update is in a fast loop trying to update
but without any change needed. I found it "cleaned" up display from crap excessive writes
when only writes needed when things changed. Also potentially saves power.....

Keep in mind this address has gaps in it, depending on display character count, see the datasheet.
Eg. these are unused addresses. So you just account for these in you coding.

I looked at this problem eons ago and datasheet is not clear that anytime you can read
back cursor address. I think at the time I concluded it could not be done, which I thought
was odd.....



Regards, Dana.
 
Last edited:
Since the LCD acts as a slave device, it should display the state of the software that controls it, so it would be unreasonable to expect it to persist the current cursor position even if such functionality was available, so reaffirming what others said here, you are the one who dictate the cursor position rather than read it; if you find it difficult to do so, it may be the time to review the structure of your program.
 
The command to read the busy flag returns the busy flag state on the display data bit 7 and the cursor position in bits 0-6. This command may not be implemented in a library but can be done directly using the info in the display datasheet. Set RS = 0, W = 1 and then read the data pins. The pin direction on the data pins to the display will need to be change to input before the command is sent. The rw pin also needs to be tied to a pin so that it can be changed from 0 to 1.
 
I think FenTrac is more on track on this. Keep in mind if you create a f()
to read the address, and using nibble mode interface you have to convert
to 8 bit address. Also the address, display line to display line, not contiguous,
data sheet has offsets you would use in your calc of your cursor position.

Its not clear to me if the address read is fixed at last instruction address or
reflects any address increments done by instruction. Check datasheet on
that.

Regards, Dana.
 
Also there is an option for these displays to move to cursor after each character is sent, or to scroll the line. That may well affect the address you read back (although I've never used that mode and so not tried)
Susan
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…