| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file encoder.c \brief Quadrature Encoder reader/driver. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'encoder.c' |
||
| 5 | // Title : Quadrature Encoder reader/driver |
||
| 6 | // Author : Pascal Stang - Copyright (C) 2003-2004 |
||
| 7 | // Created : 2003.01.26 |
||
| 8 | // Revised : 2004.06.25 |
||
| 9 | // Version : 0.3 |
||
| 10 | // Target MCU : Atmel AVR Series |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
| 14 | // to be lacking in some functionality or documentation, or may not be fully |
||
| 15 | // tested. Nonetheless, you can expect most functions to work. |
||
| 16 | // |
||
| 17 | // This code is distributed under the GNU Public License |
||
| 18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
| 19 | // |
||
| 20 | //***************************************************************************** |
||
| 21 | |||
| 22 | #include <avr/io.h> |
||
| 23 | #include <avr/interrupt.h> |
||
| 24 | |||
| 25 | #include "global.h" |
||
| 26 | #include "encoder.h" |
||
| 27 | |||
| 28 | // Program ROM constants |
||
| 29 | |||
| 30 | // Global variables |
||
| 31 | volatile EncoderStateType EncoderState[NUM_ENCODERS]; |
||
| 32 | |||
| 33 | // Functions |
||
| 34 | |||
| 35 | // encoderInit() initializes hardware and encoder position readings |
||
| 36 | // Run this init routine once before using any other encoder functions. |
||
| 37 | void encoderInit(void) |
||
| 38 | { |
||
| 39 | u08 i; |
||
| 40 | |||
| 41 | // initialize/clear encoder data |
||
| 42 | for(i=0; i<NUM_ENCODERS; i++) |
||
| 43 | { |
||
| 44 | EncoderState[i].position = 0; |
||
| 45 | //EncoderState[i].velocity = 0; // NOT CURRENTLY USED |
||
| 46 | } |
||
| 47 | |||
| 48 | // configure direction and interrupt I/O pins: |
||
| 49 | // - for input |
||
| 50 | // - apply pullup resistors |
||
| 51 | // - any-edge interrupt triggering |
||
| 52 | // - enable interrupt |
||
| 53 | |||
| 54 | #ifdef ENC0_SIGNAL |
||
| 55 | // set interrupt pins to input and apply pullup resistor |
||
| 56 | cbi(ENC0_PHASEA_DDR, ENC0_PHASEA_PIN); |
||
| 57 | sbi(ENC0_PHASEA_PORT, ENC0_PHASEA_PIN); |
||
| 58 | // set encoder direction pin for input and apply pullup resistor |
||
| 59 | cbi(ENC0_PHASEB_DDR, ENC0_PHASEB_PIN); |
||
| 60 | sbi(ENC0_PHASEB_PORT, ENC0_PHASEB_PIN); |
||
| 61 | // configure interrupts for any-edge triggering |
||
| 62 | sbi(ENC0_ICR, ENC0_ISCX0); |
||
| 63 | cbi(ENC0_ICR, ENC0_ISCX1); |
||
| 64 | // enable interrupts |
||
| 65 | sbi(IMSK, ENC0_INT); // ISMK is auto-defined in encoder.h |
||
| 66 | #endif |
||
| 67 | #ifdef ENC1_SIGNAL |
||
| 68 | // set interrupt pins to input and apply pullup resistor |
||
| 69 | cbi(ENC1_PHASEA_DDR, ENC1_PHASEA_PIN); |
||
| 70 | sbi(ENC1_PHASEA_PORT, ENC1_PHASEA_PIN); |
||
| 71 | // set encoder direction pin for input and apply pullup resistor |
||
| 72 | cbi(ENC1_PHASEB_DDR, ENC1_PHASEB_PIN); |
||
| 73 | sbi(ENC1_PHASEB_PORT, ENC1_PHASEB_PIN); |
||
| 74 | // configure interrupts for any-edge triggering |
||
| 75 | sbi(ENC1_ICR, ENC1_ISCX0); |
||
| 76 | cbi(ENC1_ICR, ENC1_ISCX1); |
||
| 77 | // enable interrupts |
||
| 78 | sbi(IMSK, ENC1_INT); // ISMK is auto-defined in encoder.h |
||
| 79 | #endif |
||
| 80 | #ifdef ENC2_SIGNAL |
||
| 81 | // set interrupt pins to input and apply pullup resistor |
||
| 82 | cbi(ENC2_PHASEA_DDR, ENC2_PHASEA_PIN); |
||
| 83 | sbi(ENC2_PHASEA_PORT, ENC2_PHASEA_PIN); |
||
| 84 | // set encoder direction pin for input and apply pullup resistor |
||
| 85 | cbi(ENC2_PHASEB_DDR, ENC2_PHASEB_PIN); |
||
| 86 | sbi(ENC2_PHASEB_PORT, ENC2_PHASEB_PIN); |
||
| 87 | // configure interrupts for any-edge triggering |
||
| 88 | sbi(ENC2_ICR, ENC2_ISCX0); |
||
| 89 | cbi(ENC2_ICR, ENC2_ISCX1); |
||
| 90 | // enable interrupts |
||
| 91 | sbi(IMSK, ENC2_INT); // ISMK is auto-defined in encoder.h |
||
| 92 | #endif |
||
| 93 | #ifdef ENC3_SIGNAL |
||
| 94 | // set interrupt pins to input and apply pullup resistor |
||
| 95 | cbi(ENC3_PHASEA_DDR, ENC3_PHASEA_PIN); |
||
| 96 | sbi(ENC3_PHASEA_PORT, ENC3_PHASEA_PIN); |
||
| 97 | // set encoder direction pin for input and apply pullup resistor |
||
| 98 | cbi(ENC3_PHASEB_DDR, ENC3_PHASEB_PIN); |
||
| 99 | sbi(ENC3_PHASEB_PORT, ENC3_PHASEB_PIN); |
||
| 100 | // configure interrupts for any-edge triggering |
||
| 101 | sbi(ENC3_ICR, ENC3_ISCX0); |
||
| 102 | cbi(ENC3_ICR, ENC3_ISCX1); |
||
| 103 | // enable interrupts |
||
| 104 | sbi(IMSK, ENC3_INT); // ISMK is auto-defined in encoder.h |
||
| 105 | #endif |
||
| 106 | |||
| 107 | // enable global interrupts |
||
| 108 | sei(); |
||
| 109 | } |
||
| 110 | |||
| 111 | // encoderOff() disables hardware and stops encoder position updates |
||
| 112 | void encoderOff(void) |
||
| 113 | { |
||
| 114 | // disable encoder interrupts |
||
| 115 | #ifdef ENC0_SIGNAL |
||
| 116 | // disable interrupts |
||
| 117 | sbi(IMSK, INT0); // ISMK is auto-defined in encoder.h |
||
| 118 | #endif |
||
| 119 | #ifdef ENC1_SIGNAL |
||
| 120 | // disable interrupts |
||
| 121 | sbi(IMSK, INT1); // ISMK is auto-defined in encoder.h |
||
| 122 | #endif |
||
| 123 | #ifdef ENC2_SIGNAL |
||
| 124 | // disable interrupts |
||
| 125 | sbi(IMSK, INT2); // ISMK is auto-defined in encoder.h |
||
| 126 | #endif |
||
| 127 | #ifdef ENC3_SIGNAL |
||
| 128 | // disable interrupts |
||
| 129 | sbi(IMSK, INT3); // ISMK is auto-defined in encoder.h |
||
| 130 | #endif |
||
| 131 | } |
||
| 132 | |||
| 133 | // encoderGetPosition() reads the current position of the encoder |
||
| 134 | s32 encoderGetPosition(u08 encoderNum) |
||
| 135 | { |
||
| 136 | // sanity check |
||
| 137 | if(encoderNum < NUM_ENCODERS) |
||
| 138 | return EncoderState[encoderNum].position; |
||
| 139 | else |
||
| 140 | return 0; |
||
| 141 | } |
||
| 142 | |||
| 143 | // encoderSetPosition() sets the current position of the encoder |
||
| 144 | void encoderSetPosition(u08 encoderNum, s32 position) |
||
| 145 | { |
||
| 146 | // sanity check |
||
| 147 | if(encoderNum < NUM_ENCODERS) |
||
| 148 | EncoderState[encoderNum].position = position; |
||
| 149 | // else do nothing |
||
| 150 | } |
||
| 151 | |||
| 152 | #ifdef ENC0_SIGNAL |
||
| 153 | //! Encoder 0 interrupt handler |
||
| 154 | SIGNAL(ENC0_SIGNAL) |
||
| 155 | { |
||
| 156 | // encoder has generated a pulse |
||
| 157 | // check the relative phase of the input channels |
||
| 158 | // and update position accordingly |
||
| 159 | if( ((inb(ENC0_PHASEA_PORTIN) & (1<<ENC0_PHASEA_PIN)) == 0) ^ |
||
| 160 | ((inb(ENC0_PHASEB_PORTIN) & (1<<ENC0_PHASEB_PIN)) == 0) ) |
||
| 161 | { |
||
| 162 | EncoderState[0].position++; |
||
| 163 | } |
||
| 164 | else |
||
| 165 | { |
||
| 166 | EncoderState[0].position--; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | #endif |
||
| 170 | |||
| 171 | #ifdef ENC1_SIGNAL |
||
| 172 | //! Encoder 1 interrupt handler |
||
| 173 | SIGNAL(ENC1_SIGNAL) |
||
| 174 | { |
||
| 175 | // encoder has generated a pulse |
||
| 176 | // check the relative phase of the input channels |
||
| 177 | // and update position accordingly |
||
| 178 | if( ((inb(ENC1_PHASEA_PORTIN) & (1<<ENC1_PHASEA_PIN)) == 0) ^ |
||
| 179 | ((inb(ENC1_PHASEB_PORTIN) & (1<<ENC1_PHASEB_PIN)) == 0) ) |
||
| 180 | { |
||
| 181 | EncoderState[1].position++; |
||
| 182 | } |
||
| 183 | else |
||
| 184 | { |
||
| 185 | EncoderState[1].position--; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | #endif |
||
| 189 | |||
| 190 | #ifdef ENC2_SIGNAL |
||
| 191 | //! Encoder 2 interrupt handler |
||
| 192 | SIGNAL(ENC2_SIGNAL) |
||
| 193 | { |
||
| 194 | // encoder has generated a pulse |
||
| 195 | // check the relative phase of the input channels |
||
| 196 | // and update position accordingly |
||
| 197 | if( ((inb(ENC2_PHASEA_PORTIN) & (1<<ENC2_PHASEA_PIN)) == 0) ^ |
||
| 198 | ((inb(ENC2_PHASEB_PORTIN) & (1<<ENC2_PHASEB_PIN)) == 0) ) |
||
| 199 | { |
||
| 200 | EncoderState[2].position++; |
||
| 201 | } |
||
| 202 | else |
||
| 203 | { |
||
| 204 | EncoderState[2].position--; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | #endif |
||
| 208 | |||
| 209 | #ifdef ENC3_SIGNAL |
||
| 210 | //! Encoder 3 interrupt handler |
||
| 211 | SIGNAL(ENC3_SIGNAL) |
||
| 212 | { |
||
| 213 | // encoder has generated a pulse |
||
| 214 | // check the relative phase of the input channels |
||
| 215 | // and update position accordingly |
||
| 216 | if( ((inb(ENC3_PHASEA_PORTIN) & (1<<ENC3_PHASEA_PIN)) == 0) ^ |
||
| 217 | ((inb(ENC3_PHASEB_PORTIN) & (1<<ENC3_PHASEB_PIN)) == 0) ) |
||
| 218 | { |
||
| 219 | EncoderState[3].position++; |
||
| 220 | } |
||
| 221 | else |
||
| 222 | { |
||
| 223 | EncoderState[3].position--; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | #endif |
Powered by WebSVN v2.8.3