LED-Key Modul am Arduino: Unterschied zwischen den Versionen

Aus eLAB Wiki
Wechseln zu: Navigation, Suche
(Programm für den Arduino)
Zeile 9: Zeile 9:
 
== Programm für den Arduino ==
 
== Programm für den Arduino ==
  
Das Arduino Programm gibt einen Timer auf der 7-Segment-Anzeige aus.
+
Das Arduino Demo aus dem Make-Magazin 4/18 zeigt die Ausgabe auf der 7-Segment-Anzeige und den LEDs, sowie die Abfrage der Tasten per Soft-SPI.
Es werden die [https://github.com/Erriez/ErriezTM1638 TM1368] und die [https://github.com/Erriez/ErriezLKM1638 LKM1638] Bibliothek benötigt.
 
  
 
<syntaxhighlight lang="Arduino">
 
<syntaxhighlight lang="Arduino">
/* JY-LKM1638 board v1.1 demo
+
/*
  *
+
  * Make: 4/2018
  * Required libraries:
+
  * Minimalistisches Beispiel zur Ansteuerung eines TM1638 mit 8-Digit 7-Segment-LED per Software-SPI, 8 LEDs und 8 Buttons
  *   https://github.com/Erriez/ErriezTM1638
+
  * Idee: https://github.com/MartyMacGyver/TM1638-demos-and-examples
https://github.com/Erriez/ErriezLKM1638
 
 
  */
 
  */
  
#include <ErriezLKM1638Board.h>
+
#include <SPI.h>
  
// Connect display pins to the Arduino DIGITAL pins
+
// Wir nutzen Software-SPI. Pins frei wählbar:
#if ARDUINO_ARCH_AVR
+
const uint8_t TM1638_CS=4;
#define TM1638_CLK_PIN      2
+
const uint8_t TM1638_CLK=2;
#define TM1638_DIO_PIN      3
+
const uint8_t TM1638_DATA=3;
#define TM1638_STB0_PIN    4
 
#elif ARDUINO_ARCH_ESP8266
 
#define TM1638_CLK_PIN      D2
 
#define TM1638_DIO_PIN      D3
 
#define TM1638_STB0_PIN    D4
 
#elif ARDUINO_ARCH_ESP32
 
#define TM1638_CLK_PIN      0
 
#define TM1638_DIO_PIN      4
 
#define TM1638_STB0_PIN    5
 
#else
 
#error "May work, but not tested on this target"
 
#endif
 
  
// Create LKM1638Board object
+
/*
LKM1638Board lkm1638(TM1638_CLK_PIN, TM1638_DIO_PIN, TM1638_STB0_PIN);
+
* @brief  Sendet Wert per SPI, togglet CLK
 +
* @param  Wert
 +
* @return  none
 +
*/
 +
void sendCommand(uint8_t value)
 +
{
 +
  digitalWrite(TM1638_CS, LOW);    // Modul CS
 +
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, value);
 +
  digitalWrite(TM1638_CS, HIGH);
 +
}
  
static uint8_t led = 0;
+
/*
static uint8_t dir = 1;
+
* @brief  Initialisiert Display
static LedColor color = LedRed;
+
* @param  none
static uint8_t ledDelay = 0;
+
* @return  none
static uint8_t ledDelayInit = 30;
+
*/
 +
void sendReset()
 +
{
 +
  // Flowchart for program design in the modes of auto address increment by 1 and fixed address:
 +
  sendCommand(0x40); // Set command for writing data into display memory, in the mode of auto address increment by 1
 +
 
 +
  digitalWrite(TM1638_CS, LOW);     // Besonderheit: kein CS Toggle waehrend der Uebertragung
 +
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xC0); // set starting address (0x0C0)
 +
 
 +
  for(uint8_t i = 0; i < 16; i++)
 +
    shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x00);   // 16x 0 senden
  
static bool countEnable = false;
+
  digitalWrite(TM1638_CS, HIGH);
static bool countUp = true;
+
}
static unsigned long countValue = 0;
 
static uint8_t countDelay = 0;
 
static uint8_t countDelayInit = 10;
 
  
static uint8_t brightness = 2;
+
/*
 +
* @brief  Gibt die Zahlen 0..9 nacheinander gleichzeitg auf allen Stellen aus
 +
*          Definiert die anzuzeigenden Segmente fuer jede Zahl
 +
* @param  none
 +
* @return  none
 +
*/
 +
void counting()
 +
{
 +
                      //0// //1// //2// //3// //4// //5// //6// //7// //8// //9//
 +
  uint8_t digits[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
 +
  uint8_t value;
  
// Function prototypes
+
  for (uint8_t value=0; value <= 9; value++)
static void handleButtons(void);
+
  {
static void handleLEDs(void);
+
    sendCommand(0x40);    //Set command for writing data into display memory, in the mode of auto address increment by 1
static void handleDisplay(void);
+
   
 +
    digitalWrite(TM1638_CS, LOW);  // Besonderheit: kein CS Toggle waehrend der Uebertragung
 +
    shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xC0);  // set starting address (0x0C0)
 +
   
 +
    for(uint8_t i = 0; i < 8; i++)
 +
    {
 +
      shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, digits[value]);  // Wert
 +
      shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x00);
 +
    }
 +
    digitalWrite(TM1638_CS, HIGH);
 +
    delay (500);
 +
  }
 +
}
  
 +
/*
 +
* @brief  Gibt Text aus. Funktioniert im Prinzip wie Zahlenausgabe, nur mit eigenen Zeichen
 +
*          Definiert die anzuzeigenden Segmente fuer jede Zahl
 +
* @param  none
 +
* @return  none
 +
*/
 +
void text()
 +
{
 +
  uint8_t scrollText[] =
 +
  {
 +
    //H// //E// //L// //L// //O// //.// //.// //.//
 +
    0x76, 0x79, 0x38, 0x38, 0x3f, 0x80, 0x80, 0x80
 +
  };
  
void setup()
+
  sendCommand(0x40);
{
+
 
    Serial.begin(115200);
+
  digitalWrite(TM1638_CS, LOW);
    while (!Serial) {
+
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xc0);
        ;
 
    }
 
    Serial.println(F("JY-LKM1638 demo"));
 
    Serial.println(F("Press a button:"));
 
    Serial.println(F("  SW1: Start/stop counting"));
 
    Serial.println(F("  SW2: Reset counter"));
 
    Serial.println(F("  SW3: Toggle increment/decrement"));
 
    Serial.println(F("  SW4: Increment brightness"));
 
    Serial.println(F("  SW5: Color LED speed up"));
 
    Serial.println(F("  SW6: Color LED speed down"));
 
    Serial.println(F("  SW7: Toggle direction color LED"));
 
    Serial.println(F("  SW8: Toggle color LED red/green"));
 
  
     // Initialize TM1638
+
  for(int i = 0; i < 8; i++)
     lkm1638.begin();
+
  {
    lkm1638.clear();
+
     shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, scrollText[i]);  // Wert
    lkm1638.setBrightness(brightness);
+
     shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x00);
 +
  }
 +
  digitalWrite(TM1638_CS, HIGH);
 +
  delay (3000);
 
}
 
}
  
void loop()
+
uint8_t readButtons(void)
 
