Hello everyone, I've made so progress:
-
Transform bipolar to unipolar signal (analog):
-
zero-crossing detection using Schmitt trigger (analog):
-
the code so far (Arduino Due):
// set constant input pins
const int button1pin = 52; // pin 52 is button1
const int button2pin = 50; // pin 50 is button2
const int button3pin = 48; // pin 48 is button3
const int zerodetection = 40; // pin 40 is the signal of zero detection
// define variables
// time since startup
unsigned long micro; // microseconds since startup
unsigned long micro100; // for sample rate of 10 kHz
unsigned long oldtime = 0; // begin of one sample
unsigned long newtime = 0; // check if one sample is over (100 microseconds passed)
//
int button1state = 0; // state of button1, default 0
int button2state = 0; // state of button2, default 0
int button3state = 0; // state of button3, default 0
int modeselect = 0; // runmode (0 for no current, 1 for dc current, 2 for ac+dc current, 4 for current control)
int edge = 0; // edge of the input signal, 0 for negative and 1 for positve
int oldzerostate = 0; // memory for last edge
int newzerostate = 0; // stores current edge
int zeropulse = 0; // variable for one single pulse of 0.1 ms at zero-crossing
int dctimer = 0; // timer counting from 0 to 100, starts from zero when zeropulse occurs
float dctimerpi; // same as dctimer, calculated from 0...100 to 0...2 PI
int acdc; // the combined dc + ac signal for DAC1 (0 to 4095)
// main setup
void setup() {
// initialize the pushbutton pins as an input
pinMode(button1pin, INPUT);
pinMode(button2pin, INPUT);
pinMode(button3pin, INPUT);
// initialize the zero-detection as input
pinMode(zerodetection, INPUT);
// change the analog read and write resolution to 12 bits
analogReadResolution(12);
analogWriteResolution(12);
}
// main loop
void loop() {
micro = micros(); // read the micros passed since board reset and save it into the variable micro
micro100 = micro / 100; // divide micro by 100 and save it into the variable micro100 (counts up by one every 100 microseconds / 0.1 milliseconds)
if (oldtime == newtime) { // if there is no difference between oldtime and newtime
newtime = micro100; // continue to read newtime
}
else { // until newtime is different from oldtime
oldtime = micro100; // set oldtime to the current time (newtime)
// check if buttons are pressed (0 for false, 1 for true)
button1state = digitalRead(button1pin);
button2state = digitalRead(button2pin);
button3state = digitalRead(button3pin);
// read the input on analog pin 0
int analogvalue0 = analogRead(A0);
// runmode (values can be 0, 1, 2, 3, 4, 5, 6, 7)
modeselect = button1state + (2 * button2state) + (4 * button3state);
// if button1 AND button2 pressed, just use button 2
if (modeselect == 3) {
modeselect = 2;
}
// if button3 AND another button is pressed (4, 5, 6, 7), just use button 3
if (modeselect > 4) {
modeselect = 4;
}
// runmode (0 for no current, 1 for dc current, 2 for ac+dc current, 4 for current control)
switch (modeselect) {
case 1: // dc only mode
analogWrite(DAC1, analogvalue0); // set A0 = DAC1 (write the value from analog in 0 to analog out 1)
break;
case 2: // dc + ac mode
edge = digitalRead(zerodetection); // 1 for positive edge, 0 for negative edge
if (oldzerostate == newzerostate) { // if no change of the edge state (says positive or stays negative)
newzerostate = edge; // check for edge state again
zeropulse = 0; // no zero-crossing detected
}
else { // if zero-crossing detected
oldzerostate = edge; // set oldzerostate zu current state
zeropulse = 1; // activate zero-crossing pulse
}
if (zeropulse == 1) { // pulse received
dctimer = 0; // reset timer
}
else { // no pulse received
dctimer++; // increase timer by one
}
dctimerpi = (dctimer + 3) * 0.062832; // calculate 0...100 to 0...2 PI, +3 for phase correction
acdc = analogvalue0 * (0.8 - 0.2 * cos(dctimerpi)); // voltage 80% DC + 20% AC
analogWrite(DAC1, acdc); // write voltage to DAC1
break;
case 4:
//control
break;
default: // no mode selected
analogWrite(DAC1, 0); // set secondary current to zero
break;
oldtime = micro100; // program executed successfull, set oldtime to current time
}
}
}
What works:
- read analog in
- read zero-crossing
- write analog out (DC only)
- write analog out (DC + snychronized AC)

What needs to be done:
- current control (mode 4 in code) - guess this will be the hardest part

