Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Gelöst: LCD zeigt seltsame Zeichen bei ESP32

18.04.2020 (👁8905)


Betrifft ESP32 nodeMCU Arduino und LiquidCrystal_I2C LCD

Beim Anschluß von LCD Devices an ein ESP32 Microcontroller kommt es immer wieder vor, dass das LCD Display nach einiger Zeit komische kryptische Zeichen anzeigt.

Ich hatte jetzt zwei Tage nach einer Lösung gesucht und verschiedene Arduino Library StÀnde hochgeladen und verschiedene Foren mit Lötungen und Störsignalen untersucht.

Das Problem lag allerdings an einer zu hohen Takt-Rate.

Lösung in Arduino / ESP32

Man kann in der Wire.h Library die Clockrate der I2C Verbindung verÀndern.

Hier zu bindet man die Wire.h ein

#include <Wire.h>

Anschliessend kann man an irgend einer Stelle die Taktrate, Taktfrequenz Àndern. Entweder im Init oder loop

Indem man Wird.setClock(Taktrate in Hz) eingibt.

Wire.setClock(10000);

Beispiel Code, Arduino

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

..

//< Setup: LCD >

int lcdColumns = 16;

int lcdRows = 2;

//LCD Adress on 0x27

LiquidCrystal_I2C lcd(0x27lcdColumnslcdRows);  

//</ Setup: LCD >

 String lcd_Text0= String("Puls.: ") ;

 String lcd_Text1= String("Freq.: ") ;

 

    Wire.setClock(10000);

    int row0=0;

    int row1=1;

    int col0=0;

    int col1=0;

  

    

    lcd.setCursor(col0, row0);

    lcd.print(lcd_Text0);

    lcd.setCursor(col0,row1);

    lcd.print(lcd_Text1);

 

 

 

 

Beispiel Code Anwendung Pulsgeber Arduino mit einstellbarer PulslÀnge PWM Duty

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

..

#include "wiring_shift_mod.h"

#define CLOCK_TYPE CLOCK_INVERT

#define CLOCK_DELAY_US 1

//====< VARIABLES >====

//< setup: TM1638 LED Buttons >

const int strobe_pin =  4;

const int clock_pin  = 16;

const int data_pin   = 17;

//</ setup: TM1638 LED Buttons >

//< button_values >

const int BTN_PULSWIDTH_DOWN=1;

const int BTN_PULSWIDTH_UP=128;

const long TIME_PUSHED_BUTTON_LONG=200;//us microsekonds

const long freq_MHz_Nanoseconds=1000;

const long freq_kHz_Nanoseconds=1000000;

const long freq_Hz_Nanoseconds=1000000000;

//</ button_values >

//< Pulse >

long nHighCyles=1;

unsigned long time_Button_press_start=0;

//</ Pulse >  

//< Setup: LCD >

int lcdColumns = 16;

int lcdRows = 2;

//LCD Adress on 0x27

LiquidCrystal_I2C lcd(0x27lcdColumnslcdRows);  

//</ Setup: LCD >

//< GPIO >

int OUT_PIN = 15;

int IN1=12;

int IN2=14;

int IN3=27;

int IN4=26;

int IN5=25;

int IN6=33;

int IN7=32;

int IN8=35;

int IN9=34;

//</ GPIO >

int min_Pulswidth_ns=166;

String lcd_Text="";

uint8_t last_buttons=0;

//====</ VARIABLES >====




//=============< SETUP >============

void setup(){

  //--------< setup() >--------

  

  //< GPIO >

  pinMode(OUT_PIN, OUTPUT);

  //</ GPIO >

  //< TM1638 Buttons >

  pinMode(strobe_pin, OUTPUT);

  pinMode(clock_pin, OUTPUT);

  pinMode(data_pin, OUTPUT);

  sendCommand(0x8f);  // activate

  //</ TM1638 Buttons >

  //< LCD >

  // initialize LCD

  //lcd.init();

  lcd.begin();

  // turn on LCD backlight                      

  lcd.backlight();

  //</ LCD >

    

  reset();

  Serial.begin(115200);

  Serial.println("\nPulswith Setter");

  uint32_t wire_clock=Wire.getClock();

  Serial.println(wire_clock);

  //--------</ setup() >--------

}

