<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Porting Atmega328 code to esp32]]></title><description><![CDATA[<p dir="auto">I have code I am writing for the Atmega 328p using the Arduino environment.<br />
What do I need to know to port the code to the ESP32 used in the M5?</p>
<p dir="auto">This is my code:</p>
<p dir="auto"><strong>/</strong>*******************************************************************<br />
This is a direct hack of adafruits ssd1306*64 i2c file<br />
Todo<br />
Add PAS (Pedal Assist Sensor) control code<br />
Actual Motor output to Motpin<br />
*********************************************************************/</p>
<p dir="auto">#include &lt;SPI.h&gt;<br />
#include &lt;Wire.h&gt;<br />
#include &lt;Adafruit_GFX.h&gt;<br />
#include &lt;Adafruit_SSD1306.h&gt;</p>
<p dir="auto">/* HAll503 Config <em>/<br />
int ThrotPin = 3; // Throttle Hall Sensor<br />
int Th = '0';       // Variable th stores reading from HALL503<br />
/</em> End of HAll503 Config <em>/<br />
int Motpin = 9; // PWM output pin for brused motor control.<br />
/</em> Motor Control <em>/<br />
int PasPin = 2; // PAS Pin<br />
int Pas = '0';<br />
/</em> PAS Sensor */</p>
<p dir="auto">#define OLED_RESET 4<br />
Adafruit_SSD1306 display(OLED_RESET);</p>
<p dir="auto">#define NUMFLAKES 10<br />
#define XPOS 0<br />
#define YPOS 1<br />
#define DELTAY 2</p>
<p dir="auto">#define LOGO16_GLCD_HEIGHT 16<br />
#define LOGO16_GLCD_WIDTH  16<br />
static const unsigned char PROGMEM logo16_glcd_bmp[] =<br />
{ B00000011, B11000000,<br />
B00001100, B00110000,<br />
B00010000, B00001000,<br />
B00100000, B00000100,<br />
B01000100, B00000010,<br />
B01000100, B00000010,<br />
B10000100, B00000001,<br />
B10000010, B00000001,<br />
B10000010, B00000001,<br />
B10000010, B00000001,<br />
B01000000, B00000010,<br />
B01000001, B10000010,<br />
B00100011, B11000100,<br />
B00010000, B00001000,<br />
B00001100, B00110000,<br />
B00000011, B11000000, };</p>
<p dir="auto">#if (SSD1306_LCDHEIGHT != 64)<br />
#error("Height incorrect, please fix Adafruit_SSD1306.h!");<br />
#endif</p>
<p dir="auto">void setup()   {<br />
Serial.begin(9600); // Only for debugging.</p>
<p dir="auto">// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)<br />
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)<br />
// init done<br />
}</p>
<p dir="auto">void loop()<br />
{<br />
display.clearDisplay();<br />
display.drawBitmap(1, 1,  logo16_glcd_bmp, 16, 16, 1);<br />
// text display tests<br />
display.setTextSize(2);<br />
display.setTextColor(WHITE);<br />
display.setCursor(20,0);<br />
Th = analogRead(ThrotPin);     // read the input pin<br />
Serial.println(Th);             //Prints to Serial terminal<br />
display.println(Th);            //Prints to display<br />
display.setCursor(0,20);<br />
Pas = analogRead(PasPin);<br />
display.println(Pas);<br />
display.display();<br />
delay(10);<br />
// analogWrite(Motpin, Th / 4); // Analogue read is 0-1024, Analogue write is 0-256<br />
}</p>
<p dir="auto">void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {<br />
uint8_t icons[NUMFLAKES][3];<br />
}</p>
<p dir="auto">void testdrawchar(void) {<br />
display.setTextSize(1);<br />
display.setTextColor(WHITE);<br />
display.setCursor(0,0);</p>
<p dir="auto">for (uint8_t i=0; i &lt; 168; i++) {<br />
if (i == '\n') continue;<br />
display.write(i);<br />
if ((i &gt; 0) &amp;&amp; (i % 21 == 0))<br />
display.println();<br />
}<br />
display.display();<br />
}**</p>
]]></description><link>https://community.m5stack.com/topic/228/porting-atmega328-code-to-esp32</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 04:56:33 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/228.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 14 Jun 2018 20:42:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Porting Atmega328 code to esp32 on Sun, 17 Jun 2018 15:36:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/calin" aria-label="Profile: calin">@<bdi>calin</bdi></a></p>
<p dir="auto">Sweet, thanks for taking the time to answer my questions.</p>
]]></description><link>https://community.m5stack.com/post/1090</link><guid isPermaLink="true">https://community.m5stack.com/post/1090</guid><dc:creator><![CDATA[ajb2k3]]></dc:creator><pubDate>Sun, 17 Jun 2018 15:36:32 GMT</pubDate></item><item><title><![CDATA[Reply to Porting Atmega328 code to esp32 on Sun, 17 Jun 2018 15:08:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ajb2k3" aria-label="Profile: ajb2k3">@<bdi>ajb2k3</bdi></a> Forget about that leaflet.<br />
Check the code from M5Stack library and the examples for M5Stack.<br />
You can learn way more from that code.</p>
<ol>
<li>
<p dir="auto">The serial baudrate is 115200 by default.</p>
</li>
<li>
<p dir="auto">Yes. The Arduino terminal will show the output.</p>
</li>
<li>
<p dir="auto">Forget about the M5 register commands. This is NOT an ancient 8bit controller like the arduino AVR.<br />
Comparing the ATmega328p from Arduino Uno with ESP32 from M5Stack is like comparing a hammer with an fighter jet...<br />
Is a real hassle to deal with ESP32 registers and better just don't...<br />
Stick with the gpio access functions.</p>
</li>
<li>
<p dir="auto">On M5Stack any pin (except the reset pin) can be used as ADC pin</p>
</li>
</ol>
]]></description><link>https://community.m5stack.com/post/1089</link><guid isPermaLink="true">https://community.m5stack.com/post/1089</guid><dc:creator><![CDATA[Calin]]></dc:creator><pubDate>Sun, 17 Jun 2018 15:08:42 GMT</pubDate></item><item><title><![CDATA[Reply to Porting Atmega328 code to esp32 on Sat, 16 Jun 2018 15:23:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/calin" aria-label="Profile: calin">@<bdi>calin</bdi></a> 在 <a href="/post/1071">Porting Atmega328 code to esp32</a> 中说：</p>
<blockquote>
<p dir="auto">If you want to use the oled display, the code should work as it is.<br />
If you want to use the M5Stack LCD, you need to know some basic C/C++ (Arduino) programming to port the code.<br />
First of all, you must switch from Adafruit_SSD1306 and Adafruit_GFX libraries to M5Stack library.</p>
</blockquote>
<p dir="auto">Is that where I replace the calls with the M5.Lcd. versions printed in the instruction leaflet?<br />
And serial.begin needs to be replaced with M5.Serial0/2.begin(9600) ?</p>
<blockquote>
<p dir="auto">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...</p>
</blockquote>
<p dir="auto">Please don't. As you can see, I need to learn how to do this myself.<br />
Is this any better? (well for the Arduino?)</p>
<p dir="auto">**#include &lt;SPI.h&gt;<br />
#include &lt;Wire.h&gt;<br />
#include &lt;Adafruit_GFX.h&gt;<br />
#include &lt;Adafruit_SSD1306.h&gt;</p>
<p dir="auto">// Pin defines.<br />
// int clkPin = A5;<br />
// int sclPin = A4;<br />
int throtPin = A3;<br />
int pasPin = A2;<br />
int spdPin = A1;<br />
int motPin = 9;<br />
// End of Pin defines.<br />
// Variable Defines.<br />
int th = '0';<br />
int pas = '0';<br />
int spd = '0';<br />
int mot = '0';<br />
//End of Variable defines.</p>
<p dir="auto">#define OLED_RESET 4<br />
Adafruit_SSD1306 display(OLED_RESET);</p>
<p dir="auto">void setup()<br />
// put your setup code here, to run once:<br />
{<br />
pinMode(throtPin, INPUT);<br />
pinMode(pasPin, INPUT);<br />
pinMode(spdPin, INPUT);<br />
pinMode(motPin, OUTPUT);<br />
Serial.begin(9600); // Only for debugging.<br />
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)<br />
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)<br />
// init done.<br />
}</p>
<p dir="auto">void loop() {<br />
// put your main code here, to run repeatedly:</p>
<p dir="auto">th = analogRead(throtPin);<br />
pas = analogRead(pasPin);<br />
spd = analogRead(spdPin);<br />
display.clearDisplay();<br />
display.setTextSize(2);<br />
display.setTextColor(WHITE);<br />
display.setCursor(20,0);<br />
display.println(th);<br />
display.setCursor(0,20);<br />
display.println(pas);<br />
display.display();<br />
Serial.println(th);             //Prints to Serial terminal<br />
Serial.println(pas);             //Prints to Serial terminal<br />
Serial.println(spd);             //Prints to Serial terminal**</p>
<p dir="auto">A couple of questions?</p>
<ol>
<li>How fast can the serial communicate on the M5?</li>
<li>Will the arduinos serial terminal show the outputs?</li>
<li>Where do I find the M5's register commands?</li>
<li>In the leaflet that came with the M5 it shows 2 ADC but the site shows more.<br />
Which is correct?</li>
</ol>
]]></description><link>https://community.m5stack.com/post/1079</link><guid isPermaLink="true">https://community.m5stack.com/post/1079</guid><dc:creator><![CDATA[ajb2k3]]></dc:creator><pubDate>Sat, 16 Jun 2018 15:23:18 GMT</pubDate></item><item><title><![CDATA[Reply to Porting Atmega328 code to esp32 on Fri, 15 Jun 2018 05:56:55 GMT]]></title><description><![CDATA[<p dir="auto">If you want to use the oled display, the code should work as it is.<br />
If you want to use the M5Stack LCD, you need to know some basic C/C++ (Arduino) programming to port the code.<br />
First of all, you must switch from Adafruit_SSD1306 and Adafruit_GFX libraries to M5Stack library.</p>
<p dir="auto">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...</p>
]]></description><link>https://community.m5stack.com/post/1071</link><guid isPermaLink="true">https://community.m5stack.com/post/1071</guid><dc:creator><![CDATA[Calin]]></dc:creator><pubDate>Fri, 15 Jun 2018 05:56:55 GMT</pubDate></item></channel></rss>