{
 
{
    handleButtons();
+
  uint8_t buttons = 0;
    handleLEDs();
+
 
    handleDisplay();
+
  digitalWrite(TM1638_CS, LOW);
    delay(10);
+
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x42);
}
 
  
static void handleButtons(void)
+
  // Ab jetzt Daten empfangen:
{
+
  pinMode(TM1638_DATA, INPUT);
    static uint8_t keysMask = 0;
 
    uint8_t keys;
 
  
     // Get keys
+
  for (uint8_t i = 0; i < 4; i++)
     keys = lkm1638.getButtons();
+
  {
 +
     uint8_t v = shiftIn (TM1638_DATA, TM1638_CLK, LSBFIRST) << i;    // Daten einlesen, kein MISO sondern gleicher Pin wie MOSI
 +
     buttons |= v;
 +
  }
  
    // Mask keys to execute increment or decrement state only when the switch is
+
  pinMode(TM1638_DATA, OUTPUT);    // wieder als Eingang
    // down the first time
+
  digitalWrite(TM1638_CS, HIGH);
    if (keys == 0) {
+
  return buttons;
        keysMask = 0;
+
}
     } else {
 
        if (keysMask == 0) {
 
            if (keys & 0x01) {
 
                // Change color
 
                if (color == LedGreen) {
 
                    color = LedRed;
 
                } else {
 
                    color = LedGreen;
 
                }
 
            } else if (keys & 0x02) {
 
                // Change direction
 
                dir ^= 1;
 
            } else if (keys & 0x04) {
 
                // Speed down
 
                if (ledDelayInit < 20) {
 
                    ledDelayInit += 2;
 
                }
 
            } else if (keys & 0x08) {
 
                // Speed up
 
                if (ledDelayInit > 2) {
 
                    ledDelayInit -= 2;
 
                }
 
            } else if (keys & 0x10) {
 
                // Change display brightness
 
                if (brightness == 7) {
 
                    brightness = 0;
 
                } else {
 
                    brightness++;
 
                }
 
                lkm1638.setBrightness(brightness);
 
            } else if (keys & 0x20) {
 
                // Toggle count up/down
 
                countUp ^= true;
 
            } else if (keys & 0x40) {
 
                // Clear count
 
                countValue = 0;
 
            } else if (keys & 0x80) {
 
                // Toggle count enable
 
                countEnable ^= true;
 
            }
 
  
            keysMask = keys;
+
 
        }
+
/*
    }
+
* @brief  Schaltet eine LED an (1) oder aus (0)
 +
* @param  Zustand
 +
*          LED (0=links)
 +
* @return  none
 +
*/
 +
void setLed(uint8_t value, uint8_t position)
 +
{
 +
  sendCommand(0x44);
 +
  digitalWrite(TM1638_CS, LOW);
 +
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xC1 + (position << 1));
 +
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, value);
 +
  digitalWrite(TM1638_CS, HIGH);
 
}
 
}
  
static void handleLEDs(void)
+
/*
 +
* @brief  Laesst die LEDs nacheinader leuchten
 +
* @param  none
 +
* @return  none
 +
*/
 +
void lauflicht ()
 
{
 
{
    if (ledDelay <= 4) {
+
  for (uint8_t i=0; i<=7; i++)
        ledDelay = ledDelayInit;
+
  {
 +
    setLed (1, i);
 +
    delay (200);
 +
    setLed (0, i);
 +
  }
 +
}
  
        lkm1638.colorLEDsOff(0xff);
+
/*
        lkm1638.colorLEDsOn((uint8_t)(1 << led), color);
+
* @brief  Fragt die Taster ab und laesst die passende LED leuchten
 +
* @param  none
 +
* @return  none
 +
*/
 +
void taster ()
 +
{
 +
  uint8_t buttons = readButtons();
 +
  uint8_t position, mask;
  
        if (dir) {
+
  for(position = 0; position < 8; position++)
            if (led >= 7) {
+
  {
                led = 0;
+
    mask = 0x01 << position;
            } else {
+
     setLed(buttons & mask ? 1 : 0, position);
                led++;
+
  }
            }
 
        } else {
 
            if (led == 0) {
 
                led = 7;
 
            } else {
 
                led--;
 
            }
 
        }
 
     } else {
 
        ledDelay--;
 
    }
 
 
}
 
}
 
+
 
static void handleDisplay(void)
+
void setup()
 
