Quadrature Decoder SetCounter() API bug?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Hi,

   

Quadrature Decoder function SetCounter() does not accept negative values (positive values are fine).

   

Example:

   

            QuadDec_SetCounter(-1); //can't set counter to a negative  value
            sprintf(strMsg1,"counter_val=%d \r\n", QuadDec_GetCounter() );
            SW_Tx_UART_PutString(strMsg1);
           

   

reading counter value in the above example returns -32767.

   

What I want is to limit counter values to some bounds, e.g. -100...+100. 

   

Regards,

   

odissey1

   

QuadDec_v2_30 (16bit)

0 Likes
12 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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 ?

   

 

   

Help.......

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

How about  

   

QuadDec_SetCounter((int)-1);

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Concerning formatiing and variadic f()'s like printf()/sprintf() -

   

 

   

    

   

          http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output

   

 

   

Also shows promotion/conversions being made by f().

   

 

   

Regards, Dana.

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Hi,

   

I do it like that (should work according to datasheet, but it doesn't):

   

            int16 counter_val;

   

            counter_val = -10;
            QuadDec_SetCounter(counter_val);       //BUG: can't set counter to negative  value

   

odissey1

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

I tested it with 8-bit QuadDecoder before with same error (but not 32bit).

   

odissey1

0 Likes
Anonymous
Not applicable

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I already informed Cypress (yesterday) but no response yet (2:00 am PST )

   

 

   

Bob

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

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

 

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Current workaround setting negative vounter value

   

int16 counter_val;
counter_val = -10;
QuadDec_SetCounter(32768-counter_val));

   

odissey1

0 Likes
Anonymous
Not applicable

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.

0 Likes