{"id":158,"date":"2013-03-10T21:28:35","date_gmt":"2013-03-11T05:28:35","guid":{"rendered":"http:\/\/schminktronics.com\/blog\/?p=158"},"modified":"2013-03-27T14:38:38","modified_gmt":"2013-03-27T22:38:38","slug":"counter-test-1","status":"publish","type":"post","link":"http:\/\/schminktronics.com\/blog\/archives\/158","title":{"rendered":"Counter Test 1"},"content":{"rendered":"<p>My first test actually went quite well. The counter worked just like I expected and I got the counter information I wanted:<\/p>\n<div id=\"attachment_161\" style=\"width: 594px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/schminktronics.com\/blog\/wp-content\/uploads\/2013\/03\/IMG_4697.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-161\" class=\"size-large wp-image-161\" alt=\"LEDs displaying a binary pattern showing the value of the counter for how long the last rotation took. MSB is the innermost LED.\" src=\"http:\/\/schminktronics.com\/blog\/wp-content\/uploads\/2013\/03\/IMG_4697-1024x682.jpg\" width=\"584\" height=\"388\" srcset=\"http:\/\/schminktronics.com\/blog\/wp-content\/uploads\/2013\/03\/IMG_4697-1024x682.jpg 1024w, http:\/\/schminktronics.com\/blog\/wp-content\/uploads\/2013\/03\/IMG_4697-300x200.jpg 300w, http:\/\/schminktronics.com\/blog\/wp-content\/uploads\/2013\/03\/IMG_4697-450x300.jpg 450w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><p id=\"caption-attachment-161\" class=\"wp-caption-text\">LEDs displaying a binary pattern showing the value of the counter for how long the last rotation took. MSB is the innermost LED.<\/p><\/div>\n<p>For this picture, I used a flash, then kept the shutter open a bit. The spinner is actually traveling clockwise, so the trail of LEDs is actually in front of the spinner, not behind it as you would expect.<\/p>\n<p>Anyway&#8211;the reason I used the flash was so I could see where each of the LEDs was mounted and could accurately count which ones are off. The MSB is the innermost one, so this counter value is 100011000100000b\u00a0which is 4620h. This is about 2.5 times smaller than what I predicted. The counter values also change a lot more than I expected. I thought the least significant 2-3 bits would vary, but I actually see the least significant 8-9 bits changing. I know the battery is running low for my battery, so <i>maybe<\/i>\u00a0the counter values I&#8217;m seeing are accurate.<\/p>\n<p>Here&#8217;s the code I used to do this:<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"line_numbers\"><pre>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n<\/pre><\/td><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #666666; font-style: italic;\">\/\/  Using the spinner board to test 328 counters<\/span>\n&nbsp;\n<span style=\"color: #339933;\">#include &lt;SPI.h&gt;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">const<\/span> <span style=\"color: #993333;\">int<\/span> HALL  <span style=\"color: #339933;\">=<\/span>  <span style=\"color: #0000dd;\">2<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ PD2<\/span>\n<span style=\"color: #993333;\">const<\/span> <span style=\"color: #993333;\">int<\/span> BLANK <span style=\"color: #339933;\">=<\/span>  <span style=\"color: #0000dd;\">8<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ PB0<\/span>\n<span style=\"color: #993333;\">const<\/span> <span style=\"color: #993333;\">int<\/span> LAT   <span style=\"color: #339933;\">=<\/span>  <span style=\"color: #0000dd;\">9<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ PB1<\/span>\n&nbsp;\n<span style=\"color: #993333;\">const<\/span> <span style=\"color: #993333;\">int<\/span> HALL_INT <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Hall effect interrupt 0 on PD2 (INT0)<\/span>\n&nbsp;\n<span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">int<\/span> savedCounter <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> hallStart<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">void<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  savedCounter <span style=\"color: #339933;\">=<\/span> TCNT1<span style=\"color: #339933;\">;<\/span>\n  TCNT1 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> setup<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  digitalWrite<span style=\"color: #009900;\">&#40;<\/span>LAT<span style=\"color: #339933;\">,<\/span> LOW<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  pinMode<span style=\"color: #009900;\">&#40;<\/span>LAT<span style=\"color: #339933;\">,<\/span> OUTPUT<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>     \n&nbsp;\n  digitalWrite<span style=\"color: #009900;\">&#40;<\/span>BLANK<span style=\"color: #339933;\">,<\/span> HIGH<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  pinMode<span style=\"color: #009900;\">&#40;<\/span>BLANK<span style=\"color: #339933;\">,<\/span> OUTPUT<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>     \n&nbsp;\n  pinMode<span style=\"color: #009900;\">&#40;<\/span>HALL<span style=\"color: #339933;\">,<\/span> INPUT_PULLUP<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Setup the 16 bit counter<\/span>\n  TCCR1A <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>                   <span style=\"color: #666666; font-style: italic;\">\/\/ Turn off output compare modes<\/span>\n  TCCR1B <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">2<\/span><span style=\"color: #339933;\">;<\/span>                   <span style=\"color: #666666; font-style: italic;\">\/\/ Turn on clk\/8 input<\/span>\n&nbsp;\n  attachInterrupt<span style=\"color: #009900;\">&#40;<\/span>HALL_INT<span style=\"color: #339933;\">,<\/span> hallStart<span style=\"color: #339933;\">,<\/span> FALLING<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  SPI.<span style=\"color: #202020;\">begin<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  SPI.<span style=\"color: #202020;\">setDataMode<\/span><span style=\"color: #009900;\">&#40;<\/span>SPI_MODE0<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  SPI.<span style=\"color: #202020;\">setBitOrder<\/span><span style=\"color: #009900;\">&#40;<\/span>LSBFIRST<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  SPI.<span style=\"color: #202020;\">setClockDivider<\/span><span style=\"color: #009900;\">&#40;<\/span>SPI_CLOCK_DIV2<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Datasheet says 35 MHz max, so 8 MHz is fine<\/span>\n&nbsp;\n  TCNT1 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> setLeds<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">unsigned<\/span> <span style=\"color: #993333;\">int<\/span> pattern<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Blank the LEDS<\/span>\n  digitalWrite<span style=\"color: #009900;\">&#40;<\/span>BLANK<span style=\"color: #339933;\">,<\/span> HIGH<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Transfer the data<\/span>\n  SPI.<span style=\"color: #202020;\">transfer<\/span><span style=\"color: #009900;\">&#40;<\/span>pattern <span style=\"color: #339933;\">&amp;<\/span> <span style=\"color: #208080;\">0xFF<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  SPI.<span style=\"color: #202020;\">transfer<\/span><span style=\"color: #009900;\">&#40;<\/span>pattern <span style=\"color: #339933;\">&gt;&gt;<\/span> <span style=\"color: #0000dd;\">8<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Latch the data<\/span>\n  digitalWrite<span style=\"color: #009900;\">&#40;<\/span>LAT<span style=\"color: #339933;\">,<\/span> HIGH<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  digitalWrite<span style=\"color: #009900;\">&#40;<\/span>LAT<span style=\"color: #339933;\">,<\/span> LOW<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  <span style=\"color: #666666; font-style: italic;\">\/\/ Unblank the LEDS<\/span>\n  digitalWrite<span style=\"color: #009900;\">&#40;<\/span>BLANK<span style=\"color: #339933;\">,<\/span> LOW<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> loop<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>savedCounter <span style=\"color: #339933;\">!=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n    setLeds<span style=\"color: #009900;\">&#40;<\/span>savedCounter<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    savedCounter <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>So, the only way to find out if my counter values are correct is to do the next test:<\/p>\n<ul>\n<li><span style=\"line-height: 15px;\">Each time I get the hall effect interrupt, read the 16 bit counter (counter 1)<\/span><\/li>\n<li>Divide that by 4096, which is 512 (the number of slices) * 8 (the prescaler), then set that value in output compare A register for 8 bit timer 2. In CTC mode, this gives us an interrupt every time the counter value is matched.<\/li>\n<li>Each time we get a timer 2 interrupt, increment the slice counter. On slices 0, 128, 256, and 384, turn on all the LEDs for one slice. This should display lines at the 12:00, 3:00, 6:00, and 9:00 positions<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My first test actually went quite well. The counter worked just like I expected and I got the counter information I wanted: For this picture, I used a flash, then kept the shutter open a bit. The spinner is actually &hellip; <a href=\"http:\/\/schminktronics.com\/blog\/archives\/158\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-spinnerhd"],"_links":{"self":[{"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":6,"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":175,"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions\/175"}],"wp:attachment":[{"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/schminktronics.com\/blog\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}