How do I setup a Due to count rising edges on a pin automatically without having to waste time on an ISR?
I have some wheel encoders, the simplist implementation is direction less where you only count rising edges.
I went the Due to count rising edges automatically and an ISR on a timer will periodically pole the current count
1 Answer 1
You need to pick a timer that isn't being used for anything else and configure it as a simple counter with the clock source set to the external clock pin for that timer.
At block level, input clock signals of each channel can either be connected to the external inputs TCLK0, TCLK1 or TCLK2, or be connected to the internal I/O signals TIOA0, TIOA1 or TIOA2 for chaining by programming the TC Block Mode Register (TC_BMR). See Figure 36-2. Each channel can independently select an internal or external clock source for its counter:
- External clock signals(1): XC0, XC1 or XC2
- Internal clock signals: MCK/2, MCK/8, MCK/32, MCK/128, SLCK This selection is made by the TCCLKS bits in the TC Channel Mode Register (TC_CMR).
I am not personally familiar with the peripherals of the Due, but chapter 36 of the SAM3X datasheet details the workings and registers that you need to set.
Your encoder signal then connects to that clock input pin and the counter then counts every incoming pulse until you reset the count from your other periodic timer ISR.
waste time on an ISR
timer will periodically pole