{"id":56,"date":"2013-08-25T00:57:01","date_gmt":"2013-08-24T13:57:01","guid":{"rendered":"https:\/\/www.newioit.com.au\/?p=56"},"modified":"2017-03-28T22:52:53","modified_gmt":"2017-03-28T11:52:53","slug":"examples-rotary-encoders-using-pcint","status":"publish","type":"post","link":"https:\/\/www.newioit.com.au\/blog\/archives\/56","title":{"rendered":"Examples &#8211; Rotary Encoders using PCINT"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>This example is about using Rotary Encoders with AVR chips. In provides a brief look at what they are, and how to use them.<\/p>\n<p>While searching the web for how to use rotary encoders, I came across a number of tried methods. Nearly all of them though used the hardware interrupt pins on the chip though. As i use these primarily for USB, i could not spare them for a rotary coder (let alone if when i wanted to use multiple rotary encoders!). For this reason, i developed my own solution using the Pin Change Interrupts (PCINT). These are similar to hardware interrupts, but trigger when any of the Pins enabled to use PCINT change. It is then up to the developer to discover what pins actually changed, and what the changes are.<\/p>\n<h3>Rotary Encoders<\/h3>\n<p>As usual, Wikipedia has the best definition of a <a title=\"Rotary Encoder on WikiPedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Rotary_encoder\" target=\"_blank\">Rotary Encoder<\/a>:<\/p>\n<p><em>A\u00a0<b>rotary encoder<\/b>, also called a\u00a0<b>shaft encoder<\/b>, is an\u00a0<a title=\"Electro-mechanical\" href=\"http:\/\/en.wikipedia.org\/wiki\/Electro-mechanical\">electro-mechanical<\/a>\u00a0device that converts the\u00a0<a title=\"Angle\" href=\"http:\/\/en.wikipedia.org\/wiki\/Angle\">angular<\/a>\u00a0position or motion of a shaft or axle to an analog or digital code.<\/em><\/p>\n<p>Rotary Encoders come in many shapes, forms and output types. The simpliest rotary coder looks like a potentiometer, but is continually turnable. Other have dual shafts (or two inputs), and can come with switches in built (depresses the rotary encoder closed the circuit). Some are made to be connected to large machinery and turned via engines and what not.<\/p>\n<p>The &#8216;analog or digital code&#8217; describes how the rotary encoder actually encodes the information we have received via input into the rotary coders (i.e. turning the encoder). The type of output we are interested in for this example are those that output in &#8216;Quadrature&#8217;.<\/p>\n<p>Quadrature is two signal that is 90 degrees out of phase with each other. That means that the direction of the turn can be indicated by which of the signals changed first (and the 2nd confirming the turn). The angular speed of the turn can also be indicated by the duration of the pulse. However, for this project, we are only going to look at which way the encoder is turning.<\/p>\n<p>To learn more about quadrature, i suggest reading my previous post detailing how to make a <a title=\"Tutorials \u2013 Quadrature Generator\" href=\"https:\/\/www.newioit.com.au\/blog\/archives\/34\" target=\"_blank\">Quadrature Generator<\/a>.<\/p>\n<h3>Example Code and Schematic<\/h3>\n<p>For this example, i&#8217;ve used the ATTiny2313 chip. Any AVR will do, but you will need to update the code with the correct ports to make it work with other variants (Even the ATMega&#8217;s). The 2313 series is a cheap chip for development, but with only 2K of program memory, it seriously limits what you can do with it (Even the newly released 4313 with 4K of PROGMEM is very limiting).<\/p>\n<p>The example im showing here has the rotary encoder as the input, a 3 leds as outputs. The leds function like this:<\/p>\n<ul>\n<li>LED1: Indicates the rotary encoder was turned in a direction.<\/li>\n<li>LED2: Indicates the rotary encoder was turned in the opposite direction to LED1.<\/li>\n<li>LED3: Indicates that an pin change interrupt has been received by the chip.<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"flag-singlepic flag-center\" alt=\"Rotary Encoder Schematic\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/flagallery\/56_examples_rotary_encoders\/rotaryencoder_schematic.png\" align=\"center\" \/><\/p>\n<p><strong>Download:<\/strong> The Eagle schematic <a title=\"Rotary Encoders using PCINT schematic\" href=\"http:\/\/ubuntuone.com\/0rrrbkVYvBdmilws9DDb6N\" target=\"_blank\">here<\/a><\/p>\n<p>Below is the code for the example. This code can also be viewed on my <a title=\"Example - Rotary Encoders with PCINT SVN\" href=\"https:\/\/www.newioit.com.au\/websvn\/listing.php?repname=group.electronics&amp;path=%2FProjects%2FExamples%2FRotaryEncoder%2F&amp;#ac1164a8af6e8d352c6aeaa2fd8739772\" target=\"_blank\">SVN Repo<\/a>. The avr-gcc <a title=\"Example - Rotary Encoder with PCINT Makefile\" href=\"https:\/\/www.newioit.com.au\/websvn\/filedetails.php?repname=group.electronics&amp;path=%2FProjects%2FExamples%2FRotaryEncoder%2FMakefile\" target=\"_blank\">Makefile<\/a> is also available in the SVN repo.<\/p>\n<pre lang=\"c\" line=\"1\" escaped=\"true\">#include <stdint.h>\r\n#include <avr\/pgmspace.h>\r\n#include <avr\/io.h>\r\n#include <avr\/interrupt.h>\r\n#include <avr\/wdt.h>\r\n\r\n#define F_CPU 12000000\r\n#include <util\/delay.h>\r\n\r\n#define DEPRESS_TIME    1\r\n\r\n#define sbi(sfr, bit)   ((sfr) |= _BV(bit))             \/\/ Set bit \r\n#define cbi(sfr, bit)   ((sfr) &= ~(_BV(bit)))          \/\/ Clear bit\r\n#define xbi(sfr, bit)   ((sfr) ^= _BV(bit))             \/\/ Flip bit\r\n#define rbi(sfr, bit)   (((sfr) >> (bit)) & 0x01)\r\n\r\nvolatile uint8_t pcIntCurr = 0;\r\nvolatile uint8_t pcIntLast = 0;\r\nvolatile uint8_t pcIntMask = 0;\r\n\r\nvolatile uint8_t timer0_ovf = 0;\r\nvolatile uint8_t time_rot = 0;\r\n\r\nvoid doInt();\r\n\r\nint main() {\r\n        cbi(DDRB, PCINT2);\r\n        cbi(DDRB, PCINT3);\r\n\r\n        TIMSK = (1<<TOIE0);                   \/\/ Eable timer overflow for Timer0\r\n        TCNT0 = 0x00;                         \/\/ Set Timer0 to 0\r\n        TCCR0B = (1<< CS02) | (1<<CS00);      \/\/ \/1024 prescaler\r\n\r\n        PORTB |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); \/\/turn on pullups\r\n        PCMSK |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); \/\/enable encoder pins interrupt sources\r\n        sei();\r\n        GIMSK |= ( 1 << PCIE ); \/\/enable pin change interupts\r\n\r\n        DDRD |= ( 1 << PD4 );\r\n        DDRD |= ( 1 << PD5 );\r\n        DDRD |= ( 1 << PD6 );\r\n\r\n        sbi(PORTD, PD6);\r\n        _delay_ms(1000);\r\n        cbi(PORTD, PD6);\r\n\r\n        for (;;) {\r\n\r\n                if (!time_rot) {\r\n                        cbi(PORTD, PD4);\r\n                        cbi(PORTD, PD5);\r\n                }\r\n\r\n                if (pcIntMask)\r\n                        doInt();\r\n        }\r\n}\r\n\r\nvoid doInt() {\r\n  xbi(PORTD, PD6);\r\n\r\n  if (rbi(pcIntCurr, PCINT2) == 0 &#038;&#038; rbi(pcIntCurr, PCINT3) == 0 &#038;&#038; rbi(pcIntMask, PCINT2) ) {\r\n        cbi(PORTD, PD5);\r\n        sbi(PORTD, PD4);\r\n        time_rot = 5;\r\n  } else if (rbi(pcIntCurr, PCINT3) == 0 &#038;&#038; rbi(pcIntCurr, PCINT2) == 0 &#038;&#038; rbi(pcIntMask, PCINT3) ) {\r\n        cbi(PORTD, PD4);\r\n        sbi(PORTD, PD5);\r\n        time_rot = 5;\r\n  }\r\n\r\n  pcIntMask = 0;\r\n}\r\n\r\nISR(TIMER0_OVF_vect) {\r\n        timer0_ovf++;\r\n\r\n        if (time_rot) {\r\n                time_rot--;\r\n        }\r\n}\r\n\r\n\r\nISR(PCINT_vect)\r\n{\r\n  pcIntCurr = PINB;\r\n  pcIntMask = pcIntCurr ^ pcIntLast;\r\n  pcIntLast = pcIntCurr;\r\n}<\/pre>\n<h3>Conclusion<\/h3>\n<p>Here are the Bitscope Logic outputs dumps for the rotary being turned in either direction. This is actually using the <a title=\"Tutorials \u2013 Quadrature Generator\" href=\"https:\/\/www.newioit.com.au\/blog\/archives\/34\" target=\"_blank\">Quadrature Generator<\/a> to provide input. This is a great way to test the rotary code, and is also great for testing how fast the MCU can detect changes. The definition of each of the traces are:<\/p>\n<ol>\n<li>WHITE - Rotary Encoder A Input.<\/li>\n<li>BROWN - Rotary Encoder B Input.<\/li>\n<li>RED -\u00a0Clock signal from the Quadrature Generator.<\/li>\n<li>ORANGE - A DIR2 turn event occurred.<\/li>\n<li>YELLOW - A DIR1 turn event occurred.<\/li>\n<li>PURPLE - A Pin Change Interrupt (PCINT) occurred.<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"flag-singlepic flag-center\" alt=\"Rotary Encoder direction 1\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/flagallery\/56_examples_rotary_encoders\/re_dir1.png\" align=\"center\" \/><\/p>\n<p><img decoding=\"async\" class=\"flag-singlepic flag-center\" style=\"line-height: 1.714285714; font-size: 1rem;\" alt=\"Rotary Encoder direction 2\" src=\"https:\/\/www.newioit.com.au\/blog\/wp-content\/flagallery\/56_examples_rotary_encoders\/re_dir2.png\" align=\"center\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Gallery of all images in this post:<\/strong><\/p>\n<p>[flagallery gid=3]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This example is about using Rotary Encoders with AVR chips. In provides a brief look at what they are, and how to use them. While searching the web for how to use rotary encoders, I came across a number of tried methods. Nearly all of them though used the hardware interrupt pins on the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-electronics","category-examples"],"_links":{"self":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":10,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":153,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions\/153"}],"wp:attachment":[{"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.newioit.com.au\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}