//=============</ SETUP >============





//=============< Main_Loop >============

void loop(){

  //--------< Main Loop() >--------

  //--< Read IN >--

  //int valIN =0;

  //actInput=0;

  //< read buttons TM1638 >

  uint8_t buttons = readButtons();

  //</ read buttons TM1638 >

  

  //< check buttons changed >

  bool buttons_changed=false;

  bool buttons_value_changed=false;

  if(buttons != last_buttons) buttons_value_changed=true;

  last_buttons=buttons; //save buttons state

  //</ check buttons changed >

  

  //< check push button time >

  unsigned long timeDiff_Button_pushed = millis()- time_Button_press_start ;

  //Serial.println("timeDiff_Button_pushed= " + String(timeDiff_Button_pushed));

  //</ check push button time >

  

  

  //----< check Button pushed >----

  if(buttons>0 )

  {

    //----< buttons pushed >---- 

    if ((buttons_value_changed==true ) || (timeDiff_Button_pushed > TIME_PUSHED_BUTTON_LONG))  

    {

      //----< Buttons_changed >----

      buttons_changed=true;

      Serial.println("Buttons changed to " + String(buttons));  //*value= 1 2 4 8 16 32 64 128  left to right

      time_Button_press_start=millis();

      

      //< show_button_led >

      for(uint8_t position = 0; position < 8; position++)

      {

        uint8_t mask = 0x1 << position;

        setLed(buttons & mask ? 1 : 0, position); //show Buttons on LED

      }

      //</ show_button_led >

      //--</ Read IN >--

    

      //--< calculate_buttons >--

      switch(buttons)

      {

        case BTN_PULSWIDTH_UP:

          nHighCyles=nHighCyles+1;

          break;

          

        case BTN_PULSWIDTH_DOWN:

          nHighCyles=nHighCyles-1;

          break;

      }

        

      if(nHighCyles<1) nHighCyles=1;

      //--</ calculate_buttons >--

  

      Serial.println(" nHighCycles=" + String(nHighCyles));

      //----</ Buttons_changed >----

    }

    //----</ buttons pushed >----

  }

  //----</ check Button pushed >----

  //----</ check Button long_pushed >----  

  

  

  

  //---< Signal OUT >---

  //*Square Signal with n-times ON

  //*short Impuls 1-20 ON=> 100ns-2us

  //1 x GPIO-Updae-Cycle=166ns nanoseconds

  //----< @Loop: nPulses >----

  int nPulses =20;

  for (int iPulse=0;iPulse<=nPulses;iPulse++)

  {

    //--< @loop: HIGH-Cycles >--

    for(long iHIGH=0;iHIGH<nHighCyles;iHIGH++)

    {

      digitalWrite(OUT_PIN, HIGH);   //PULS_ON

    }

    //--</ @loop: HIGH-Cycles >--

    

    //--< @loop: LOW-Cycles >--

    for(long iHIGH=0;iHIGH<nHighCyles;iHIGH++)

    {

      //< OFF >

      digitalWrite(OUT_PIN, LOW);    // PULS_OFF

      //</ OFF >

    }

    //--</ @loop: LOW-Cycles >--

  }

  //----</ @Loop: nPulses >----

  //*Service-Works in long Off-time

  //*long time

  delayMicroseconds(1000);              // wait for a second

  //--< OFF-Signal >--

  //---</ Signal OUT >---

  

  //< calculate >

  double nsPulswidth= double(nHighCyles * min_Pulswidth_ns);

  float frequency;

  long freqNanoseconds=0;

  String freqRangeText="";

  if (nsPulswidth<freq_MHz_Nanoseconds)

  {

    freqRangeText="MHz";

    frequency=double( freq_MHz_Nanoseconds / nsPulswidth );

  }

  else if (nsPulswidth<freq_kHz_Nanoseconds)

  {

    freqRangeText="kHz";

    frequency=double( freq_kHz_Nanoseconds / nsPulswidth );

  }

  else 

  {

    freqRangeText="Hz";

    frequency=double( freq_Hz_Nanoseconds / nsPulswidth );

  }

  //Serial.println("nHighCycles=" + String(nHighCyles)  + " nsPulswidth=" + String(nsPulswidth) + " frequency=" + String(frequency) );

  //</ calculate >

  

  //----< LCD Output >----

  if (buttons_changed==true)

  {

    long lngPulswidth=nsPulswidth;

    long lngFrequency=frequency;

    String lcd_Text0= String("Puls.: ") + String(lngPulswidth)  + " ns";

    lcd_Text0=lngPulswidth;

    //String lcd_Text0="line0..";

    //lcd_Text0=lcd_Text0.substring(0,15);

    String lcd_Text1= String("Freq.: ")  + String(lngFrequency) + " " + freqRangeText ;

    lcd_Text1=lngFrequency;

    //String lcd_Text1="line1..";

    //lcd_Text1=lcd_Text1.substring(0,15);

    

    //String lcd_Text1= String(nsPulswidth);

    //String lcd_Text1= String(float(nHighCyles));

    

    Serial.println(lcd_Text0 + " / " + lcd_Text1);

  

    //< display info on LCD >

    //lcd.clear(); 

    //lcd.begin();

    //lcd.home();

    Wire.setClock(10000);

    int row0=0;

    int row1=1;

    int col0=0;

    int col1=0;

  

    

    lcd.setCursor(col0, row0);

    lcd.print(lcd_Text0);

    lcd.setCursor(col0,row1);

    lcd.print(lcd_Text1);

    //</ display info on LCD >

  }

  //----</ LCD Output >----

  //--------</ Main Loop() >--------

}

