PSoC 5LP Digital Filter block 2 complement

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

cross mob
Anonymous
Not applicable

Hi Everybody,

   

Why does the Digital Filter block works in 2 complements If the filter does not deal with negative values?

   

In the examples they write the following code line:

   

    /* Convert the 2's complement value to an unsigned 8-bit value
     * The VDAC expects an unsigned 8-bit value as input.
     */

   

VDAC8_SetValue(Filter_Read8(Filter_CHANNEL_A) + 128u);

   

 

   

What mechanism of conversion is that? Since I know digital electronic I've never heard about converting 2 complements in this way. For me, the binary should be inverted ( the 0s become 1s and the 1s become 0s) and then summed by 1.

   

How can I convert a non 2 complement's number to a 2 complement's number in PSoC 5LP?

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

Daniel,

   

The output of the Filter is a signed value (+/-), but DAC accepts only uint8, for that reason the output of the Filter has to be shifted to produce only positive values in 0-255 range.

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

"For me, the binary should be inverted ( the 0s become 1s and the 1s become 0s) and then summed by 1."

   

This is perfectly right for negative numbers.

   

 

   

The filter component does work with negative values. The conversion was made to ensure that the result is always >= 0 because the values were sent to the DAC which does not accept negative values.

   

"How can I convert a non 2 complement's number to a 2 complement's number in PSoC 5LP?"

   

A non 2 complement number is an uint which you may always assign to an int of bigger size which then will be treated as a 2's complement number. The underlying C-compiler is GCC, so there is (regarding this point) nothing special to PSoCs, you may consult any (modern) C-manual to check for conversions.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

But in the case of filter all the variables already are uint8 and they are 2 complement.

   

My intention is to get the signal from Channel A, made some processing ( pretty basic actually) and send It to Channel B.

   

I am doing It by C/C++ code, but I don't know how to pass the one way to another. Could you give some (very short) example?

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

Data transfer can be done using DMA or with the provided APIs for the filter.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Sorry, But when I said "pass" I was talking about converting. Let's suppose that I want to make an offset after Channel A, then It will be the input for Channel B. Is the following code correct? ( Ignore the filter effects on Channel A and Channel B in this moment, because It is not the focus now).

   

 

   

    uint8 read = Filter_Read8(Filter_CHANNEL_A+128u) ;
    uint8 offset = 50;

        read+=offset ;  
        Filter_Write8(Filter_CHANNEL_B, read-128u);
        read = Filter_Read8(Filter_CHANNEL_B) + 128u;
   
    
    VDAC8_SetValue(read);

0 Likes
Anonymous
Not applicable

I wrote another code, but this time all the operations already are made in 2 complement so It wasn't needed to convert the filter signal.

   

Thanks for the help!

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

uint8 read = Filter_Read8(Filter_CHANNEL_A+128u) ; You do not need to add 128!!!

   

read+=offset may lead to a false result when read was > 77

   

read = Filter_Read8(Filter_CHANNEL_B) + 128u; should be called only after a value has been calculated, so poll the status register of the filter. Again, why are you adding 128?? That has been done only in the DAC example to adjust the output range of the filter to the input range of the DAC.

   

There is no need to "convert" the 2's complement numbers, they are already valid 8-bit integers. When interpreting as int8 they are negative numbers when bit 7 is set, all calculations will go well.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Yes, that's right. After some time, tests and building a binary-decimal table for uint8(in 2 complements) I could get the correct results by making only 2 complements operations. Then only when I had to pass It to VDAC that I added 128u.

   

Thanks for the help!

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

You're always welcome, Daniel!

   

 

   

Bob

0 Likes