counter.h@0:f4c92b67e7be, 2011-11-25 (annotated)
- Committer:
- daveTshave
- Date:
- Fri Nov 25 13:51:58 2011 +0000
- Revision:
- 0:f4c92b67e7be
- Child:
- 1:8c1bace3af7b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daveTshave | 0:f4c92b67e7be | 1 | #ifndef counter_H |
daveTshave | 0:f4c92b67e7be | 2 | #define counter_H |
daveTshave | 0:f4c92b67e7be | 3 | |
daveTshave | 0:f4c92b67e7be | 4 | /** |
daveTshave | 0:f4c92b67e7be | 5 | * Includes |
daveTshave | 0:f4c92b67e7be | 6 | */ |
daveTshave | 0:f4c92b67e7be | 7 | #include "mbed.h" |
daveTshave | 0:f4c92b67e7be | 8 | |
daveTshave | 0:f4c92b67e7be | 9 | /** |
daveTshave | 0:f4c92b67e7be | 10 | * Simple counter |
daveTshave | 0:f4c92b67e7be | 11 | */ |
daveTshave | 0:f4c92b67e7be | 12 | class counter { |
daveTshave | 0:f4c92b67e7be | 13 | |
daveTshave | 0:f4c92b67e7be | 14 | public: |
daveTshave | 0:f4c92b67e7be | 15 | |
daveTshave | 0:f4c92b67e7be | 16 | |
daveTshave | 0:f4c92b67e7be | 17 | /** |
daveTshave | 0:f4c92b67e7be | 18 | * Constructor. |
daveTshave | 0:f4c92b67e7be | 19 | * |
daveTshave | 0:f4c92b67e7be | 20 | * Attaches the encode function to the rise/fall edge of |
daveTshave | 0:f4c92b67e7be | 21 | * channels A. |
daveTshave | 0:f4c92b67e7be | 22 | * |
daveTshave | 0:f4c92b67e7be | 23 | * @param channelA mbed pin for counter input. |
daveTshave | 0:f4c92b67e7be | 24 | * @param pulsesPerRev Number of pulses in one revolution. |
daveTshave | 0:f4c92b67e7be | 25 | */ |
daveTshave | 0:f4c92b67e7be | 26 | counter(PinName channelA,int pulsesPerRev); |
daveTshave | 0:f4c92b67e7be | 27 | |
daveTshave | 0:f4c92b67e7be | 28 | /** |
daveTshave | 0:f4c92b67e7be | 29 | * Reset the encoder. |
daveTshave | 0:f4c92b67e7be | 30 | * |
daveTshave | 0:f4c92b67e7be | 31 | * Sets the pulses and revolutions count to zero. |
daveTshave | 0:f4c92b67e7be | 32 | */ |
daveTshave | 0:f4c92b67e7be | 33 | void reset(void); |
daveTshave | 0:f4c92b67e7be | 34 | |
daveTshave | 0:f4c92b67e7be | 35 | /** |
daveTshave | 0:f4c92b67e7be | 36 | * Read the number of pulses recorded by the encoder. |
daveTshave | 0:f4c92b67e7be | 37 | * |
daveTshave | 0:f4c92b67e7be | 38 | * @return Number of pulses which have occured. |
daveTshave | 0:f4c92b67e7be | 39 | */ |
daveTshave | 0:f4c92b67e7be | 40 | int getPulses(void); |
daveTshave | 0:f4c92b67e7be | 41 | |
daveTshave | 0:f4c92b67e7be | 42 | /** |
daveTshave | 0:f4c92b67e7be | 43 | * Read the number of revolutions recorded by the encoder on the index channel. |
daveTshave | 0:f4c92b67e7be | 44 | * |
daveTshave | 0:f4c92b67e7be | 45 | * @return Number of revolutions which have occured on the index channel. |
daveTshave | 0:f4c92b67e7be | 46 | */ |
daveTshave | 0:f4c92b67e7be | 47 | int getRevolutions(void); |
daveTshave | 0:f4c92b67e7be | 48 | |
daveTshave | 0:f4c92b67e7be | 49 | private: |
daveTshave | 0:f4c92b67e7be | 50 | |
daveTshave | 0:f4c92b67e7be | 51 | /** |
daveTshave | 0:f4c92b67e7be | 52 | * Update the pulse count. |
daveTshave | 0:f4c92b67e7be | 53 | * |
daveTshave | 0:f4c92b67e7be | 54 | * Called on every rising edge of channels A. |
daveTshave | 0:f4c92b67e7be | 55 | * |
daveTshave | 0:f4c92b67e7be | 56 | */ |
daveTshave | 0:f4c92b67e7be | 57 | void encode(void); |
daveTshave | 0:f4c92b67e7be | 58 | |
daveTshave | 0:f4c92b67e7be | 59 | InterruptIn channelA_; |
daveTshave | 0:f4c92b67e7be | 60 | |
daveTshave | 0:f4c92b67e7be | 61 | int pulsesPerRev_; |
daveTshave | 0:f4c92b67e7be | 62 | volatile int pulses_; |
daveTshave | 0:f4c92b67e7be | 63 | volatile int revolutions_; |
daveTshave | 0:f4c92b67e7be | 64 | |
daveTshave | 0:f4c92b67e7be | 65 | }; |
daveTshave | 0:f4c92b67e7be | 66 | |
daveTshave | 0:f4c92b67e7be | 67 | #endif /* counter_H */ |