{"id":3965,"date":"2017-01-30T07:52:16","date_gmt":"2017-01-30T05:52:16","guid":{"rendered":"http:\/\/heiniger-net.blog-net.ch\/?p=3965"},"modified":"2017-01-14T09:35:16","modified_gmt":"2017-01-14T07:35:16","slug":"lava-lamp-project-part-2-firmware","status":"publish","type":"post","link":"https:\/\/heiniger-net.blog-net.ch\/en\/archives\/3965\/lava-lamp-project-part-2-firmware\/","title":{"rendered":"Lava lamp project, part 2 \u2014 firmware"},"content":{"rendered":"<p>In <a href=\"\/archives\/3930\/lavalamp-project-1\/\">part 1<\/a>, I described the hard\u00adware of my project of a com\u00adput\u00ader con\u00adtrol\u00adlable lava lamp. In this sec\u00adond part, I want to dis\u00adcuss the nec\u00ades\u00adsary firmware, which is the soft\u00adware nec\u00ades\u00adsary to teach the <a href=\"http:\/\/www.picaxe.com\/What-Is-PICAXE\" target=\"_blank\">Picaxe Micro\u00adcon\u00adtroller<\/a>&nbsp;the desired functionality.<\/p>\n<p>We need to keep two areas in&nbsp;focus:<\/p>\n<ol>\n<li>We need to be able to read the desired col\u00ador val\u00adues from the <span class=\"caps\">USB<\/span> ser\u00adi\u00adal port, defin\u00ading the desired inten\u00adsi\u00adties of the col\u00adors green, red and&nbsp;blue.<\/li>\n<li>These desired val\u00adues have to be turned into actu\u00adal light in desired col\u00ador and inten\u00adsi\u00adty. This means, the LEDs have to be turned on.<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class><span class=\"eztoc-hide\" style=\"display:none;\">Tog\u00adgle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\" \/><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\" \/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1 \"><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/heiniger-net.blog-net.ch\/en\/archives\/3965\/lava-lamp-project-part-2-firmware\/#A_few_words_about_the_Picaxe\">A few words about the Picaxe<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/heiniger-net.blog-net.ch\/en\/archives\/3965\/lava-lamp-project-part-2-firmware\/#Functional_principles_of_the_firmware\">Func\u00adtion\u00adal prin\u00adci\u00adples of the firmware<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/heiniger-net.blog-net.ch\/en\/archives\/3965\/lava-lamp-project-part-2-firmware\/#Possible_improvements\">Pos\u00adsi\u00adble improvements<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/heiniger-net.blog-net.ch\/en\/archives\/3965\/lava-lamp-project-part-2-firmware\/#Listing\">List\u00ading<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"A_few_words_about_the_Picaxe\"><\/span>A few words about the Picaxe<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The Picaxe has a pre\u00adde\u00adfined set of com\u00admands, which is sim\u00adi\u00adlar to <span class=\"caps\">BASIC<\/span> and which is quite pow\u00ader\u00adful. For exam\u00adple, a sin\u00adgle com\u00adman\u00adnd can read in a row of input data assign them to mem\u00ado\u00adry. But the proces\u00adsor speed is lim\u00adit\u00aded. This is under\u00adstand\u00adable, greater work\u00ading speeds would mean more heat dis\u00adsi\u00adpa\u00adtion and then the micro\u00adcon\u00adtroller would need cool\u00ading. This would sig\u00adnif\u00adi\u00adcant\u00adly increase the effort required for design\u00ading a device with this kind of processor.<\/p>\n<p>The Picaxe is avail\u00adable in dif\u00adfer\u00adent ver\u00adsions, which dif\u00adfer in pack\u00adage size and spe\u00adcial equip\u00adment, such as the num\u00adber of digital\/analog con\u00advert\u00aders or pulse width mod\u00adu\u00adla\u00adtors. The small\u00adest mod\u00adel is avail\u00adable as a 8 pin dual-in-line chip and sports the fol\u00adlow\u00ading pin&nbsp;out:<\/p>\n<p><a href=\"\/files\/2017\/01\/pinout08m2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3968 size-large aligncenter\" src=\"\/files\/2017\/01\/pinout08m2-1024x185.jpg\" width=\"1024\" height=\"185\" srcset=\"https:\/\/heiniger-net.blog-net.ch\/files\/2017\/01\/pinout08m2-1024x185.jpg 1024w, https:\/\/heiniger-net.blog-net.ch\/files\/2017\/01\/pinout08m2-300x54.jpg 300w, https:\/\/heiniger-net.blog-net.ch\/files\/2017\/01\/pinout08m2.jpg 1544w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><\/a><\/p>\n<ul>\n<li>2 pins for <span class=\"caps\">5V<\/span> and ground (pow\u00ader supply)<\/li>\n<li>2 pins for ser\u00adi\u00adal input and out\u00adput. These pins are being used for pro\u00adgram\u00adming and lat\u00ader for trans\u00admis\u00adsion of the desired col\u00ador val\u00adues. They could also be used as nor\u00admal input or out\u00adput pins if desired.<\/li>\n<li>1 dig\u00adi\u00adtal&nbsp;input<\/li>\n<li>3 very ver\u00adsa\u00adtile input\/output pins with numer\u00adous spe\u00adcial functions<\/li>\n<\/ul>\n<p><a href=\"http:\/\/heiniger-net.blog-net.ch\/files\/2017\/01\/picaxe-prototype-board.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-medium wp-image-3976\" src=\"http:\/\/heiniger-net.blog-net.ch\/files\/2017\/01\/picaxe-prototype-board-300x225.jpg\" alt width=\"300\" height=\"225\" srcset=\"https:\/\/heiniger-net.blog-net.ch\/files\/2017\/01\/picaxe-prototype-board-300x225.jpg 300w, https:\/\/heiniger-net.blog-net.ch\/files\/2017\/01\/picaxe-prototype-board.jpg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\"><\/a><\/p>\n<p>Here is the&nbsp;<a href=\"http:\/\/www.picaxe.com\/docs\/picaxem2.pdf\" target=\"_blank\">data sheet for the <span class=\"caps\">PICAXE<\/span><\/a>.&nbsp;<\/p>\n<p>The Picaxe has an inte\u00adgrat\u00aded devel\u00adop\u00adment envi\u00adron\u00adment (<span class=\"caps\">IDE<\/span>), called \u201c<a href=\"http:\/\/www.picaxe.com\/Software\/PICAXE\/PICAXE-Editor-6\/#download\" target=\"_blank\">Picaxe Edi\u00adtor 6<\/a>\u2033, which can be used for devel\u00adop\u00ading, sim\u00adu\u00adlat\u00ading and trans\u00adfer\u00adring pro\u00adgrams to the Picaxe. There are cost effec\u00adtive pro\u00adto\u00adtyp\u00ading boards avail\u00adable as well. I bought myself&nbsp;<a href=\"http:\/\/www.picaxe.com\/Hardware\/Project-Boards\/PICAXE-08-Proto-Board\/\" target=\"_blank\">his board<\/a>&nbsp;for my first attempts.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Functional_principles_of_the_firmware\"><\/span>Functional principles of the firmware<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The light\u00ading inten\u00adsi\u00adty of LEDs is not con\u00adtrolled by vary\u00ading the pow\u00ader sup\u00adply volt\u00adage (which could be done using a ana\u00adlog out\u00adput), as is done for light bulbs. Instead it is done by vary\u00ading the width of puls\u00ades at an appro\u00adpri\u00adate fre\u00adquen\u00adcy fast enough so that the flick\u00ader is not noticed by the human eye. The small Picaxe-08M2 used by me has one pulse-width-mod\u00adu\u00adla\u00adtion (pwm) out\u00adput, as can be seen in the above pin out. But that is only enough for one of the col\u00adors red, green or blue, not for all three of them. Only the sig\u00adnif\u00adi\u00adcant\u00adly larg\u00ader mod\u00adels <span class=\"caps\">14M2<\/span> or <span class=\"caps\">20M2<\/span> would have 3, or even 4, pwm out\u00adputs. For this rea\u00adson, I have to pro\u00adgram my own pwm out\u00adput man\u00adu\u00adal\u00adly, which, alas, can only be done with lim\u00adit\u00aded speed, which means that my <span class=\"caps\">LED<\/span> flick\u00aders slight\u00adly. That is a pity, but it can not be avoid\u00aded with\u00adin the lim\u00adit\u00aded space. The larg\u00ader Picaxe chips would require more space not avail\u00adable inside the lava&nbsp;lamp.<\/p>\n<p>To use the pro\u00adcess\u00ading pow\u00ader of the Picaxe as effec\u00adtive as pos\u00adsi\u00adble, I built my firmware like&nbsp;this:<\/p>\n<p>One big loop is repeat\u00ading for ever. Inside this loop, the fol\u00adlow\u00ading things are happening:<\/p>\n<ol>\n<li>First, all three col\u00ador LEDs are turned on, if their cor\u00adre\u00adpond\u00ading num\u00adber of duty cycles is greater than&nbsp;zero.<\/li>\n<li>In an inside loop, which is always per\u00adformed the same num\u00adber of&nbsp;times<\/li>\n<li>\u2026each col\u00ador, whose duty cycle expired, is being turned off.<\/li>\n<li>\u2026also, we wait 3 Mil\u00adlisec\u00adonds for the begin\u00adning of an input datagram<\/li>\n<li>If a char\u00adac\u00adter \u201cC\u201d is rec\u00adog\u00adnized, we jump to a sub\u00adpro\u00adgram, which reads the rest of the data\u00adgram and recal\u00adcu\u00adlates and adjusts the duty cycles of each&nbsp;color.<\/li>\n<li>After the num\u00adber of inside loop cycles is reached, we jump back to the out\u00ader loop and the whole cycle starts from scratch.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>The length of an out\u00adside loop cycle deter\u00admines the amount of flick\u00ader\u00ading of the LEDs. The short\u00ader one out\u00adside loop cycle, the less flickering.<\/p>\n<p>The inside loop cycle length is deter\u00admined by the ser\u00adi\u00adal input wait\u00ading time, which has to be per\u00adformed reg\u00adu\u00adlar\u00adly, in order to read desired col\u00ador changes trans\u00admit\u00adted from the con\u00adnect\u00aded com\u00adput\u00ader. The longer this wait\u00ading time, the more reli\u00adably data\u00adgrams will be rec\u00adog\u00adnized, but the more flick\u00ader\u00ading occurs as well. I fig\u00adured 3 mil\u00adlisec\u00adonds should be quite ok. If dur\u00ading this time, a char\u00adac\u00adter \u201cC\u201d is noticed, we quick\u00adly jump out of the inside loop and per\u00adform a subp\u00adgram to read three more char\u00adac\u00adters, whose val\u00adue defines the bright\u00adness of the col\u00adors red, green and blue, respec\u00adtive\u00adly. The pos\u00adsi\u00adble bright\u00adness val\u00adues go from 0 (\u201ccon\u00adstant off\u201d) to n+1 (\u201ccon\u00adstant&nbsp;on\u201d).<\/p>\n<p>The num\u00adber of inner cycles defines the \u201cflick\u00ader fre\u00adquen\u00adcy\u201d of the LEDs, inverse\u00adly pro\u00adpor\u00adtion\u00adal. This means: The less the LEDs are sup\u00adposed to flick\u00ader, the less times the inner loop can be per\u00adformed, and the less num\u00adber of bright\u00adness lev\u00adels are pos\u00adsi\u00adble. The more lev\u00adels of bright\u00adness are desired, the more the LEDs will flick\u00ader. I thought that 7 inner cycles make a use\u00adful compromise.<\/p>\n<p>The ser\u00adi\u00adal port receives the desired col\u00ador and bright\u00adness through a \u201cdata\u00adgram\u201d con\u00adsist\u00ading of four bytes of data. Every data\u00adgram starts with a \u201cC\u201d, and the fol\u00adlow\u00ading three dig\u00adits define the num\u00adber of inner cycles for the col\u00adors red, green and blue. Here are a few examples:<\/p>\n<ul>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C000<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is com\u00adplete\u00adly turned off, or&nbsp;dark<\/li>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C888<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is com\u00adplete\u00adly turned on, or&nbsp;white<\/li>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C800<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is&nbsp;rot<\/li>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C080<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is&nbsp;green<\/li>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C008<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is&nbsp;blue<\/li>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C440<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is yel\u00adlow with about half intensity<\/li>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C808<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is pur\u00adple with full&nbsp;power<\/li>\n<li><span class=\"quo\">\u2018<\/span><span class=\"caps\">C740<\/span>\u2019 \u2014 <span class=\"caps\">LED<\/span> is&nbsp;brown<\/li>\n<li>etc.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Unfor\u00adtu\u00adnate\u00adly, the ser\u00adi\u00adal receiv\u00ader is not depend\u00adable enough to receive every data\u00adgram clean\u00adly. Most of the time, the com\u00adput\u00ader has to send the data\u00adgram mul\u00adti\u00adple times until it is received. An acknowl\u00adedge\u00adment is not cur\u00adrent\u00adly imple\u00adment\u00aded in my&nbsp;code.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Possible_improvements\"><\/span>Possible improvements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The code could be made more flex\u00adi\u00adble, for exam\u00adple by mak\u00ading the num\u00adber of inside loop cycles vari\u00adable. With this, the com\u00adput\u00ader could reduce the flick\u00ader, if only a few bright\u00adness lev\u00adels are required.<\/p>\n<p>An ack\u00adowledg\u00adment mech\u00ada\u00adnism for sig\u00adnalling suc\u00adcess\u00adful recep\u00adtion of a data\u00adgram to the com\u00adput\u00ader would be use\u00adful so that it knows it can stop repeat\u00ading its last datagram.<\/p>\n<p>Also, it would be nice to make the switch or key do some\u00adthing, such as turn\u00ading the <span class=\"caps\">LED<\/span> on or off, or start\u00ading a spe\u00adcial col\u00ador pro\u00adgram on the&nbsp;<span class=\"caps\">LED<\/span>.<\/p>\n<p>You can, of course, also exper\u00adi\u00adment with the adjustments:<\/p>\n<ul>\n<li>Clock fre\u00adquen\u00adcy of the proces\u00adsor. Cur\u00adrent\u00adly set to 16 MHz. The faster, the less flick\u00ader. 16 MHz is the max\u00adi\u00admum fre\u00adquen\u00adcy pos\u00adsi\u00adble for the Picaxe <span class=\"caps\">08M2<\/span>.<\/li>\n<li>Num\u00adber of inner loop cycles for one work\u00ading cycle. Cur\u00adrent\u00adly 7. The less cycles, the less flick\u00ader. The more inner cycles, the more lev\u00adels of bright\u00adness are available.<\/li>\n<li>Wait\u00ading time for begin of data\u00adgram. Cur\u00adrent\u00adly 3ms. The longer this time the more reli\u00adably data\u00adgrams will be rec\u00adog\u00adnized, but the greater the flicker.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Listing\"><\/span>Listing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>I don\u2019t want to list the whole firmware pro\u00adgram here. Just&nbsp;<a href=\"\/about\/contact\/\">ask me<\/a>, if you are interested<\/p>","protected":false},"excerpt":{"rendered":"<p>Sor\u00adry, this entry is only avail\u00adable in Deutsch.<\/p>\n <a class=\"more-link\" href=\"https:\/\/heiniger-net.blog-net.ch\/en\/archives\/3965\/lava-lamp-project-part-2-firmware\/\"><span class=\"more-msg\">Con\u00adtin\u00adue reading&nbsp;\u2192<\/span><\/a>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wp_typography_post_enhancements_disabled":false,"iawp_total_views":17,"footnotes":""},"categories":[11,13],"tags":[],"class_list":["post-3965","post","type-post","status-publish","format-standard","hentry","category-computer","category-home-stories"],"_links":{"self":[{"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/posts\/3965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/comments?post=3965"}],"version-history":[{"count":2,"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/posts\/3965\/revisions"}],"predecessor-version":[{"id":3990,"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/posts\/3965\/revisions\/3990"}],"wp:attachment":[{"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/media?parent=3965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/categories?post=3965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heiniger-net.blog-net.ch\/en\/wp-json\/wp\/v2\/tags?post=3965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}