Porting Atmega328 code to esp32



  • I have code I am writing for the Atmega 328p using the Arduino environment.
    What do I need to know to port the code to the ESP32 used in the M5?

    This is my code:

    /*******************************************************************
    This is a direct hack of adafruits ssd1306*64 i2c file
    Todo
    Add PAS (Pedal Assist Sensor) control code
    Actual Motor output to Motpin
    *********************************************************************/

    #include <SPI.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>

    /* HAll503 Config /
    int ThrotPin = 3; // Throttle Hall Sensor
    int Th = '0'; // Variable th stores reading from HALL503
    /
    End of HAll503 Config /
    int Motpin = 9; // PWM output pin for brused motor control.
    /
    Motor Control /
    int PasPin = 2; // PAS Pin
    int Pas = '0';
    /
    PAS Sensor */

    #define OLED_RESET 4
    Adafruit_SSD1306 display(OLED_RESET);

    #define NUMFLAKES 10
    #define XPOS 0
    #define YPOS 1
    #define DELTAY 2

    #define LOGO16_GLCD_HEIGHT 16
    #define LOGO16_GLCD_WIDTH 16
    static const unsigned char PROGMEM logo16_glcd_bmp[] =
    { B00000011, B11000000,
    B00001100, B00110000,
    B00010000, B00001000,
    B00100000, B00000100,
    B01000100, B00000010,
    B01000100, B00000010,
    B10000100, B00000001,
    B10000010, B00000001,
    B10000010, B00000001,
    B10000010, B00000001,
    B01000000, B00000010,
    B01000001, B10000010,
    B00100011, B11000100,
    B00010000, B00001000,
    B00001100, B00110000,
    B00000011, B11000000, };

    #if (SSD1306_LCDHEIGHT != 64)
    #error("Height incorrect, please fix Adafruit_SSD1306.h!");
    #endif

    void setup() {
    Serial.begin(9600); // Only for debugging.

    // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
    // init done
    }

    void loop()
    {
    display.clearDisplay();
    display.drawBitmap(1, 1, logo16_glcd_bmp, 16, 16, 1);
    // text display tests
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(20,0);
    Th = analogRead(ThrotPin); // read the input pin
    Serial.println(Th); //Prints to Serial terminal
    display.println(Th); //Prints to display
    display.setCursor(0,20);
    Pas = analogRead(PasPin);
    display.println(Pas);
    display.display();
    delay(10);
    // analogWrite(Motpin, Th / 4); // Analogue read is 0-1024, Analogue write is 0-256
    }

    void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
    uint8_t icons[NUMFLAKES][3];
    }

    void testdrawchar(void) {
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);

    for (uint8_t i=0; i < 168; i++) {
    if (i == '\n') continue;
    display.write(i);
    if ((i > 0) && (i % 21 == 0))
    display.println();
    }
    display.display();
    }**



  • If you want to use the oled display, the code should work as it is.
    If you want to use the M5Stack LCD, you need to know some basic C/C++ (Arduino) programming to port the code.
    First of all, you must switch from Adafruit_SSD1306 and Adafruit_GFX libraries to M5Stack library.

    I can port this code for you to M5Stack but i think you should learn some arduino programming so you can write better code than this one...



  • @calinPorting Atmega328 code to esp32 中说:

    If you want to use the oled display, the code should work as it is.
    If you want to use the M5Stack LCD, you need to know some basic C/C++ (Arduino) programming to port the code.
    First of all, you must switch from Adafruit_SSD1306 and Adafruit_GFX libraries to M5Stack library.

    Is that where I replace the calls with the M5.Lcd. versions printed in the instruction leaflet?
    And serial.begin needs to be replaced with M5.Serial0/2.begin(9600) ?

    I can port this code for you to M5Stack but i think you should learn some arduino programming so you can write better code than this one...

    Please don't. As you can see, I need to learn how to do this myself.
    Is this any better? (well for the Arduino?)

    **#include <SPI.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>

    // Pin defines.
    // int clkPin = A5;
    // int sclPin = A4;
    int throtPin = A3;
    int pasPin = A2;
    int spdPin = A1;
    int motPin = 9;
    // End of Pin defines.
    // Variable Defines.
    int th = '0';
    int pas = '0';
    int spd = '0';
    int mot = '0';
    //End of Variable defines.

    #define OLED_RESET 4
    Adafruit_SSD1306 display(OLED_RESET);

    void setup()
    // put your setup code here, to run once:
    {
    pinMode(throtPin, INPUT);
    pinMode(pasPin, INPUT);
    pinMode(spdPin, INPUT);
    pinMode(motPin, OUTPUT);
    Serial.begin(9600); // Only for debugging.
    // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
    // init done.
    }

    void loop() {
    // put your main code here, to run repeatedly:

    th = analogRead(throtPin);
    pas = analogRead(pasPin);
    spd = analogRead(spdPin);
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(20,0);
    display.println(th);
    display.setCursor(0,20);
    display.println(pas);
    display.display();
    Serial.println(th); //Prints to Serial terminal
    Serial.println(pas); //Prints to Serial terminal
    Serial.println(spd); //Prints to Serial terminal**

    A couple of questions?

    1. How fast can the serial communicate on the M5?
    2. Will the arduinos serial terminal show the outputs?
    3. Where do I find the M5's register commands?
    4. In the leaflet that came with the M5 it shows 2 ADC but the site shows more.
      Which is correct?


  • @ajb2k3 Forget about that leaflet.
    Check the code from M5Stack library and the examples for M5Stack.
    You can learn way more from that code.

    1. The serial baudrate is 115200 by default.

    2. Yes. The Arduino terminal will show the output.

    3. Forget about the M5 register commands. This is NOT an ancient 8bit controller like the arduino AVR.
      Comparing the ATmega328p from Arduino Uno with ESP32 from M5Stack is like comparing a hammer with an fighter jet...
      Is a real hassle to deal with ESP32 registers and better just don't...
      Stick with the gpio access functions.

    4. On M5Stack any pin (except the reset pin) can be used as ADC pin



  • @calin

    Sweet, thanks for taking the time to answer my questions.