Hallo,
bin dabei eine Lib zu bauen für meinen GrayEncoder, arbeitet mit Hallsensoren und Magneten.
Erst einfache Funktion zusammengestellt, dann geschafft wie man einen Port einer Funktion übergibt. Funktioniert.
Und nun wollte ich daraus eine Lib bauen, weil ich mehrere gekapselte Rückgabewerte benötige.
Und nun scheitert es an der Portübergabe an die Lib. Weil das mit Zeigern und call by Referenz geschieht.
Was ist an der Lib noch falsch?
hier der Sketch der für sich funktioniert
#include <SPI.h>
#include <DogLcdSPI.h> // nutzt Hardware SPI
// Struktur Deklaration
struct TLE4905 { // Typname "TLE4905"
byte Phase_A;
byte Phase_B;
int Direction;
} Sektor1, Sektor2, Sektor3; // Strukturvariablen Sektor1, Sektor2, ...
volatile int enc_counter; // Zähler
DogLcdSPI lcd(22, 24);
void setup() {
pinMode(53, OUTPUT);
SPI.begin();
lcd.begin(DOG_LCD_M163);
Sektor3.Phase_A = 4; // Port.C Bit 4
Sektor3.Phase_B = 5; // Port.C Bit 5
}
void loop() {
Sektor3.Direction = GrayEncoder(&PINC, Sektor3.Phase_A, Sektor3.Phase_B);
lcd.setCursor(0,1);
lcd.print(enc_counter); lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(Sektor3.Direction); lcd.print(" ");
} // loop Ende
// ****** Funktionen ******* //
int GrayEncoder (volatile uint8_t *Port, byte A, byte B)
{
static int enc_last = 0x02;
int i = 0;
static int Direction = 0;
byte PHASE_A=0;
byte PHASE_B=0;
PHASE_A = (*Port >> A & 1); // einzelnes Bit heraus fischen
PHASE_B = (*Port >> B & 1);
lcd.setCursor(13,0); lcd.print(PHASE_B);
lcd.setCursor(15,0); lcd.print(PHASE_A);
if( PHASE_A ) {
i = 1;
}
if( PHASE_B ) {
i ^= 3; // convert gray to binary
}
i -= enc_last; // difference new - last
if( i & 1 ) { // bit 0 = value (1)
enc_last += i; // store new as next last
enc_counter += (i & 2) - 1; // bit 1 = direction (+/-), Zähler
Direction = (i & 2) - 1; // Richtungsanzeige (+1 / -1)
}
return Direction;
}
Compilermeldung mit der Lib.
Arduino: 1.6.8 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
GrayEncoder:17: error: invalid conversion from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]
GrayEncoder Sektor3 (&PINC, 4, 5); // Port.C an Pin "Bit 4", "Bit 5"
^
In file included from C:\Users\Worker\Documents\Arduino\libraries\GrayEncoder\examples\GrayEncoder\GrayEncoder.ino:13:0:
C:\Users\Worker\Documents\Arduino\libraries\GrayEncoder/GrayEncoder.h:12:5: error: initializing argument 1 of 'GrayEncoder::GrayEncoder(uint8_t, uint8_t, uint8_t)' [-fpermissive]
GrayEncoder (volatile uint8_t, uint8_t, uint8_t); // Port, Port.Bit, Port.Bit
^
exit status 1
invalid conversion from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]
im Anhang die Lib Dateien.
GrayEncoder.h (862 Bytes)
GrayEncoder.cpp (887 Bytes)
GrayEncoder.ino (697 Bytes)