Quadrature Decoder function SetCounter() does not accept negative values (positive values are fine).
QuadDec_SetCounter(-1); //can't set counter to a negative value
sprintf(strMsg1,"counter_val=%d \r\n", QuadDec_GetCounter() );
reading counter value in the above example returns -32767.
What I want is to limit counter values to some bounds, e.g. -100...+100.
Is -1 in the compiler considered a byte, long,.......?
If you are using a 32 bit counter should it be -1L in the function argument.
I am weak here in C, but fact you are returning an int makes me wonder if -1
argument was taken as a long ?
I did not check yet if there are differences between the PSoC4 and the PSoC3/5 implementation, talking about PSoC4:
What size (width) did you specify? 16 or 32bits? I bet 32.
How is the target (you read the counter into) declared? 16 or 32bits? I'm sure 16.
On the other hand: a 32bit integer cannot be converted to string with "%d"-format that will accept (and assume) only 16bit integers.
Concerning formatiing and variadic f()'s like printf()/sprintf() -
Also shows promotion/conversions being made by f().
I do it like that (should work according to datasheet, but it doesn't):
counter_val = -10;
QuadDec_SetCounter(counter_val); //BUG: can't set counter to negative value
Astonishingly there seems to be something wrong with the 16bit wide QuadDecoder. As a workaround use the 32bit wide, I checked that it works on a PSoC5LP
If that is the case, it is best to post a case to Cypress support.
I might also look at the listing to see what is output of the compiler.
In continuation of the dicussion, QuadDecoder has a rollover issue (IMHO). For example 8-bit encoder counter going up like that: (maximum of 127 is not reached and counter flips to 0):
0,1,...,126 -> (rollover) -> 0, 1...
while I expected it going upto 127 and then rolling over to -128, like that:
0, 1, ..., 126, 127 -> (rollover) -> -128, -127,...,0
Similarly, 8-bit counter goes down like that:
0,-1,....,-127 ->(rollover) ->0,-1...
When it would rather expected to roll over to max positive value like that:
0,-1,...,-127, -128 -> (rollover) -> 127, 126, ...,1, 0
Current workaround setting negative vounter value
counter_val = -10;
I havn't use the quarture decoder before, but would the sign of the counter in this case indicates the relative phase of A and B?
But The rollover counter value is stange and needs to be address.