Would love your help with a project I am working on. I'm working on an inverter project using irfz44n and bc547.My code, proteus design and output is down below. What can i do to get better output?
/*
This code was based on Swagatam SPWM code with changes made to remove errors. Use this code as you would use any other Swagatam’s works.
Atton Risk 2017
*/
const int sPWMArray[] = {500,500,750,500,1250,500,2000,500,1250,500,750,500,500}; // This is the array with the SPWM values change them at will
const int sPWMArrayValues = 13; // You need this since C doesn’t give you the length of an Array
// The pins
const int sPWMpin1 = 3;
const int sPWMpin2 = 5;
// The pin switches
bool sPWMpin1Status = true;
bool sPWMpin2Status = true;
void setup()
{
pinMode(sPWMpin1, OUTPUT);
pinMode(sPWMpin2, OUTPUT);
}
void loop()
{
// Loop for pin 1
for(int i(0); i != sPWMArrayValues; i++)
{
if(sPWMpin1Status)
{
digitalWrite(sPWMpin1, HIGH);
delayMicroseconds(sPWMArray[i]);
sPWMpin1Status = false;
}
else
{
digitalWrite(sPWMpin1, LOW);
delayMicroseconds(sPWMArray[i]);
sPWMpin1Status = true;
}
}
// Loop for pin 2
for(int i(0); i != sPWMArrayValues; i++)
{
if(sPWMpin2Status)
{
digitalWrite(sPWMpin2, HIGH);
delayMicroseconds(sPWMArray[i]);
sPWMpin2Status = false;
}
else
{
digitalWrite(sPWMpin2, LOW);
delayMicroseconds(sPWMArray[i]);
sPWMpin2Status = true;
}
}
}
const int ArraySize = 13; // You need this since C doesn’t give you the length of an Array
const int sPWMArray[ArraySize] = {500, 500, 750, 500, 1250, 500, 2000, 500, 1250, 500, 750, 500, 500}; // This is the array with the SPWM values change them at will
const int sPWMpin1 = 3;
const int sPWMpin2 = 5;
bool sPWMpin1Status = true;
#define makePinHIGH(b) (b)<13?(b)<8?PORTD|=1<<(b):PORTB|=1<<(b-8):(PORTC|=(1<<(b-13)))
#define makePinLOW(b) (b)<13?(b)<8?PORTD&=~(1<<(b)):PORTB&=~(1<<(b-8)):PORTC&=~(1<<(b-13))
void setup(){
pinMode(sPWMpin1, OUTPUT);
pinMode(sPWMpin2, OUTPUT);
}
void loop(){
for (int i = 0; i < ArraySize; i++) {
if (sPWMpin1Status) {
makePinLOW(sPWMpin2);
makePinHIGH(sPWMpin1);
sPWMpin1Status--;
}
else {
makePinLOW(sPWMpin1);
makePinHIGH(sPWMpin2);
sPWMpin1Status++;
}
delayMicroseconds(sPWMArray[i]);
}
}
The point of having a simulation is that you can safely try different designs? If you actually build a circuit, please try not to electrocute yourself.
An inverter like that can simply be bitbanged at 50 Hz (or 60 Hz).
No need for sine wave.
The transformer will filter off the edges. The result will not be an exact sine, but close enough for most applications.