miguelzea35 0 Posted October 15, 2019 Share Posted October 15, 2019 I'm trying to read two encoders to know the angular position using interrupts but something is wrong, the code compiles but in serial just prints "0", what can i change? volatile long Left_Encoder_Ticks = 0; volatile bool LeftEncoderBSet; #define Right_Encoder_PinA PC_5 #define Right_Encoder_PinB PC_6 volatile long Right_Encoder_Ticks = 0; volatile bool RightEncoderBSet; void setup() { Serial.begin(9600); SetupEncoders(); } void SetupEncoders() { pinMode(Left_Encoder_PinA, INPUT_PULLUP); pinMode(Left_Encoder_PinB, INPUT_PULLUP); attachInterrupt(Left_Encoder_PinA, do_Left_Encoder, RISING); pinMode(Right_Encoder_PinA, INPUT_PULLUP); pinMode(Right_Encoder_PinB, INPUT_PULLUP); attachInterrupt(Right_Encoder_PinA, do_Right_Encoder, RISING); } void loop() { Update_Encoders(); } void Update_Encoders() { Serial.print(Left_Encoder_Ticks); Serial.print("\t"); Serial.print(Right_Encoder_Ticks); Serial.print("\n"); } void do_Left_Encoder() { LeftEncoderBSet = digitalRead(Left_Encoder_PinB); Left_Encoder_Ticks -= LeftEncoderBSet ? -1 : +1; } void do_Right_Encoder() { RightEncoderBSet = digitalRead(Right_Encoder_PinB); Right_Encoder_Ticks += RightEncoderBSet ? -1 : +1; } Quote Link to post Share on other sites
Rei Vilo 692 Posted October 15, 2019 Share Posted October 15, 2019 Why aren't you using the built-in QEP inputs? The TM4C123 features 2 QEP with TTL inputs natively. See my separate answer to your previous thread. You may need to declare the variables as volatile. Quote Link to post Share on other sites
terjeio 134 Posted October 16, 2019 Share Posted October 16, 2019 Here is my interrupt driven QEI implementation - not a Energia library but may provide some ideas? The code is not 100% reliable for the MPG_GetPosition() function though - interrupts should be disabled during the memcpy call... Terje Quote Link to post Share on other sites
Kannannatesh 0 Posted October 16, 2019 Share Posted October 16, 2019 Try this attachInterrupt(digitalPinToInterrupt(Left_Encoder_PinA), do_Right_Encoder, RISING); Quote Link to post Share on other sites
miguelzea35 0 Posted October 16, 2019 Author Share Posted October 16, 2019 I tried another code to prove only one encoder but think I can't use HIGH and LOW for interrupts in Energiai nt A = PD_1; //variable A a pin digital 2 (DT en modulo) int B = PD_2; //variable B a pin digital 4 (CLK en modulo) float GRADOS = 0; int ANTERIOR = 0; // almacena valor anterior de la variable POSICION volatile int POSICION = 0; // variable POSICION con valor inicial de 50 y definida // como global al ser usada en loop e ISR (encoder) void setup() { pinMode(A, INPUT); // A como entrada pinMode(B, INPUT); // B como entrada // pinMode(23, OUTPUT); Serial.begin(9600); // incializacion de comunicacion serie a 9600 bps attachInterrupt(digitalPinToInterrupt(A), encoder, LOW);// interrupcion sobre pin A con // funcion ISR encoder y modo LOW Serial.println("Listo"); // imprime en monitor serial Listo } void loop() { // digitalWrite(23, HIGH); if (POSICION != ANTERIOR) { // si el valor de POSICION es distinto de ANTERIOR GRADOS=POSICION*0.35; Serial.print(POSICION); // imprime valor de POSICION Serial.print(" "); Serial.println(GRADOS); ANTERIOR = POSICION ; // asigna a ANTERIOR el valor actualizado de POSICION } } void encoder() { static unsigned long ultimaInterrupcion = 0; // variable static con ultimo valor de // tiempo de interrupcion unsigned long tiempoInterrupcion = millis(); // variable almacena valor de func. millis if (tiempoInterrupcion - ultimaInterrupcion > 0.5) { // rutina antirebote desestima // pulsos menores a 5 mseg. if (digitalRead(B) == HIGH) // si B es HIGH, sentido horario { POSICION++ ; // incrementa POSICION en 1 } else { // si B es LOW, senti anti horario POSICION-- ; // decrementa POSICION en 1 } POSICION = min(2000000, max(-2000000, POSICION)); // establece limite inferior de 0 y // superior de 100 para POSICION ultimaInterrupcion = tiempoInterrupcion; // guarda valor actualizado del tiempo } // de la interrupcion en variable static } Quote Link to post Share on other sites
energia 484 Posted November 11, 2019 Share Posted November 11, 2019 I posted a library a while ago which could be of use. It can be found here: https://github.com/energia-libraries/RotaryEncoder bluehash 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.