Drehgeber am STM§" F103

/*******Interrupt-based Rotary Encoder Sketch*******
by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/

static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  Serial.begin(115200); // start the serial monitor link
}

void PinA(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void PinB(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void loop(){
  if(oldEncPos != encoderPos) {
    Serial.println(encoderPos);
    oldEncPos = encoderPos;
  }
}

Dieser Sketch macht auf einem Uno, was er soll. Ich habe ihn umgeschrieben für einen STMF103, und er Compiler mecker über folgende Zeile:
" if(reading == B00001100 && aFlag) {  "//check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge

Warum?

static int pinA = PB10;
static int pinB = PB11;
volatile bool aFlag = 0;
volatile bool bFlag = 0;
volatile uint16_t encoderPos = 0;
volatile uint16_t oldEncPos = 0;
volatile uint16_t reading = 0;

setup(){
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pinA), PinA, RISING);
attachInterrupt(digitalPinToInterrupt(pinB), PinB, RISING);
pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)

}
void PinA(){
detachInterrupt(0); //stop interrupts happening before we read pin values
reading = PINB & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos --; //decrement the encoder's position count
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
attachInterrupt(0,PinA,RISING);
}

void PinB(){
detachInterrupt(1); //stop interrupts happening before we read pin values
reading = PINB & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos ++; //increment the encoder's position count
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
attachInterrupt(1,PinB,RISING); //restart interrupts
}

  1. Formatiere Deinen Beitrag damit man was versteht.
  2. Wer benutzt hier einen STM§F103?

Grüße Uwe

Hallo,

vergiss den Mist mit Phasenauswertung mittels Interrupt. Das ist die reine Seuche im Internet. Diesen Unsinn hat mal einer in Netz gestellt und nun machen den Blödsinn alle nach. Das ist ähnlich das ein hornalter L298N einfach nicht ausstirbt.

Lies dich hier ein und dein Drehgeber wird auch ausgeleiert noch funktionieren.
Wie man es richtig macht.

Übrigens wird die Toolchain für STM einen anderen Compiler verwenden. Manche lieb gewonnene Komfortsyntax wird dort fehlen bzw. anders sein. Lies die Fehlermeldung und behebe sie.

Die Fehlermeldung lautet wie? Warum wird das immer vergessen?

Meine Glaskugel sagt, er möchte eine Klammer mehr haben oder die Binärschreibeweise muss anders lauten. Irgend sowas.

Fehlermeldung: " 'PINB' was not declared in this scope"
Wundert mich nicht wirklich, es sollte der PORT B ausgelesen werden, und dann logisch verknüpft werden, da an diesem der Drehgeber hängt. Wie mache ich das? Geht anscheinend nicht so einfach wie beim Atmega 328. Hängt wohl hiermit zusammen: GPIO Input data register.

Kurze Suche, etwas fummeln; port = GPIOB->regs->IDR & 0x0004; // 0x0004 sind die Bits die überprüft werden sollen

Hallo,

PINB ist ein definierter Registername bei AVR. Von solchen Meldungen wirst noch viel mehr bekommen wenn du diesen behoben hast. STM ist nun einmal nicht AVR. Anderer Controller andere Register. Du wirst mittels Manual alles anpassen müssen.

was soviel in diesem Fall nicht ist, aber danke für den Hinweis. Bin dabei

Übrigens:
Der STM32F103 hat eine menge Timer!
Ein großer Teil davon kann Drehgeber in seiner Hardware eigenständig dekodieren.
Somit sollte es nicht die Notwendigkeit geben, das "Händisch" auszuwerten zu müssen.

Falls es einen interessiert,. habe es hiermit hinbekommen.
Rotary encoder with blue pill - Arduino for STM32

Hallo,

erneut mit Interrupt?
Und kannst du dein Eingangsposting mittels Code Tags noch formatieren?

Ne, ist ganz ohne INT. Hat auch mit dem Eingangsposting nix mehr zu tun.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.