3 * Rotary encoder routine.
4 * Copyright (C) 2013-10-21 K.Ohta <whatisthis.sowhat ai gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 * This library / program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this library; see the file COPYING. If not, write to the
18 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21 * As a special exception, if you link this(includeed from sdcc) library
22 * with other files, some of which are compiled with SDCC,
23 * to produce an executable, this library does not by itself cause
24 * the resulting executable to be covered by the GNU General Public License.
25 * This exception does not however invalidate any other reasons why
26 * the executable file might be covered by the GNU General Public License.
30 static unsigned char renc_state_a;
31 static unsigned char renc_state_b;
32 unsigned char renc_dir;
33 unsigned int renc_count;
34 static unsigned int renc_reset_count;
35 //static unsigned char renc_penalty;
36 unsigned char renc_flag;
39 void rencoder_init(void)
45 renc_state_a = RENC_PH_A;
46 renc_state_b = RENC_PH_B;
53 void rencoder_restart(void)
55 // T1CONbits.TMR1ON = 1;
56 T1CON = 0b10110100;// RD16, T1RUN=0, 1/32 fOSC, TNR1CS=0, TMR1ON=0;
57 TMR1H = (65536 - 400) >> 8; // Tick = 4us, Count = 600(2.4ms)
58 TMR1L = (65536 - 400) & 255;
59 // T1CONbits.TMR1ON = 0;
66 void rencoder_start(void)
70 // rencoder_restart();
71 IOCB = 0b01100000; // RB5,RB6
79 void rencoder_inthook(void)
82 // IOCBbits.IOCB4 = 1;
84 if(renc_flag != 0) return;
85 // renc_state_a = RENC_PH_A;
86 // renc_state_b = RENC_PH_B;
93 void rencoder_tmrhook(void)
107 void rencoder_stop(void)
115 IOCB = 0b00000000; // RB5,RB6
122 void rencoder_count(void)
124 unsigned char dir = RENC_NONE;
125 unsigned char pha = RENC_PH_A;
126 unsigned char phb = RENC_PH_B;
128 // if(renc_flag != 0) return;
129 // if(renc_penalty != 0){
131 // if(renc_penalty > 1) {
137 * Count sequence description of rotary-encoder:
138 * See figure of http://homepage1.nifty.com/rikiya/software/113ROTENC.htm .
140 if(pha != renc_state_a) {
141 if(pha == 1){ // A: RISE UP
142 if(phb == 0){ // B: Stable
149 } else if(phb != renc_state_b){
150 if(phb == 1){ // B: RISE UP
151 if(pha == 0){ // A: Stable
160 if(renc_reset_count >= 5000) { // 3200ms
162 renc_dir = RENC_NONE;
165 renc_reset_count = 0;
172 if(dir == RENC_NONE) return;
174 if(dir == renc_dir) {
180 renc_reset_count = 0;