Loading...
Pages: [1]   Go Down
Author Topic: Encoder PANASONIC EVEQDBRL416B  (Read 319 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Für meine Bastelei habe ich bei Polin Panasonic Encoder eingekauft.
Ich war richtig enttäuscht, das keine Library auf Arduino Seite mit diesem Encoder richtig laufen kann. Ich habe meinen eigenen Library geschrieben.
Ich hoffe es wird für andere auch nützlich sein.

Code:
/*
  Pan_Encoder_ABC.cpp - A library for reading Encoder PANASONIC EVEQDBRL416B.

  Copyright 2012 Andrew Buckin(aka, "ka_ru"), except for the code at the end marked
  "The following was taken from the Arduino's code."  That portion is copyright by
  Andrew Buckin and is distributed under the GNU Lesser General Public License 2.1
  or any subsequent version.

  For those portions Copyright by Andrew Buckin, the following license applies.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Questions?  Send mail to ka_ru2003@msn.com
*/

#include <PinChangeInt.h>

enum PinAssignments {
  encoderPinA = A2,   // rigth
  encoderPinB = A3,   // left
  clearButton = 8    // another two pins
};

volatile unsigned int encoderPos = 100;  // a counter for the dial
unsigned int lastReportedPos = 1;   // change management
static boolean rotating=false;      // debounce management

// interrupt service routine vars
volatile boolean ABc = false;
volatile boolean BBc = false;
volatile boolean AAc = false;
volatile boolean BAc = false;
int8_t clicks = 0;      // Counter to indicate cumulative clicks in either direction
int8_t direction = NULL;   // indicator
int8_t enc = NULL;   // indicator

void setup() {

  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  pinMode(clearButton, INPUT);
 // turn on pullup resistors
  digitalWrite(encoderPinA, HIGH);
  digitalWrite(encoderPinB, HIGH);
  digitalWrite(clearButton, HIGH);

  PCintPort::attachInterrupt(encoderPinA, &doEncoderA, CHANGE);
  PCintPort::attachInterrupt(encoderPinB, &doEncoderB, CHANGE);

  Serial.begin(9600);  // output
}

// main loop, work is done by interrupt service routines, this one only prints stuff
void loop() {
  rotating = true;  // reset the debouncer
  
  if (direction != NULL){
   enc = !NULL;
    if (direction > 0) clicks++;
    else clicks--;
    direction = NULL;
  }
    
  if (enc != NULL) {
    Serial.print("Index:");
    Serial.println(encoderPos  + clicks, DEC);
    enc = NULL;
  }
 
  if (digitalRead(clearButton) == LOW )  {
    encoderPos = 0;
  }
}

// Interrupt on A changing state
void doEncoderA(){
  //delayMicroseconds(50);
  AAc = digitalRead(encoderPinA);
  BAc = digitalRead(encoderPinB);
  if (!ABc && AAc && !BBc && !BAc){direction=1;}
  if (ABc && !AAc && BBc && BAc){direction=1;}
  if (!ABc && AAc && BBc && BAc){direction=-1;}
  if (ABc && !AAc && !BBc && !BAc){direction=-1;}

}

// Interrupt on B changing state
void doEncoderB(){
  //delayMicroseconds(50);
  ABc = digitalRead(encoderPinA);
  BBc = digitalRead(encoderPinB);
}


« Last Edit: December 26, 2012, 08:10:40 am by ka_ru » Logged

Forum Moderator
BZ (I)
Offline Offline
Brattain Member
*****
Karma: 162
Posts: 15752
+39 349 2158303
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wenn man eine Bibiothek schreibt und die anderen zu Verfügung stellen will, dann sollte man sie klar und einfach schreiben.
Die Verwendung von false und true neben HIGH und LOW und NULL vereinfacht die Sache nicht.

Welchen Sinn hat delayMicroseconds(50); in der Interruptfunktion?

Grüße Uwe
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Logik und Benutzung von false, true, HIGH, LOW, NULL sind unterschiedlich.
Eingang kann nur LOW, NULL sein usw. smiley
Logged

Forum Moderator
BZ (I)
Offline Offline
Brattain Member
*****
Karma: 162
Posts: 15752
+39 349 2158303
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Logik und Benutzung von false, true, HIGH, LOW, NULL sind unterschiedlich.
Eingang kann nur LOW, NULL sein usw. smiley
Ach wirklich?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 164
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Die Verwendung von false und true neben HIGH und LOW und NULL vereinfacht die Sache nicht.
Ich verstehe das so, dass false bzw. true etc.. in einer lib generell nicht verwendet werden sollen.

Ganz davon abgesehen ist in deiner "Pan_Encoder_ABC.cpp" eine setup und eine loop Funktion vorhanden. Die haben dort auch nichts zu suchen.
Im großen und ganzen ist dein Code für mich ein Sketch und als Lib nicht zu gebrauchen.
Wo ist die "Pan_Encoder_ABC.h"?
Logged

Arduino 1.0.3|MEGA2560
SSD1289 TFT Touch|UGUI|DS18B20|SD|DS1307
ADNS2610|RFM12|OOK|SSD1303|DHT22

Pages: [1]   Go Up
Print
 
Jump to: