grblHAL / ESP32

grblHAL driver for ESP32
Other
70 stars 42 forks source link

Changing pin mapping - MKS Tinybee #95

Closed terjeio closed 7 months ago

terjeio commented 7 months ago
          i got a question, not related  to the ticket. how can i single control these pins for another project...  https://github.com/makerbase-mks/MKS-TinyBee/issues/113

Originally posted by @SphaeroX in https://github.com/grblHAL/ESP32/issues/27#issuecomment-1939419368

terjeio commented 7 months ago

You can create a new pin map based on the original, easiest is just copying it to _my_machinemap.h and enable it in my_machine.h. FYI there is support for an I2C I/O expander in the codebase as well, it will be a bit more work to integrate that in a derived board map.

SphaeroX commented 7 months ago

As I already said, it has nothing to do with grblHAL, I just hope that someone here can kindly help me so that I can switch the outputs. Due to the complexity of the code, I unfortunately can't see through it. I tried to create it in a simple way, unfortunately, the outputs do not switch.

// IOExpander.h
#ifndef IO_EXPANDER_H
#define IO_EXPANDER_H

#define DATA_PIN 27
#define LATCH_PIN 26
#define CLOCK_PIN 25

void setupIOExpander()
{
    pinMode(DATA_PIN, OUTPUT);
    pinMode(LATCH_PIN, OUTPUT);
    pinMode(CLOCK_PIN, OUTPUT);
}

void test()
{
    byte patternHigh = 0b11111111;
    byte patternLow = 0b00000000;

    Serial.println("HIGH");
    digitalWrite(LATCH_PIN, LOW);
    for (int i = 0; i < 3; i++)
    {
        shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, patternHigh);
    }
    digitalWrite(LATCH_PIN, HIGH);

    delay(1000);

    Serial.println("LOW");
    digitalWrite(LATCH_PIN, LOW);
    for (int i = 0; i < 3; i++)
    {
        shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, patternLow);
    }
    digitalWrite(LATCH_PIN, HIGH);
}

#endif
terjeio commented 7 months ago

Here is the code for shifting out the data. You have the latch signal the wrong way? It should be high shifting in, low when latching. See the datasheet for the shift register.

SphaeroX commented 7 months ago

no idea how I did it but it works now. I had completely disconnected the power and tried to switch and measure only the data pin on the esp332, which worked. Maybe a Hardware issue. After that, the code worked.

heres my code now (working):

// IOExpander.h
#ifndef IO_EXPANDER_H
#define IO_EXPANDER_H

#define DATA_PIN 27
#define LATCH_PIN 26
#define CLOCK_PIN 25

#define NUM_REGISTERS 3   
#define PINS_PER_REGISTER 8 

byte registerStates[NUM_REGISTERS] = {0, 0, 0};

void setupIOExpander()
{
    pinMode(DATA_PIN, OUTPUT);
    pinMode(LATCH_PIN, OUTPUT);
    pinMode(CLOCK_PIN, OUTPUT);
}

void updateShiftRegisters()
{
    digitalWrite(LATCH_PIN, LOW);
    for (int i = NUM_REGISTERS - 1; i >= 0; i--)
    {
        shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, registerStates[i]);
    }
    digitalWrite(LATCH_PIN, HIGH);
}

void setPin(int pin, bool state)
{
    int registerIndex = (pin - 1) / PINS_PER_REGISTER;
    int pinIndex = (pin - 1) % PINS_PER_REGISTER;

    if (state)
        registerStates[registerIndex] |= (1 << pinIndex);
    else
        registerStates[registerIndex] &= ~(1 << pinIndex);

    updateShiftRegisters();
}
#endif