# Sinusoidal generator with PSIM

Discussion in 'Programmer's Corner' started by tomson, Mar 21, 2011.

1. ### tomson Thread Starter New Member

Sep 6, 2010
16
0
I've posted it originally in the Electronics forum but guess here's the place for this kind of question.

Hello. My task is to simulate sinusoidal generator with PSIM using two methods - absolute values and table of differences. Parameteres of the generator are : Amplitude u=255 V, frequency f=50H; sampling frequency 5kHz. I have created arrays of norm and diff values with C. Now I've thought of a method of iterating through elements of the array and in each time step, and pick the corresponding value. If function is in the first quarter - iterate array from the beginning. If it will be in the second one - iterate arrayfrom the end. If quarter is third - iterate from the beginning with values multiplied by one. Finally in the fourth one - go through array from the end, multiplying elements by -1.

Now I should somehow create a function, which in each time step takes value from the array, and depending on the place on the sinusoid either multiplies it by -1 or not. But how can I store the information which element from the array should be taken now and if array should be iterated from the beggining or the end ?

Because the previous post had been deleted, here are tho seurce codes once again. First calculated arrays :

Code ( (Unknown Language)):
1.
2. int main(){
3.     double amp = 128;
4.     float norm[25];
5.     float diff[25];
6.     float s;
7.     int i;
8.
9.     for(i=0; i< 25; i++){
10.         if(i == 0){
11.             norm[i] = 0;
12.         }
13.         norm[i] = i*2*M_PI/100;
14.         s = amp*sin(norm[i]);
15.         norm[i] = s;
16.     }
17.
18.     for(i=0; i<25; i++){
19.         if(i == 0){
20.             diff[i] = 0;
21.         }
22.         diff[i] = norm[i] - norm[i-1];
23.     }
24.
25.     return 0;
26. }
27. [/i][/i][/i][/i][/i][/i][/i]

and here's template for the dll function :

Code ( (Unknown Language)):
1. __declspec(dllexport)opc1(double t, double dt, double *in, double *out){
2.     double u = in[0];
3.     out[0] = y;
4. }
5.

Last edited: Mar 21, 2011
2. ### nigelwright7557 Senior Member

May 10, 2008
488
71
You can work out which phase you are in from the angle.
0-90 = phase 1, 90-180-= phase 2 etc.

3. ### tomson Thread Starter New Member

Sep 6, 2010
16
0
this leaves only issue with finding which element from the array should be taken next.

4. ### tomson Thread Starter New Member

Sep 6, 2010
16
0
and what about storing the current counter value in a file ? Will the system manage to read/write from/to a file 5k times per second ?