//DC-DC push-pull driver(23.4kHz) + Sine modulator for full bridge(16kHz)
//N.Bastiayanov 11.2021
//MCU clock 8 MHz, BOD level 4.3V
//PB5(ADC0,RESET)used like analog input for voltage feedback (VFB).
//To avoid Reset activation,voltage on PB5 must be 2.5V min.
//PB2 is free
#define f 170 // 23.4kHz
#define freq 37 // 50Hz
#define top 23 //leight of top/bottom of sine
#define dv 244
byte d=127;
byte v;
int an=4000;
byte an1;
void setup(){
PORTB=0;
DDRB=B00011011; // PORTB 0,1,3,4 output.PB2 free
CLKPR = 0x80; CLKPR = 0; //set clock to 8MHz
customDELAY(1000000);// time for stabilize the supply voltage
// after the charge of the input capacitor
//Set timer-counter1 (DC-DC convertor)
OCR1C=f; //timer1 counting to OCR1C value-determines the frequency (23.4kHz)
OCR1B=f/2; //OCR1C value/2 for equal pulse lengths
DTPS1=B00000011; //dead time prescaller
DT1B=B00110011; //dead time 1.5us
GTCCR=B01010000; //set PB3 and PB4 for timer1 outputs
TCNT1=120; // Important!!! limiting the leight of first pulse of push-pull driver
// and prevent saturation of the pulse transformer at start.
TCCR1=B00000010; //set timer1 prescaller = start timer1
customDELAY(5000);//time for charge the HV capacitor
// Set timer-counter0 (sine modulated PWM)
OCR0A =0;OCR0B =0;
TCCR0A = B11110001;
TCCR0B = B00000001;
}
//Sine modulation
void loop() {
for (byte p = 0; p < 2; p++) { //positive and negative semiperiod
//the first half of the semiperiod,positive or negative depending on p
byte y = 0;
for (byte b = 6; b > 0; b--) { //6 sections, each with a decreasing slope and leght
for (byte a = 0; a < 3+b*2; a++) {//decreasing leght of sections (0-14 to 0-4)
y = y + b;
v=y*d/dv;
if(p){OCR0A = 127-v;OCR0B =127+v;}
else {OCR0A = 127+v;OCR0B =127-v;}
customDelay(freq);
}
}
//Last(7-th) horizontal section
for(byte a=1;a<top;a++){customDelay(freq);}//top/bottom of sine
//Voltage feedback via PB5
int s=analogRead(A0);
if(s>700&&d>0){d--;}
if(s<698&&d<127){d++;}
//d=127;
//second falf...
for (byte b = 1; b < 7; b++) { //6 sections, each with a increasing slope and leight
for (byte a = 0; a < 3+b*2; a++) {//increasing leght of sections (4-0 to 14-0)
y = y - b;
v=y*d/dv;
if(p){OCR0A = 127-v;OCR0B =127+v;}
else {OCR0A = 127+v;OCR0B =127-v;}
customDelay(freq);
}
}
}
}
void customDelay(byte l) { //small precision delay
for (byte m = 0; m < l; m++) {
__asm__("nop\n\t");
}
}
void customDELAY(unsigned long l) { //big delay
for (unsigned long m = 0; m < l; m++) {
__asm__("nop\n\t");
}
}