{"id":2320,"date":"2017-06-16T17:28:42","date_gmt":"2017-06-16T17:28:42","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=2320"},"modified":"2017-06-28T19:37:51","modified_gmt":"2017-06-28T19:37:51","slug":"just-making-it-revisiting-my-cube","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/06\/16\/just-making-it-revisiting-my-cube\/","title":{"rendered":"just making it &#8211; revisiting my cube"},"content":{"rendered":"<p>Some time back, I had a fair amount lot of free time.\u00a0 I spent the time working with a friend Mikhail in the evenings to create my own\u00a0<a href=\"http:\/\/blog.paranoidprofessor.com\/index.php\/2016\/09\/24\/just-making-it-a-4x4x4-led-cube\/\">LED cube from scratch<\/a>.\u00a0 It was actually quite a bit of fun, the electronics was not overly complicated with most of it being done by a <a href=\"https:\/\/www.raspberrypi.org\/\">Raspberry Pi<\/a>.\u00a0It was fun to see both the hardware and the software side by side.<\/p>\n<div id=\"attachment_2350\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/pi-powered-cube.jpg\"><img aria-describedby=\"caption-attachment-2350\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2350\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/pi-powered-cube-1024x899.jpg\" alt=\"\" width=\"640\" height=\"562\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/pi-powered-cube-1024x899.jpg 1024w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/pi-powered-cube-300x263.jpg 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/pi-powered-cube-768x674.jpg 768w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-2350\" class=\"wp-caption-text\">raspberry pi powered cube<\/p><\/div>\n<p>The problem with that particular cube was using the Raspberry pi. \u00a0The Raspberry pi is a great platform but it is a computer and it needs time to start up the operating system and more importantly it needs to be shutdown in an organized manner.<\/p>\n<p>The Raspberry pi is an awesome little computer but the startup and shutdown times are my main problem. \u00a0I only have a few hundred lines of code that needs to be run so I don&#8217;t need a four core 1.2 gHz computer, only need a few megaherz.<\/p>\n<h2>Arduino to the rescue<\/h2>\n<p>The first release of the Raspberry pi was in 2008 at 900 mHz for 35 dollars was truly amazing but it wasn&#8217;t the first of this type of hardware for do-it-yourself electronic projects.<\/p>\n<p>I actually was never involved in the DIY electronics when the Arduino came out but I think that the Arduino was pretty much the pioneer of this space. \u00a0The <a href=\"http:\/\/www.arduino.org\/\" target=\"_blank\">Arduino<\/a> was not only hardware but it was open source hardware. \u00a0The fact that it was open source helped it to expand and multiply into the number of models that we see today. \u00a0Well, being open sourced helped but also due to the release of ever increasing <a href=\"https:\/\/en.wikipedia.org\/wiki\/Atmel_AVR_ATtiny_comparison_chart\" target=\"_blank\">sophisticated processors<\/a> from <a href=\"https:\/\/en.wikipedia.org\/wiki\/Atmel\" target=\"_blank\">ATMEL<\/a>\u00a0which powered these Arduinos.<\/p>\n<p>Today it is\u00a0possible to get what is essentially little computer that is not much larger than a couple of postage stamps.<\/p>\n<p><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmega328-unassembled.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-2325\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmega328-unassembled-218x300.jpg\" alt=\"\" width=\"218\" height=\"300\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmega328-unassembled-218x300.jpg 218w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmega328-unassembled-768x1056.jpg 768w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/atmega328-unassembled-745x1024.jpg 745w\" sizes=\"(max-width: 218px) 100vw, 218px\" \/><\/a><\/p>\n<p>This &#8220;advanced&#8221; processor is running 20 MHz and contains 32 KB of program memory. Not quite the latest gaming personal computer\u00a0but it is similar to the original <a href=\"https:\/\/en.wikipedia.org\/wiki\/Apple_II\" target=\"_blank\">Apple ][ personal computer<\/a>, but it is more than enough to pass the commands to the cube to switch leds on and off using the I2C protocol.<\/p>\n<h2>New Hardware<\/h2>\n<p>The Raspberry\u00a0Pi supports I2C but the Atmega328 does as well. \u00a0The I2C protocol is just a matter of two lines &#8211; a data line and a clock line. \u00a0As far as the hardware was concerned, all I needed to do was to connect the data, clock, power and ground to the Arduino atmega328.<\/p>\n<p>On the software side, I was pleasantly surprised at actually how easy\u00a0it was using the Arduino libraries to do I2C and to convert my code over.<\/p>\n<p>Sure, I did have to make a number of small changes but the process was pretty painless. \u00a0Replace my printf statements with Serial.println statements but the main changes were connected with the I2C protocol.<\/p>\n<p>The libraries to do this were part of the standard development environment. \u00a0The class that you need to use is the Wire class.<\/p>\n<p>Old Code<\/p>\n<pre>   bcm2835_i2c_setSlaveAddress(cathode);\r\n   char cmd2[] = { IODIRA, 0x00, 0x00 };\r\n   check_retcode(bcm2835_i2c_write(cmd2,sizeof(cmd2)));<\/pre>\n<p>New code<\/p>\n<pre>   char cmd2[] = { IODIRA, 0x00, 0x00 };\r\n   pi_i2c_write_command(cathode, cmd2, sizeof(cmd2));\r\n<\/pre>\n<p>The <strong>pi_i2c_write_command<\/strong> is my own method but it simply does call the three methods (beginTransmission, write, endTransMision) that are required in order to send an I2C command. \u00a0Only\u00a0five lines of the twenty six are required, the rest were used for monitoring the program while it was being ported.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvoid pi_i2c_write_command(int device_address, char cmd[], char len)\r\n{\r\n  int idx = 0;\r\n  if (debug != 0)\r\n  {\r\n    Serial.println(&quot;begin pi_i2c_write_command\\n&quot;);\r\n    Serial.print(&quot;address &quot;);\r\n    Serial.println(device_address, HEX);\r\n    Serial.print(&quot;arguments &quot;);\r\n    Serial.println(len, DEC);\r\n    for (idx = 0; idx &amp;amp;lt; len; idx++)\r\n    {\r\n      Serial.print(cmd[idx], DEC);\r\n      Serial.print(&quot; &quot;);\r\n    }\r\n    Serial.println(&quot;\\n&quot;);\r\n  }\r\n\r\n  Wire.beginTransmission(device_address);\r\n  for (idx = 0; idx &amp;amp;lt; len; idx++)\r\n    Wire.write(cmd[idx]);\r\n  Wire.endTransmission();\r\n\r\n  if (debug != 0)\r\n    Serial.println(&quot;end pi_i2c_write_command\\n&quot;);\r\n}\r\n<\/pre>\n<h2><\/h2>\n<div id=\"attachment_2366\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/arduino-powered-cube.jpg\"><img aria-describedby=\"caption-attachment-2366\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2366\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/arduino-powered-cube-1024x892.jpg\" alt=\"\" width=\"640\" height=\"558\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/arduino-powered-cube-1024x892.jpg 1024w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/arduino-powered-cube-300x261.jpg 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/05\/arduino-powered-cube-768x669.jpg 768w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-2366\" class=\"wp-caption-text\">arduino powered cube<\/p><\/div>\n<h2>\u00a0The Problems<\/h2>\n<p>I did have one small problem while porting my software that was really an oversight. Before you try and transmit any data via the I2C bus you need to initialize it first with the a call to &#8220;begin&#8221;.<\/p>\n<pre>Wire.begin();<\/pre>\n<p>If you don&#8217;t make this call, none of the subsequent calls do anything. \u00a0In my haste, I did have this line but it was in the wrong spot in my initialization code.<\/p>\n<p>My code\u00a0does have comments but it has been a while since I was using the 23017 16 bit I\/O expander. \u00a0So I did have a few questions how it worked exactly.<\/p>\n<p>While doing my debugging I did read up more about the chip and I do have a few clarifications that describe the code&#8217;s behavior.<\/p>\n<table class=\"w3-table-all\">\n<tbody>\n<tr>\n<th>register<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>IOCON<\/td>\n<td>This register is used to set various configuration options on the chip. The two most interesting as far as this program goes is the BANK (bit 7) and the SEQOP option (bit 5).<\/p>\n<p>The BANK bit changes the addresses of the various registers. \u00a0When BANK=1 then the address for IODIRB is 0x01 but when the BANK=0 then the address for IODIRB is 0x10.<\/p>\n<p>The order of the registers is more interesting in conjunction with the SEQOP register. \u00a0The SEQOP=1 allows the chip to write multiple bytes sequentially. \u00a0When the BANK=0 then the register pairs are next to each other.<\/td>\n<\/tr>\n<tr>\n<td>IODIRA<\/p>\n<p>IODIRB<\/td>\n<td>These\u00a0registers are used to set the direction of the I\/O. The IODIRA register controls the direction for first 8bit port (PORTA) and the IODIRB register controls the direction forthe second 8bit pot (PORTB).<\/td>\n<\/tr>\n<tr>\n<td>OLATA<\/p>\n<p>OLATB<\/td>\n<td>These registers are used to set or clear the latches for the two ports.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The other important detail is that the current flows to the various LED&#8217;s\u00a0(via the I\/O expander) when the anode (layer) is set to on and the cathode (column) is set to off. \u00a0This sounds exactly what you would expect for lighting up a LED, power goes to anode and ground goes to cathode.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time back, I had a fair amount lot of free time.\u00a0 I spent the time working with a friend Mikhail in the evenings to create my own\u00a0LED cube from scratch.\u00a0 It was actually quite a bit of fun, the &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/06\/16\/just-making-it-revisiting-my-cube\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2320"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2320"}],"version-history":[{"count":12,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2320\/revisions"}],"predecessor-version":[{"id":2435,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2320\/revisions\/2435"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}