DanielPHuber 16 Posted October 22, 2015 Share Posted October 22, 2015 A decoder from a motor has 2 square wave outputs that deliver 256 pulses/revolution (duty cycle 1:1). The two outputs are 90 degree out of phase for direction information. I want to count pulse, in one direction up, in the other down. Towards this aim I attach to the first decoder output following routine: void countStep(){ if(digitalRead(_CIN2)){_pos++;} else {_pos--;} } "_CIN2" is the second decoder output and "pos" the counter variable. This works well until up to approx. 16kHz. But for higher frequencies it counts in the wrong direction. I first guessed, that the phase relation between the 2 signals is not constant, but an independent measurement showed it to be rock stable. Another guess is, that the interrupt is too slow (maybe because of the "digitalRead"). I am running a MSP430G2553 with 16MHz. Can somebody verify or falsify this guess? Quote Link to post Share on other sites
solipso 8 Posted October 22, 2015 Share Posted October 22, 2015 I do not use Energia, but i doubt that digitalRead function is interrupt based. Just try to whack up a real ISR for both pins incrementing or decrementing your variable depending on previous state variables of respective pins. Quote Link to post Share on other sites
DanielPHuber 16 Posted October 22, 2015 Author Share Posted October 22, 2015 In between I found the excellent post from this forum : digitalReadFast and speed of digitalReadStarted by altineller, Aug 19 2014 05:41 PM and you bet, the problem was the "digitalRead". I replaced digitalRead(_CIN2) by: P1IN & 0b00100000) and it works. It is astonishing that for a CPU running with 16MHz, digitalRead is so slow. According to the above postdigitalRead takes approx. 18 uS what explains my problem. Quote Link to post Share on other sites
spirilis 1,265 Posted October 22, 2015 Share Posted October 22, 2015 Yeah, digitalRead (and digitalWrite, pinMode, analogWrite, etc) is kind've a beast. It's the cost of having an elaborate abstraction layer that takes a simple integer identifier and distills it into platform-native operations. tripwire 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.