Newbie here!! Starting a loop while its still finishing?

Hi guys

I have an issue and I apologise if its simple and I'm stupid.

I wish to control 6 MOSFETS to change a single phase DC current to square wave 3 phase.

In order to do so I need to run 6 pins but as the loop ends the start must begin before a full stop to stay in phase.

I would rather be pointed in a direction rather than a straight up answer..

Also is his possible??

I'm not sure I understand what you are asking here. The time between calls to loop will be tiny (a few clock cycles so just a few microseconds).
While I appreciate you want to work it out for yourself a bit, some more clarity would be needed to allow us to point you in the right direction.

Ok, I'm sorry for the lack of information

I will begin with what i call the start.

I will be using Square Digital Waves here not Analogue

3 Phase AC Sin Wave

in a 50Hz 1 Full Cycle is 2Hz (Positive Wave + Negative Wave)

Now, The First Phase Begins at 0 degrees
The Second Begins at 120 Degrees
The Third at 240 Degrees

However the First Phase ends at 360 degrees (2Hz)
The Second Phase Begins while the first one is finishing, and third phase begins while the second is finishing.

I need the First Phase to begin as the Third finishes. But so Far all i can get is a loop that doesn't bring in the first wave with the third.

How can i really do this? btw im learning in C

This was my failed attempt.

int PhaseOnePin_P = 22;
int PhaseTwoPin_P = 24;
int PhaseThreePin_P = 26;
float PhaseDelay = 400;
int PhaseDuration = 100;
int BufferDelay = 1;

void setup()
{
pinMode (PhaseOnePin_P, OUTPUT);
pinMode (PhaseTwoPin_P, OUTPUT);
pinMode (PhaseThreePin_P, OUTPUT);
}

void loop ()
{
digitalWrite (PhaseOnePin_P, HIGH);
delay (PhaseDelay);
digitalWrite (PhaseTwoPin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseOnePin_P,LOW);
delay (PhaseDuration3);
digitalWrite (PhaseThreePin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseTwoPin_P, LOW);
delay (PhaseDuration);
digitalWrite (PhaseOnePin_P, HIGH);
delay (PhaseDuration
2);
digitalWrite (PhaseThreePin_P, LOW);

}

There is probably a better way to do it, but when I plot out your signals, both phase one and two are high for 5/12 of a cycle. I think you need to write low before the phase delay.

  digitalWrite (PhaseOnePin_P, HIGH);
  delay (PhaseDelay);
  digitalWrite (PhaseOnePin_P, LOW);
  digitalWrite (PhaseTwoPin_P, HIGH);
  delay (PhaseDelay);
  digitalWrite (PhaseTwoPin_P, LOW);
  digitalWrite (PhaseThreePin_P, HIGH);
  delay (PhaseDelay);
  digitalWrite (PhaseThreePin_P, LOW);

It should be possible to do it completely with that technique, split your period into 6, then do it as:
1 ON, 2 OFF, 3 ON
1 ON, 2 OFF, 3 OFF
1 ON, 2 ON , 3 OFF
1 OFF, 2 ON, 3 OFF
1 OFF, 2 ON, 3 ON
1 OFF, 2 OFF, 3 ON

If you set the outputs on each part it will always stay in phase perfectly

C2* if i turn off my pin before the phase delay the phase will not get any signal will it not?. It needs to get signal for 5/12 then a delay of 2/12 then negative on 5/12, This is to do with the way the MOSFET's are working, if by chance i turn the positive and negative on at the same time it will be a dead short and i will have dust for MOSFET's. Also at 50Hz and a delay of 200 micro seconds between cross overs, I will never see the delay with a motor.

I managed to work it out by using some If statements, but I'm still seeing some little problems. One is that my PhaseThreePin_P turns on at the start, even though i stated only after the count reaches 1.

Could i have a digitalRead so that it will correct its self in it goes out of phase?

How difficult would it be to time everything with the on board Oscillator??

There also must be a Much cleaner way to write this?

