Reputation: 50
NOTE: I left out the code for the other two phases (V and W), not necessary for the example.
I'm pretty sure, the way I'm handling the "on" and "off" times like this is not an efficient way to do it.
I want to achieve an "on" and "off" pulsation using a look up table. The timer should be compared with the current selected value of the table and switches the output if the value is passed.
Problem: I think I handle the timer "wrong". I'm not getting the right amount of pulses (my understanding is, that I should get as many pulses as the values inside of the array, in this case 300+). Looking on an oscilloscope I noticed, that there are only a few pulses.
Would I be better using the "output compare register"?
What other tips/ideas do you have?
bool posTOneg = true; // Variable for the change from positive to negative. 'true' in the beginning.
///// CONFIGURATION OF THE PINS /////
int igbt_high_u = 11;
int igbt_high_v = 10;
int igbt_high_w = 9;
int igbt_low_u = 6;
int igbt_low_v = 5;
int igbt_low_w = 3;
///// LOOKUP TABLE /////
int UsinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,
44,47,49,52,54,57,59,61,64,66,69,71,73,76,78,80,83,85,88,90,92,94,97,99, //42 Werte bis hier hin
101,103,106,108,110,113,115,117,119,121,124,126,128,130,132,134,136,138,140,142,144,146,
148,150,152,154,156,158,160,162,164,166,168,169,171,173,175,177,178,180,182,184,185,187,188,190,192,193, // 90
195,196,198,199,201,202,204,205,207,208,209,211,212,213,215,216,217,219,220,221,222,223,224,225,226,227,
228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,246, // 142
247,247,247,248,248,248,248,249,249,249,249,249,250,250,250,250,249,249,249,249,249,248,
248,248,248,247,247,247,246,246,245,245,244,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234, // 190
233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,217,216,215,213,212,211,209,208,207,205,204,
202,201,199,198,196,195,193,192,190,188,187,185,184,182,180,178,177,175,173,171,169,168,166,164,162,160, // 242
158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,121,119,117,115,113,110,108,106,
103,101,99,97,94,92,90,88,85,83,80,78,76,73,71,69,66,64,61,59,57,54,52,49,47,44,42,39,37,34,32,30, // 300
27,24,22,19,17,15,12,10,7,5,2,1}; // 312
///// SIZE OF THE ARRAY /////
int sizeU = sizeof(UsinPWM)/sizeof(int); // Variable for PWM array (size)
///// OFFSET 120 GRAD /////
int indexU = 0; // zaehlervariable
///// FUNCTION FOR THE PHASE U (POS AND NEG HALF-CYCLE) /////
void posHalbU (int i)
{
if(TCNT0 < UsinPWM[i]) // Compare timer with the current value of the look up table
{
digitalWrite(igbt_low_v, HIGH); // Triggering hardware
digitalWrite(igbt_low_w, HIGH);
digitalWrite(igbt_high_u, HIGH);
}
else
{
digitalWrite(igbt_high_u, LOW);
digitalWrite(igbt_low_v, LOW);
digitalWrite(igbt_low_w, LOW);
}
}
void negHalbU(int i)
{
if(TCNT0 < UsinPWM[i])
{
digitalWrite(igbt_high_v, HIGH);
digitalWrite(igbt_high_w, HIGH);
digitalWrite(igbt_low_u, HIGH);
}
else
{
digitalWrite(igbt_high_v, LOW);
digitalWrite(igbt_high_w, LOW);
digitalWrite(igbt_low_u, LOW);
}
}
void setup()
{
Serial.begin(9600);
///// CONFIGURE PWM /////
pinMode(igbt_high_u, OUTPUT);
pinMode(igbt_high_v, OUTPUT);
pinMode(igbt_high_w, OUTPUT);
pinMode(igbt_low_u, OUTPUT);
pinMode(igbt_low_v, OUTPUT);
pinMode(igbt_low_w, OUTPUT);
}
void loop()
{
///// CALL PHASE U /////
if(posTOneg)
{
posHalbU(indexU);
}
else
{
negHalbU(indexU);
}
indexU = indexU + 1;
if(indexU == sizeU)
{
indexU = 0; // Reset of the counter variable
posTOneg = !posTOneg; // Negate the boolean value. Swapping between pos and neg all the time
}
}
Below is the result I am generating. About 20 impulses are created, and these are periodically created.
Upvotes: 0
Views: 740
Reputation: 1844
Use a for loop
to automatically index through the sine array. Write the data using analogWrite() to a PWM pin at a dedicated time interval. The time interval x array size = sine period.
The output will look terrible ... just a rapidly varying PWM signal stretching 0-5 V. After filtering appropriately, a Sine wave will magically appear.
Upvotes: 2