# Cypress Developer CommunityTM

## PSoC 5LP Digital Filter block 2 complement

Summary: 9 Replies, Latest post by Bob Marlowe on 01 Dec 2015 06:23 AM PST
User
107 posts

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.
*/

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?

User
531 posts

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.

User
9301 posts

"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

User
107 posts

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?

User
9301 posts

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

Bob

User
107 posts

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 offset = 50;

User
107 posts

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!

User
9301 posts

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

User
107 posts

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!

User
9301 posts

You're always welcome, Daniel!

Bob