9 Replies Latest reply on Dec 1, 2015 6:23 AM by JoMe_264151

    PSoC 5LP Digital Filter block 2 complement

    userc_40398

      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?

        • 1. Re: PSoC 5LP Digital Filter block 2 complement
          BoTa_264741

          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.

          • 2. Re: PSoC 5LP Digital Filter block 2 complement
            JoMe_264151

            "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

            • 3. Re: PSoC 5LP Digital Filter block 2 complement
              userc_40398

              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?

              • 4. Re: PSoC 5LP Digital Filter block 2 complement
                JoMe_264151

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

                   

                 

                   

                Bob

                • 5. Re: PSoC 5LP Digital Filter block 2 complement
                  userc_40398

                  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);

                  • 6. Re: PSoC 5LP Digital Filter block 2 complement
                    userc_40398

                    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!

                    • 7. Re: PSoC 5LP Digital Filter block 2 complement
                      JoMe_264151

                      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

                      • 8. Re: PSoC 5LP Digital Filter block 2 complement
                        userc_40398

                        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!

                        • 9. Re: PSoC 5LP Digital Filter block 2 complement
                          JoMe_264151

                          You're always welcome, Daniel!

                             

                           

                             

                          Bob