By
OOP (Object Oriented Programming) facilities, any problem relating to
programming complexity – especially in structural programming
concept - can be solved easily and even improved quickly. For example,
some functions can be packed into a method of a class to make a
meaning or semantic of code to be more comprehensible. In addition, a
programming language, like C++ that we are going to use, provides
overloading-operator operations that make it even simpler and shorter to use. Like our post here, those facilities are used to interface
our LCD.
Related
posts:
Schematic Circuit
The schematic circuit on Figure 1 above is the same as our previous
post, Configuring and Using HD44780u (lm016L) LCD
with AVR atmega8535 that we used a structural programming concept
to drive the LCD. On our code bellow you may compare how easy it is,
to configure LCD pins and send any string of characters to it.
Main Source code
Now let’s see our code:
#include "Lcd_stream.hpp"
int main()
{
//configure pins
Lcd_stream screen('C',2,3,4,5,6,7);
//print "Hello" on default (x:0,y:0) cursor position
screen << "Hello";
//set cursor (x:4,y:1)
screen(4,1);
//then print on it
screen << "World!";
while(1);
return 0;
}
On the code lines above, we can see that the first thing to do is to
‘include’ Lcd_stream.hpp’ file. To do this, of course we need
that library to use, you can find the library including its Makefile
configuration on the github link bellow:
Source code:
On line:
Lcd_stream screen('C',2,3,4,5,6,7);
we create an instance or object of Lcd_stream called ‘screen’.
The arguments passed to this class constructor are pins
configuration. As seen that ‘C’ character on one of the arguments
indicates its DDDx register - that is DDRC - followed by its bits
number. These cause six bits of port C of the microcontroller we use
active, see our configuration on Figure 1 above.
The next line is:
screen << "Hello";
this
line simply prints “Hello” to the LCD screen on the default
cursor x:0 and y:0. this line uses operator<<() function to print the string. And finally followed by the next lines:
screen(4,1);
screen << "World!";
the first line sets cursor position of the LCD on x:4 and y:1 and
print “World” to that position. They are done by operator()() and
operator<<() method on the Lcd_stream class. As can be seen,
the code will be shorter by using this operator. Note that
operator()() is also called a functor.
After running the code, the result will appear as on figure 1 above.
That’s it! How we can drive the LCD using overloading operator in
atmega8535 MCU, it’s so simple to print any string. And for you who
aren’t yet familiar with these operators or library used in this
post, just ask me question in the following comment.
Thanks for reading!
0 Comments
Post a Comment