cancel
Showing results for 
Search instead for 
Did you mean: 

Timer interrupt flag behavior in encoder mode

Lee3
Associate III

I have an STM32G474, but this encoder/timer behavior is likely similar for other MCUs. I have the timer set up in quadrature encoder mode on channels 1 and 2 and I'm using input capture on channel 3. This is tied to the encoder index so that it will latch the encoder value when the index occurs. This works but I also want to know when the first index has occurred. I tried reading CC3IF in the SR directly, but I've found it is temperamental. It seems to flag but only for a brief period. I'm not resetting any flags or writing to SR.

Can anyone tell me about this use case and what behavior I should expect from CC3IF?

Below is some code:

class QEPEncoder final : public EncoderBase {
 public:
   QEPEncoder(TIM_TypeDef &regs) : EncoderBase(), regs_(regs) { 
     regs_.SMCR = 3;  // quadrature mode
     regs_.CCMR1 = (1 << TIM_CCMR1_CC1S_Pos) | (1 << TIM_CCMR1_CC2S_Pos) |  // input selection
        (1 << TIM_CCMR1_IC1F_Pos) | (1 << TIM_CCMR1_IC2F_Pos); // filters at 1
     regs_.CCMR2 = (1 << TIM_CCMR2_CC3S_Pos) | (2 << TIM_CCMR2_IC3F_Pos); // index filter at 2
     regs_.CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E;  // enable
     regs_.CR1 |= TIM_CR1_CEN;
   }
   int32_t read() { value_ = regs_.CNT; return value_; }
   int32_t get_value() const { return value_; }
   int32_t get_index_pos() { return regs_.CCR3; }
   bool index_received() { return regs_.SR & TIM_SR_CC3IF; }
 private:
   TIM_TypeDef &regs_;
   int32_t value_ = 0;
};

1 ACCEPTED SOLUTION

Accepted Solutions
2 REPLIES 2

0693W000008xrPdQAI.pngJW

Thank you much, that was my issue. My debugging was taking me in the wrong direction otherwise, thinking it was something with the configuration or specific to the encoder mode.