{
 
{
    if (countEnable) {
+
  digitalWrite(TM1638_CS, HIGH);
        if (countDelay <= 5) {
+
  digitalWrite(TM1638_DATA, LOW);
            countDelay = countDelayInit;
+
  digitalWrite(TM1638_CLK, LOW);
 +
  pinMode(TM1638_CS, OUTPUT);
 +
  pinMode(TM1638_DATA, OUTPUT);
 +
  pinMode(TM1638_CLK, OUTPUT);
  
            if (countUp) {
+
  sendCommand(0x8F); // Display ON, max. Helligkeit
                if (countValue == 99999999) {
+
  sendReset();
                    countValue = 0;
+
}
                } else {
 
                    countValue++;
 
                }
 
            } else {
 
                if (countValue == 0) {
 
                    countValue = (uint32_t)99999999UL;
 
                } else {
 
                    countValue--;
 
                }
 
            }
 
        } else {
 
            countDelay--;
 
        }
 
    }
 
  
    lkm1638.setPrintPos(0);
+
void loop()
     lkm1638.print(countValue);
+
{
 +
  counting();
 +
  text();
 +
  lauflicht();
 +
  sendCommand(0x88);    // Display ON, min. Helligkeit (Datenblatt Kap. 5.3)
 +
  while (1)
 +
     taster();
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
= web Links =
 +
 +
* [https://github.com/MartyMacGyver/TM1638-demos-and-examples Github - Marty MacGyver TM1638 Demos]
 +
* [https://github.com/Erriez/ErriezTM1638 TM1368]
 +
* [https://github.com/Erriez/ErriezLKM1638 LKM1638]
 +
  
 
[[Category:Elektronik]]
 
[[Category:Elektronik]]
 
[[Category:Arduino]]
 
[[Category:Arduino]]

Version vom 1. Februar 2020, 15:16 Uhr

Anschluss des HC-SR04 an den Arduino

zum Anschluss des LED-Key Board an ein Arduino Board werden drei I/O Pins benötigt.

LED&Key Verdrahtung

LED&Key Schaltplan

Programm für den Arduino

Das Arduino Demo aus dem Make-Magazin 4/18 zeigt die Ausgabe auf der 7-Segment-Anzeige und den LEDs, sowie die Abfrage der Tasten per Soft-SPI.

/*
 * Make: 4/2018
 * Minimalistisches Beispiel zur Ansteuerung eines TM1638 mit 8-Digit 7-Segment-LED per Software-SPI, 8 LEDs und 8 Buttons
 * Idee: https://github.com/MartyMacGyver/TM1638-demos-and-examples
 */

#include <SPI.h>

// Wir nutzen Software-SPI. Pins frei wählbar:
const uint8_t TM1638_CS=4;
const uint8_t TM1638_CLK=2;
const uint8_t TM1638_DATA=3;

/*
 * @brief   Sendet Wert per SPI, togglet CLK
 * @param   Wert
 * @return  none
 */
void sendCommand(uint8_t value)
{
  digitalWrite(TM1638_CS, LOW);    // Modul CS
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, value);
  digitalWrite(TM1638_CS, HIGH);
}

/*
 * @brief   Initialisiert Display
 * @param   none
 * @return  none
 */
void sendReset()
{
  // Flowchart for program design in the modes of auto address increment by 1 and fixed address:
  sendCommand(0x40);  // Set command for writing data into display memory, in the mode of auto address increment by 1
  
  digitalWrite(TM1638_CS, LOW);     // Besonderheit: kein CS Toggle waehrend der Uebertragung
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xC0);  // set starting address (0x0C0)
  
  for(uint8_t i = 0; i < 16; i++)
    shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x00);    // 16x 0 senden

  digitalWrite(TM1638_CS, HIGH);
}

/*
 * @brief   Gibt die Zahlen 0..9 nacheinander gleichzeitg auf allen Stellen aus
 *          Definiert die anzuzeigenden Segmente fuer jede Zahl
 * @param   none
 * @return  none
 */
void counting()
{
                       //0// //1// //2// //3// //4// //5// //6// //7// //8// //9//
  uint8_t digits[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
  uint8_t value;

  for (uint8_t value=0; value <= 9; value++)
  {
    sendCommand(0x40);    //Set command for writing data into display memory, in the mode of auto address increment by 1
    
    digitalWrite(TM1638_CS, LOW);   // Besonderheit: kein CS Toggle waehrend der Uebertragung
    shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xC0);  // set starting address (0x0C0)
    
    for(uint8_t i = 0; i < 8; i++)
    {
      shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, digits[value]);  // Wert
      shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x00);  
    }
    digitalWrite(TM1638_CS, HIGH);
    delay (500);
  }
}

