Hi.
I am making a DIY 8-bit Gray-code optical absolute rotary encoder project and I'm 3 days into programming the PIC18LF4520 MCU with MPLAB IDE and the C18 compiler.
I have the 8 photo transistor output signal lines (one for each code 'ring' of the disc) as inputs to PORTA (RA0-7 LSB to MSB) of a PIC18LF4520. I am reading voltages on each of the PORTA pins (checked with multimeter).
What I want and have tried to do is basically convert/decode PORTA, which is receiving Gray-code input, to a decimal angular position. I wish to then print this value to a 16X2 LCD.
For now I'm not calculating the angular position, I just want to output the position of the disc in binary ie. position 0 (00000000), 1 (00000001), 2 (00000010) etc etc up to 255 (11111111).
e.g. at any given position PORTA will contain a positional Gray-code:
*PORTA = 0b00110101 (8-bit Gray-code) ......which is decimal 53
*convert to binary = 0b00100110 ......which is decimal 38
*output this decimal number to LCD connected to port D (I already have the LCD working to output strings and numbers),
After converting a code from gray to binary, you need to convert it in to ascii to display it on the LCD. If you want the binary value displayed them use something like this.
Binarycode=0b00100110;
for(i=0;i<8;i++)
{
Scratchcode=Binarycode && 0x80;
if(scratchcode==1)
{
here send LCD the ascii code of 1
}
else
{
send LCD the ascii code of 1
}
Binarycode=Binarycode<1);
}
The problem is I'm not quite getting the output I'm after on the LCD...and I don't think its just LCD formatting problems. I have at one time got a clean number 0-255 (8-bit) but the increment was not as expected is jumping from say 10 to 53, thus making me think I needed to adjust the disc/sensor alignment. But after carefully re-adjusting the disc and checking that I'm getting expected voltage on each pin of PORTA, I now have output like this: 227, 36:, 2:3, 236 and so on.....
View attachment 42311
I have attached the code above. I know its a drag looking at other peoples code, so here is a summary of what I think are possible problem areas in my project/code:
(1)
Is the following permissable?
unsigned char GrayWindDir=0;
GrayWindDir=PORTA;
In my mind this will store the 8-bit gray number occuring on PORTA in variable GrayWindDir.....but im not convinced
No problem with this, you even dont need to initialize it to 0, it will be set to PORTA.
(2)
I have tested the function LCDWriteInt(); but all variables holding the 8-bit numbers (like GrayWindDir and BinaryWindDir) I have made of type unsigned char. but LCDWriteInt() accepts integers....it did seem to work though.
Anyway any help or direction will be most appreciated