Sinewave modulation and PID

19 Jul 2010 . Edited: 19 Jul 2010

Hi,

 

I'm using the mbed based on the LPC1768 processor to generate a sinwave @ 10kHz. I'm using Digital output whit the PortOut instruction, and I plug a DAC the pin 5 to 16 to get the sinewave.

The program works fine, but when I want add code like using output to implement PID routine, the sinwave frequency decrease because of others instructions.

I post the code I use, someone can take a look and see if it's possible to optimize the program to run it faster, or if I already reach the speed limit of the mbed ?

 

Thanks,

 

#include "mbed.h"

PortOut s1(Port0, 0x18783C3);
PortOut s_test(Port1, 0x80000000);

#define GET(flag, bit) (((flag) & (1 << (bit))) >> bit )

int main() {

    int y[200];
    int i=0;

    for (i=0;i<200;i++)
       y[i]=(0.5*sin((i*2*3.14159)/200)+0.5)*4095;

//put the sinewave in a vector

    while (1) {

        for (i=0;i<200;i++) {

          s1 = GET(y[i],0)*(1 << 9 ) + GET(y[i],1)*(1 << 8 )+ GET(y[i],2)*(1 << 7 ) + GET(y[i],3)*(1 << 6 ) + GET(y[i],4)*(1 << 0 ) + GET(y[i],5)*(1 << 1 )
                 + GET(y[i],6)*(1 << 18 ) + GET(y[i],7)*(1 << 17 ) + GET(y[i],8)*(1 << 15 ) + GET(y[i],9)*(1 << 16 ) + GET(y[i],10)*(1 << 23 ) + GET(y[i],11)*(1 << 24 ) ;
             
//put the value of the vector in the port output bit by bit

        }

    }
}
19 Jul 2010

Hi Mirko,

You could always process the vector re-arrangement up-front, then that would show whether it is impacting your program performance and avoid having to recalculate each time. For example:

#include "mbed.h"

PortOut s1(Port0, 0x18783C3);
PortOut s_test(Port1, 0x80000000);

#define GET(flag, bit) (((flag) & (1 << (bit))) >> bit )

int main() {

    int y[200];
    int i=0;

    for (i=0;i<200;i++)
       y[i]=(0.5*sin((i*2*3.14159)/200)+0.5)*4095;

    for (i=0;i<200;i++) {
       y[i] = GET(y[i],0)*(1 << 9 ) + GET(y[i],1)*(1 << 8 )+ GET(y[i],2)*(1 << 7 ) + GET(y[i],3)*(1 << 6 ) + GET(y[i],4)*(1 << 0 ) + GET(y[i],5)*(1 << 1 )
                 + GET(y[i],6)*(1 << 18 ) + GET(y[i],7)*(1 << 17 ) + GET(y[i],8)*(1 << 15 ) + GET(y[i],9)*(1 << 16 ) + GET(y[i],10)*(1 << 23 ) + GET(y[i],11)*(1 << 24 ) ;
    }


    while (1) {
        for (i=0;i<200;i++) {
          s1 = y[i];             
        }
    }
}
Hope that helps,

Simon

27 Jul 2010

the easiest way would be to have a very simple RTOS on the MCU first, then you can do may complex things virtually at the same time... currently you are trying to do two different things, one after another