//=============</ Main_Loop >============












//=============< functions: TM1638 >============

void sendCommand(uint8_t value)

{

  digitalWrite(strobe_pin, LOW);

  shiftOutMod(data_pin, clock_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, value);

  digitalWrite(strobe_pin, HIGH);

}

void reset()

{

  sendCommand(0x40); // set auto increment mode

  digitalWrite(strobe_pin, LOW);

  shiftOutMod(data_pin, clock_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0xc0);   // set starting address to 0

  for(uint8_t i = 0; i < 16; i++)

  {

    shiftOutMod(data_pin, clock_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0x00);

  }

  digitalWrite(strobe_pin, HIGH);

}



uint8_t readButtons(void)

{

  uint8_t buttons = 0;

  digitalWrite(strobe_pin, LOW);

  shiftOutMod(data_pin, clock_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0x42);

  pinMode(data_pin, INPUT);

  for (uint8_t i = 0; i < 4; i++)

  {

    //*ShiftIn: Einlesen 8* Bit von data_pin ->in->byte v. clock_type: 

    uint8_t v = shiftInMod(data_pin, clock_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US) << i;  //*bitshiftleft i-bits (2):  0001 -> 0100

    buttons |= v;   //*OR 00|01 -> 01

  }

  

  pinMode(data_pin, OUTPUT);

  digitalWrite(strobe_pin, HIGH);

  return buttons;

}







void setLed(uint8_t valueuint8_t position)

{

  pinMode(data_pin, OUTPUT);

  sendCommand(0x44);

  digitalWrite(strobe_pin, LOW);

  shiftOutMod(data_pin, clock_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, 0xC1 + (position << 1));

  shiftOutMod(data_pin, clock_pin, LSBFIRST, CLOCK_TYPE, CLOCK_DELAY_US, value);

  digitalWrite(strobe_pin, HIGH);

}

//=============</ functions: TM1638 >============