/*
 * @brief   Gibt Text aus. Funktioniert im Prinzip wie Zahlenausgabe, nur mit eigenen Zeichen
 *          Definiert die anzuzeigenden Segmente fuer jede Zahl
 * @param   none
 * @return  none
 */
void text()
{
  uint8_t scrollText[] =
  {
    //H// //E// //L// //L// //O// //.// //.// //.//
    0x76, 0x79, 0x38, 0x38, 0x3f, 0x80, 0x80, 0x80
  };

  sendCommand(0x40);
  
  digitalWrite(TM1638_CS, LOW);
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xc0);

  for(int i = 0; i < 8; i++)
  {
    shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, scrollText[i]);  // Wert
    shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x00);  
  }
  digitalWrite(TM1638_CS, HIGH);
  delay (3000);
}

uint8_t readButtons(void)
{
  uint8_t buttons = 0;
  
  digitalWrite(TM1638_CS, LOW);
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0x42);

  // Ab jetzt Daten empfangen:
  pinMode(TM1638_DATA, INPUT);

  for (uint8_t i = 0; i < 4; i++)
  {
    uint8_t v = shiftIn (TM1638_DATA, TM1638_CLK, LSBFIRST) << i;    // Daten einlesen, kein MISO sondern gleicher Pin wie MOSI
    buttons |= v;
  }

  pinMode(TM1638_DATA, OUTPUT);     // wieder als Eingang
  digitalWrite(TM1638_CS, HIGH);
  return buttons;
}


/*
 * @brief   Schaltet eine LED an (1) oder aus (0)
 * @param   Zustand
 *          LED (0=links)
 * @return  none
 */
void setLed(uint8_t value, uint8_t position)
{
  sendCommand(0x44);
  digitalWrite(TM1638_CS, LOW);
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, 0xC1 + (position << 1));
  shiftOut (TM1638_DATA, TM1638_CLK, LSBFIRST, value);
  digitalWrite(TM1638_CS, HIGH);
}

/*
 * @brief   Laesst die LEDs nacheinader leuchten
 * @param   none
 * @return  none
 */
void lauflicht ()
{
  for (uint8_t i=0; i<=7; i++)
  {
    setLed (1, i);
    delay (200);
    setLed (0, i);
  }
}

/*
 * @brief   Fragt die Taster ab und laesst die passende LED leuchten
 * @param   none
 * @return  none
 */
void taster ()
{
  uint8_t buttons = readButtons();
  uint8_t position, mask;

  for(position = 0; position < 8; position++)
  {
    mask = 0x01 << position;
    setLed(buttons & mask ? 1 : 0, position);
  }
}
  
void setup()
{
  digitalWrite(TM1638_CS, HIGH);
  digitalWrite(TM1638_DATA, LOW);
  digitalWrite(TM1638_CLK, LOW);
  pinMode(TM1638_CS, OUTPUT);
  pinMode(TM1638_DATA, OUTPUT);
  pinMode(TM1638_CLK, OUTPUT);

  sendCommand(0x8F);  // Display ON, max. Helligkeit
  sendReset();
}

void loop()
{
  counting();
  text();
  lauflicht();
  sendCommand(0x88);    // Display ON, min. Helligkeit (Datenblatt Kap. 5.3)
  while (1)
    taster();
}

web Links