messages are centered
between 0x20 space
characters:
char lcdmsg_servo1[]
= “ SERVO1 “;
char lcdmsg_servo2[]
= “ SERVO2 “;
Next, we would clear the
16-bit position counter and
limit our counts to 0 and 1.
We must be sure to initialize
both the high and low bytes of
the POSCNT counter and the
MAXCNT register:
PHOTO 2. Here’s a
shot of the hardware I
used to build and test
the optical encoder
code contained within
this article.
POSCNTH = 0;
POSCNTL = 0;
MAXCNTH = 0;
MAXCNTL = 1;
oldbyte = POSCNTL;
Our optical encoder code
simply looks at the value of
POSCNTL which will always be
0 or 1, and sends the message
associated with the POSCNTL
count value to the LCD:
do{
if(oldbyte !=
POSCNTL)
{
switch(POSCNTL)
{
case 0:
line1;
for(msg8=0;msg8<8;++msg8)
lcd_send_byte(1,lcdmsg_
servo1[msg8]);
oldbyte = POSCNTL;
break;
case 1:
line1;
for(msg8=0;msg8<8;++msg8)
lcd_send_byte(1,lcdmsg_
servo2[msg8]);
oldbyte = POSCNTL;
break;
}
}
}while(1);
POSCNTH = 0;
POSCNTL = 9;
MAXCNTH = 0;
MAXCNTL = 9;
oldbyte = POSCNTL + 0x30;
rotorbyte = oldbyte;
lcd_gotoxy(1,1);
lcd_send_byte(1,rotorbyte);
Notice we’ve added another variable called rotorbyte.
The oldbyte variable will force a change just as it did in the
SERVO alternate message code. The displayed character will
be derived from the rotorbyte variable.
The lcd_gotoxy function will service one-line to four-line
LCD modules with 20 characters or less per line. We are
using it here to place the LCD cursor at the far left cell of
line 1. Upon powerup or a CPU reset, a 9 will be displayed
at this position. Here’s the cursor positioning code:
As the optical encoder’s shaft is turned, the value of
POSCNTL will increment or decrement causing the oldbyte
variable to differ from the new POSCNTL value.
void lcd_gotoxy( char x, char y)
{
// where x = lcd row (1,2,3,4) and y = column (1
thru 20)
char address;
switch (x)
{
case 1:
PIC18F2431
HI-TECH PICC- 18 PRO
Microchip
www.microchip.com
48 SERVO 07.2010