tag:blogger.com,1999:blog-38511448773222234962024-03-16T03:12:19.350+02:00Liviu IstrateA blog where you can find libraries for microcontrollers, mainly for AVR devices, tutorials about communication protocols on embedded devices, and DIY electronic projects.Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.comBlogger72125tag:blogger.com,1999:blog-3851144877322223496.post-65798708297056505942024-03-15T06:39:00.005+02:002024-03-15T06:47:05.593+02:00Delay functions for Silicon Labs microcontrollers<p>
Sometimes you need to delay the program execution, for example when waiting
for another device to startup or respond and the software cannot continue. An
interrupt driven delay is better in most cases but not in all cases. For this
purpose I made two functions one for milliseconds and one for microseconds,
that uses NOPs to delay the program execution for a certain amount of time.
The milliseconds delay is quite accurate, the microseconds one, not so much.
</p>
<p>
For this to work, the SYSCLK must be defined first. It is recommended to be
defined in the project settings. You can find a short tutorial here on how to
add a global define in Simplicity Studio:
<a href="https://www.programming-electronics-diy.xyz/2024/03/defining-sysclk-or-fcpu-in-simplicity.html" target="_blank">https://www.programming-electronics-diy.xyz/2024/03/defining-sysclk-or-fcpu-in-simplicity.html</a>. </p><h3 style="text-align: left;">Tested on:</h3><p>- C8051F330</p><p>For other microcontrollers the following SFR declarations include file should be replaced accordingly:</p><pre style="line-height: 125%; margin: 0;"><span style="color: #bc7a00;">#include <SI_C8051F330_Register_Enums.h></span></pre><p></p><div style="text-align: left;"><br /></div>
<h2 style="text-align: left;">Library usage</h2>
<div style="text-align: left;"> </div>
<h3 style="text-align: left;">Milliseconds delay</h3>
<div style="text-align: left;"> <br /></div>
<!--HTML generated using hilite.me-->
<div style="background: rgb(248, 248, 248); border-color: gray; border-image: none; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">_delay_ms(<span style="color: #b00040;">uint32_t</span> ms)
</pre>
</div>
<div style="text-align: left;"> <br /></div>
<h3 style="text-align: left;">Microseconds delay</h3>
<div style="text-align: left;"> <br /></div>
<!--HTML generated using hilite.me-->
<div style="background: rgb(248, 248, 248); border-color: gray; border-image: none; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0px;">_delay_us(<span style="color: #b00040;">uint32_t</span> us)
</pre>
</div>
<div style="text-align: left;"> <br /></div>
<h2 id="Download">Download</h2>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
overflow:hidden;padding:5px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
font-weight:normal;overflow:hidden;padding:5px 5px;word-break:normal;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:middle}
.tg .tg-fymr{border-color:inherit;font-weight:bold;text-align:left;vertical-align:middle}
.tg .tg-0lax{text-align:left;vertical-align:middle}
@media screen and (max-width: 767px) {.tg {width: auto !important;}.tg col {width: auto !important;}.tg-wrap {overflow-x: auto;-webkit-overflow-scrolling: touch;}}
</style>
<div class="tg-wrap">
<table class="tg" style="table-layout: auto; width: auto;">
<tbody>
<tr>
<td class="tg-0pky" colspan="3"><b>delay</b></td>
</tr>
<tr>
<td class="tg-0pky">v1.0</td>
<td class="tg-0pky">
<a href="https://drive.google.com/drive/folders/1FmXvvzXxcTsdNAbNhXeyX2hDXPK95P32?usp=sharing" rel="nofollow" target="_blank">delay</a>
</td>
<td class="tg-0pky">Contains delay.h and delay.c</td>
</tr>
</tbody>
</table>
</div>
Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-23092780927422120672024-03-15T03:41:00.012+02:002024-03-15T04:11:51.196+02:00UART library for Silicon Labs microcontrollers using interruptsThis UART library is made for Silicon Labs microcontrollers that can be used for
serial communications.<br>
<p>
UART is a type of serial interface, as opposed to a parallel interface. A
parallel interface can work at higher speeds but the disadvantage is that it
needs multiple input/output lines. Other examples of serial interfaces are SPI
and I2C. </p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpuvnVSaCxbUzcSmMsW6jJ2rg6IMjuOO1wMqQcvBxKXL4LcYnq5clLsVQLQDMpmZpLY3RZzj25Can63cNlkSKgcHff0l3MCdj7W6W2bFeQMs-7DKmNVmq_pC2a_flHoeCLOxRIJC3XUC9QkwquXFli9ZXfPWsDhh_8OYpFb7_oMOpaWmQz4SusyQiHMus/s1280/UART%20for%20Silabs%20thumbnail.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="UART library for Silicon Labs microcontrollers using interrupts" border="0" data-original-height="720" data-original-width="1280" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpuvnVSaCxbUzcSmMsW6jJ2rg6IMjuOO1wMqQcvBxKXL4LcYnq5clLsVQLQDMpmZpLY3RZzj25Can63cNlkSKgcHff0l3MCdj7W6W2bFeQMs-7DKmNVmq_pC2a_flHoeCLOxRIJC3XUC9QkwquXFli9ZXfPWsDhh_8OYpFb7_oMOpaWmQz4SusyQiHMus/w640-h360/UART%20for%20Silabs%20thumbnail.png" title="UART library for Silicon Labs microcontrollers using interrupts" width="640"></a></div><br><p></p>
<h2 style="text-align: left;">Supported Devices</h2>
<p style="text-align: left;">
At the moment only following devices are supported. I might add more in the
feature. It might work for other similar devices not listed below.<br>
</p>
<ul style="text-align: left;">
<li>C8051F330</li>
</ul>
<p><span></span></p>
<a href="https://www.programming-electronics-diy.xyz/2024/03/uart-library-for-silicon-labs.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-79858448070305714382024-03-15T02:09:00.002+02:002024-03-15T02:10:36.072+02:00Defining SYSCLK or F_CPU in Simplicity Studio<p>
The macro SYSCLK or F_CPU specifies the CPU frequency and is used by libraries
such as <delay.h> to calculate the delay based on the frequency of the
processor or by UART libraries to calculate baud rate.
</p>
<p>Up until now I used to define F_CPU like this:</p>
<pre style="line-height: 125%; margin: 0px;"><span style="color: #bc7a00;">#define F_CPU 24500000UL</span></pre>
<p></p>
<p>
Defining this way will work but it could lead to issues and confusion when you
have multiple files that define this macro. The ideal way is to define it in a
single place. This could be a Makefile if you are using custom Makefiles or in
the IDE project configuration.
</p>
<h2 style="text-align: left;">
Defining SYSCLK / F_CPU in Simplicity Studio<br />
</h2>
<p>
Open the project properties window in
<b>Project -> Properties (Alt+Enter)</b> then in the left panel select the
<b>C/C++ Build -> Settings </b>menu. Then in <b>Tool Settings</b> - >
<b>Keil 8051 Compiler</b> -> <b>Symbols </b>use the green
<b>+ button</b> to define a new symbol <b>SYSCLK=2450000</b>UL. Of course the
SYSCLK value depends on your CPU frequency that in this example is 24500000
Hertz or 24.5MHz. The UL at the end stands for unsigned integer.
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXhqMFlP_yJBDcLwWM4DEtRfIDf01Ee7XVfsB_3Ro-_ylzGaXwfz-URTC7h52jqYz0Sc7wNc2QSCT37LCgR8i2q-3jB8P-_t5ZJsW_e9iZc_a24tRI1CXdXnMRGlMnGSpo3jLgUF1Zf8OPVLhyphenhyphenGEiTGZtPi-9GMcV6CrRGYT-7qqx5AW9xmkyTH2roXPQ/s659/Simplicity%20Studio%20v4%20-%20SYSCLK%20-%2001.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="Defining SYSCLK symbol in Simplicity Studio 01" border="0" data-original-height="659" data-original-width="448" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXhqMFlP_yJBDcLwWM4DEtRfIDf01Ee7XVfsB_3Ro-_ylzGaXwfz-URTC7h52jqYz0Sc7wNc2QSCT37LCgR8i2q-3jB8P-_t5ZJsW_e9iZc_a24tRI1CXdXnMRGlMnGSpo3jLgUF1Zf8OPVLhyphenhyphenGEiTGZtPi-9GMcV6CrRGYT-7qqx5AW9xmkyTH2roXPQ/w273-h400/Simplicity%20Studio%20v4%20-%20SYSCLK%20-%2001.jpg" title="Defining SYSCLK symbol in Simplicity Studio 01" width="273" /></a>
</div>
<br />
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiShb5jTptYJhYL_lQEa46PlUOfkTakqrT2ma9bd7xoTaA6hyphenhyphenuqZJSR5xcZhq5aBFzGInXA3lQhAvIL3K-2jAQ-vQ1p7MHJ9yZKbBOVOqwJsYcpWUqdnDin1K9Ht4M3TJb171R21by4tpKYksy6U__Xik7dNK6IjZ67eS5HiEGDTeJqsYAP9_1ehMokGSs/s834/Simplicity%20Studio%20v4%20-%20SYSCLK%20-%2002.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="Defining SYSCLK symbol in Simplicity Studio 02" border="0" data-original-height="834" data-original-width="800" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiShb5jTptYJhYL_lQEa46PlUOfkTakqrT2ma9bd7xoTaA6hyphenhyphenuqZJSR5xcZhq5aBFzGInXA3lQhAvIL3K-2jAQ-vQ1p7MHJ9yZKbBOVOqwJsYcpWUqdnDin1K9Ht4M3TJb171R21by4tpKYksy6U__Xik7dNK6IjZ67eS5HiEGDTeJqsYAP9_1ehMokGSs/w614-h640/Simplicity%20Studio%20v4%20-%20SYSCLK%20-%2002.jpg" title="Defining SYSCLK symbol in Simplicity Studio 02" width="614" /></a>
</div>
<p>
That's it. Now all included files in your project will use a single SYSCLK
value without giving errors such as "<i>SYSCLK is not defined</i>", "<i>SYSCLK
redefined</i>", etc.
</p>
<p>
Since this method of defining is not as obvious as the first one, it is easy
to forged to do it when starting a new project but there is a simple solution
for that - using preprocessor conditionals.
</p>
<!--HTML generated using hilite.me--><div style="background: rgb(248, 248, 248); border-color: gray; border-image: none; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #bc7a00;">#ifndef SYSCLK</span>
#warning <span style="color: #ba2121;">"SYSCLK not defined. Define it in project properties."</span>
<span style="color: #bc7a00;">#elif SYSCLK != 24500000</span>
#warning <span style="color: #ba2121;">"Wrong SYSCLK frequency!"</span>
<span style="color: #bc7a00;">#endif</span>
</pre></div>
<p>
This macro can be placed at the beginning of the <i>main.c</i> file before any type
of code. If the SYSCLK is not defined it will output a warning. If SYSCLK is
defined but is not the value specified in the macro, it will also output a
warning. This is also helpful to see what the CPU frequency is and to ensure
that the defined value in project properties is correct.
</p>
Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-48131552249275209602024-03-05T01:32:00.019+02:002024-03-14T02:43:36.528+02:00Open source software for programming Silicon Labs microcontrollers using C2 interface<p>
The C2 Programmer is designed for programming Silicon Labs microcontrollers
over the C2 interface by the use of an AVR microcontroller with the necessary
firmware and an USB to serial adapter. The software is written in JavaScript
and is based on
<a href="https://sourceforge.net/projects/scriptcommunicator/" target="_blank">ScriptCommunicator</a>
by Stefan Zieker.
</p>
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<br>
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiigur3xQchTm56qgpoLC1eS_gfOk3E9y8Of3Ly-dPC4-Dp_FgsdBpxUwVMeKsKEszlMC3tiolT4PaEWZGCMggWkx69Ztuz9AweoeKpRt-8TNIEYvaMwg1Fo_CuQs9Jontq0SrpjGJuUDBwOeVWseOoPyd-Gu4LKJQCakuy9V0IaqYqEo8OKiN86I_AQTk/s730/C2%20Programmer.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="Custom open source C2 programmer" border="0" data-original-height="694" data-original-width="730" height="380" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiigur3xQchTm56qgpoLC1eS_gfOk3E9y8Of3Ly-dPC4-Dp_FgsdBpxUwVMeKsKEszlMC3tiolT4PaEWZGCMggWkx69Ztuz9AweoeKpRt-8TNIEYvaMwg1Fo_CuQs9Jontq0SrpjGJuUDBwOeVWseOoPyd-Gu4LKJQCakuy9V0IaqYqEo8OKiN86I_AQTk/w400-h380/C2%20Programmer.jpg" title="Custom open source C2 programmer" width="400"></a></div><br>
<br>
<p></p>
<h2 style="text-align: left;">What does it do</h2>
<ul style="text-align: left;">
<li>
Save flash memory to .bin (binary), .txt (hex format) or .hex (Intel hex)
file.
</li>
<li>Write an Intel hex file to flash memory.</li>
<li>Erase device option.</li>
</ul>
<h2 style="text-align: left;">What does not do</h2>
<ul style="text-align: left;">
<li>Everything else.<br></li>
</ul>
<br>
<h2 style="text-align: left;">Contents</h2>
<ul>
<li><a href="#Motivation">Motivation</a></li>
<li><a href="#Theory_of_operation">Theory of operation</a></li>
<li><a href="#Program_over_C2_interface">How to program a microcontroller over C2 interface</a></li>
<ul>
<li><a href="#Hardware_and_pins">Hardware and C2 interface pins</a></li>
<li><a href="#Script_Communicator_app">Script Communicator app</a></li>
<li><a href="#C2_Programmer_GUI">C2 Programmer GUI</a></li>
<ul>
<li><a href="#Saving_flash_to_file">Saving flash memory to file</a></li>
<li><a href="#Writing_flash_from_file">Writing flash memory from file</a></li>
<li><a href="#C2_Programmer_as_USB_to_Serial">C2 Programmer as an USB to Serial</a></li>
</ul>
<li><a href="#workflow">The workflow</a></li>
</ul>
<li><a href="#The_C2_Interface">The C2 Interface</a></li>
<li><a href="#Increasing_serial_transfer_rate">Increasing serial transfer rate</a></li>
<li><a href="#Download">Download</a></li>
</ul>
<br>
<h2 id="Motivation" style="text-align: left;">Motivation</h2>
<p>
Why use a custom programmer when you can buy one? The reason I have started
developing this software is not necessarily cost related. I could have just
bought a programmer and be done with it instead of coding hours and hours, but
I thought it will be a good addition for the community since being custom made
it can be... customized. There are other reasons such as, maybe the commercial
one is not available in your area or not in stock, or you might want to
program only one or two microcontrollers and consider is not worth investing
in a programmer.
</p>
<p>
Actually this is how the project started. I have an RDM-6300 RFID card reader
that I thought is a writer too. Then I saw it's using the C8051F330
microcontroller from Silabs and decided to give it a try and make it also
write RFID cards or at least add support for other chips. By not buying a
programmer I made one that I could use in my project and also make it
available for others.</p><p>Note that debugging is not supported so if you need this feature, consider
buying a commercial programmer.
</p>
<h2 id="Theory_of_operation" style="text-align: left;">Theory of operation</h2>
<p>
Application on the computer instructs the firmware on an AVR microcontroller
on what commands to send to the C2 interface of the programmed device. The
communication between computer and programming device (the AVR) is done using
an USB to serial converter.
</p>
<p>
The application is build on top of ScriptCommunicator which is a scriptable
cross-platform data terminal that supports serial port (RS232, USB to serial),
UDP, TCP client/server, SPI, I2C, and CAN. Script Communicator it's an awesome
application that unfortunately is not that well known. The best part is that
the interface can be made to look how you want by using the Qt Designer
included with the app.
</p>
<p><span></span></p>
<a href="https://www.programming-electronics-diy.xyz/2024/03/c2-programmer.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-6486831617479892192024-01-24T02:39:00.001+02:002024-01-24T02:40:09.066+02:00Defining F_CPU in Microchip Studio<p>
The macro F_CPU specifies the CPU frequency and is used by libraries such as
<delay.h> to calculate the delay based on the frequency of the processor
or by UART libraries to calculate baud rate.
</p>
<p>Up until now I used to define F_CPU like this:</p>
<pre style="line-height: 125%; margin: 0px;"><span style="color: #bc7a00;">#define F_CPU 16000000UL</span></pre>
<p></p>
<p>
Defining this way will work but it could lead to issues and confusion when you
have multiple files that define this macro. The ideal way is to define it in a
single place. This could be a Makefile if you are using custom Makefiles or in
the IDE project configuration.
</p>
<h2 style="text-align: left;">Defining F_CPU in Microchip Studio</h2>
<p>
Open the project properties window in <b>Project -> Properties (Alt+F7)</b> then
in the left panel select the <b>Toolchain</b> menu. Next in <b>AVR/GNU C Compiler ->
Symbols</b> use the green <b>+ button</b> to define a new symbol <b>F_CPU=16000000UL</b>. Of
course the F_CPU value depends on your CPU frequency that in this example is
16000000 Hertz or 16MHz. The UL at the end stands for unsigned integer.
</p>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;">
<tbody>
<tr>
<td style="text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSfxgS2Id4YUlXPAFYeAg1j-Z3yvNDgVFraZxWjf0YU-p71nOHI2dLgd7rkhswBCDjSsqRVyjEwcDLLfNRABV7nlulps970VqkKeGLNzIHR16V0KFS2vWLByPDoGAkTk1sLuXkJJwngowXHPfsxEj03978IFLPBLyqyrB2Y41iL6_EmtG1fbT3opAxFx8/s1464/Defining%20symbols%20in%20Microchip%20Studio.jpg" style="margin-left: auto; margin-right: auto;"><img alt="Defining F_CPU symbol in Microchip Studio" border="0" data-original-height="486" data-original-width="1464" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSfxgS2Id4YUlXPAFYeAg1j-Z3yvNDgVFraZxWjf0YU-p71nOHI2dLgd7rkhswBCDjSsqRVyjEwcDLLfNRABV7nlulps970VqkKeGLNzIHR16V0KFS2vWLByPDoGAkTk1sLuXkJJwngowXHPfsxEj03978IFLPBLyqyrB2Y41iL6_EmtG1fbT3opAxFx8/w640-h212/Defining%20symbols%20in%20Microchip%20Studio.jpg" title="Defining F_CPU symbol in Microchip Studio" width="640" /></a>
</td>
</tr>
<tr>
<td class="tr-caption" style="text-align: center;">
<i>click to enlarge</i><br />
</td>
</tr>
</tbody>
</table>
<p>
That's it. Now all included files in your project will use a single F_CPU
value without giving errors such as "F_CPU is not defined", "F_CPU redefined",
etc.
</p>
<p>
Since this method of defining is not as obvious as the first one, it is easy
to forged to do it when starting a new project but there is a simple solution
for that - using preprocessor conditionals.
</p>
<!--HTML generated using hilite.me--><div style="background: rgb(248, 248, 248); border-color: gray; border-image: none; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #bc7a00;">#ifndef F_CPU</span>
#warning <span style="color: #ba2121;">"F_CPU not defined. Define it in project properties."</span>
<span style="color: #bc7a00;">#elif F_CPU != 16000000</span>
#warning <span style="color: #ba2121;">"Wrong F_CPU frequency!"</span>
<span style="color: #bc7a00;">#endif</span>
</pre></div>
<p>This macro can be placed at the beginning of the main.c file before any type of code. If the F_CPU is not defined it will output a warning. If the F_CPU is defined but is not the value specified in the macro, it will also output a warning. This is also helpful to see what the CPU frequency is and to ensure that the defined value in project properties is correct.</p>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-62956081654069988052024-01-04T05:35:00.012+02:002024-03-10T01:23:40.669+02:00How to program an UPDI AVR microcontroller using avrdude and USB to serial programmer <p>
After buying some ATtiny402 microcontrollers I've noticed that newer AVR
models from Microchip are now using the UPDI interface for programming.
Previously the programming was done using SPI or UART if a bootloader was
present.
</p>
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgou6F1juvShEeZKWR340Sonj39IK13AKOmsfEhMlMvfYRFhk3tqflvPNNaFk8IbBg026USP_mZsJifUvEigJSbl_6PocmIU8KslU_Ekfhof1Eb1_z9DDXKX9Y7qTmfnJ2Dbr90rRTa4NSlshIa_uW9aeHn8rV62MIBXKNe1prd4nTWAC29HN3L-Wx0ihs/s3375/Programming%20UPDI%20using%20avrdude%20cover.png" style="margin-left: 1em; margin-right: 1em;"><img alt="How to program an UPDI AVR microcontroller using avrdude and USB to serial programmer" border="0" data-original-height="1637" data-original-width="3375" height="194" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgou6F1juvShEeZKWR340Sonj39IK13AKOmsfEhMlMvfYRFhk3tqflvPNNaFk8IbBg026USP_mZsJifUvEigJSbl_6PocmIU8KslU_Ekfhof1Eb1_z9DDXKX9Y7qTmfnJ2Dbr90rRTa4NSlshIa_uW9aeHn8rV62MIBXKNe1prd4nTWAC29HN3L-Wx0ihs/w400-h194/Programming%20UPDI%20using%20avrdude%20cover.png" title="How to program an UPDI AVR microcontroller using avrdude and USB to serial programmer" width="400"></a>
</div>
<div style="text-align: left;"> </div>
<h2 style="text-align: left;">Contents</h2>
<ul>
<li><a href="#What_is_UPDI" rel="tag">What is UPDI and how do I use it?</a></li>
<li><a href="#reset_mcu_when_UPDI_is_shared_with_RESET" rel="tag">Can i manually reset the microcontroller when UPDI is shared with RESET?</a></li>
<li><a href="#hardware_for_programming_using_UPDI_interface" rel="tag">What hardware do i need for programming using UPDI interface?</a></li>
<li><a href="#regular_serial_adapter_with_UPDI" rel="tag">How to use a regular serial adapter module with UPDI</a></li>
<ul>
<li><a href="#What_software_do_I_need" rel="tag">What software do I need?</a></li>
<li><a href="#Programming_UPDI_device_using_avrdude" rel="tag">Programming an UPDI device using avrdude, Microchip Studio and a USB to serial module</a></li>
</ul>
<li><a href="#Bonus_tips" rel="tag">Bonus tips</a></li>
<li><a href="#Configuring_pins_on_UPDI_microcontrollers" rel="tag">Configuring pins on UPDI microcontrollers</a></li>
</ul>
<p></p>
<div id="What_is_UPDI" style="text-align: left;"> </div><h2 id="What_is_UPDI" style="text-align: left;">What is UPDI and how do I use it?</h2>
<p>
<b>UPDI</b> stands for <b>Unified Program and Debug Interface</b> and is
proprietary to <b>Microchip</b>. In many ways is similar to 1 wire UART. The
main advantage is that it can be used for programming and also for debugging.
Now there is no need for SPI, bootloader, debugWire... It's all Unified in one
pin and one interface.<br>
</p>
<p>
On certain devices such as ATtiny, the UPDI and Reset are on the same pin. In
each case, the UPDI pin can also be used as a GPIO pin. When UPDI and Reset
share the same pin, the functionality can be selected using the specific fuse.
By default the fuse is set to select UPDI as a pin function. If you change the
fuse and enable the Reset then you will need a 12V programmer to be able to
program the microcontroller.
</p>
<p>
To prevent false triggering when the line is idle, it is recommended to have a
pull-up resistor of at least 10k on the UPDI pin. Although some say that it
works without problems even without a pull-up resistor so if you are using the
UPDI pin also as a GPIO pin, you might consider not placing the resistor.
</p>
<p>
Source:
<a href="https://microchip.my.site.com/s/article/AVR---Hardware-Design-Considerations-for-UPDI-pin" target="_blank">https://microchip.my.site.com/s/article/AVR---Hardware-Design-Considerations-for-UPDI-pin</a>.
</p>
<h3 id="reset_mcu_when_UPDI_is_shared_with_RESET" style="text-align: left;">
Can i manually reset the microcontroller when UPDI is shared with RESET?
</h3>
<div style="text-align: left;">
<span></span></div><a href="https://www.programming-electronics-diy.xyz/2024/01/how-to-program-updi-avr-microcontroller.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-58474052321027342452023-12-20T03:12:00.038+02:002024-02-03T06:30:36.116+02:00Library for TMC2209 driver - AVR<p>
This library is used to control one or more TMC2209 modules via UART using an
AVR microcontroller. Apart from reading and writing the TMC2209 registers,
this library can also be used to drive a stepper motor by using the
<a href="https://www.programming-electronics-diy.xyz/2023/10/steppercon-library-for-controlling.html" target="_blank">stepperCon</a>
library in the background. The stepperCon library provides non blocking
functions by using an interrupt, to drive multiple stepper motors with custom
acceleration and speed, to keep track of motor positions, 3-axis motor
coordination, as well as other useful functions. UART library is also
provided that can use up to 2 USART peripherals at the same time: one for interfacing with the driver IC and one for debugging
(assuming the microcontroller has two USART peripherals).
</p>
<p>The serial communication has some extra features for checkup such as:</p>
<ul style="text-align: left;">
<li>
reads the interface transmission counter (IFCNT) register after every write
to ensure the IC received the correct data.
</li>
<li>compares CRCs.</li>
<li>checks if correct number of bytes has been received.</li>
<li>
if an error occurs it retries 2 times then sets a communication error flag
that the user can check after each function is executed and take the
appropriate action if an error occurs. </li></ul><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQsDZ0fKhjzNAvn-lzcu4bZV4Yyfwxh9OVq8tvFdtSOqZQz92sjG0-8h19pHp3aYFnlTozjOIhrUephkeCMoizxLhhehsEgZDZFMKnTUgRLuluX8gD30dubLbguaNQ-Tt_zfC7HluERD1CNG-WY5OTOzqsplZpH9K2BvBYqXQtOVwiKN-w7aM4TePqD2w/s4000/TMC2209%20library%20poster.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="Library for TMC2209 driver - AVR" border="0" data-original-height="2250" data-original-width="4000" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQsDZ0fKhjzNAvn-lzcu4bZV4Yyfwxh9OVq8tvFdtSOqZQz92sjG0-8h19pHp3aYFnlTozjOIhrUephkeCMoizxLhhehsEgZDZFMKnTUgRLuluX8gD30dubLbguaNQ-Tt_zfC7HluERD1CNG-WY5OTOzqsplZpH9K2BvBYqXQtOVwiKN-w7aM4TePqD2w/w400-h225/TMC2209%20library%20poster.jpg" title="Library for TMC2209 driver - AVR" width="400"></a></div>The principle of operation is read-modify-write so there is no need to keep
all driver IC registers in memory. The library only uses an 8 bytes union
structure called
<i><span>replyDatagram</span></i> where received and
data to send is stored temporary.<br>
<p></p>
<p>
If you wish to learn on how to wire the TMC2209 driver, you can find a tutorial
<a href="https://www.programming-electronics-diy.xyz/2023/12/tmc2209-stepper-driver-module-tutorial.html" target="_blank">here</a>.
</p>
<h2>Contents</h2>
<ul>
<li><a href="#Library_Structure">Library Structure</a></li>
<li><a href="#Default_Settings">Default Settings</a></li>
<li><a href="#Usage">Usage</a></li>
<li><a href="#Documentation">Documentation</a></li>
<ul>
<li><a href="#TMC_addDriver">Create a structure object for each driver</a></li>
<li><a href="#TMC_init">Initialization</a></li>
<li><a href="#TMC_readVersion">Read IC version</a></li>
<li><a href="#TMC_readStatus">Read Global Status Flags (GSTAT)</a></li>
<ul>
<li><a href="#TMC_statusIsReset">GSTAT check if module was Reset</a></li>
<li><a href="#TMC_statusDriverError">GSTAT check if Driver Error</a></li>
<li><a href="#TMC_statusChargePumpUV">GSTAT check if Charge Pump is Under Voltage</a></li>
</ul>
<li><a href="#TMC_deviceConnectionLost">Status check for UART error</a></li>
<li><a href="#TMC_readPwmScaleSum">Read register PWM_SCALE_SUM</a></li>
<li><a href="#TMC_readPwmScaleAuto">Read register PWM_SCALE_AUTO</a></li>
<li><a href="#TMC_readPwmOfsAuto">Read register PWM_OFS_AUTO</a></li>
<li><a href="#TMC_readPwmGradAuto">Read register PWM_GRAD_AUTO</a></li>
<li><a href="#TMC_readStallGuard">Read register SG_RESULT (StallGuard)</a></li>
<li><a href="#TMC_readTstep">Read register TSTEP</a></li>
<li><a href="#TMC_readDrvStat">Read register DRV_STATUS (Driver Status Flags)</a></li>
<ul>
<li><a href="#TMC_checkStandstil">DRV_STATUS check Standstill</a></li>
<li><a href="#TMC_checkChopper">DRV_STATUS check Chopper type</a></li>
<li><a href="#TMC_checkCS">DRV_STATUS check CS_ACTUAL</a></li>
<li><a href="#TMC_checkTempFlags">DRV_STATUS check Temperature flags</a></li>
<li><a href="#TMC_checkOpenLoad">DRV_STATUS check Open Load flags</a></li>
<li><a href="#TMC_checkLowSideShort">DRV_STATUS check Low Side Short flags</a></li>
<li><a href="#TMC_checkShortToGnd">DRV_STATUS check Short to Ground flags</a></li>
<li><a href="#TMC_checkOverTemp">DRV_STATUS check Over-temperature flag</a></li>
<li><a href="#TMC_checkOverTempPreWarning">DRV_STATUS check Over-temperature Pre-warning flag</a></li>
</ul>
<li><a href="#TMC_setMicrostepResolution">Set micro-step resolution</a></li>
<li><a href="#TMC_setSpeed">Set speed</a></li>
<li><a href="#TMC_setAcceleration">Set acceleration and deceleration</a></li>
<li><a href="#TMC_setDirection">Set direction</a></li>
<li><a href="#TMC_setChopper">Set chopper</a></li>
<li><a href="#TMC_spreadCycleThreshold">Set SpreadCycle threshold</a></li>
<li><a href="#TMC_setIrunIhold">Set register IHOLD_IRUN</a></li>
<li><a href="#TMC_setTPowerDown">Set register TPOWERDOWN</a></li>
<li><a href="#TMC_setStallGuard">Set register SGTHRS (StallGuard)</a></li>
<li><a href="#TMC_coolStepThreshold">Set register TCOOLTHRS (CoolStep threshold)</a></li>
<li><a href="#TMC_setCoolStep">Set register COOLCONF (CoolStep)</a></li>
<li><a href="#TMC_setStandstillMode">Set Standstill mode</a></li>
<li><a href="#TMC_softEnable">Enable/disable driver by software</a></li>
<li><a href="#TMC_moveTo">Move to absolute position</a></li>
<li><a href="#TMC_move">Move to relative position</a></li>
<li><a href="#TMC_startStepGeneratorRPS">Start TMC2209 internal step generator</a></li>
<li><a href="#TMC_stopStepGenerator">Stop TMC2209 internal step generator</a></li>
<li><a href="#TMC_stealthChopAT">StealthChop Automatic Tuning (AT)</a></li>
</ul>
<li><a href="#Download">Download</a></li>
</ul>
<span></span><a href="https://www.programming-electronics-diy.xyz/2023/12/library-for-tmc2209-driver-avr.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-3007099462362290562023-12-14T10:46:00.034+02:002024-01-22T03:08:40.024+02:00TMC2209 stepper driver module tutorial<p>
My first stepper driver module was
<a href="https://www.programming-electronics-diy.xyz/2017/10/how-to-use-a4988-stepper-motor-driver.html#more" target="_blank">A4988</a>
and recently I have decided to give TMC2209 a try hearing how silent and
energy efficient it is. Driving the A4988 is simple - just set direction,
pulse the step pin and the motor moves. You can do the same thing with TMC2209
but this thing can do much more. It has UART interface, registers, algorithms
and an 83 pages datasheet. I might be slow but I had to read the datasheet 3
times to wrap my head around on what things can this thing do. In my opinion
it is a very good stepper driver so I made a <a href="https://www.programming-electronics-diy.xyz/2023/12/library-for-tmc2209-driver-avr.html" target="_blank">library</a> for it and also this
tutorial to better help in using the library. Although the IC presented in
this tutorial is TMC2209, most information can still be applied to other TMC
ICs.<br>
</p>
<h2>Contents</h2>
<ul>
<li><a href="#TMC2209_description">TMC2209 description</a></li>
<li><a href="#Modes_of_operation">Modes of operation</a></li>
<li><a href="#TMC2209_module_pinout">TMC2209 module pinout</a></li>
<li><a href="#Attaching_TMC2209_to_UART">Attaching the TMC2209 to a microcontroller UART</a></li>
<li><a href="#Setting_analog_current">Setting the analog motor current</a></li>
<li><a href="#Setting_digital_current">Setting the digital motor current</a></li>
<li><a href="#Automatic_Standstill_Power_Down">Automatic Standstill Power Down</a></li>
<li><a href="#StealthChop2_SpreadCycle">StealthChop2 and SpreadCycle Driver</a></li>
<ul>
<li><a href="#StealthChop">StealthChop</a></li>
<ul>
<li><a href="#StealthChop_Automatic_Tuning">StealthChop Automatic Tuning</a></li>
<li><a href="#Freewheeling_and_Passive_Braking">Freewheeling and Passive Braking</a></li>
</ul>
<li><a href="#SpreadCycle">SpreadCycle</a></li>
</ul>
<li><a href="#Combine_StealthChop_and_SpreadCycle">Combine StealthChop and SpreadCycle</a></li>
<li><a href="#StallGuard4">StallGuard4 Load Measurement</a></li>
<ul>
<li><a href="#StallGuard4_vs_StallGuard2">StallGuard4 vs. StallGuard2</a></li>
<li><a href="#Tuning_StallGuard4">Tuning StallGuard4</a></li>
<li><a href="#StallGuard4_Update_Rate">StallGuard4 Update Rate</a></li>
<li><a href="#Detecting_a_Motor_Stall">Detecting a Motor Stall</a></li>
<li><a href="#Limits_of_StallGuard4_Operation">Limits of StallGuard4 Operation</a></li>
</ul>
<li><a href="#CoolStep">CoolStep Operation</a></li>
<ul>
<li><a href="#Setting_up_CoolStep">Setting up CoolStep</a></li>
<li><a href="#Tuning_CoolStep">Tuning CoolStep</a></li>
<li><a href="#Response_Time">Response Time</a></li>
</ul>
<li><a href="#Driver_Diagnostic_Flags">Driver Diagnostic Flags</a></li>
<ul>
<li><a href="#Temperature_Measurement">Temperature Measurement</a></li>
<li><a href="#Short_Protection">Short Protection</a></li>
<li><a href="#Open_Load_Diagnostics">Open Load Diagnostics</a></li>
<li><a href="#Diagnostic_Output">Diagnostic Output</a></li>
</ul>
<li><a href="#Internal_Step_Pulse_Generator">Internal Step Pulse Generator</a></li>
<li><a href="#External_Reset">External Reset</a></li>
<li><a href="#Links">Links</a></li>
</ul>
<div id="TMC2209_description" style="text-align: left;"> </div><h2 id="TMC2209_description" style="text-align: left;">TMC2209 description</h2>
<p>
The TMC2209 is an ultra-silent motor driver IC for two phase stepper motors,
developed by <a href="https://www.trinamic.com" target="_blank">trinamic</a>.
TMC2209 pinning is compatible to other drivers as well as to the TMC2208. </p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjGnM_UJx0lRPjRH_pw6ewaqLLlNVRGMZ5FD2qn8cio1U-5hWzvryxPAc1VTrIQrcSAM5HlTTkhlTTYYdX5ev-eNax7KFHPtdUPz4ciL8VF7P5_mKqwCwsZD1IiFevW_HKo9YOT06LvEpOpEl9wJ_MY-BNnaU4fZbTdN6WaFnOWA-DA9bMaZgJLSXtnw0/s4000/TMC2209%20cover.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="TMC2209 stepper driver module tutorial" border="0" data-original-height="2250" data-original-width="4000" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjGnM_UJx0lRPjRH_pw6ewaqLLlNVRGMZ5FD2qn8cio1U-5hWzvryxPAc1VTrIQrcSAM5HlTTkhlTTYYdX5ev-eNax7KFHPtdUPz4ciL8VF7P5_mKqwCwsZD1IiFevW_HKo9YOT06LvEpOpEl9wJ_MY-BNnaU4fZbTdN6WaFnOWA-DA9bMaZgJLSXtnw0/w400-h225/TMC2209%20cover.jpg" title="TMC2209 stepper driver module tutorial" width="400"></a></div>Multiple versions of modules can be found that are using this IC, however the
stand-alone chip can be used for a DIY module that could provide some cost
savings.<p></p>
<h3 style="text-align: left;">Features:<br></h3>
<ul style="text-align: left;">
<li><span style="font-family: trebuchet;">2-phase stepper motors up to 2.8A coil current (peak), 2A RMS</span></li>
<li><span style="font-family: trebuchet;">STEP/DIR Interface with 8, 16, 32 or 64 microstep pin setting</span></li>
<li><span style="font-family: trebuchet;">Smooth Running 256 microsteps by MicroPlyer™ interpolation</span></li>
<li><span style="font-family: trebuchet;">StealthChop2™ silent motor operation</span></li>
<li><span style="font-family: trebuchet;">SpreadCycle™ highly dynamic motor control chopper</span></li>
<li><span style="font-family: trebuchet;">StallGuard4™ load and stall detection for StealthChop</span></li>
<li><span style="font-family: trebuchet;">CoolStep™ current control for energy savings up to 75%</span></li>
<li><span style="font-family: trebuchet;">Low RDSon, Low Heat-Up LS 170mΩ & HS 170mΩ (typ. at 25°C)</span></li>
<li><span style="font-family: trebuchet;">Voltage Range 4.75… 29V DC</span></li>
<li><span style="font-family: trebuchet;">Internal Sense Resistor option (no sense resistors required)</span></li>
<li><span style="font-family: trebuchet;">Passive Braking, Freewheeling, and automatic power down</span></li>
<li><span style="font-family: trebuchet;">Single Wire UART & OTP for advanced configuration options</span></li>
<li><span style="font-family: trebuchet;">Integrated Pulse Generator for standalone motion</span></li></ul><div style="text-align: left;"> </div><ul style="text-align: left;">
</ul>
<div style="text-align: left;"><span></span></div><a href="https://www.programming-electronics-diy.xyz/2023/12/tmc2209-stepper-driver-module-tutorial.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-15695668940150389792023-10-23T05:15:00.007+03:002023-10-23T06:26:03.612+03:00Pin interrupt library for AVR microcontrollers<p>
This library can help to easily configure a pin interrupt on AVR
microcontrollers without the need to check the datasheet every time. At the
moment only PCINT pins are supported.
</p>
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfFkNGmjML5Z3XrVyRlbS9cNIYJuCM7RNKow8Uj_2TwwkixdHoDyrAAbtCpWnvaf0pXJXv8aD-bWCeas4r2Uur4gjr90aoSOXEdAdA1yAqGAp_KkLW1B_19IIyEUr3b7j4EsHe-4ZFwJaXB0uA3Pyorc39PUEKHdllT_L995E89aGi_wPGuRkt7FnOUqo/s3125/Interrupt%20poster.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Pin interrupt library for AVR devices" border="0" data-original-height="3125" data-original-width="2250" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfFkNGmjML5Z3XrVyRlbS9cNIYJuCM7RNKow8Uj_2TwwkixdHoDyrAAbtCpWnvaf0pXJXv8aD-bWCeas4r2Uur4gjr90aoSOXEdAdA1yAqGAp_KkLW1B_19IIyEUr3b7j4EsHe-4ZFwJaXB0uA3Pyorc39PUEKHdllT_L995E89aGi_wPGuRkt7FnOUqo/w288-h400/Interrupt%20poster.png" title="Pin interrupt library for AVR devices" width="288"></a>
</div>
<p></p>
<p>
In this blog post, I will explain how to use interrupts on AVR
microcontrollers. Interrupts are a powerful feature that allow the
microcontroller to respond to external or internal events without constantly
polling for them. Interrupts can improve the performance and efficiency of
your code, as well as enable new functionalities.
</p>
<h2 style="text-align: left;">What are interrupts?</h2>
<p>
An interrupt is a signal that causes the microcontroller to temporarily stop
its current execution and jump to a special function called an interrupt
service routine (ISR). The ISR performs the necessary actions to handle the
interrupt, and then returns to the original program flow. The ISR can be
triggered by various sources, such as:
</p>
<p></p>
<ul style="text-align: left;">
<li>External pins (EXTINT)</li>
<li>Pin changes (PCINT)</li>
<li>Timers/Counters (TIMER)</li>
<li>Serial communication (USART, SPI, TWI)</li>
<li>Analog-to-digital conversion (ADC)</li>
<li>Analog comparator (AC)</li>
<li>Watchdog timer (WDT)</li>
<li>EEPROM ready (EE READY)</li>
<li>Store program memory ready (SPM READY)</li>
</ul>
<p></p>
<span></span><a href="https://www.programming-electronics-diy.xyz/2023/10/pin-interrupt-library-for-avr.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-13541967638232964462023-10-03T01:43:00.057+03:002024-02-03T06:34:56.767+02:00stepperCon library for controlling stepper motor drivers using timer interrupt on AVR devices<p>
The stepperCon library allows you to control multiple stepper motors at the
same time. It can interface with any stepper motor driver that can take inputs
such as step and direction. The library also has some extra useful
functionalities such as motor coordination using Bresenham line algorithm -
useful for controlling 3 axis motors for example, free spin - where the motor
keeps spinning, and positioning the motor at a certain degree from 1 to 360 by
finding the shortest path.
</p>
<p>
The maximum speed (step rate) on a 16MHz microcontroller is 25kHz, however the
maximum practical speed is 6.25kHz or 8.33kHz depending on the motor, load,
supply voltage and micro-step resolution. A higher voltage can yield higher
RPM and with 1/32 micro-step resolution the step rate can be 25kHz as oppose
to 6.25kHz when using 1/8 micro-step resolution.<br>
</p>
<p>
Even with multiple motors running at the same time, the speed can be
maintained and that is because the speed profile is segmented and calculated
while the interrupt routine (ISR) is not executing steps. So the time spent
inside the ISR is low since the calculations for acceleration and deceleration
are not done there. Only the stepping is done inside the ISR. Having an
interrupt to generate the steps is preferred over a function in the main loop
where other code could delay the stepping function thus causing motor
stuttering.
</p>
<p>
The stepper library includes the
<a href="https://www.programming-electronics-diy.xyz/2021/01/millis-and-micros-library-for-avr.html" target="_blank">micros </a>library that is used to trigger a timer interrupt every 40us and as a perk
you also have a way to keep track of time in your project with a 40us time
resolution.<br>
</p>
<div class="separator" style="clear: both; text-align: center;">
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/UUM_oAXt7Rc?si=2veorq8HnsoZAmEC" title="YouTube video player" width="560"></iframe>
</div>
<div style="text-align: left;"> </div>
<h2 style="text-align: left;">stepperCon library characteristics</h2>
<ul style="text-align: left;">
<li>
<span style="font-family: verdana; font-size: small;">interface with multiple stepper motor drivers</span>
</li>
<li>
<span style="font-family: verdana; font-size: small;">up to 3 coordinated motors for X, Y and Z (can be extended for
more)</span>
</li>
<li>
<span style="font-family: verdana; font-size: small;">
maximum stepping rate speed of 6.25kHz (or 8.33kHz if the motor can have
high acceleration). Speeds are given for a 16MHz CPU. With higher CPU
clock the stepping rate will also be higher.
</span>
</li>
<li>
<span style="font-family: verdana; font-size: small;">
angular positioning function that finds the shortest path from current
angle to target angle
</span>
</li>
<li>
<span style="font-family: verdana; font-size: small;">perpetual motion</span>
</li>
<li>
<span style="font-family: verdana; font-size: small;">timer interrupt driven</span>
</li>
<li>
<span style="font-family: verdana; font-size: small;">individual settings for acceleration and deceleration</span>
</li>
</ul>
<span></span><a href="https://www.programming-electronics-diy.xyz/2023/10/steppercon-library-for-controlling.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-1575448015903821752023-08-07T01:53:00.003+03:002023-10-03T01:33:15.893+03:00Plotting data from a logic analyzer - CSV and LibreOffice Calc<p>
Sometimes it is useful to visualize data from a logic analyzer in a graphical
way but unfortunately not many applications can do that so I came up with a
solution involving CSV data and LibreOffice Calc. Here I am using Logic from
Saleae but any software that can export the data to a CSV file can work.
</p>
<div class="separator" style="clear: both; text-align: center;">
<a
href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU3a93onq8uIYtwYz98NBKnl37wtwuLMl38CRHhijgfTUt2wnU3uWXC5k-7MCIna26p5YvE-YcVKE4LtGhc2owf5JNhiBgs2-JAdXIMPPuF9SXphlFyCkYL2MF1Ee3ygdxuuNt3B28Malrm1Bgkp7fVy7iH33iPmet_8r0He_1b_iT9mxWi_1IhqVeyfI/s1280/Plotting%20data%20from%20a%20logic%20analyzer.png"
style="margin-left: 1em; margin-right: 1em;"
> width="640"></a>
</div>
<h2 style="text-align: left;">Usage</h2>
<p style="text-align: left;">
First thing to do is to export the data from the logic analyzer software to a
CSV file format. In Logic 2 this can be easily achieved by going to
<b>File -> Export Data</b> or by pressing CTRL + SHIFT + S.
</p>
<div class="separator" style="clear: both; text-align: center;">
<a
href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc7ezIjaLL_iK4ADPp4gNArj_G_KM7JFWLgKWAyRfcG5HLhfYXGfSXAk9E333XMPtVzxsthPecV2vxa2Wuvq60MEzOno14vE9O_xxkMrpAfY65OufoQyq7-K6JOWywsdfjCcCmrZv2smzxBzHpe44maAmJyBWuLEOjr96uWhNf3h4gQ2l3RaE4Wvp0KRU/s700/Logic%20analyzer%20-%20Export%20data.jpg"
style="margin-left: 1em; margin-right: 1em;"
> width="299"></a>
</div>
<br><span></span><a href="https://www.programming-electronics-diy.xyz/2023/08/plotting-data-from-logic-analyzer-csv.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-55606601406000355392023-06-10T03:22:00.016+03:002024-03-06T00:09:24.284+02:00Breadboard Development Board for AVR microcontrollers | Breaduino<p>
A development board is an essential tool when working with projects that
involve a microcontroller and most of the time you will also need a breadboard
and a power supply. So why not combining all these in a single product?<br>
</p>
<p>
Meet Breaduino - a custom development board build around ATmega328PB
microcontroller. If you are a beginner in electronics or microcontrollers,
building a development board is not only rewarding but also educational.
</p>
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2LUxD_wlbPk92aTDBxmPxrFua5R7UNG5ySzheFgDnPahrMcY7ahX3yecoqNm-oefM4Xu7rL5flysxBlff1abYXK1fZ6ZHlTIC9CltZcc_Lv1uN7yznj43-xcId5Tp7DwYf1Ck4B4xEeYb56N7A8k1TMK3uL7J28Nn9gqHCfOYGDRe_0n3XNS3GYce/s1280/Dev%20Board%20Thumbnail.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Breadboard Development Board for AVR microcontrollers | Breaduino" border="0" data-original-height="720" data-original-width="1280" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2LUxD_wlbPk92aTDBxmPxrFua5R7UNG5ySzheFgDnPahrMcY7ahX3yecoqNm-oefM4Xu7rL5flysxBlff1abYXK1fZ6ZHlTIC9CltZcc_Lv1uN7yznj43-xcId5Tp7DwYf1Ck4B4xEeYb56N7A8k1TMK3uL7J28Nn9gqHCfOYGDRe_0n3XNS3GYce/w640-h360/Dev%20Board%20Thumbnail.png" title="Breadboard Development Board for AVR microcontrollers | Breaduino" width="640"></a>
</div><p>The Breaduino dev board is designed to be used with two breadboards where it
outputs three voltages: 12V, 5V and 3.3V. It includes a TFT display, micro SD
card socket, input current measurement and a rotary encoder for user
interaction. On the right side of the case is space for a small logic analyzer
and/or a 9V battery. All these are optional modules. Even the breadboard is
optional so the board can be used standalone. Since this is a customizable
board it can be modified to fit your needs.
</p><p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj0bzEGKTvej21PicgGk-oq7GXMVirb_dGgSsypM0J6pq2FgMMmXKtmGbUMW83sXBMfm9dz7nHPqDyfwkL0z6k5qJ7mS7MhgdbnLg4s01Vqy-mYUwytJvd6kp_VT0zXY2LRFkRRC0-HegX_2wmM7zayoh7SHfzcs_C-46HcNT5Rjg3x1BVE9gHGo-w/s1100/Dev%20Board%203D%20-%20Top%20(with%20THT).png" style="margin-left: 1em; margin-right: 1em;"><img alt="Breadboard Development Board for AVR microcontrollers | Breaduino" border="0" data-original-height="920" data-original-width="1100" height="536" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjj0bzEGKTvej21PicgGk-oq7GXMVirb_dGgSsypM0J6pq2FgMMmXKtmGbUMW83sXBMfm9dz7nHPqDyfwkL0z6k5qJ7mS7MhgdbnLg4s01Vqy-mYUwytJvd6kp_VT0zXY2LRFkRRC0-HegX_2wmM7zayoh7SHfzcs_C-46HcNT5Rjg3x1BVE9gHGo-w/w640-h536/Dev%20Board%203D%20-%20Top%20(with%20THT).png" title="Breadboard Development Board for AVR microcontrollers | Breaduino" width="640"></a>
</div>
<br><span></span><a href="https://www.programming-electronics-diy.xyz/2023/06/breadboard-development-board-for-avr.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-22840414962534454512022-08-28T23:31:00.016+03:002023-10-04T03:34:27.446+03:00IR remote control library for AVR microcontrollers<p>
This library can be used for sending or receiving
remote controller codes using a microcontroller. The supported protocols at the moment are NEC and RC-5 since these are the most commonly used.
</p>
<p>
The IR library is very easy to use and it needs a 16-bit timer for both
sending and receiving. Timer 1 is used for this purpose. Before diving into the code,
let's see how a remote controller works and take a closer look at their
protocols.</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2FGSxDqbR4W4ikH2M_Wpl0xPxQkDhHc4iKJLayqx9gIV6sQP_ebVXLUlEOGLLPqDUulplTdNVM20SASssa7_znsZZv6WRzIQhDgVK1wqShOYmNlfy1R97DaQDsm9P5bgRZcHP87S8XdQz-NZ_z8QfkNIfYOaH8M6xMTkOqj1ZE3plt8o9Ep-udKns/s625/remote%20cover.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="625" data-original-width="594" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2FGSxDqbR4W4ikH2M_Wpl0xPxQkDhHc4iKJLayqx9gIV6sQP_ebVXLUlEOGLLPqDUulplTdNVM20SASssa7_znsZZv6WRzIQhDgVK1wqShOYmNlfy1R97DaQDsm9P5bgRZcHP87S8XdQz-NZ_z8QfkNIfYOaH8M6xMTkOqj1ZE3plt8o9Ep-udKns/s320/remote%20cover.png" width="304"></a></div><br> <p></p>
<h2 style="text-align: center;">Contents</h2>
<ul>
<li><a href="#How_remote_controller_works">How a remote controller works</a></li>
<ul>
<li><a href="#Infrared_light">Infrared light</a></li>
<li><a href="#Modulation">Modulation</a></li>
<li><a href="#The_transmitter">The transmitter</a></li>
<li><a href="#The_receiver">The receiver</a></li>
<li><a href="#NEC_protocol">NEC protocol</a></li>
<li><a href="#Philips_RC5_protocol">Philips RC-5 protocol</a></li>
</ul>
<li><a href="#The_library">The IR Remote library</a></li>
<li><a href="#Download">Download</a></li>
</ul>
<p id="How_remote_controller_works" style="text-align: left;"> </p><h1 id="How_remote_controller_works" style="text-align: left;">How a remote controller works</h1>
<p>
A remote control is using optical communication to send wireless data to a
receiver device. For this purpose, the infrared light was chosen. The data is
transmitted on top of a carrier frequency that is usually 38kHz. There are
many schemes of encoding the data because there are many manufacturers of
consumer products. The most commonly used protocols are NEC, RC-5, RC-6, Sony. </p><p><span></span></p><a href="https://www.programming-electronics-diy.xyz/2022/08/ir-remote-control-library-for-avr.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-30365251626186840972022-08-07T06:18:00.077+03:002024-03-15T03:21:09.996+02:00UART library for AVR microcontrollers using interruptsThis is a UART library that is made for AVR microcontrollers that can be used
for serial communications.<br>
<p>
UART is a type of serial interface, as opposed to a parallel interface. A
parallel interface can work at higher speeds but the disadvantage is that it
needs multiple input/output lines. Other examples of serial interfaces are SPI
and I2C.
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFkwGMDHxz7YWRSet-oZdwop89JQvjPMDlKBVyPamDO7twodSpimy5KYbHdxBSnBXXVTFtFiX09yipc0z8NLIdEuhJlYOuZ0HF4M9UvX7qZ7tWo9QbiBhP6UvFcrD52G5BIPu5Gg99YV3QD77pIf0fRg99aXZGkCCkptoj9gJSBHkDcYaclb9bdulY/s1200/uart%20poster.png" style="margin-left: 1em; margin-right: 1em;"><img alt="UART library for AVR microcontrollers using interrupts" border="0" data-original-height="630" data-original-width="1200" height="336" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFkwGMDHxz7YWRSet-oZdwop89JQvjPMDlKBVyPamDO7twodSpimy5KYbHdxBSnBXXVTFtFiX09yipc0z8NLIdEuhJlYOuZ0HF4M9UvX7qZ7tWo9QbiBhP6UvFcrD52G5BIPu5Gg99YV3QD77pIf0fRg99aXZGkCCkptoj9gJSBHkDcYaclb9bdulY/w640-h336/uart%20poster.png" title="UART library for AVR microcontrollers using interrupts" width="640"></a>
</div>
<h2 style="text-align: left;">Features</h2>
<ul style="text-align: left;">
<li>Custom Baud rate</li>
<li>Asynchronous or synchronous modes</li>
<li>Supports serial frames with 5, 6, 7, 8, or 9 data bits</li>
<li>Odd, even or no parity</li>
<li>Error detection</li>
<li>
Multi-processor communication mode used to address multiple devices on the
same serial bus
</li>
<li>Double speed asynchronous communication mode</li>
</ul>
<p><span></span></p>
<a href="https://www.programming-electronics-diy.xyz/2022/08/uart-library-for-avr-microcontrollers.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-21393996364870156232022-08-04T00:00:00.006+03:002023-01-01T20:58:41.806+02:00Tutorial on how to program an AVR ATmega328PB microcontroller using Atmel Studio and a bootloader<p>
In this tutorial you will be learning how to use Microchip Studio (previously
known as Atmel Studio) to program an AVR microcontroller over UART using the
Optiboot bootloader. The hardware necessary is very inexpensive. All you need
is an ISP (In System Programming) module such as USBTinyISP (around 3$) and an
USB to Serial adapter that is around the same price and you can even build it
yourself if you wish.
</p>
<p><span></span></p>
<a href="https://www.programming-electronics-diy.xyz/2022/08/tutorial-on-how-to-program-avr.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-86924862782897080452022-07-25T07:12:00.011+03:002023-05-23T01:02:32.032+03:00Library for MCP4725 DAC for AVR microcontrollers<p>
The MCP4725 is a low-power, high accuracy, single channel, 12-bit buffered
voltage output Digital-to-Analog Convertor (DAC) with non-volatile memory
(EEPROM). Its on-board precision output amplifier allows it to achieve
rail-to-rail analog output swing. The advantage of this DAC is the A0 pin that
can be used to control multiple DACs by changing their address (more on that
later).<br>
</p>
<p>
This DAC library also includes an I2C driver that is needed by the AVR to
communicate with the MCP4725.</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSbLXmyJqTKZZfYSL5mZ4dnvfDhZsGN3txEZV12N8wzBTY84tm0Ltw0_nNQu8ap5mwi_56Nc38Tbbvt4zJmIVowpDgmckpmHepgla3udAt8Mu5tDD6d7olOffsNMSKUBcwr8H2cz6f2JU3w6vZdjBLp5h1HNraI2hAOcN660Q_PKYBFaOcmyqiklyd/s1200/MCP4725%20poster.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Library for MCP4725 DAC for AVR microcontrollers" border="0" data-original-height="630" data-original-width="1200" height="336" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSbLXmyJqTKZZfYSL5mZ4dnvfDhZsGN3txEZV12N8wzBTY84tm0Ltw0_nNQu8ap5mwi_56Nc38Tbbvt4zJmIVowpDgmckpmHepgla3udAt8Mu5tDD6d7olOffsNMSKUBcwr8H2cz6f2JU3w6vZdjBLp5h1HNraI2hAOcN660Q_PKYBFaOcmyqiklyd/w640-h336/MCP4725%20poster.png" title="Library for MCP4725 DAC for AVR microcontrollers" width="640"></a></div><br><p></p>
<h4>Contents <br></h4>
<ol>
<li><a href="#Pin_Descriptions">Pin Descriptions</a></li>
<li><a href="#Wiring_up_one_or_multiple_DACs">Wiring up one or multiple DACs</a></li>
<li><a href="#MCP4725_Characteristics">MCP4725 Characteristics</a></li>
<ul>
<li><a href="#MCP4725_DAC_address">3.1 MCP4725 DAC address</a></li>
<li><a href="#Output_Voltage">3.2 Output Voltage</a></li>
</ul>
<li><a href="#Code_Example">Code Example</a></li>
<li><a href="#Using_the_MCP4725_library">Using the MCP4725 library</a></li>
<ul>
<li><a href="#Initialization_function">Initialization function</a></li>
<li><a href="#Set_DAC_voltage">Set DAC voltage</a></li>
<li><a href="#Convert_voltage_to_DAC_unit">Convert voltage to DAC unit</a></li>
<li><a href="#Power_bits">Power bits</a></li>
<li><a href="#Read_DAC">Read DAC</a></li>
<li><a href="#General_call">General call</a></li>
</ul>
<li><a href="#Download">Download</a></li>
</ol>
<h2 id="Pin_Descriptions" style="text-align: left;"> </h2><h2 id="Pin_Descriptions" style="text-align: left;">1. Pin Descriptions <br></h2>
<p> </p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_If8emG-_s8MsVmQH7R-DyFh-1xyjo52dE4QFzvsJroSAnNeg_w2Ez83QtpsfjoTeEQQtnhon-MQNddzXaXSlywxOW4tbCZPxEB9m2s2HOdBfNXQSWTasHZmgJCG3kxVVNPYm28_WKskWRmrA6T4leBYENk6BtsPYOnfuEzrQvPlNr7wsr59SSf3R/s947/Pin%20Descriptions.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="MCP4725 DAC pin description" border="0" data-original-height="319" data-original-width="947" height="216" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_If8emG-_s8MsVmQH7R-DyFh-1xyjo52dE4QFzvsJroSAnNeg_w2Ez83QtpsfjoTeEQQtnhon-MQNddzXaXSlywxOW4tbCZPxEB9m2s2HOdBfNXQSWTasHZmgJCG3kxVVNPYm28_WKskWRmrA6T4leBYENk6BtsPYOnfuEzrQvPlNr7wsr59SSf3R/w640-h216/Pin%20Descriptions.jpg" title="MCP4725 DAC pin description" width="640"></a>
</div>
<p></p>
<p></p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjup_PfpwFa3mhSHCgacj6PqP2re7VBn_l8jPrMaJTI0SAepJmdnqFs1jRS7DPgQ9uvs5bbMnfOT50c9GrfmyDA4UxLk6anVT4M56hfnLGiL9q8nSfSJOhKzURnRPAlDBW10_6aqKnNe76nkOg1Y4Ou3SYRXUtozAkfF8FIIsshlJL0ufE11vB8iIH/s1011/MCP4725%20pins.png" style="margin-left: 1em; margin-right: 1em;"><img alt="MCP4725 pins" border="0" data-original-height="758" data-original-width="1011" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjup_PfpwFa3mhSHCgacj6PqP2re7VBn_l8jPrMaJTI0SAepJmdnqFs1jRS7DPgQ9uvs5bbMnfOT50c9GrfmyDA4UxLk6anVT4M56hfnLGiL9q8nSfSJOhKzURnRPAlDBW10_6aqKnNe76nkOg1Y4Ou3SYRXUtozAkfF8FIIsshlJL0ufE11vB8iIH/w320-h240/MCP4725%20pins.png" title="MCP4725 pins" width="320"></a>
</div>
<p></p>
<p>
<b><span></span><span></span></b>
</p>
<a href="https://www.programming-electronics-diy.xyz/2022/07/library-for-mcp4725-dac-for-avr.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-16527748970343474932022-07-13T05:29:00.012+03:002023-05-02T18:30:13.249+03:00SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 2 of 2) <p>
This is part 2 of the tutorial on SD card specifications. In
<a href="https://www.programming-electronics-diy.xyz/2022/07/sd-card-tutorial-interfacing-sd-card.html" target="_blank">part 1</a>
of the tutorial we made functions necessary for SPI communication and card
initialization in SPI mode. At the end of this second part you should be able
to read and write an SD card.
</p>
<p align="center" style="break-after: avoid; line-height: 100%; margin-bottom: 0.08in; margin-top: 0.17in; page-break-after: avoid;">
<span face="Liberation Sans, sans-serif"><span style="font-size: medium;"><b>Contents</b></span></span>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;">
<a href="#__RefHeading___Toc14964_1413886508">5. Reading/Writing Data Blocks
</a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc27390_1413886508">5.1 Block Length </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc27392_1413886508">5.2 CMD17 – Reading a Single Block
</a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.39in;">
<a href="#__RefHeading___Toc27394_1413886508">5.2.1 Read Errors </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc27396_1413886508">5.3 CMD24 – Writing a Single Block
</a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;">
<a href="#__RefHeading___Toc35338_1413886508">6. Conclusion or Confusion </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;">
<a href="#__RefHeading___Toc34713_1413886508">7. Links </a></p><p lang="zxx" style="line-height: 100%; margin-bottom: 0in;"><br></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKNkYy0HG3MpHI2Vyw4Qszgn4fSXpfaSQFHilkwwXMu9nP1yx_xLbi5A11ad86HaS91CsDIVH1BJdXyCl1CTaB13RhqVwqt7BH-PSyq9-j-9GQaaKImjc4QyWYmqlxPKnLl_DKTkuptR0sAwls1WCUmB-gIB1AqCB6GKWaFN0av69at7B49sXTEbxu/s1200/SD%20card%20interface%20part%202%20poster.png" style="margin-left: 1em; margin-right: 1em;"><img alt="SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 2 of 2)" border="0" data-original-height="630" data-original-width="1200" height="336" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKNkYy0HG3MpHI2Vyw4Qszgn4fSXpfaSQFHilkwwXMu9nP1yx_xLbi5A11ad86HaS91CsDIVH1BJdXyCl1CTaB13RhqVwqt7BH-PSyq9-j-9GQaaKImjc4QyWYmqlxPKnLl_DKTkuptR0sAwls1WCUmB-gIB1AqCB6GKWaFN0av69at7B49sXTEbxu/w640-h336/SD%20card%20interface%20part%202%20poster.png" title="SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 2 of 2)" width="640"></a></div><br><p lang="zxx" style="line-height: 100%; margin-bottom: 0in;"><br></p><h1 class="western">5. Reading/Writing Data Blocks</h1>
<h2 class="western">
<a name="__RefHeading___Toc27390_1413886508"></a>5.1 Block Length
</h2>
<p>
Block length can be set in Standard Capacity SD cards using CMD16
(SET_BLOCKLEN) however for SDHC and SDXC cards, the block length is always set
to 512 bytes. Since nowadays most if not all cards are of high capacity type,
we will only consider the latter in this tutorial.
</p>
<p><span></span></p>
<a href="https://www.programming-electronics-diy.xyz/2022/07/sd-card-tutorial-interfacing-sd-card_13.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com2tag:blogger.com,1999:blog-3851144877322223496.post-43154563306163925472022-07-13T00:04:00.027+03:002024-02-07T02:50:10.569+02:00SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 1 of 2)<p>This is a two-part series tutorial about the SD protocol used by SD flash memory cards.<br></p><p>Personally I learn better using practical examples instead of abstract data,
and for this reason I have constructed this tutorial as a step by step with
practical code examples written in C language, that can be followed by anyone
with basic programming skills and knowledge on how to use a microcontroller.
Although I am using an ATmega328P in this tutorial, the concepts extend to any
microcontroller.</p><p></p><blockquote><i><span style="font-size: small;"><span style="font-family: verdana;">In case you just need a library to read/write a memory card, you can find one here: <a href="https://www.programming-electronics-diy.xyz/2022/07/sd-memory-card-library-for-avr.html" target="_blank">https://www.programming-electronics-diy.xyz/2022/07/sd-memory-card-library-for-avr.html</a>. This tutorial will help in understanding the low-level interactions between a microcontroller and a memory card but it will not be enough to use an SD card in a practical way. Apart from an SD card driver, that is the code described in this article, you will also need a file system driver such as FAT16 or FAT32 in order to read or write files. Writing and reading data to a memory card without a file system, is like using a large EEPROM.</span><br></span></i></blockquote><p></p>
<p align="center" style="break-after: avoid; line-height: 100%; margin-bottom: 0.08in; margin-top: 0.17in; page-break-after: avoid;">
<span face="Liberation Sans, sans-serif"><span style="font-size: medium;"><b>Contents</b></span></span>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;">
<a href="#__RefHeading___Toc1790_1413886508">1. General Description </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc34968_1413886508">1.1 Bus Mode and Clock Speed
</a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc34970_1413886508">1.2 Read/Write Mode Selection
</a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;">
<a href="#__RefHeading___Toc1792_1413886508">2. SD Card Hardware Interface
</a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc1794_1413886508">2.1 microSD Card Schematic SPI Interface
</a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;">
<a href="#__RefHeading___Toc1819_1413886508">3. SPI Setup </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;">
<a href="#__RefHeading___Toc14942_1413886508">4. Card Initialization </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14944_1413886508">4.1 Power Up Sequence </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14946_1413886508">4.2 Sending Commands </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14948_1413886508">4.3 Initialization Flow </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14950_1413886508">4.4 CMD0 </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14952_1413886508">4.5 Response R1 </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14954_1413886508">4.6 CMD8 </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14956_1413886508">4.7 Response R7 </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14958_1413886508">4.9 CMD58 </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14960_1413886508">4.10 Response R3 </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;">
<a href="#__RefHeading___Toc14962_1413886508">4.11 ACMD41 & CMD55 </a>
</p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;"><span style="color: #999999;">5. Reading/Writing Data Blocks</span></p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;"><span style="color: #999999;">5.1 Block Length</span></p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;"><span style="color: #999999;">5.2 CMD17 – Reading a Single Block</span></p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.39in;"><span style="color: #999999;">5.2.1 Read Errors</span></p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in; margin-left: 0.2in;"><span style="color: #999999;">5.3 CMD24 – Writing a Single Block</span></p>
<p lang="zxx" style="line-height: 100%; margin-bottom: 0in;"><span style="color: #999999;">6. Links</span></p>
<p align="left">
<br>
</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRptPEbxr-9NZJRoVgSyQE6gl_KH6qqP6HMmyLvZQtaepZUQ8XGEb6EKrbJzk4g2HDqEZ_EmcVdzAUQjCXlt3l383kljYjlXYgLPAXz_wsSLm03KGJFg2efnwc3ZDgHrRx-gOAcjUO1Eenl6b2tAFC632MgRSAGo5IN8Ff7lwivZJhDKNsyAqNl7iK/s1200/SD%20card%20interface%20part%201%20poster.png" style="margin-left: 1em; margin-right: 1em;"><img alt="SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 1 of 2)" border="0" data-original-height="630" data-original-width="1200" height="336" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRptPEbxr-9NZJRoVgSyQE6gl_KH6qqP6HMmyLvZQtaepZUQ8XGEb6EKrbJzk4g2HDqEZ_EmcVdzAUQjCXlt3l383kljYjlXYgLPAXz_wsSLm03KGJFg2efnwc3ZDgHrRx-gOAcjUO1Eenl6b2tAFC632MgRSAGo5IN8Ff7lwivZJhDKNsyAqNl7iK/w640-h336/SD%20card%20interface%20part%201%20poster.png" title="SD card tutorial - Interfacing an SD card with a microcontroller over SPI (part 1 of 2)" width="640"></a></div><br>
<h1 class="western">
<a name="__RefHeading___Toc1790_1413886508"></a>1. General Description
</h1>
<p>
The Secure Digital (SD) Card was developed by the
<a href="https://en.wikipedia.org/wiki/SD_Association">SD Association (SDA)</a>
as an improvement over MMCs. The SD Card specifications were originally
defined by MEI (Matsushita Electric Company), Toshiba Corporation and SanDisk
Corporation. Currently, the specifications are controlled by the Secure
Digital Association (SDA).
</p>
<p>
In addition to the mass storage specific flash memory chip, the SD Card
includes an on-card intelligent controller which manages interface protocols,
security algorithms for copyright protection, data storage and retrieval, as
well as Error Correction Code (ECC) algorithms, defect handling and
diagnostics, power management and clock control.</p><p><span></span></p><a href="https://www.programming-electronics-diy.xyz/2022/07/sd-card-tutorial-interfacing-sd-card.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com5tag:blogger.com,1999:blog-3851144877322223496.post-59825535964179175052022-07-07T02:43:00.137+03:002024-02-06T04:24:08.066+02:00SD Memory Card Library for AVR Microcontrollers - SD and FAT Driver<p>
This is a library for reading and writing of SD flash memory cards using FAT16
or FAT32 file systems, over the SPI protocol, and is designed with embedded
systems in mind. It includes an SD card driver that uses the SPI interface and
a FAT driver that is controlled by the host microcontroller.
</p>
<p></p>
<p>
There are two main standards for flash memory cards: MMC (MultiMediaCards) and
SD (Secure Digital), SD being the most used today. SD was developed by the
<a href="https://en.wikipedia.org/wiki/SD_Association" target="_blank" title="">SD Association (SDA)</a>
as an improvement over MMCs. These cards have basically a flash memory array
and a microcontroller inside that controls erasing, reading, writing, error
controls, and wear leveling of the flash array. The data is transferred
between the memory card and the host controller as data blocks in units of 512
bytes.
</p>
<p>
The recommended file systems for memory cards are FAT12/16/32 or exFAT.
Maximum volume sizes for each one are: 256MB, 4GB, 16TB* for FAT32 and 128PB
for exFAT.
</p>
<p>
<span style="color: #444444;">*<i>Windows will refuse to format cards over 32GB using FAT32, offering exFAT
and NTFS as an option but there are workarounds and third-party software
that can do that.</i></span><br>
</p>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
.tg .tg-tbqi{border-color:inherit;font-size:large;text-align:left;vertical-align:top}
.tg .tg-fymr{border-color:inherit;font-weight:bold;text-align:left;vertical-align:top}
</style>
<table class="tg">
<thead>
<tr>
<th class="tg-0pky"><br></th>
<th class="tg-tbqi" colspan="2">FAT16</th>
<th class="tg-tbqi" colspan="2">FAT32</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky"><br></td>
<td class="tg-fymr">Flash</td>
<td class="tg-fymr">RAM</td>
<td class="tg-fymr">Flash</td>
<td class="tg-fymr">RAM</td>
</tr>
<tr>
<td class="tg-fymr">Read</td>
<td class="tg-0pky">6.1k</td>
<td class="tg-0pky">605</td>
<td class="tg-0pky">6.6k</td>
<td class="tg-0pky">607</td>
</tr>
<tr>
<td class="tg-fymr">Write</td>
<td class="tg-0pky">7.8k</td>
<td class="tg-0pky">607</td>
<td class="tg-0pky">8.6k</td>
<td class="tg-0pky">609</td>
</tr>
<tr>
<td class="tg-fymr">Read/Write</td>
<td class="tg-0pky">7.9k</td>
<td class="tg-0pky">607</td>
<td class="tg-0pky">8.7k</td>
<td class="tg-0pky">609</td>
</tr>
</tbody>
</table>
<p style="text-align: left;">
<span style="color: #444444;"><span style="font-size: small;"><i>Some rounded up values of the code footprint depending on the functions
used: for reading, writing or both.
</i></span></span><span style="color: #444444;"><span style="font-size: small;"><i>RAM size is expressed in bytes and it includes 512 bytes for the
read/write buffer.</i></span></span><br>
</p>
<h4>Features</h4>
<ul>
<li><span style="color: #444444;">Communication protocol: SPI</span></li>
<li>
<span style="color: #444444;">Supported memory cards: SD cards (MMC's are not implemented)</span>
</li>
<li><span style="color: #444444;">Includes SD driver: Yes</span></li>
<li>
<span style="color: #444444;">Supported file systems: FAT16 and FAT32</span>
</li>
<li>
<span style="color: #444444;">Support for LFN (Long File Names): Yes (up to 255 characters)</span>
</li>
<li><span style="color: #444444;">Formatting utility: No</span></li>
<li>
<span style="color: #444444;">Multiple files and folders instances: Yes</span>
</li>
<li>
<span style="color: #444444;">Ability to create folders and directories</span>
</li>
<li>
<span style="color: #444444;">Delete files: Yes, with recursive removal of files and sub-directories
</span>
</li>
<li>
<span style="color: #444444;">FSInfo (FAT32): not implemented in order to reduce the code size. The
only situations when this could be a downside is when querying for free
space, creating files/folders or expand them since this is when it is
necessary to search for a free cluster. If the card is mostly empty, even
if it has a large capacity, the search for a free cluster will be very
quick. As the card is filled it will take longer (few seconds).</span>
</li>
</ul>
<div style="text-align: left;"> </div>
<h4>Contents <br></h4>
<ol>
<li><a href="#SD_Card_Pins">SD Card Pins</a></li>
<li><a href="#Schematic_Interface">Schematic Interface</a></li>
<li><a href="#Return_Values">Return Values</a></li>
<li><a href="#File_Object_Structures">File Object Structures</a></li>
<li><a href="#Filenames">File names</a></li>
<li>
<a href="#The_Buffer_the_Writing_and_the_Flush">The Buffer, the Writing and the Flush</a>
</li>
<li><a href="#Code_Examples">Code Examples</a></li>
<li><a href="#Library_Configuration">Library Configuration</a></li>
<li>
<a href="#Functions_and_their_usage">Library Usage<br></a>
</li>
<ol>
<li><a href="#Volume_Management">Volume Management</a></li>
<ol>
<li><a href="#Card_Detect">Card Detect</a></li>
<li><a href="#Card_Initialization">Card Initialization</a></li>
<li><a href="#Volume_Free_Space">Volume Free Space</a></li>
<li><a href="#Volume_Capacity">Volume Capacity</a></li>
<li>
<a href="#Read_Volume_Label_Serial_Number">Read Volume Label and Serial Number</a>
</li>
</ol>
<li><a href="#Directory_Access">Directory Access</a></li>
<ol>
<li><a href="#Make_Directory">Make Directory</a></li>
<li><a href="#Open_Directory">Open Directory</a></li>
<li><a href="#Open_Directory_by_Index">Open a Directory by Index</a></li>
<li><a href="#Go_Back">Go Back</a></li>
<li><a href="#Find_by_Index">Find by Index</a></li>
<li><a href="#Find_Next">Find Next</a></li>
<li><a href="#Count_Items">Count Items</a></li>
</ol>
<li><a href="#File_Access">File Access</a></li>
<ol>
<li><a href="#Make_File">Make File</a></li>
<li>
<a href="#Delete_File_Folder_by_Index">Delete File or Folder by Index</a>
</li>
<li>
<a href="#Delete_File_Folder_by_Name">Delete File or Folder by Name</a>
</li>
<li><a href="#Open_File">Open File</a></li>
<li><a href="#Open_File_by_Index">Open File by Index</a></li>
<li><a href="#Set_File_Pointer">Set File Pointer</a></li>
<li><a href="#Seek_File_End">Seek File End</a></li>
<li><a href="#Get_File_Pointer">Get File Pointer</a></li>
<li><a href="#Write_Float_Number">Write Float Number</a></li>
<li><a href="#Write_Number">Write Number</a></li>
<li><a href="#Write_String">Write String</a></li>
<li><a href="#Write_File">Write File</a></li>
<li><a href="#Truncate_File">Truncate File</a></li>
<li><a href="#Flush_Data">Flush Data</a></li>
<li><a href="#Read_File">Read File</a></li>
<li><a href="#End_of_File_Check">End of File Check</a></li>
<li><a href="#Error_Flag_Check">Error Flag Check</a></li>
<li><a href="#Clear_Error_Flag">Clear Error Flag</a></li>
<li><a href="#Get_Filename">Get Filename</a></li>
<li><a href="#Get_Index">Get Index</a></li>
<li><a href="#Get_File_Size">Get File Size</a></li>
<li><a href="#Get_Write_Date_Time">Get Write Date and Time</a></li>
<li><a href="#Get_File_Attributes">Get File Attributes</a></li>
<li><a href="#Setting_File_Date_Time">Setting File Date and Time</a></li>
</ol>
</ol>
<li><a href="#Download">Download SD Card Library</a></li>
</ol>
<div id="SD_Card_Pins" style="text-align: left;">
<br>
</div>
<h1 id="SD_Card_Pins" style="text-align: left;">SD Card Pinout</h1>
<p style="text-align: left;">
<span style="color: #444444;">Both MMC/SD </span><span style="color: #444444;">standards have their own proprietary protocols but they also support SPI
which can be selected during card initialization. Since microcontrollers
have SPI integrated hardware, this is the most used interface for memory
cards.</span>
</p>
<p style="text-align: left;"><span></span><span></span></p>
<a href="https://www.programming-electronics-diy.xyz/2022/07/sd-memory-card-library-for-avr.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com12tag:blogger.com,1999:blog-3851144877322223496.post-1153266175015657342022-05-12T06:15:00.019+03:002024-02-07T03:57:06.172+02:00Displaying Bitmap images on ST7735 display from a memory card using an AVR microcontroller<p>
This is an extension for the
<a
href="https://www.programming-electronics-diy.xyz/2022/02/library-for-st7735-tft-display-driver.html"
target="_blank"
>ST7735 display driver library</a
>
made for displaying Bitmap images stored on an SD memory card on the ST7735
TFT display using an AVR microcontroller. The Bitmaps can be of 16-bit or
24-bit colors. Since the ST7735 display driver only accepts 16-bit colors, the
24-bit images will be converted to 16-bits.<br>
</p>
<p>Here is a short demo video:</p>
<div class="separator" style="clear: both; text-align: center;">
<iframe
allowfullscreen=""
class="BLOG_video_class"
height="480"
src="https://www.youtube.com/embed/pXcLA68oYNI"
width="270"
youtube-src-id="pXcLA68oYNI"
></iframe>
</div>
<br>
<p>
The images must be resized first to fit the size of your display. A free and
popular software for this task is Irfanview. Open the image using Irfanview
then press CTRL+R, specify the new image size then save it as a .bmp and place
the images on the memory card in a folder of your choice.
</p>
<span></span><a href="https://www.programming-electronics-diy.xyz/2022/05/displaying-bitmap-images-on-st7735.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-56617139679149470682022-02-05T06:28:00.098+02:002024-02-07T02:42:15.898+02:00Library for ST7735 TFT display driver for AVR microcontrollers<p>
There are already some libraries for the ST7735 display driver such as the one
from Adafruit but made only for Arduino. I needed one for AVR microcontrollers
in general, so I made this one. With a bit of twiking it could also be adapted
to work with other types of microcontrollers. This library is based on the one
from Adafruit but with some functions removed, modified and others added. The
interface needs 4 microcontroller pins and it uses SPI for communication.
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/a/AVvXsEhNhLik4v9-qtR4nLnegq2MMMgyUSMBpM7QOs5s6cySqmCdzyqXcLAiA9ebJpnbOS6C-H0LYWlbRAX55grwLfTg92I8PpNivvDdFxXIROAg7xHzw_NH5JWyFxu3ODQu1AnvrZ024cwcyrLOiNbvUunqo26GXY0nv5ZmcWOC3u7UgvbflPWLLYAsmTF5=s440" style="margin-left: 1em; margin-right: 1em;"><img alt="Library for the ST7735 TFT display driver for AVR microcontrollers" border="0" data-original-height="346" data-original-width="440" height="252" src="https://blogger.googleusercontent.com/img/a/AVvXsEhNhLik4v9-qtR4nLnegq2MMMgyUSMBpM7QOs5s6cySqmCdzyqXcLAiA9ebJpnbOS6C-H0LYWlbRAX55grwLfTg92I8PpNivvDdFxXIROAg7xHzw_NH5JWyFxu3ODQu1AnvrZ024cwcyrLOiNbvUunqo26GXY0nv5ZmcWOC3u7UgvbflPWLLYAsmTF5=w320-h252" title="Library for the ST7735 TFT display driver for AVR microcontrollers" width="320"></a>
</div>
<br>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEg9BuzJzKukl4kfNsKV3ccO91TDZ6TXGc6sc0gYopZbJXzNQEf5WsNOJo-S51rOXNYC_t3Wtoi2g_rHl8eV0avprrEVIxw92tSMGY3iV3JPJouCLsdfSbUK9AtPD-1N7OAPvzFJCmE3FEaaORbpFH_Xswp5JuoiTkiiK_mRlcZObK8i_Vzn9aP1fny0=s3417" style="margin-left: 1em; margin-right: 1em;"><img alt="Library for the ST7735 TFT display driver for AVR microcontrollers demo" border="0" data-original-height="2210" data-original-width="3417" height="414" src="https://blogger.googleusercontent.com/img/a/AVvXsEg9BuzJzKukl4kfNsKV3ccO91TDZ6TXGc6sc0gYopZbJXzNQEf5WsNOJo-S51rOXNYC_t3Wtoi2g_rHl8eV0avprrEVIxw92tSMGY3iV3JPJouCLsdfSbUK9AtPD-1N7OAPvzFJCmE3FEaaORbpFH_Xswp5JuoiTkiiK_mRlcZObK8i_Vzn9aP1fny0=w640-h414" title="Library for the ST7735 TFT display driver for AVR microcontrollers demo" width="640"></a></div><p></p><p>There are many GUI elements that can be created and above are a few examples such as: selectable menus, scrollbars, check-boxes and animated icons. I made those in Inkscape for a battery charger project.<br></p>
<h4 style="text-align: left;">Contents:</h4>
<ul>
<li><a href="#ST7735wiring">Wiring the ST7735 TFT Display</a></li>
<li><a href="#ST7735Library">ST7735 Library</a></li>
<ul>
<li><a href="#InitFunction">Initialization function and types of ST7735 modules</a></li>
<li><a href="#TheColors">The Colors</a></li>
<li><a href="#CoordinateSystem">The Cursor and the Coordinate System</a></li>
<li><a href="#ClearDisplay">Clearing the display</a></li>
<li><a href="#TextFunctions">Text Functions</a></li>
<ul>
<li><a href="#FontFamily">Setting the font family</a></li>
<li><a href="#BoldFont">Making the font bold</a></li>
<li><a href="#CustomFonts">Creating your own custom fonts</a></li>
<li><a href="#DisplayText">Display Text</a></li>
<li><a href="#DisplayChar">Display a single character</a></li>
<li><a href="#FontSize">Set the font size</a></li>
<li><a href="#FontColor">Set the font color</a></li>
</ul>
<li><a href="#DisplayNumbers">Display Numbers</a></li>
<li><a href="#AlignElements">Aligning Elements on Screen</a></li>
<ul>
<li><a href="#Textalignment">Text alignment</a></li>
<li><a href="#Textboundaries">Set text boundaries</a></li>
<li><a href="#NewLine">Print text on the next line</a></li>
<li><a href="#Gettextwidth">Get text width</a></li>
<li><a href="#Gettextheight">Get text height</a></li>
<li><a href="#Getlinepos">Get position of a line of text</a></li>
<li><a href="#Getcursorposition">Get cursor position</a></li>
<li><a href="#Setcursorposition">Set cursor position</a></li>
<li><a href="#Getdisplaywidthandheight">Get display width and height</a></li>
<li><a href="#Getlastpixelcoordinate">Get last pixel coordinate</a></li>
</ul>
<li><a href="#PrimitiveShapes">Drawing Primitive Shapes</a></li>
<ul>
<li><a href="#Drawingpixels">Drawing pixels</a></li>
<li><a href="#Lines">Lines</a></li>
<li><a href="#Rectangles">Rectangles</a></li>
<li><a href="#Circles">Circles</a></li>
<li><a href="#Triangles">Triangles</a></li>
<li><a href="#Linethickness">Line thickness</a></li>
</ul>
<li><a href="#Bitmaps">Bitmap Images and Icons</a></li>
<ul>
<li><a href="#RGBBitmaps">RGB Bitmaps on MCU flash memory</a></li>
<li><a href="#CardImages">Bitmaps images from memory cards</a></li>
<li><a href="#MonochromeBitmaps">Monochrome Bitmaps/Icons</a></li>
<li><a href="#CreatingMonochromeIcons">Creating Monochrome Icons in GIMP for TFT Displays</a></li>
<li><a href="#ArrayofIcons">Array of Icons - Icon Sets</a></li>
</ul>
<li><a href="#ReadingFromST7735">Reading data from ST7735 driver</a></li>
<li><a href="#ExtraFunctions">Extra Functions</a></li>
<ul>
<li><a href="#TurnDisplayOnOff">Turn Display On/Off</a></li>
<li><a href="#IdleMode">Idle Mode</a></li>
<li><a href="#SleepMode">Sleep Mode</a></li>
<li><a href="#BacklightOnOff">Backlight On/Off</a></li>
<li><a href="#RotateDisplay">Rotate the display</a></li>
<li><a href="#InvertColors">Invert colors</a></li>
</ul>
<li><a href="#LibraryConfiguration">Library Configuration</a></li>
<li><a href="#BasicCodeExample">Basic Code Example</a></li>
</ul>
<li><a href="#Download">Download ST7735 Library</a></li>
</ul>
<br>
<h2 id="ST7735wiring" style="text-align: left;">Wiring the ST7735 TFT display</h2>
<p>
It is important to note that the power supply voltage for the ST7735 display
driver is maximum 3.3V and same for the other input pins. Some TFT modules
include a 3.3V regulator and a voltage level shifter in which case it can be
used with 5V microcontrollers, if not some level shifting must be done if the
microcontroller is not powered from 3.3V.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRXGuowXHm4ln3-Ceh78i3TWf8rQ3m7dQ1m9TgSqQkiVY3v5K2M22Ds86x1gcpZ4UVZXJqKjFxsEQkPyUUB8Z9Vxp3d6-DaT239isvFaT70LvOr1igK0uhUmyYXXC2DUZ0C6ty15mC9tjqu38_cE-94CyqzMPEHlIjRlai4wlV1n20ZQeZ9kZSCsEF/s1093/Display%20and%20voltage%20shifter.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="ST7735 connection diagram with voltage level shifter" border="0" data-original-height="634" data-original-width="1093" height="372" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRXGuowXHm4ln3-Ceh78i3TWf8rQ3m7dQ1m9TgSqQkiVY3v5K2M22Ds86x1gcpZ4UVZXJqKjFxsEQkPyUUB8Z9Vxp3d6-DaT239isvFaT70LvOr1igK0uhUmyYXXC2DUZ0C6ty15mC9tjqu38_cE-94CyqzMPEHlIjRlai4wlV1n20ZQeZ9kZSCsEF/w640-h372/Display%20and%20voltage%20shifter.jpg" title="ST7735 connection diagram with voltage level shifter" width="640"></a></div><p></p><p>This schematic is an example that also includes an SD card. Here, MISO is used for DC pin (Data/Command) since the ST7735 doesn't use the MISO in the 1-Wire SPI protocol, and also as MISO (Master In Slave Out) for the SD card. The 74AHC125D can't be used for this pin since is not bi-directional, hence the mosfet level shifter.<br></p>
<h3 style="text-align: left;">
ST7735 connection diagram with voltage level shifters and pinout<br>
</h3>
<p><b>1 - GND </b>- ground power supply</p>
<p><b>2 - VCC</b> - 3.3V power supply (minimum 150mA)</p>
<p><b>3 - SCL</b> - SPI serial clock pin (SCK)<br></p>
<p><b>4 - SDA</b> - SPI serial data input/output (MOSI)</p>
<span></span><a href="https://www.programming-electronics-diy.xyz/2022/02/library-for-st7735-tft-display-driver.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com2tag:blogger.com,1999:blog-3851144877322223496.post-65021866119009424452021-11-09T07:10:00.004+02:002022-01-16T22:42:49.053+02:00Mains short circuit protection, current limiter using incandescent light bulb in series<p>This is a short circuit protection unit that will limit the current when working with mains voltage in case of a short circuit. It is useful for testing repaired equipment and can also be used to test transformers and light bulbs as a bonus. If a short circuit would to occur all that will happen would be to light up the incandescent light bulb thus protecting the equipment and household wiring.<br></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHMBZjst_VkaZ4W0TzwjKgg-jxOEW54QGCoNBKQUa-H15pX4_TPbSW7UqW8OrRzwykVv8rI83Yv_Gzjib7FEkkzsPpN2ZmvblG98UX8NgbLryksHu6X5TMcXRicprhLoeG4F10ORhNVbI/s4520/IMG_2.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="Mains short circuit protection and current limiter using tungsten lamps in series" border="0" data-original-height="2927" data-original-width="4520" height="414" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHMBZjst_VkaZ4W0TzwjKgg-jxOEW54QGCoNBKQUa-H15pX4_TPbSW7UqW8OrRzwykVv8rI83Yv_Gzjib7FEkkzsPpN2ZmvblG98UX8NgbLryksHu6X5TMcXRicprhLoeG4F10ORhNVbI/w640-h414/IMG_2.jpg" title="Mains short circuit protection and current limiter using tungsten lamps in series" width="640"></a></div><br><h2 style="text-align: left;">How to build a mains short circuit protection unit for testing and repairing electrical equipment and transformers<br></h2><p>The working principle of this unit is very simple - an incandescent tungsten light bulb is placed in series with the load. If the current draw by the load is small comparative to the bulb's power, the light bulb will not glow and it's resistance will be low. In case of a short circuit the higher current draw will cause the light bulb to heat up and glow and that will increase the resistance of the tungsten filament therefore limiting the current.</p><p>When cold the incandescent tungsten filament has a low resistance (~25-50 ohms for a 75W bulb). When mains power is applied to it the filament will get glowing hot and the hotter a metal is the more agitated the electrons are, bouncing everywhere, and so creating the effect of a higher resistance when hot versus when cold.</p><span></span><a href="https://www.programming-electronics-diy.xyz/2021/11/mains-short-circuit-protection-current.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com1tag:blogger.com,1999:blog-3851144877322223496.post-33667934648587562422021-10-22T07:10:00.024+03:002023-05-23T01:28:55.107+03:00DAC library for MCP4706, MCP4716, MCP4726 and AVR microcontrollers<p>
This library is intended for the MCP47x6 series of DAC devices that includes
MCP4706 (8-bit DAC), MCP4716 (10-bit DAC) and MCP4726 (12-bit DAC).
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioc3cR3gTY2PtT5MZe7No2aCj7Vbw5iHy66aO2kNkOBc580xWSTpu-3bpAGrAwVC9Sqk3EqczQM8CmZQqYpNK6K58-GLZnChXQ9VqjvAnlMLzUZs0EAErJEosTiIEgaDUx9P9WI3P6u30/s770/MCP47x6+poster.png" style="margin-left: 1em; margin-right: 1em;"><img alt="DAC library for MCP4706, MCP4716, MCP4726 and AVR microcontrollers" border="0" data-original-height="610" data-original-width="770" height="318" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioc3cR3gTY2PtT5MZe7No2aCj7Vbw5iHy66aO2kNkOBc580xWSTpu-3bpAGrAwVC9Sqk3EqczQM8CmZQqYpNK6K58-GLZnChXQ9VqjvAnlMLzUZs0EAErJEosTiIEgaDUx9P9WI3P6u30/w400-h318/MCP47x6+poster.png" title="DAC library for MCP4706, MCP4716, MCP4726 and AVR microcontrollers" width="400"></a>
</div>
<h4 style="text-align: left;">Table of Contents</h4>
<ul style="text-align: left;">
<li><a href="#DAC_pinout" rel="">DAC pinout</a></li>
<li><a href="#DAC_schematic" rel="">DAC schematic</a></li>
<li><a href="#MCP47x6_General_Description" rel="">MCP47x6 General Description</a></li>
<ul>
<li><a href="#Output_Buffer" rel="">Output Buffer</a></li>
<li><a href="#Calculating_output_voltage" rel="">Calculating the DAC output voltage</a></li>
<li><a href="#Step_Voltage" rel="">Resolution/Step Voltage</a></li>
<li><a href="#Power_Down_Operation" rel="">Power-Down Operation</a></li>
<li><a href="#Device_I2C_Address" rel="">Device I2C Address</a></li>
</ul>
<li><a href="#DAC_Library" rel="">DAC Library</a></li>
<ul>
<li><a href="#Code_example" rel="">Code example</a></li>
<li><a href="#Selecting_DAC_device" rel="">Selecting DAC device</a></li>
<li><a href="#Writing_the_volatile_DAC_register" rel="">Writing the volatile DAC register</a></li>
<li><a href="#Write_volatile_configuration_register" rel="">Write volatile configuration register</a></li>
<li><a href="#Write_volatile_and_nonvolatile_memory" rel="">Write volatile and nonvolatile (EEPROM) memory</a></li>
<li><a href="#Read_volatile_and_nonvolatile_memory" rel="">Read volatile and nonvolatile memory</a></li>
<li><a href="#Convert_voltage_to_DAC_value" rel="">Convert voltage to DAC value</a></li>
<li><a href="#General_call" rel="">General call</a></li>
<li><a href="#Resetting_I2C_interface" rel="">Resetting the I2C interface</a></li>
<li><a href="#Download" rel="">Download</a></li>
</ul>
</ul>
<br>
<h2 id="DAC_pinout" style="text-align: left;">MCP47x6 DAC pinout</h2>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBUo4pOOveRY4Y6XxO05TuQzTU642q4B6oO6sJKNpiZjtbdVN3OSIazFo3G6KxNh5GNXAPXUkz5kFKna3RdfmIjRF3IKJ02d104lCFdyGE6XX9AAr9KBQs-GzYw7Srz-PlS6a4mX_Yv-w/s571/MCP47x6+DAC+pinout.jpg" style="margin-left: 1em; margin-right: 1em;"><img alt="MCP47x6 DAC pinout" border="0" data-original-height="339" data-original-width="571" height="238" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBUo4pOOveRY4Y6XxO05TuQzTU642q4B6oO6sJKNpiZjtbdVN3OSIazFo3G6KxNh5GNXAPXUkz5kFKna3RdfmIjRF3IKJ02d104lCFdyGE6XX9AAr9KBQs-GzYw7Srz-PlS6a4mX_Yv-w/w400-h238/MCP47x6+DAC+pinout.jpg" title="MCP47x6 DAC pinout" width="400"></a>
</div>
<br>
<p style="text-align: left;">
1 - <b>VOUT</b>: this is the DAC analog output voltage. In Normal mode, the DC
impedance of the output pin is about 1Ω. In Power-Down mode, the output pin is
internally connected to a known pull-down resistor of 1 kΩ, 125 kΩ, or 640 kΩ.
The VOUT pin can drive up to 100 pF of capacitive load in parallel with a 5 kΩ
resistive load. It is recommended to use a load with RL greater than 5 kΩ.
Driving large capacitive loads can cause stability problems for voltage
feedback op amps.
</p>
<span></span><a href="https://www.programming-electronics-diy.xyz/2021/10/dac-library-for-mcp4706-mcp4716-mcp4726.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-38101831269205752532021-10-19T05:23:00.014+03:002023-06-11T02:25:36.581+03:00I2C and TWI (Two Wire Interface) library for AVR microcontrollers<p>
In the last article I talked about
<a href="https://www.programming-electronics-diy.xyz/2021/10/how-i2c-and-twi-two-wire-interface.html" target="_blank">How I2C and TWI protocol works</a>
and we saw that they are mostly the same so this library works for both I2C
and TWI serial interfaces. You don't have to know every detail about how the
I2C protocol works but I strongly recommend reading the article to
have a general idea about it, and that way it will be easier to use this
library.</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcTfYFCKqeFVWxh7fGCvDfydcaFPfoI9eOBQwErBOSJn5au-jG7zELH9UaeXy5edRETpbVOmunHTuU89kSfgLtkb0EjmEBDKvpmuXq0WK9rbCLULlY1IuC6F0H7IJti8u9pv3lLg4PGto/s770/TWI+poster.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="610" data-original-width="770" height="254" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcTfYFCKqeFVWxh7fGCvDfydcaFPfoI9eOBQwErBOSJn5au-jG7zELH9UaeXy5edRETpbVOmunHTuU89kSfgLtkb0EjmEBDKvpmuXq0WK9rbCLULlY1IuC6F0H7IJti8u9pv3lLg4PGto/s320/TWI+poster.png" width="320"></a></div><br><p></p>
<h2 style="text-align: left;">
Using the I2C, TWI library with an AVR microcontroller <br>
</h2>
<h3 style="text-align: left;">Setting the library file <br></h3>
<p>As always, first include the library file:</p>
<pre style="line-height: 125%; margin: 0px;"><span style="color: #557799;">#include "twi.h"</span></pre>
<p>
Most AVR microcontrollers have two TWI modules TWI0 and TWI1 so to choose
between the two there is the following line of code:
</p>
<pre style="line-height: 125%; margin: 0px;"><span style="color: #557799;">#define TWI_MODULE_NUMBER 0 </span><span style="color: #888888;">// TWI module 0 or 1</span></pre>
<p>The default module is TWI0.</p>
<h3 style="text-align: left;">Functions </h3><br>
<pre style="line-height: 125%; margin: 0px;"><span style="color: #b00040;">void</span> TWI_Init(<span style="color: #b00040;">uint32_t</span> frequency)</pre>
<p>
Used to initialize the TWI module. This will set the TWI bit rate and enable global interrupts. 400kHz is the maximum TWI speed that regular AVR microcontrollers supports although I have managed to talk to a DAC at 577kHz.<br></p><p><i>frequency</i>: can be one of the following constants or any value between 100-400kHz.</p><pre style="line-height: 125%; margin: 0;"><span style="color: #bc7a00;">#define TWI_400KHZ 400000 </span><span style="color: #408080; font-style: italic;">// Hz</span>
<span style="color: #bc7a00;">#define TWI_100KHZ 100000 </span><span style="color: #408080; font-style: italic;">// Hz</span></pre>
<p></p>
<a href="https://www.programming-electronics-diy.xyz/2021/10/i2c-and-twi-two-wire-interface-library.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com0tag:blogger.com,1999:blog-3851144877322223496.post-33980058369830542802021-10-16T07:19:00.017+03:002021-10-22T02:41:25.453+03:00How the I2C and TWI (Two Wire Interface) protocol works<p><b>I<sup>2</sup>C</b> is a two-wire serial bus communication protocol invented by Phillips in 1982. <b>TWI </b>stands for Two Wire Interface and, for the most part, this bus is
identical to I²C. The name TWI was introduced by Atmel and other
companies to avoid conflicts with trademark issues related to I²C. Because these two protocols are almost the same I will refer to them interchangeably throughout the course of this article.</p><p>This protocol is very used nowadays by all sorts of devices such as DACs, LCDs, sensors, etc. so it's worth learning about it. <br></p><p>As a disclaimer I need to mention that most parts of this article contains fragments from the ATmega328, 324 datasheet and MCP4706 datasheet. I liked how they explained the I2C, TWI protocol and since not many people read the datasheets I want to share this information with anyone interested in the TWI protocol.</p><p>If you're interested in a I2C, TWI library it can be found at this link <a href="https://www.programming-electronics-diy.xyz/2021/10/i2c-and-twi-two-wire-interface-library.html" target="_blank">I2C, TWI library</a>.<br></p><h3 style="text-align: left;"><span class="fontstyle0">Features</span>
<br style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></h3>
<ul style="text-align: left;">
<li>7-bit Address Space Allows up to 128 Different Slave Addresses</li>
<li>Multi-master Arbitration Support</li>
<li>General call addressing</li>
</ul>
<p>The I2C interface specifies different communication bit rates. These are referred to as Standard, Fast or HighSpeed modes.</p>
<ul style="text-align: left;">
<li>Standard mode: bit rates up to 100 kbit/s</li>
<li>Fast mode: bit rates up to 400 kbit/s</li>
<li>High-Speed mode (HS mode): bit rates up to 3.4 Mbit/s</li>
</ul>
<p>High-Speed mode is currently unsupported by the TWI.</p>
<h4 style="text-align: left;">Table of Contents</h4>
<ul style="text-align: left;">
<li><a href="#TwoWireSerialInterfaceBus" rel="">Two-Wire Serial Interface Bus</a></li>
<li><a href="#TWITerminology" rel="">TWI Terminology</a></li>
<li><a href="#DataTransferandFrameFormat" rel="">Data Transfer and Frame Format</a></li>
<ul>
<li><a href="#STARTandSTOPConditions">START and STOP Conditions</a></li>
<li><a href="#AddressPacketFormat">Address Packet Format</a></li>
<li><a href="#DataPacketFormat">Data Packet Format</a></li>
<li><a href="#CombiningAddressandDataPackets">Combining Address and Data Packets into a Transmission</a></li>
</ul>
<li><a href="#MultimasterBusSystems">Multi-master Bus Systems, Arbitration, and Synchronization</a></li>
<ul>
<li><a href="#SCLSynchronizationBetweenMasters">SCL Synchronization Between Multiple Masters</a></li>
<li><a href="#ArbitrationBetweenTwoMasters">Arbitration Between Two Masters </a></li>
</ul>
<li><a href="#PracticalI2Cexample1">Practical I2C example 1: Writing data to MCP4706 DAC device</a></li>
<li><a href="#PracticalI2Cexample2">Practical I2C example 2: Reading data from MCP4706 DAC device</a><br></li>
</ul>
<p id="TwoWireSerialInterfaceBus" style="text-align: left;"> </p><h2 id="TwoWireSerialInterfaceBus" style="text-align: left;">Two-Wire Serial Interface Bus <br></h2>
<p>The TWI protocol is able to interconnect up to 128 different devices using only two bidirectional bus lines: one for clock (<b>SCL</b>) and one for data (<b>SDA</b>). The only external hardware needed to implement the bus is a single pull-up resistor for each of the TWI bus lines. All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are inherent in the TWI protocol.</p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBR1xfgnazLMfPRxaniy5ftkV42b5Oy9LyC6c3arSzGdrQ8iH5Cb6qJbmPgOeb6lhvFVz3Cu0uc9rrhKIe4N-1HbZbdkWz24B_OETOCgl3WNbOwzYajSb8aeJCm5wNRhqmCPSoAwg7vko/s1018/TWI+Bus+Interconnection.jpg" style="margin-left: auto; margin-right: auto;"><img alt="TWI Bus Interconnection" border="0" data-original-height="448" data-original-width="1018" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBR1xfgnazLMfPRxaniy5ftkV42b5Oy9LyC6c3arSzGdrQ8iH5Cb6qJbmPgOeb6lhvFVz3Cu0uc9rrhKIe4N-1HbZbdkWz24B_OETOCgl3WNbOwzYajSb8aeJCm5wNRhqmCPSoAwg7vko/w400-h176/TWI+Bus+Interconnection.jpg" title="TWI Bus Interconnection" width="400"></a></td></tr><tr><td class="tr-caption" style="text-align: center;"><i><span class="fontstyle0">TWI Bus Interconnection</span></i>
<br style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></td></tr></tbody></table><p></p><p>The number of devices that can be connected to the bus is only limited by the bus capacitance limit of 400pF and the 7-bit slave address space. The SCL and SDA pins are open-drain configurations. For this reason these pins require a pull-up resistor.</p>
<a href="https://www.programming-electronics-diy.xyz/2021/10/how-i2c-and-twi-two-wire-interface.html#more"></a>Liviu Istratehttp://www.blogger.com/profile/15953813368190792312noreply@blogger.com1