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

Arduino ESP32 Code: Ausgabe-Pulses mit veränderbarer Pulsbreite per Button und Anzeige auf LCD und Digits

25.04.2020 (👁9332)

Arduino ESP32 Code: Erstellen eines Ausgabe-Pulses mit veränderbarer Pulsbreite per Button und Anzeige auf LCD und Digits

Der folgende Arduino ESP32 nodeMCU Code macht folgendes (mit Arduino ESP32 Mikrocontroller Code).

Über zwei Buttons wird an einem digitalen Ausgabe-Pin ein Puls mit einer einstellbaren Pulsbreite erzeugt

Bedienung

Ausgabe der Pulsbreite in Nano-Sekunden auf einem LCD2x16

Eingabe:

Button S1:  reduziert die Pulsbreite (ca. 160ns)

Button S8: erweitert die Pulsbreite um je 1 Minimale Puls breite. (ca. 160 ns)

Ausgabe:

Auf LCD Pulsbreite in Nanosekunden und die umgerechnete Frequenz

Die Pulsbreite liegt beim ESP32 nodeMCU bei minimal 160ns.

ESP32 Code

Bausteile:

ESP32 nodeMCU

TM1638 Button, LED und Digit-Board

LCD von LiquidCrystal_I2C

 

 

Entwickelt auf Arduino IDE

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

#include "wiring_shift_mod.h"

#include <TM1638lite.h>

/*Variable Puls Output with LCD */

#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 >

// I/O pins on the Arduino connected to strobe, clock, data

// (power should go to 3.3v and GND)

//TM1638lite tm(4, 7, 8);

TM1638lite tm(41617);

//< 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

  reset();

  //</ TM1638 Buttons >

  //< TM1638 DIGITS >

  tm.reset();

  //</ TM1638 DIGITS >



  //< LCD >

  // initialize LCD

  //lcd.init();

  lcd.begin();

  // turn on LCD backlight                      

  lcd.backlight();

  //</ LCD >

    

  

  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 >--  

  //< 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="";

  int fExponent=0;

  if (nsPulswidth<freq_MHz_Nanoseconds)

  {

    freqRangeText="MHz";

    fExponent=6;

    frequency=float( freq_MHz_Nanoseconds / nsPulswidth );

  }

  else if (nsPulswidth<freq_kHz_Nanoseconds)

  {

    freqRangeText="kHz";

    fExponent=3;

    frequency=float( freq_kHz_Nanoseconds / nsPulswidth );

  }

  else 

  {

    freqRangeText="Hz";

    fExponent=0;

    frequency=float( freq_Hz_Nanoseconds / nsPulswidth );

  }

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

  //</ calculate >

  

  //----< LCD+Digits Output >----

  if (buttons_changed==true)

  {

    long lngPulswidth=nsPulswidth;

    long lngFrequency=frequency;

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

    String lcd_Text1= String("Freq.: ")  + String(frequency,0) + " " + freqRangeText ;

    

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

  

    //--< LCD >--

    //< display info on LCD >

    Wire.setClock(10000);   //solves LCD Error

    

    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 >--

    //----< TM1638-Digits Output >----

    tm.displayText(String(frequency,0)  + " X" + fExponent);

    //----</ TM1638-Digits Output >----

  }

  //----</ LCD+Digits Output >----

  

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

}

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












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

void sendCommand(uint8_t value)

{

  //*sendCommand TM1638 Buttons and LED Board

  digitalWrite(strobe_pin, LOW);

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

  digitalWrite(strobe_pin, HIGH);

}

void reset()

{

  //*reset TM1638 Buttons and LED Board

  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)

{

  //*readButtons TM1638 Buttons and LED Board

  

  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 >============