I am trying to display an encoder count to an LCD display using interrupt-on-change. The custom encoder outputs pulses on one of two pins depending on the direction turned. The encoder outputs 4 pulses per degree (1440 pulses per 360 degree revolution).
I'm attempting to display the value in an xx.xx format. In other words, it should display an angle of 0.25 degrees per increment such as 2.25, 36.50, 45.75,etc.. The only way I can accomplish this is by my following code. While this does work, it seems there should be a much simpler way to achieve the same thing. Any help would be greatly appreciated.
I am using a PIC18F4520 @ 20MHz.
NOTE: Yes I am aware of built in Quadrature and other features of this MCU, however, these are not an option for this specific encoder.
Code: Select all
int count1;
int countx;
int A_STEP;
char countx_txt[9];
char count1_txt[9];
int Tresult1;
int Tresult2;
void interrupt(){
// Evaluate encoder1
if(INTCON.RBIF==1){
if(!ENC1_DWN) //Turn counterclockwise
{
A_STEP=(A_STEP-1); //count down
LED_DOWN = 0;
LED_UP = 1;
}
else if(!ENC1_UP) //Turn clockwise
{
A_STEP=(A_STEP+1); //count up
LED_DOWN = 1;
LED_UP = 0;
}
INTCON.RBIF=0; //Clear port B interrupts flag
return;
}
void main(){
count1 = 0; // Default value = 0
count2 = 0; // Default value = 0
CustomChar0();
do {
count1 = (abs (A_STEP))/4; // Get absolute value of count (eliminate -) and extract whole number
countx = (count1*25%100); // Extract each count (.25)
IntToStr(Count1, Count1_txt);
IntToStr(Countx, Countx_txt);
Tresult1 = strlen(Count1_txt); // Calculate the character length of count1 string
Tresult2 = strlen(Countx_txt); // Calculate the character length of countx string
Lcd_Out(1,1,ltrim(rtrim(Count1_txt))); // Display count1 number
Tresult1 = Tresult1+1; // Add 1 to the character length calculation
Lcd_Out(1, Tresult1, "."); // Add decimal point
Tresult1 = Tresult1+1; // Add 1 to the character length calculation
Lcd_Out(1,Tresult1,ltrim(rtrim(Countx_txt))); // Display countx number
Tresult2 = (Tresult1-1)+Tresult2+1; // Add both character length results + 1 to the character length calculation
Lcd_Chr(1,Tresult2,0); // Display degree character
delay_ms(50);
Lcd_Out(1,1," ");
Lcd_Out(2,1," ");
LED_DOWN = 1;
LED_UP = 1;
} while(1);
} // endless loop