Rotary
Encoder Demo

Introduction
This is a demonstration of the 'Rotary encoder (8-bit
count)' sub-circuit. This generic sub-circuit is for
rotary encoders that require a single 5V supply. Example
applications are instrument panel controls and motor
shaft position indicators.
The target device used for this demonstration
is a PIC16F84 running at 4MHz.
Operation
'ENC_1DO()' is called at regular intervals by the
RTCC timer interrupt service routine This serves to
update the 8 bit counter 'ENC_1COUNT' whenever the
shaft of the rotary encoder is moved. When the shaft
is rotated, 'ENC_1COUNT' increments or decrements
depending on the direction of rotation.
The value of 'ENC_1COUNT' is transmitted
out of the RS232_1 port every 100mS.
Testing
Connect TX_OUT of RS232_1 to a remote terminal such
as Windows Hyper-Terminal. Rotating the encoder shaft
will either increment or decrement the values displayed
on the terminal.
Application code
See application code below. To use it, select and
copy (ctr-insert) the below and past it into a text
editor and save as a 'C' file in your working directory.
You may also need to change the build output path
using 'Project | Options'
///////////////////////////////////////////////////////////////////////
//// APPRTRYE.C ////
//// ////
//// Demo application of rotory encoder. ////
//// ////
//// This program is an example of how your application code is ////
//// is organised. Note the #include "RTRYENC.C", this include ////
//// file was generated by QuickBuilder and contains all ////
//// driver code. ////
//// ////
//// This demo is intended for a PIC16F84 ////
//// Compile using CCS 'C' visit www.quickbuilder.co.uk/qb/ccs ////
///////////////////////////////////////////////////////////////////////
#include <16F84.H>
#include
#fuses XT,NOPROTECT,NOWDT
// port defines
#define PORTA 5
#define PORTB 6
#define TRISA 0x85
#define TRISB 0x86
#define RTCC_ZERO INT_TIMER0
#INCLUDE "RTRYENC.C"
#int_rtcc
void RTCC_ISR()
{
ENC8_1DO(); // service shaft encoder
}
void main()
{
INIT_SUB_CIRCUITS();
// start timer interrupts approx 0.5ms interval
// @clock=4.0MHz
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
enable_interrupts(RTCC_ZERO);
enable_interrupts(GLOBAL);
while(1)
{
// delay 1/10 sec
delay_ms(100);
// disbale interrupts when sending results
disable_interrupts(RTCC_ZERO);
printf(RS232_1OUT, "%03u\r", ENC8_1COUNT);
enable_interrupts(RTCC_ZERO);
}
}