Author Topic: dsPIC33E DSP in MPLABX XC16  (Read 1059 times)

0 Members and 1 Guest are viewing this topic.

Offline NiHaoMikeTopic starter

  • Super Contributor
  • ***
  • Posts: 9390
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
dsPIC33E DSP in MPLABX XC16
« on: August 30, 2020, 10:26:20 pm »
Is there a good example out there of how to properly implement DSP using MPLABX XC16 on the dsPIC33E? The examples I have seen are in assembly, but I prefer to use C.

I'm looking to implement a power measurement algorithm that takes in two signed arrays of 16 bit voltage and current readings (1024 samples long, about 10-20 times per second) as well as measure the DC offset of those readings so they can be compensated for during the downsampling stage. I know I could use a standalone power meter chip for that but I'm already using the dsPIC for other tasks like generating the subcarrier frequencies and encoding data for transmission over the power lines.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3635
  • Country: it
Re: dsPIC33E DSP in MPLABX XC16
« Reply #1 on: August 31, 2020, 05:39:22 am »
Nah, just use assembly. Better yet, create a function in assembly and call it from C with a function prototype.. i can show you how if you need

The reason is that some instructions like REPEAT and DO  are not used by the compiler (exception: REPEAT is ALWAYS used along with the divide instruction as it's a requirement)
Also, you can't use some specialized functions like the modulo address generator
Also, the usage of the builtins that have to deal with the accumulators is clunky at best.
Also, the compiler may not be as smart in register usage with dsp instructions
Also, depending on the dsPIC in question you may want to do the thing in its own ISR using its own register set like for mantaining pointers and intermediate results. Better handle that in assembly



At first for DSP and lower level stuff like table load and store in flash i used inline assembly, then i converted everything to C because it's better that the compiler don't see inline assembly (it breaks its operations, also easier to mantain), then i created assembly files called with C function prototypes, the best approach
« Last Edit: August 31, 2020, 05:41:35 am by JPortici »
 

Online Berni

  • Super Contributor
  • ***
  • Posts: 5121
  • Country: si
Re: dsPIC33E DSP in MPLABX XC16
« Reply #2 on: August 31, 2020, 06:14:33 am »
You can add a single assembly file into your project, write a function in it and then call that function from the C code.

But for munching 20000 samples per second i really don't see why it would need to be tightly optimized DSP code. Just do it in C, turn on optimizations and it should be plenty fast enough. You can try a bit of manual loop unrolling to help the loop run faster. But if its still too slow for you then just go look into the disassembly of this C function and see what is taking too long, perhaps giving you hints on how to write the C function better. And if that is still not fast enough you could just copypaste the disassembled function into a assembler source file and optimize it yourself by hand.

Squeezing the very last drop of performance out of a CPU unfortunately does mean messing with assembler. But if you do things right C code should be fast enough for the job in most cases.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf