7/05/2018

HOW TO MAKE SMPS FERRITE TRANSFORMER (CHOPPER)

We are going to make a ferrite or chopper transformer in a very simple and more understanding way.

Chopper transformers or ferrite core transformers are popularly called SMPS transformers which are found almost in all current electronic gadgets, mostly in power inverters.

Though chopper transformers or ferrite core transformers makes devices less bulky and more portable, most technicians and up-coming engineers sees it as big challenge because it seems to be complex in their world as beginners.

In this practical work, we are going to learn how to make a ferrite core transformer for DC to DC as well as AC to DC.

I will also give you a part 2 of this, which we will learn how to make a DC to AC ferrite pure sine inverter or High frequency inverter.

FERRITE TRANSFORMER PRACTICAL WINDING 


In this first practicals, we will learn how to wind a ferrite transformer to step down DC voltage and also how to step up DC voltage.

Things needed:

Caliper or Rule, ferrite core, AWG 26 or any, Calculator.


WINDING RULES:

1. Don't use a single thick wire.

2. Don't use bare or uncoated wire.

3. Insulate each section of winding. 


START OF PRACTICAL WORK.

Lets make a 48V DC transformer with two outputs; 15V DC and 12V DC using the circuit diagram below:

At the end of this practicals, we can use the ferrite transformer in our DSPIC30F2010 inverter board in this link


For this work, am going to use a core i took from an old board. I don't know the core area, so i will measure it using my caliper.

First, you have to remove the core from the bubing and remove all old winddings as well. The ferrite core can be separated by applying heat. I separated mine by heating it around 400 degree Celsius. There are times that i have also separated it by putting soldering iron on it for some time.

Now measure length and width of the bubing as shown in image 2.

Image 1



Image 2 



CALCULATING THE NUMBER OF PRIMARY TURNS 



To calculate the primary number of turns for our 48V step down converter, The formula below is used.




From the formula, we need to look for Ae, freq and Vmax. before we can calculate for the number of primary turns. 

Ae has already been measured and calculated as 0.72 cm square.

Vmax is the maximum voltage we can have from a 48V battery and that is 60V


Frequency to be used for this converter is 78000 Hz using the frequency calculation formula on our circuit diagram. That formula is given in the data sheet of the UC3845 IC.

Now that we have all the parameters in the formula, we can calculate the number of turns by putting the values into the formula as below:



Now we know our number of primary turns. Lets calculate for number of secondary turns.

Please note that, if the converter is connected to a 48V battery system, the battery voltage will go close to 60V during charging and will drop to about 40V when battery is in use.

For us to get a continuous stable output even when battery drops to 40V, a built in PWM function of the switching IC is implemented through feedback.

Though PWM will work, the PWM has a maximum width (duty ratio). With our UC3845, the max duty ratio is 50%. This means that when battery level is high, duty ratio will be less than 50%.

On the other hand, as battery is draining, the switching time (duty cycle) will be increased but only to a maximum of 50%.

As the switching IC switches at max duty cycle of 50% and output voltage drops, the switching transistor will overheat and burn as a result of no regulation.

It is therefore necessary to calculate secondary turns in a way that will factor battery draining in the calculation.



SECONDARY TURNS CALCULATIONS



FACTORING BATTERY DRAIN

1. Subtract Min voltage from Max Vmax (60V-40V) = 20V

2. Divide answer by 2 ( 20/2) = 10V

3. Add answer in point 2 to all your expected output voltage (15V+10V) = 25V

4. Add answer in point 2 to all your expected output voltage (12V+10V) = 22V

now because we want to always have a stable output voltages, 10V which we got from point 2 is added to all our output voltages so that we can calculate for secondary turns which will keep our outputs always regulated even when the battery gets as low as 36V.

15V output Number of secondary Turns


NUMBER OF TURNS FOR 12V OUTPUT (AUXILIARY)




HOW TO DO THE WINDING



The winding of our high frequency transformer can now be done by:

1. Using a single 26 AWG copper and wind 71 turns in clockwise on the bubing.

2. When done, insulate the primary windings using heat resistance tape.

3. Double the 26 AWG copper and wind 30 turns in clockwise on the same bubing to get secondary 15V and insulate as done before.

4. Finally, wind the Auxiliary 12V by going 26 turns in clockwise and insulating it using heat resistance tape.

Example 2: lets make a ferrite transformer to step down 180V- 240V AC to 13.2V DC using the circuit below.




To start calculating for our number of turns, we have to convert our input AC range (180V AC - 240V AC) to DC range.

This has to be done because the transformer we are going to design will work on a rectified DC input to produce an output.

To convert our Input AC to equivalent DC (RMS), we multiply our input AC by the square root of two as shown below.
Now our input DC range obtained after rectification will be

255.6VDC to 339.4VDC.

We can now freely calculate for the number of primary turns using same steps as we did for the 48V step down.



CALCULATING NUMBER OF TURNS FOR STEP UP DC TO DC CONVERTER TRANSFORMERS



Step up DC to DC transformer calculations are same as step down calculations. This time a lower voltage is your input and a higher voltage is your output. You can also use the circuit above for step up converters.


Please note that all calculations done here applies to step up converters too.


Don't forget to leave your questions and answers in the comment box.


THANKS TO ALL READERS.

DSPIC30f2010 PURE SINE INVERTER CIRCUIT WITH CHARGER

This is a highly reliable sine wave inverter circuit designed using digital signal processing IC (DSP30f2010) made by micro-chip.

DSP30f2010 PURE SINE INVERTER WITH CHARGER

Read dsp30f2010 mppt from HERE

THE DSP30f2010 INVERTER . 

The circuit am sharing today is an inverter circuit which can also charge every inverter/solar battery with a selected charge current.
As said earlier, the inverter / circuit runs on a digital signal processing micro-controller made by micro-chip. You can download the datasheet by clicking Here.

After testing the code and the hardware as designed by Mr. Sanjay, I decided to modify it so that it can be able to stand all tests of time. After the modification of hardware and code, the system has truly become 99% reliable and has the following functions and protections.


1. Ability to work as inverter or UPS.

2. Mosfet protection interrupt

3. Better battery charging algorithm

4. Low voltage protection

5. high voltage Protection

6. Works on 12V, 24V, 48V, 96V, 120V

7. Overload protection

8. Over charging protection

9. Over battery protection

10. Load display In KVA

11. Cooling fan failure detection and tripping with alarm

12. NTC sensor failure detection and tripping with alarm

13. Over Temperature protection

14. The circuit board can be used to repair all pure sine inverters.

15. Set your own maximum load, charging current and set almost everything by yourself if you don't   want the default values

16. Low battery alarm

17. Low Battery cut-off


18. The board can be used for control board replacement for all single phase low frequency pure sine wave inverters (MUST, POWER STAR, FELICITY, KAMA POWER, POWER JACK etc. )


19. The control board can be used to build high current 3 stages charger with current rating from 1A-100A

20.  will use either 16x2 or 16x4 LCD

21. Source code can be use for SMPS INVERTERS AS WELL





   12V-120V INVERTER PURE SINE WAVE CONTROL BOARD CIRCUIT



 VERSION 3.0

to make your own inverter transformer for this circuit click below



Read more on make inverter transformer


MOSFET BRIDGE CIRCUIT (POWER AMPLIFIER CIRCUIT)

The mosfet bridge circuit for pure sine inverters is also know as H-bridge or Power amplifying Stage. Click below to read more
about mosfet bridge circuit and how it works.



BUY MOSFET H BRIDGE BOARD



DSP30F2010 INVERTER CONTROL BOARD

Below are pictures of the professional inverter board.
it can be used for 12V systems, 24V systems, 48V system with no modification to the parts. 96V and 120V systems will need to change R11 to a higher value.

BUY CONTROL BOARD WITH OR WITHOUT COMPONENTS

If you are interested in buying the control boards together with a programmed DSPIC30f2010, kindly send me email 
opanin17@gmail.com



BUY PLAIN BOARD WITH PROGRAMMED IC





BUY ASSEMBLED PCB WITH PROGRAMMED IC


You can buy either PCB with the micro controller which is already programmed or buy only the programmed micro controller and use the circuit on this site to build your own PCB.

SIGNAL FROM MOSFET DRIVE WITH 20KHz. PWM

DSPINVERTER drive







BUY PROGRAM SOURCE CODE

If anyone is intrested in buying the source code for programing the PIC micro controller for this inverter, You can contact.
Mr. SANJAY by sending him mail on microsanju@gmail.com

You can also buy source code from me by sending me mail on opanin17@gmail.com.

People who are not into C programming can buy just the HEX file to program the ic without edditing. You should therefore state what you need in your mail.



CODE FOR DSPIC30F2010 PURE SINE INVERTER WITH CHARGER


//////////////////////////////////////////////////////////////////////
#include <p30f2010.h>
#include <libpic30.h>


_FOSC(CSW_ON_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
_FBORPOR(MCLR_EN & PBOR_ON & BORV_42 & RST_PWMPIN & PWMxH_ACT_HI & PWMxL_ACT_HI);
_FGS (CODE_PROT_ON)
#define buzzer    LATEbits.LATE4
#define change    LATDbits.LATD1
#define stbyinv 1
#define upsmode 2
#define invmode 3
#define modekey 4
#define upkey   5
#define downkey 6
#define stbyups 7
#define fanheat 700


///////////////////////////////////////////////////////////////////////////////////////////
 const signed int sine_table[91]={0,174,348,523,697,871,1045,1218,1391,1564,1736,1908,2079,2249,2419,2588,2756,2923,3090,
                  3255,3420,3583,3746,3907,4067,4226,4383,4539,4694,4848,5000,5150,5299,5446,
                  5591,5735,5877,6018,6156,6293,6427,6560,6691,6819,6946,7071,7193,7313,7431,7547,7660,7771,
                  7880,7986,8090,8191,8290,8386,8480,8571,8660,8746,8829,8910,8987,9063,9135,9205,9271,9335,
                  9396,9455,9510,9563,9612,9659,9702,9743,9781,9616,9848,9876,9902,9925,9945,9961,9975,9986,
                  9993,10000,10000};

signed int factory[17]={3,12,0,350,220,250,400,180,400,180,141,104,102,117,50,10};
signed int setting[17];
char arr[4];

struct
{
unsigned int batt_full:1;
unsigned int frstdisp:1;
unsigned int setup:1;
unsigned int gravity:1;
unsigned int onflag:1;
unsigned int vcorrect:1;
unsigned int chrcorrect:1;
unsigned int chon:1;
unsigned int swon:1;
unsigned int nofeed:1;
unsigned int shorttrip:1;
unsigned int zincfail:1;
unsigned int mainsok:1;
unsigned int lbwarn:1;
unsigned int olwarn:1;
unsigned int olcut:1;
unsigned int lbcut:1;
unsigned int fault:1;
unsigned int hiload:1;
unsigned int msgrtn:1;
unsigned int chrmsgrtn:1;
unsigned int firston:1;
unsigned int fan_error:1;
unsigned int over_battery:1;
unsigned int ntc_fail:1;
}flags;

unsigned int chshoot;
unsigned int chshootdly=0;
unsigned int flicker=0;
int *adjust;
char *ptr;
int adj;
int *value;
unsigned int chadj;
unsigned int champs1;
signed int duty_cycle_1;
signed int p=0,flag=1,rising=1;

signed int pr=0,kflag=1,rising1=1;
unsigned int amplitude=0;

unsigned int pb,qb,cth,ctl;
////////////////////////////////
/*
///my settings to display load in wattage//

unsigned int overload_1;
unsigned int overload_2;
unsigned int max_load;
unsigned int total_load;
unsigned int load_decimal;
unsigned int ld_p;

 */

///////////////////////////////
unsigned int battery_full;
unsigned int setdly;
unsigned int menudelay;
unsigned int lcdtmr;
unsigned int lcdtmr1;
unsigned int counter;
unsigned int resumedelay;
unsigned int buzzcount;
unsigned int buzzdelay;
unsigned int buzzoftme;
unsigned int buzzontme;
unsigned int peakfail;
unsigned int oldelay;
unsigned int tripdly;
unsigned int rounds;
unsigned int chrdly;
unsigned int slowdly;
unsigned int fbdly;
unsigned int ctfaildly;
unsigned int deadshortdly;
unsigned int lbdly;
unsigned int acdly;
unsigned int keydly;
unsigned int msgrtndly;
unsigned int gravitydly;
unsigned int fltdly;
unsigned int blinkdly;
unsigned int startdly;
////////////////////////////////
unsigned long endtimer;
unsigned long controlavg;
unsigned long mainsavg;
unsigned long outavg;
unsigned long wattsavg;
unsigned long batavg;
unsigned long keyavg;
unsigned long heatavg;
unsigned int  modeavg;
/////////////////////////////////
unsigned int indummy;
unsigned int outdummy;
unsigned int ctdummy;
unsigned int keys;
unsigned int ntcvalue;
unsigned int batts;
unsigned int batrecharge;
//////////////////////////////////
unsigned int total_loaddisp;
unsigned int loaddisp;
unsigned int acoutdisp;
unsigned int mainsdisp;
unsigned int battdisp;
unsigned int battvolts;
unsigned int batrestart;
unsigned int loadpercent;
unsigned int champs;
unsigned int chdisp;
unsigned int mainsvolt;
unsigned int acout;
unsigned int keyvalue;
unsigned int key;
unsigned int heat;
unsigned int post;
unsigned int err;
unsigned int cherr;
unsigned int prect;
unsigned int initcount;
int yaxis;
///////////////////////////////////
signed int   upspeak;
signed int   acpeak;
signed int   ctpeak;
signed int   batclb;
signed int   mainsclb;
unsigned int chrclb;
unsigned int loadclb;
unsigned int pdctemp;
////////////////////////////////////

unsigned int deadshort;
unsigned int controlvolt;
unsigned int setvout;
unsigned int setchramp;
unsigned int setoverload;
unsigned int setupshi;
unsigned int setupslo;
unsigned int setinvhi;
unsigned int setinvlo;
unsigned int setbatful;
unsigned int setbatwrn;
unsigned int setbatlo;
unsigned int setbatres;
unsigned int defaults;
unsigned int setmaxwatts;

/////////////////////////////////////

unsigned int x,y,u;
unsigned long Ax,bx,Wt;
unsigned int aclo;
unsigned int achi;
unsigned int zinc;

/////////////////////////////////////
void InitADC1();
extern void Eeprom_WriteWord(unsigned short  pushAddressOffset, unsigned short value);
extern unsigned short Eeprom_ReadWord(unsigned short  pushAddressOffset);
void delay_ms(unsigned int gs);
void delay_us(unsigned int gs);

///////////////////////////////////////////////
const char str0[17]= "SYSTEM SETTINGS" ;
const char str20[17]=" BATTERY :    V" ;

//const char str24[17]="SWITCH IS ON ";
const char str25[17]="MAINS INPUT:   V";
const char str26[17]=" OUTPUT :   V   ";
const char str27[17]=" LOAD :    %    ";
//////////////////////////////////////////
const char str29[17]="SORRY BATT. LOW" ;
const char str30[17]="WAS OVERLOADED " ;
const char str31[17]="SHORT CCT FAULT" ;
const char str32[17]="  LOW BATTERY"   ;
const char str33[17]=" OVER LOADING"   ;
const char str34[17]="  NO OUTPUT "    ;
const char str35[17]="TRIP:SYNC FAULT ";
const char str36[17]="CHRGING.CUR:    ";
const char str38[17]="MANY CIRCUITS ";

//const char str40[17]="   FAULT";
const char str41[17]="  CHECK OUPUT   ";
const char str42[17]=" CHK FET/FORMER ";
const char str43[17]="  SYSTEM IS OFF ";
const char str44[17]="! REDUCE LOAD   ";
const char str45[17]="CONNECT CHARGER ";
const char str46[17]=" AUTO START ON  ";
const char str47[17]=" CHARGING MODE  ";
const char str48[17]="BATTERIES ERROR ";
const char str49[17]="REDUCE BATTERIES";
const char str50[17]="BATTERY IS FULL ";
const char str51[17]="CHGING COMPLETE ";
//const char str52[17]="LOAD:   WATT";
const char str53[17]="TEMPERATURE HIGH";
const char str54[17]="  CHECK FAN"     ;
const char str55[17]="HEAT SENSOR ERR" ;
const char str56[17]="   CHECK NTC    ";
const char str58[17]="  COMPLETE"      ;

#include "lcdsoft.h"
#include "functions.h"
void clear_flag();
void trip(int s);
int getvalue(int ch);
void chargeron();
void chroff();
void invon();
void invoff();
void stabilise();
void Modulate();
void find_key();
int findpeak(int ct);
void mains_stat_check();
void find_mainsvolt();
void find_batvolt();
void find_champs();
void find_upsvolt();
void find_load();
void  overload_check();
void lobat_check();
void chr_stabilize();
////////////////////////////////////
void feed_buzz(int a,int b,int c)
     {
     buzzdelay=0;
     buzzoftme=b;
     buzzontme=a;
     buzzcount=c;
     }
//////////////////////////////////////////////////////
void __attribute__((__interrupt__,no_auto_psv)) _FLTAInterrupt(void)
    {


_FLTAIF = 0;
    }
//////////////////////////////////////////////////////////
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void)
   {
    _T1IF = 0;   // Clear interrupt flag
     if(PORTEbits.RE8==0)
     {
     fltdly++;
     if(fltdly>3000)
     {
      flags.olcut=1;
     __asm__ volatile ("reset");
     }
     }
    counter++;
    acpeak=getvalue(0);        //4.4 us
    indummy=acpeak;
    acpeak=indummy-508;
    if(acpeak<0)
    {
    acpeak=508-indummy;
    if(!flags.onflag)
    {
    rising=0;
    flag=0;
    }
    }
   
    mainsavg+=acpeak;

    if(flags.setup)
     {
OVDCON=   0X0000;
change=0;
buzzer=0;
     }

     if(!flags.setup)
    {
    if((indummy<600)&&(indummy>400)&&(flags.swon))
    {
    if(peakfail<150)
    {
    peakfail++;
    }
    if(peakfail==149)
    {
    if(flags.vcorrect)
    {
    invon(150);
    }
    }
    }
    else
    {
    peakfail=0;
    }

    if(flags.onflag)
    {
    Modulate();                         //4.4 us ///MODULATE SINE PWM OVER 50 HZ/////////////
    upspeak=getvalue(1);                //4.4 us
    outdummy=upspeak;              
    upspeak=outdummy-508;
    if(upspeak<0)
    upspeak=508-outdummy;
    outavg+=upspeak;
    if(upspeak<150)
     {
     fbdly++;
     if(fbdly>30000)                  // IF NO FEED BACK DETECTED///////////////
     {
     flags.fault=1;                  // TRIP: FB FAULT
     trip(5);
     }
     }
    else
    fbdly=0;
      }
    else
    {
    rising=1;
    flag=1;
    loadpercent=0;
    acout=0;
    }

    ctpeak=getvalue(2);

    if(flags.onflag)
    {
    if((ctpeak>750)||(ctpeak<250))
    {
    deadshortdly++;                  //OUTPUT DEAD SHORT TRIP//////////SHORT CKT FAULT
    if(deadshortdly>1500)
    {
    deadshort=1;
    OVDCON=   0X0000;
    trip(1);
    }
    }
    }

else
        {

        if(flags.chon==1)
        {
        if((ctpeak<508)||(ctpeak>512))
        ctfaildly=0;
        else
        {
        ctfaildly++;
        if(ctfaildly>10000)  // IF CT FEEDBACK FAIL, CHARGER OFF!
        {
        chroff();
        }
        }
        }
        }



    if(ctpeak>506)
    {
    pb=ctpeak-506;
    if(cth<pb)
    cth=pb;
    } 
    if(ctpeak<506)
    {
    qb=506-ctpeak;
    if(ctl<qb)
    ctl=qb;
    } 
    wattsavg+=cth+ctl;
    ctl=cth=0;
       heat=getvalue(3);    // GET NTC VALUE FOR FAN FUNCTION                      
     }       
   
                     
    batavg+=getvalue(5);   

lcdtmr1++;
    lcdtmr++;
if(counter==359)//////////////////////   20 milli sec checking sequence /////////////////
     {
     mainsvolt=__builtin_divud(mainsavg,288)+mainsclb;    //2.2 microseconds
  mainsavg=0;
   if(mainsvolt<90)
     {
     mainsvolt=0;
     mainsdisp=0;
     }
   else
     {
     if((indummy>180)&&(indummy<900))
     {
     if(mainsdisp==0)
     {
     mainsdisp=mainsvolt-80;
    
     }
     if(mainsdisp<mainsvolt-2)
     {
     mainsdisp++;
     }
     if(mainsdisp>mainsvolt+2)
     {
     mainsdisp--;
     }
     }
     }


     find_batvolt();  //get battery voltage


     keyavg+=getvalue(4);    // get switch status 
     keydly++;
     if(keydly==10)
     find_key();

     if(!flags.setup)
     {
     mains_stat_check(); // mains voltage status checking
     if(flags.onflag==1)
     {
     acout=__builtin_divud(outavg,285);
     outavg=0;  
     if(acoutdisp<acout-2)
     acoutdisp++;
     if(acoutdisp>acout+2)
     acoutdisp--;

     find_load();
     overload_check();
     lobat_check();
     }
     }
    
     wattsavg=0;

      if(buzzcount>0)
      {
      buzzdelay++;
      if(buzzdelay<=buzzontme)
      buzzer=1;
      else
      {
      buzzer=0;
      if(buzzdelay>=buzzoftme)
      {
      buzzdelay=0;
      buzzcount--;
      fltdly=0;
      }
      }
      }
      else
      {
      FLTACON=0x0001;
      IEC2bits.FLTAIE = 1;
      if(rounds>0)
      {
      if((!flags.onflag)&&(flags.swon)&&(rounds<4))
      {
      if(mainsvolt<110)
      {
      clear_flag();
      invon(0);
      }
      }
      }
      if(rounds==4)
      change=0;
      }
   
     if(!flags.setup)
    {
    if(flicker==0)
    {
    if((resumedelay==200)||(flags.mainsok==1))
    {
    LCD_DB6=1;
    if(flags.swon==1)
    {
    blinkdly++;
    if(blinkdly<30)
    LCD_DB7=1;
    else
    LCD_DB7=0;
    if(blinkdly>60)
    blinkdly=0;
    }
    else
    LCD_DB7=0;
    }
    else
   {
   LCD_DB6=0;
   if(flags.onflag==1)
   LCD_DB7=1;
   else
   LCD_DB7=0;
   }
   }


if((key==modekey)&&(flags.setup==0))
{
menudelay++;
if(menudelay>124)
flags.setup=1;
}
else
menudelay=0;
}

if(flags.setup==1)
{
setdly++;
if(setdly>3000)
__asm__ volatile ("reset");
}

  counter=0;

   }
   cherr=mainsvolt;
   }

//////////////////////////////////////
void init_PWM()
    {
    PTCON=    0XE003;
    PTMR =    0x0000;
    PTPER = PDC1=PDC2=  1230; //20 khz pwm
    SEVTCMP = 0x0000;
    PWMCON1 = 0x0033;
    PWMCON2 = 0x0000;
    DTCON1=   0X0059;      // 3.5 us dead band delay
    FLTACON = 0x0000;
    OVDCON=   0X0000;
    PTCONbits.PTEN = 1;
    IEC2bits.PWMIE = 0;        // disable PWM interrupts
   
    T1CON = 0XE000;        
    TMR1 = 0;           
    PR1 = 1355;         //20 khz timer
   _T1IF = 0;         
   _T1IE = 1;        //  enable timer interrupts
    }
/////////////////////////////////////////////////
void memread()
{
int x;
for(x=0;x<16;x++)
{
setting[x]=Eeprom_ReadWord(x);
}
}
//////////////////////////////////////////////////////////
void memwrite()
{

int x;
for(x=0;x<16;x++)
{
if(flags.setup==0)
Eeprom_WriteWord(x,factory[x]);
else
Eeprom_WriteWord(x,setting[x]);
}
}
//////////////////////////////////////////////
/*
void modedisp()
{
if(flags.msgrtn)
return;
if((key==upsmode)||(key==stbyups))
{
//printmes(str22,100,1);         //"\fU.P.S MODE ON";
if(key==upsmode)
{
aclo=setupslo;
achi=setupshi;
}
}
}
//if((key==invmode)||(key==stbyinv));      
                                        //"\fINVERTER MODE ON";
}*/
//////////////////////////////////////////////////////
/*void swdisp()
{
if(flags.msgrtn)
return;


if((key==stbyinv)||(key==stbyups))              //"\fSBY SWITCH OFF";
{

aclo=setinvlo;
achi=setinvhi;
}

}
*/


//                         MAIN ROUTINE                             
//*****************************************************************************/
int main()
{
TRISF=0X0000;
TRISE=0X010F;
TRISD=0X0000;
TRISC=0X0000;
TRISB=0XFFFF;
OVDCON=0X0000;
PWMCON1 = 0x0000;
PTCONbits.PTEN = 0;
buzzer=0;
lcd_init();
InitADC1();
init_PWM();
keyvalue=getvalue(4);
     if(((keyvalue>660)&&(keyvalue<680))||((keyvalue>560)&&(keyvalue<575)))
     {
     flags.swon=1;
     flags.firston=1;
     }

prect=getvalue(2);

flags.frstdisp=0;

defaults=Eeprom_ReadWord(14);
if(defaults!=50)
{
memwrite();
}

while(1)
{
while(PORTEbits.RE7==0)
{
 flags.msgrtn=0;
printmes(str31,100,1);     // SHORT CKT FAULT
printmes(str41,100,2);     // check output
if(flags.swon==0)
__asm__ volatile ("reset");
}

while(flags.nofeed==1)
{
 flags.msgrtn=0;
printmes(str34,100,1);     // NO FEED BACK
printmes(str42,100,2);     // CHK FET/FORMER
if(flags.swon==0)
__asm__ volatile ("reset");

while(flags.lbcut==1)
{
  flags.msgrtn=0;
printmes(str29,100,1);        //" TRIP:LOW-BATTERY ";
printmes(str43,100,2);        //SYSTEM IS OFF; 
if((flags.swon==0)||(resumedelay>175))
__asm__ volatile ("reset");
}


while((flags.olcut==1)||(flags.olwarn))
{
  flags.msgrtn=0;
printmes(str30,100,1);        //" TRIP:OVER-LOAD ";
printmes(str44,100,2);         // PLS REDUCE LOAD
if((flags.swon==0)||(resumedelay>175))
__asm__ volatile ("reset");
}

while(flags.olwarn)
{
  flags.msgrtn=0;
//printmes(str33,100,1);        //" WARN:OVER-LOAD ";
printmes(str44,100,2);        // PLS REDUCE LOAD
if(flags.swon==0)
__asm__ volatile ("reset");
}

while(flags.lbwarn==1)
{
  flags.msgrtn=0;
feed_buzz(2,50,25);
printmes(str32,100,1);        //"WARN:LOW-BATTERY";
printmes(str45,100,2);        //CONNECT CHARGER
printmes(str20,1,2);          //battery
if(flags.swon==0)
__asm__ volatile ("reset");

}
if(!flags.onflag==1)
{
//printmes(str59,100,1);       //"\fTITLE NAME
printmes(str38,100,1);       //"\fTITLE NAME
printmes(str43,100,2);     //   //"\system is off:";
printmes(str20,1,1);         // 0/0/"\fBATTERY V:";

}


if(mainsvolt>aclo+10)
{
    printmes(str25,2,2);     //   //"\fMAINS VOLT:";
}  

while(flags.chon==1)
{
if(champs==0)
goto outmes;
printmes(str36,3,1);          //"AC CHARGR:";
printmes(str20,1,2);         // 0/0/"\fBATTERY V:";
printmes(str25,2,2);        //"\fMAINS VOLT:"

if((change==0)&&(flags.swon==1))
{
printmes(str25,2,2);        //"\fMAINS VOLT:"
}
if((battvolts>=setbatful)&&(champs<=40))
{
 printmes(str50,100,1);      // battery is full
// printmes(str51,100,2);      //charging complete

}
}
outmes:


if((key==upsmode)||(key==invmode))
{
if(flags.onflag==1)
{
lcd_init();
printmes(str26,0,1);        //"\fINVERTER V:";
printmes(str27,4,2);        //"\fTOTAL LOAD:";;
printmes(str20,1,1);         // 0/0/"\fBATTERY V:"

//printmes(str25,2,2);     //   //"\fMAINS VOLT:";
//printmes(str52,4,2);        //"\fTOTAL WATT:";
}
}
/*else
{
//modedisp();
//swdisp();
printmes(str25,2,2);        //"\fMAINS VOLT:";
}
*/

if(heat<=fanheat-65)
{
feed_buzz(20,48,25);
printmes(str53,100,1);
printmes(str54,100,2);
if(flags.swon==1)
__asm__ volatile ("reset");
}

if(heat>=fanheat+320)
{
feed_buzz(2,48,25);
printmes(str55,100,1);
printmes(str56,100,2);
if(flags.swon==1)
__asm__ volatile ("reset");
}

if(battvolts>155)
{
printmes(str48,100,1);         ///over battery
printmes(str49,100,2);
}


if(menudelay>=125)
{
flags.setup=1;
menudelay=0;
printmes(str0,100,1);           //"\fCALIBRATION MENU";
while(key==modekey);
while(1)
{
if(key==modekey)
{
setchramp=setting[5]/10;
loadclb=setting[3];
functions();
memwrite();
flags.setup=0;
__asm__ volatile ("reset");
}
}

}

}


}

//main
///////////////////////////////////////////////////////////////////////////////////////
void InitADC1()
{
    ADPCFG = 0;       // Lowest 6 PORTB pins are analog inputs
    ADCON1 = 0;          // Manually clear SAMP to end sampling, start conversion
    ADCON2 = 0;          // Voltage reference from AVDD and AVSS
    ADCON3 = 0x0003;     // Manual Sample, ADCS=5 -> Tad = 3*Tcy
    ADCON1bits.ADON = 1; // Turn ADC ON
}

//////////////////////////////////////////
void delay_ms(unsigned int gs)
{
  while(gs--)
{
__delay32(55000);

}

}
///////////////////////////////////////////////////////////
void delay_us(unsigned int gs)
{
 while(gs--)
 __delay32(3000);
}
int getvalue(int ch)
{
ADCON1bits.DONE=0;
ADCHS = ch;        
ADCON1bits.SAMP = 1;     
__delay32(50);             
ADCON1bits.SAMP = 0;     
while (!ADCON1bits.DONE);
return ADCBUF0;

//////////////////////////////////
void clear_flag()
     {
     flags.olwarn=0;
     flags.lbwarn=0;
     flags.olcut=0;
     flags.lbcut=0;
     flags.nofeed=0;
     flags.zincfail=0;
     }
////////////////////////////////////
void trip(int s)
     {
     invoff();
     clear_flag();
     if(s==1)
     flags.olcut=1;
     if(s==2)
     flags.lbcut=1;
     if(s==4)
     flags.zincfail=1;
     if(s==5)
    flags.nofeed=1;
  if(s>2)
   {
    feed_buzz(8,48,25);
    flags.fault=1;
    rounds=0;
     }
     else
   {
     rounds++;
    feed_buzz(8,48,25);
    }
     flags.msgrtn=1;
     }
/////////////////////////////////////
void chargeron()
     {
     if(heat>=fanheat+300)
     return;
     if(heat<=fanheat-60)
     return;
     if(flags.onflag)
     return;
     chdisp=0;
     if(battvolts<90)
     return;
     if(battvolts>155)
     return;
     if(flags.chon==1)
     return;
     if(mainsvolt<150)
     return;
     if(chrdly<500)
     chrdly++;
     if(chrdly==499)
     {
     if(flags.gravity==1)
     {
     if(battvolts>batrestart)
     {
     chrdly=0;
     return;
     }
     gravitydly++;
     if(gravitydly<20)
     {
     chrdly=0;
     return;
     }
     gravitydly=0;
     }
     flags.gravity=0;
     PTCON=    0XE002;
     PTPER = 615;
     PWMCON1 = 0x0330;
     OVDCON=0X0A05;
     DTCON1=0X0000;
     PDC1=PDC2=0;
     flags.chon=1;
     chrdly=0;
   
     endtimer=0;
     }
     }
///////////////////////////////////////
void chroff()
    {
    OVDCON = 0x0000;
    PWMCON1 = 0x0033;
    DTCON1=0X0030;
    PTPER = 1230;
    PDC1=PDC2=0;
    flags.chon=0;
    champs=0;
    chrdly=0;
    }
/////////////////////////////////////////////////
void chr_stabilize()
     {
int kdly;

     if(flags.onflag)
     return;

     if(mainsvolt>cherr+5)
     {
     chroff();
     return;
     }

     if((battvolts>155)||(battvolts<90))
     {
     chroff();
     return;
     }

     if(mainsvolt<150)
     {
     chroff();
     return;
     }


//////////////////////////////////////////////////////////////
    if(battvolts>setbatful+1)
    {
    flags.chrcorrect=1;
     if(champs>35)
    {
    PDC1--;
    PDC2--;
    }
    }

 if(champs<30)
kdly=0;
else
kdly=4;




 if(battvolts<=setbatful)
     {
    if(flags.chrcorrect==0)
     {
     slowdly++;
     if(slowdly<kdly)
     return;
     slowdly=0;
     }

        if(champs<setchramp)
     {
     if(PDC1<900)
     {
     PDC1++;
     PDC2++;
     if(flags.chrcorrect==0)
     {
     if((champs<35)&&(champs>25))
     flags.frstdisp=1;
     }
     }
     }
    if(champs>setchramp)
       {
    flags.chrcorrect=1;
    if(PDC1>1)
    {
    PDC1--;
    PDC2--;
    }
    }

    }

    }
//void batt_full()
//{
  //  if((battvolts>=setbatful+1)&&(champs<=40))
    //  flags.batt_full=1; 
//}
////////////////////////////////////////////////
void find_champs()
     {
  
     x=__builtin_divud(wattsavg,359);             //1.03 microseconds
     Ax=__builtin_muluu(x,mainsdisp);
     bx=__builtin_divsd(Ax,battvolts);
     Ax=__builtin_muluu(bx,1000);
     bx=__builtin_divsd(Ax,450-chrclb);
     champs=bx;

   
if(flags.chrcorrect==1)
{
chshootdly++;
if(chshootdly==20)
{
 if(setchramp>chshoot)
setchramp--;
chshootdly=0;
}
}

     if(champs<10)

     {
     champs=0;
     chdisp=0;
     }
    
     if((battvolts<setbatful+2)&&(champs>35))
     {
     if(chdisp<champs-1)
     chdisp++;
     if(chdisp>champs+1)
     chdisp--;
     endtimer=0;
     }
     if((battvolts>=setbatful+2)&&(champs<35))
     {
     endtimer++;
     if(endtimer>100)
     {
     if(chdisp>12)
     chdisp--;
     else
     {
     flags.gravity=1;
     chroff();
     }
     endtimer=0;
     }
     }


    if(chdisp>chshoot)
    chdisp=chshoot;




     pb=qb=ctl=cth=0;
     }


//////////////////////////////////////
void invon(int k)
   {
   if(heat>=fanheat+300)
   return;
   if(heat<=fanheat-60)
   return;
   if(battvolts>155)
   return;    
   if(flags.olcut==1)
   return;
   if(flags.lbcut==1)
   return;
   if(flags.fault==1)
   return;
   if(flags.setup)
   return;
   if(flags.onflag)
   return;

   OVDCON = 0x0000;
   flags.onflag=1;
   flags.chon=0;  
   IEC2bits.FLTAIE = 0;
   FLTACON=0x0000;
   ctfaildly=0;
   fbdly=0;
   lbdly=0;
   oldelay=0;
   deadshortdly=0;
   chrdly=0;
   champs=0;
   ctfaildly=0;
   loadpercent=0;
   resumedelay=0;
   deadshort=0;
   flags.gravity=0;
   if(flag==0)
   p=90;
   else
   p=0;
   amplitude=k;
   buzzontme=51;
   buzzcount=1;
   buzzdelay=0;
   controlavg=u=0;
   change=1;
   PWMCON1 = 0x0033;
   DTCON1=0X0059;
   PTPER = 1230;
   OVDCON = 0x0F0f;
   }
////////////////////////////////////
  void invoff()
   {
  if(flags.onflag==0)
  return;
   flags.onflag=0;
   OVDCON = 0x0000;
   buzzer=0;
   flags.vcorrect=0;
   counter=0;
   }
//////////////////////////////////////////////////
void stabilise()
{
controlvolt=__builtin_divud(controlavg,90);
controlvolt+=43;
if(controlvolt<setvout)
{
err=setvout-controlvolt;
if(flags.vcorrect)
amplitude+=err;
else
amplitude++;
}
else
{
flags.vcorrect=1;
}

if(controlvolt>setvout)
{
err=controlvolt-setvout;
if(flags.vcorrect)
amplitude-=err;
}
controlavg=0;
}
///////////////////////////////////////

void Modulate()
{
if(flags.onflag==0)
return;

if(rising==1)
{
p++;
if((mainsvolt<50)&&(flags.mainsok==0))
{
if(p==45)
{
if(flag==1)
{
if(outdummy<200)
{
trip(4);// flags.zincfail=1;
}
}
}
}
u++;
controlavg+=upspeak;
}

if(p==91)
{
rising=0;
p=90;
u=0;
stabilise();



  if(resumedelay==200)
    {
    if((indummy<400))
    {
    IEC2bits.FLTAIE = 0;
    FLTACON=0x0000;
    flags.mainsok=1;
    change=0;
  __delay32(50000);
    flags.onflag=0;
    OVDCON = 0x0000;
    feed_buzz(10,50,3);
    peakfail=0;
    resumedelay=0;
    }
    }


    }
if(rising==0)
{
p--;
u++;
controlavg+=upspeak;
}
if(p<0)
{
rising=1;
p=0;
u=0;
stabilise();

if(flag==1)
{
flag=0;
}
else
{
flag=1;
}
}
duty_cycle_1=sine_table[p];
duty_cycle_1=((long)duty_cycle_1*amplitude)>>11;
if(duty_cycle_1>1230)
{
duty_cycle_1=1230;
}
if(duty_cycle_1<=0)
{
duty_cycle_1=0;
}
if(flag==1)
{
PDC1=1230+duty_cycle_1;
PDC2=1230-duty_cycle_1;
}
if(flag==0)
{
PDC1=1230-duty_cycle_1;
PDC2=1230+duty_cycle_1;
}
}
////////////////////////////////////////
void find_key()
     {
     keyvalue=__builtin_divsd(keyavg,10);
     keyavg=0;
     keydly=0;

     if(keyvalue>750)
     {
     key=7;//stbyups;

     if(keyvalue>850)
     {
     key=1;//stbymode;
     }
     if(flags.swon==1)
     {
     flags.swon=0;
    if((!flags.chon)&&(!flags.setup))
    __asm__ volatile ("reset");
     }
     flags.firston=1;
     peakfail=0;
     aclo=setinvlo;
     achi=setinvhi;
     return;
     }
 


     if((keyvalue>510)&&(keyvalue<530))
     {
     key=4;//modekey
     setdly=0;
     return;
     }


    if(flags.setup)
     {
     if((keyvalue>680)&&(keyvalue<705))
     {
     key=6;//downkey
     setdly=0;
     return;
     }

     if((keyvalue>590)&&(keyvalue<610))
     {
     key=5;//upkey
     setdly=0;
     return;
     }
     }

     if((keyvalue>660)&&(keyvalue<700))
     {
     if(flags.swon==0)
     {
     flags.swon=1;
     if(resumedelay==0)
     invon(0);
     }
     aclo=setinvlo;
     achi=setinvhi;
     key=3;//invmode
     return;
     }

     if((keyvalue>560)&&(keyvalue<575))
     {
     if(flags.swon==0)
     {
     flags.swon=1;
     if(resumedelay==0)
     invon(0);
     }
     aclo=setupslo;
     achi=setupshi;
     key=2;//upsmode;
     return;
     }
     }

//////////////////////////////////////////////////
void mains_stat_check()
    {
if(flags.fault==1)
   return;
    if((mainsvolt>aclo+10)&&(mainsvolt<achi-10))
    {
    if(resumedelay<200)
    resumedelay++;
    if(resumedelay==199)
    {
     clear_flag();
     rounds=0;
     setchramp=setting[5]+40;
    }

 if((key==3)||(key==2))
    {
    if(flags.firston==1)
    {
    flags.vcorrect=1;
    flags.firston=0;
    }
    }



      if(!flags.onflag)
      {
      if(resumedelay==200)
      {
      change=0;



      if(flags.chon==0)
      {
      slowdly=0;
      chargeron();
      flags.chrcorrect=0;
      }
      else
      {
      find_champs();
      chr_stabilize();
      }
    



      }
      }
      }
 
else
    {
 if((mainsvolt<aclo)||(mainsvolt>achi))
    {
    if(flags.chon)
    chroff();
    flags.mainsok=0;
    resumedelay=0;
    if(flags.firston==0)
    {
    if((key==3)||(key==2))
    {
    startdly++;
    if(startdly>10)
    {
    invon(150);
    startdly=0;
    }
    }
    else
   startdly=0;
    }
    }
    }
    }
/////////////////////////////////////////////////
void find_batvolt()
     {
     battvolts=__builtin_divsd(batavg,366);   ///1.307 microseconds
   __asm__ volatile ("mov   _battvolts, w3");
   __asm__ volatile ("mov     #0xffff, w1");
   __asm__ volatile ("mul.uu  w3,w1,w2");
   __asm__ volatile ("mov   w3,_battvolts");
     battvolts+=batclb+17;
     batavg=0;
     if(battdisp<battvolts)
     battdisp++;
     if(battdisp>battvolts)
     battdisp--;

 if(battvolts>=setbatful+1)
     battdisp=setbatful+1;

     }

////////////////////////////////////////////////

////////////////////////////////////////////////
void find_load()
     {
     x=__builtin_divud(wattsavg,loadclb);             //1.03 microseconds
     Ax=__builtin_muluu(x,125);
     bx=__builtin_divud(Ax,100);  //2.25 microseconds
     x=bx ;                    
     loadpercent=x;               // 1.2 microseconds
     if(loadpercent<10)
     {
     loadpercent=0;
     loaddisp=0;
     }
     if(loaddisp<loadpercent)
     loaddisp++;
     if(loaddisp>loadpercent)
     loaddisp--;
     }
/////////////////////////////////////////////////
void  overload_check()
     {
     if(flags.fault==1)
     return;
     if(loadpercent>=100)
     {
     oldelay++;

     if(loadpercent>110)
     tripdly=20;
     else if(loadpercent>105)
     tripdly=40;
     else
     tripdly=60;


if(tripdly==60)
{
if(oldelay>tripdly)
   {
 if(!flags.lbwarn)
     {
     if(!flags.olwarn)
     {
     flags.olwarn=1;
     if(!flags.lbwarn)
     flags.msgrtn=1;
     feed_buzz(10,50,7800);
     }
 }
}
}
     else
     {
     if(oldelay>tripdly)
     {
     if(!flags.olcut)
     {
     trip(1);
     }
     }
     }
     }
     else
     {
     if(flags.olwarn)
     {
     buzzer=0;
     buzzcount=0;
     flags.olwarn=0;
   flags.msgrtn=0;
     }
     oldelay=0;
     }



     }

/////////////////////////////////////////////////
void lobat_check()
     {
    if(flags.fault==1)
     return;
    if(flags.onflag==0)
     return;
   if(battvolts<setbatwrn)
     {
     lbdly++;
    if(lbdly>150)
     {
     if(!flags.lbwarn)
     {
     flags.olwarn=0;
     flags.lbwarn=1;
     flags.msgrtn=1;
     }
   if(battvolts<setbatlo)
     {
     trip(2);
     }
     }
     }
else
     lbdly=0;
     if(battvolts>setbatwrn)
     {
     if(flags.lbwarn)
     {
     flags.lbwarn=0;
     }
     }
     }

//////////////////////////////////////////////////////////////////////////////

Featured Post

MAKE INVERTER TRANSFORMER

Inverter transformers are just like any other power transformers except that inverter transformers works in the reversed form of the standa...

Contributors