void loop ()
{
digitalWrite (PhaseOnePin_P, HIGH);
if (count = 1);
{
digitalWrite(PhaseThreePin_P,HIGH);
count = 0;
}
if (count =1);
{
delay(PhaseDuration);
digitalWrite (PhaseThreePin_P, LOW);
}
delay (PhaseDuration2);
digitalWrite (PhaseTwoPin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseOnePin_P,LOW);
delay (PhaseDuration
2);
digitalWrite (PhaseThreePin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseTwoPin_P, LOW);
delay (PhaseDuration*2);
digitalWrite (PhaseOnePin_P, HIGH);

Those if statements are wrong. if (count =1) will always be true, = assigns the value of count and then returns the result of the assignment, it should be a ==, i.e. if (count==1), which is a comparison. Also you shouldn't have semi-colons at the end of your if() line, that terminates the if statement there.

Irish_Features:
There also must be a Much cleaner way to write this?

There is :slight_smile: the way I suggested:

void loop(){
   setPins(HIGH,LOW,HIGH);
   delay(Period_over_6);
   setPins(HIGH,LOW,LOW);
   delay(Period_over_6);
   setPins(HIGH,HIGH,LOW);
   delay(Period_over_6);
   setPins(LOW,HIGH,LOW);
   delay(Period_over_6);
   setPins(LOW,HIGH,HIGH);
   delay(Period_over_6);
   setPins(LOW,LOW,HIGH);
   delay(Period_over_6);
}

void setPins(char pin1,char pin2,char pin3){
   digitalWrite(Phase1Pin_P,pin1);
   digitalWrite(Phase2Pin_P,pin2);
   digitalWrite(Phase3Pin_P,pin3);
}

I'm not sure how it should be, but I've plotted both mine and toby's output. I added what I suspect could be the sine wave of an LRC circuit on mine. I'd like to figure out how it's done as well as I do have a few 3-phase motors I'd like to run.

3-phase.png

Usually you drive 2 phases at a time, the other being open-circuit. That does require 6 pins to
control all 6 MOSFETs of the 3-phase bridge. Its called trapezoidal drive.

LADS!!!!!

Thank you very much, im so happy with this!

I just need to code in an On switch and Off Switch
My final Code For the phase shift is:

int Period_over_6 = 50;
int Phase1Pin_P = 22;
int Phase2Pin_P = 24;
int Phase3Pin_P = 26;
int Phase1Pin_N = 23;
int Phase2Pin_N = 25;
int Phase3Pin_N = 27;

void setup (){
pinMode (Phase1Pin_P, OUTPUT);
pinMode (Phase2Pin_P, OUTPUT);
pinMode (Phase3Pin_P, OUTPUT);
pinMode (Phase1Pin_N, OUTPUT);
pinMode (Phase2Pin_N, OUTPUT);
pinMode (Phase3Pin_N, OUTPUT);
}

void loop(){
setPins_P(HIGH,LOW,HIGH);
setPins_N(LOW,HIGH,LOW);
delay(Period_over_6);
setPins_P(HIGH,LOW,LOW);
setPins_N(LOW,HIGH,HIGH);
delay(Period_over_6);
setPins_P(HIGH,HIGH,LOW);
setPins_N(LOW,LOW,HIGH);
delay(Period_over_6);
setPins_P(LOW,HIGH,LOW);
setPins_N(HIGH,LOW,HIGH);
delay(Period_over_6);
setPins_P(LOW,HIGH,HIGH);
setPins_N(HIGH,LOW,LOW);
delay(Period_over_6);
setPins_P(LOW,LOW,HIGH);
setPins_N(HIGH,HIGH,LOW);
delay(Period_over_6);

}

void setPins_P(char pin1,char pin2,char pin3){
digitalWrite(Phase1Pin_P,pin1);
digitalWrite(Phase2Pin_P,pin2);
digitalWrite(Phase3Pin_P,pin3);
}

void setPins_N(char pin4,char pin5,char pin6){
digitalWrite(Phase1Pin_N,pin4);
digitalWrite(Phase2Pin_N,pin5);
digitalWrite(Phase3Pin_N,pin6);
}

That's the same square wave drive, not trapezoidal.

BTW how are your MOSFETs wired - are they all n-channel? Using suitable drivers with bootstrapped gate drive
voltage? Or are the high-side MOSFETs p-channel? What voltage?

The voltage is max 5kW but I have not decided just yet...

After some thought... That code won't do what I want :frowning:

The crossover time between MOSFETS needs to be small but it cannot be the same as the phase duration... I want a variable option, with time....

An analogue input (potentiometer) to serial read the value....

BUT....

I cannot run 3 voids at one time can I??? Or is there another option??

I don't know what you mean by 'a variable option, with time' but if you want a timed overlap / underlap between the outputs for different phases switching, then simply handle that in the same way you're handling the rest of the timing.

I cannot run 3 voids at one time

No you can't, and please stop calling them 'voids'. They are functions. Loop() is a function. The word void is an adjective - it is the return type of the function. Void loop() is a function named loop which has a void return type.

That would be a very easy change to make:

void loop(){

   int val=analogRead(A0);
   Period_over_6=map(val,0,1023,MIN_PERIOD,MAX_PERIOD);

   setPins_P(HIGH,LOW,HIGH);
   setPins_N(LOW,HIGH,LOW);
   delay(Period_over_6);
   setPins_P(HIGH,LOW,LOW);
   setPins_N(LOW,HIGH,HIGH);
   delay(Period_over_6);
   setPins_P(HIGH,HIGH,LOW);
   setPins_N(LOW,LOW,HIGH);
   delay(Period_over_6);
   setPins_P(LOW,HIGH,LOW);
   setPins_N(HIGH,LOW,HIGH);
   delay(Period_over_6);
   setPins_P(LOW,HIGH,HIGH);
   setPins_N(HIGH,LOW,LOW);
   delay(Period_over_6);
   setPins_P(LOW,LOW,HIGH);
   setPins_N(HIGH,HIGH,LOW);
   delay(Period_over_6);
}

You have not said how these are wired up. You were also asked about the voltage and you answered with a power.
You need to show what hardware you have before you can even begin to write code for it.
Normally the hardware for this would be a h- bridge not just FETs.

Wow.... Are first time 22 year old engineering STUDENTS always insulted so strongly?? I started my statement with " I haven't decided yet" so I though, we'll instead of not answering I will give a power, then that person may think "Oooooohhhh he's running a more industrial system, that may rule this/ that out..."

Variable ... With time.. Basically, that cryptic statement may be decrypted to say I wish to vary time, and delays being my only use of "Time" I wish to have variable delays....

Voids ...... I'm sorry, I said this once... So one time is clearly waaaayyyy to many. I see that you understood exactly what I said though... So need for the attitude?? Wish I was as wise...

I could not be bothered with this shit... Good luck!! DELETE THIS THREAD.....

Are first time 22 year old engineering STUDENTS always insulted so strongly

No only the hyper sensitive ones.

Wish I was as wise...

you will be one day when the chip is removed from you shoulder and you stop throwing your toys out of the pram.

So you badly explain your situation and then get upset when some on asks you to explain.
If you think this is attitude then you ain't seen nothing yet.

I could not be bothered with this shit.

It's free advice, if you don't like it then you can always get your money back.

DELETE THIS THREAD

No.

ok..

I do apologize, My head was in a really shit place when i Wrote this, and for some reason i just went a bit mental.

I respect that this is free advise for my benifit and everyone is only trying to help. So i would like to retract my previous outburst and substitute a more Cheery "Im sorry, Im new to this game"

Grumpy Mike, I agree, the chip has been sanded down.. I will take my nose out of the air now and chill.

OK let me Begin again if i can.

I'm not sure the voltage as I'm not designing the circuits, my dad (electrical engineer) is, I just wanted to learn how to program to PIC16F627, But i want to begin learning with the arduino because it seems easier. and i presume with a little translation, what works on the arduino will work on the PIC?

Mission Statement
I wish to Control 6 MOSFET's, to produce a Square Wave 3 Phase Alternating Current.

I also wish to add an On switch and an Off switch, aswell as a Potentiometer that will control Frequency.

Now, As im new to the C Language i find it difficult to write a code to run 3 separate Functions at one time.

The Positive and Negative Pulse i wish to have a duration that is Variable with the Potentiometer and a Cross over period between both pulses so that i don't end up with a Direct Short.

Basically im recreating a Variable Speed Drive.

OK let me Begin again if i can.

Yep no problems :slight_smile:

what works on the arduino will work on the PIC?

Yes if you are programming the PIC in C, the arduino has lots of built in functions to extend the language but that is what functions do. All the source code is available if you want to transfer the function to another platform.

As im new to the C Language i find it difficult to write a code to run 3 separate Functions at one time.

Basically the system will only do one thing at a time, that is called being single threaded. It doesn't stop you from writing stuff that looks like it is doing three things at once.

I wish to Control 6 MOSFET's, to produce a Square Wave 3 Phase Alternating Current.

There are lots of ways of doing this, I have drawn out a three phase square wave for you it is attached.
If you look at it you can see there is a basic time interval, you can control this with a delay function with the value being given from reading a pot.
Now if you number each of those intervals you will see when each phase signal has to change state. So you need to write a loop and have a loop counter for one cycle. At each count you need to write your output high or low as specified by the diagram.
Have a go at coding that and see how you go. You can always come back and ask if you are stuck.

3 phase (4.33 KB)