Jump to content

Recommended Posts

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?


Link to post
Share on other sites

In between I found the excellent post from this forum :


digitalReadFast and speed of digitalRead

Started by altinellerAug 19 2014 05:41 PM


and you bet, the problem was the "digitalRead". I replaced




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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...