C programing regarding 1602 LCD screen

  • Thread starter Thread starter Alex_Sanders
  • Start date Start date
  • Tags Tags
    Lcd Screen
AI Thread Summary
The discussion focuses on understanding a C function for interfacing with a 1602 LCD screen, particularly the operations related to checking if the LCD is busy. The function involves manipulating a special variable, DataPort, which interacts with hardware registers rather than standard memory locations. The use of bitwise operations, specifically "DataPort & 0x80", is explained as a method to isolate the highest order bit, with implications for the return value being either 0 or 1. Clarifications on the roles of the EN_CLR and EN_SET macros are suggested, as their functions are crucial for proper operation. Overall, the thread emphasizes the importance of consulting the specific hardware documentation for accurate implementation.
Alex_Sanders
Messages
73
Reaction score
0
Here's a couple of programs that I really couldn't figure out some of their lines:

This function checks if the 1602 is busy, I suppose it means whether data are being passed to the registers of 1602:

bit LCD_Check_Busy(void)
{
DataPort= 0xFF; // Dataport is P0. What does this do? Is this some kind of initialization? Couldn't you just leave P0 alone until you really start using it?
RS_CLR; // RS register set to 0
RW_SET; //R@ register set to 1 (write)
EN_CLR;
_nop_();
EN_SET; // Here, EN register set to 0 then set back to 1 again, thus forming a rising edge, but 1602 was enabled when a falling edge was detected? Maybe it's not trying to enable the 1602 at all? Or may be there is a "Not" logic in the circuit? But I checked the schematic, there is none?
return (bit)(DataPort & 0x80); // I have no idea how this very crucial line works.
}


I'd really appreciate your help, thanks in advance and, in case there is not sufficient information to answer those question, please let me know.
 
Technology news on Phys.org
Okay what platform is this EXACTLY?! And what compiler/tools, etc.

DataPort: On embedded devices you will often have "register" variables that are special in the sense that when you write to them, you will not just be writing to a memory location like you would in normal C, rather the act of writing bytes to that variable has some kind of actual effect which is defined in your part documentation. "Write this bit to 1 in order to ready this other data port for reading" is an idiom I've seen before. Writing 0xFF to a variable of course writes a full byte of all 1s. I don't know what this variable does but I bet it's in your documentation somewhere.

EN_SET: I can't know what this does without reading the documentation. It might be helpful to look up exactly what the EN_CLR and EN_SET macros do.

"return (bit)(DataPort & 0x80);" This is bit arithmetic. "DataPort & 0x80" means "the value of Dataport, with every bit individually ANDed with 0x80". 0x80 is of course a byte with only the highest order bit set. So DataPort & 0x80 means "mask off the highest order bit of DataPort". (The fact that you previously wrote 0xFF to DataPort is irrelevant because it is probably not a normal variable.) "DataPort & 0x80" will thus always be equal to either 0x00 or 0x80. I don't know what "bit" is, but probably it's typedef'd to bool or something, so I *imagine* casting to bit has the effect of snapping it to the values "either 0x00 or 0x01", but I'd have to see how bit is defined in your headers.
 
Coin said:
Okay what platform is this EXACTLY?! And what compiler/tools, etc.

DataPort: On embedded devices you will often have "register" variables that are special in the sense that when you write to them, you will not just be writing to a memory location like you would in normal C, rather the act of writing bytes to that variable has some kind of actual effect which is defined in your part documentation. "Write this bit to 1 in order to ready this other data port for reading" is an idiom I've seen before. Writing 0xFF to a variable of course writes a full byte of all 1s. I don't know what this variable does but I bet it's in your documentation somewhere.

EN_SET: I can't know what this does without reading the documentation. It might be helpful to look up exactly what the EN_CLR and EN_SET macros do.

"return (bit)(DataPort & 0x80);" This is bit arithmetic. "DataPort & 0x80" means "the value of Dataport, with every bit individually ANDed with 0x80". 0x80 is of course a byte with only the highest order bit set. So DataPort & 0x80 means "mask off the highest order bit of DataPort". (The fact that you previously wrote 0xFF to DataPort is irrelevant because it is probably not a normal variable.) "DataPort & 0x80" will thus always be equal to either 0x00 or 0x80. I don't know what "bit" is, but probably it's typedef'd to bool or something, so I *imagine* casting to bit has the effect of snapping it to the values "either 0x00 or 0x01", but I'd have to see how bit is defined in your headers.


Thanks 800 pounds Coin, you helped me a great deal in figuring out those "commands" that being written to those registers. Thank you.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
25
Views
2K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
13
Views
7K
Replies
16
Views
3K
Replies
9
Views
4K
Back
Top