Christos Margiolis: embedded Christos Margiolis http://margiolis.net/tags/embedded/ Thermometer using PIC16F877A and BME280 http://margiolis.net/w/pic_therm/ Mon, 14 Feb 2022 00:00:00 +1200 <p>This embedded system measures temperature and humidity and outputs the data on an LCD. The BME280 sensor can also do pressure but I didn&rsquo;t implement it because the MCU had no more space available. <a href="https://git.sr.ht/~crm/pic_therm">The whole project can be found here</a>; I&rsquo;ve also included datasheets for the MCU, sensor and LCD.</p> <p>Development was done on FreeBSD using <a href="http://margiolis.net/w/pic_freebsd">the workflow I describe here</a>. If you&rsquo;re using Microchip&rsquo;s own tools, you&rsquo;ll have to make some small changes to the code, but nothing too extreme.</p> <p><a href="https://git.sr.ht/~crm/pic_therm/tree/master/item/src">The source code</a>.</p> <p><img src="http://margiolis.net/files/pic_therm_main.jpg" alt=""></p> <h2 id="components">Components</h2> <ul> <li>Microchip PIC16F877A-I/P microcontroller.</li> <li>Adafruit BME280 temperature, humidity and pressure sensor.</li> <li>16x2 LCD.</li> <li>1x 16MHz crystal oscillator.</li> <li>2x 10kΩ resistor.</li> <li>2x 300Ω resistor.</li> <li>1x 10kΩ potentiometer.</li> <li>2x 22pF ceramic capacitor.</li> <li>2x LED.</li> <li>2x push-button.</li> <li>Breadboard and wires.</li> <li>3x AAA batteries (4.5V total) or a 9V battery with a 5V voltage divider.</li> </ul> <h2 id="safe-temperature-range">Safe temperature range</h2> <table border="solid"> <tr> <th>Component</th> <th>Operating temperature</th> </tr> <tr> <td>PIC16F877A</td> <td>-40°C - 85°C</td> </tr> <tr> <td>BME280</td> <td>-40°C - 85°C</td> </tr> <tr> <td>LCD</td> <td>-20°C - 70°C</td> </tr> </table> <p>So, it&rsquo;s best for the system to operate in the -20°C to 70°C range.</p> <h2 id="schematic">Schematic</h2> <p>You can also get the <a href="https://git.sr.ht/~crm/pic_therm/tree/master/item/schem/pic.pdf">PDF version</a>.</p> <p><img src="http://margiolis.net/files/pic_therm_schem.png" alt=""></p> PIC microcontroller development on FreeBSD http://margiolis.net/w/pic_freebsd/ Sun, 23 Jan 2022 00:00:00 +1200 <p>Tested on FreeBSD 13.0. This article has also been mirrored on the <a href="https://wiki.freebsd.org/Microcontrollers/PIC">FreeBSD Wiki</a>.</p> <h2 id="prerequisites">Prerequisites</h2> <p><a href="http://sdcc.sourceforge.net/">sdcc</a> is a C compiler for microprocessors. It says that PIC microprocessors are unmaintained, but I&rsquo;ve found it to be pretty reliable so far (take this with a grain of salt, I&rsquo;m no expert). The port can be found under:</p> <pre tabindex="0"><code>lang/sdcc </code></pre><p>Page 75 of the <a href="http://sdcc.sourceforge.net/doc/sdccman.pdf">sdcc user manual</a> lists the supported PIC devices. Header files can be found under <code>/usr/local/share/sdcc</code>.</p> <p>For programming the MCU, I&rsquo;ve found pk2cmd to work alright with PICKit2 (or Chinese clones), but there&rsquo;s no port for FreeBSD anymore. The Makefile won&rsquo;t install files properly, so we have some extra work to do afterwards:</p> <pre tabindex="0"><code>$ git clone https://github.com/psmay/pk2cmd.git $ cd pk2cmd/pk2cmd # gmake freebsd install clean # mv /usr/share/pk2/PK2DeviceFile.dat /usr/local/bin # rm -rf /usr/share/pk2 </code></pre><p>Supported devices for pk2cmd are listed <a href="https://github.com/psmay/pk2cmd/blob/master/pk2cmd/ReadmeForPK2CMDLinux2-6.txt">here</a>.</p> <h2 id="detecting-and-programming-the-mcu">Detecting and programming the MCU</h2> <p>Avoid using just the -P option to auto-detect the MCU, as the VPP the PICKit2 applies to the chip trying to detect it can damage the MCU. Instead, use the chip number beforehand as shown below. Also, use the -C option to check if the chip is blank.</p> <p>If any of the following pk2cmd commands fail, make sure everything <em>really is</em> wired properly:</p> <pre tabindex="0"><code>$ pk2cmd -P PIC16F877A -C Device is blank Operation Succeeded </code></pre><p>Compile your source code. The target executable is the <code>.hex</code> file sdcc will output. Replace <code>pic14</code> and <code>16f877a</code> with the appropriate names for your device:</p> <pre tabindex="0"><code>$ sdcc --use-non-free -mpic14 -p16f877a main.c </code></pre><p>Erase the PIC (if it wasn&rsquo;t already blank) and flash the new code. Again, use the appropriate names:</p> <pre tabindex="0"><code>$ pk2cmd -P PIC16F877A -E $ pk2cmd -P PIC16F877A -X -M -F main.hex </code></pre><p>If all went well, you should get an output similar to this:</p> <pre tabindex="0"><code>PICkit 2 Program Report 23-1-2022, 21:01:29 Device Type: PIC16F877A Program Succeeded. Operation Succeeded </code></pre>