- Sat May 21, 2016 6:17 pm
#189902
Hello everyone,
I have used rotary encoder for distance measuring. In my circuit, I have one device LCD display. I have realized that without LCD display, encoder works fine. When I add LCD display, it starts missing steps and gives me wrong output.
Here is my basic codes:
/* Rotary encoder read example */
#define ENC_A A0 // connected to analog pin
#define ENC_B A1 // connected to analog pin
#define ENC_PORT PINC
double distance=0;
void setup()
{
/* Setup encoder pins as inputs */
pinMode(ENC_A, INPUT);
digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT);
digitalWrite(ENC_B, HIGH);
Serial.begin (115200);
Serial.println("Start");
}
void loop()
{
static uint16_t counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
/**/
tmpdata = read_encoder();
if( tmpdata ) {
Serial.print("Counter value: ");
Serial.println(counter, DEC);
counter += tmpdata;
}
}
Does anyone has any suggestion
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}
I have used rotary encoder for distance measuring. In my circuit, I have one device LCD display. I have realized that without LCD display, encoder works fine. When I add LCD display, it starts missing steps and gives me wrong output.
Here is my basic codes:
/* Rotary encoder read example */
#define ENC_A A0 // connected to analog pin
#define ENC_B A1 // connected to analog pin
#define ENC_PORT PINC
double distance=0;
void setup()
{
/* Setup encoder pins as inputs */
pinMode(ENC_A, INPUT);
digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT);
digitalWrite(ENC_B, HIGH);
Serial.begin (115200);
Serial.println("Start");
}
void loop()
{
static uint16_t counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
/**/
tmpdata = read_encoder();
if( tmpdata ) {
Serial.print("Counter value: ");
Serial.println(counter, DEC);
counter += tmpdata;
}
}
Does anyone has any suggestion
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}