DSPIC30f2010 C Code Sanjay

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 16

#include

#include
#include "DataEEPROM.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 faultin LATEbits.LATE8


#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 640

///////////////////////////////////////////////////////////////////////////////////
////////
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,766
0,7771,

7880,7986,8090,8191,8290,8386,8480,8571,8660,8746,8829,8910,8987,9063,9135,9205,927
1,9335,

9396,9455,9510,9563,9612,9659,9702,9743,9781,9616,9848,9876,9902,9925,9945,9961,997
5,9986,
9993,10000,10000};

signed int factory[16]={0,0,100,350,220,80,265,180,270,100,142,108,100,115,5000};


signed int setting[16];
char arr[4];

struct
{
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;
}flags;

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

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


int amplitude=0;

int pb,qb,cth,ctl;
////////////////////////////////
unsigned int setdly;
unsigned int menudelay;
unsigned int lcdtmr;
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 round;
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 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;

///////////////////////////////////
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 x,y,u;


unsigned long Ax,bx;
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]= "CALIBRATION MENU";
const char str20[17]="BATTERY V: V";
const char str21[17]="INVERTER MODE ON";
const char str22[17]=" U.P.S MODE ON ";
const char str23[17]=" SBY SWITCH OFF ";
const char str24[17]=" SBY SWITCH ON ";
const char str25[17]="MAINS VOLT: V";
const char str26[17]="INVERTER V: V";
const char str27[17]="TOTAL LOAD: %";
//////////////////////////////////////////
//const char str28[17]=" TRIP:C-T FAULT ";
const char str29[17]="TRIP:LOW-BATTERY";
const char str30[17]=" TRIP:OVERLOAD ";
const char str31[17]="SHORT CKT FAULT";
const char str32[17]="WARN:LOW-BATTERY";
const char str33[17]=" WARN:OVERLOAD ";
const char str34[17]=" TRIP:F-B FAULT ";
const char str35[17]="TRIP:SYNC FAULT ";
const char str36[17]="AC CHARGR: A";
const char str38[17]=" DSP SINEWAVE ";

#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
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)
{
flags.fault=1;
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++;
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)
{
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);
}

batavg+=getvalue(5);
lcdtmr++;
if(counter==359)////////////////////// 20 milli sec
{
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();

keyavg+=getvalue(4);
keydly++;
if(keydly==10)
find_key();

if(!flags.setup)
{
mains_stat_check();
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(round>0)
{
if((!flags.onflag)&&(flags.swon)&&(round<4))
{
if(mainsvolt<110)
{
clear_flag();
invon(0);
}
}
}
if(round==4)
change=0;
}

if(!flags.setup)
{

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;
}
}

counter=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");
}

}
cherr=mainsvolt;
}

//////////////////////////////////////
void init_PWM()
{
PTCON= 0XE003;
PTMR = 0x0000;
PTPER = PDC1=PDC2= 1230;
SEVTCMP = 0x0000;
PWMCON1 = 0x0033;
PWMCON2 = 0x0000;
DTCON1= 0X0059;
FLTACON = 0x0000;
OVDCON= 0X0000;
PTCONbits.PTEN = 1;
IEC2bits.PWMIE = 0; // disable PWM interrupts

T1CON = 0XE000;
TMR1 = 0;
PR1 = 1355;
_T1IF = 0;
_T1IE = 1;
}
/////////////////////////////////////////////////
void memread()
{
int x;
for(x=0;x<15;x++)
{
setting[x]=Eeprom_ReadWord(x);
}
}
//////////////////////////////////////////////////////////
void memwrite()
{

int x;
for(x=0;x<15;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); //"\fU.P.S MODE ON";
if(key==upsmode)
{
aclo=setupslo;
achi=setupshi;
}
}

if((key==invmode)||(key==stbyinv))
printmes(str21,100); //"\fINVERTER MODE ON";

}
//////////////////////////////////////////////////////
void swdisp()
{
if(flags.msgrtn)
return;
if((key==stbyinv)||(key==stbyups)) //"\fSBY SWITCH OFF";
{
printmes(str23,100);
aclo=setinvlo;
achi=setinvhi;
}

if((key==upsmode)||(key==invmode))
printmes(str24,100); //"\fSBY SWITCH ON";
}

// 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();
}
memread();
batclb=setting[0];
mainsclb=setting[1];
chrclb=setting[2];
loadclb=700-setting[3];
setvout=setting[4];
setchramp=setting[5];
chshoot=setchramp;
setchramp+=30;
setupshi=setting[6];
setupslo=setting[7];
setinvhi=setting[8];
setinvlo=setting[9];
setbatful=setting[10];
setbatwrn=setting[11];
setbatlo=setting[12];
setbatres=setting[13];
defaults=setting[14];
battdisp=90;
aclo=setinvlo;
achi=setinvhi;
batrestart=setbatful-13;
menudelay=0;
while(1)
{
//lcd_init();
//
//while(1)
//{

//lcd_init();
//printmes(str36,3); //"AC CHARGR:";

//flags.msgrtn=0;
////printmes(str20,1); //"\fBATTERY V:";
//

//printmes(str25,2); //"\fMAINS VOLT:";

//printmes(str27,4); //"\fTOTAL LOAD:";


//
////printmes(str23,0);
////
//printmes(str24,100); //"\fSBY SWITCH ON";
////
////
////
//printmes(str26,0); //"\fINVERTER V:";
////
////printmes(str27,4); //"\fTOTAL LOAD:";
//
//
//
//
//
//}
//

while(PORTEbits.RE8==0)
{
flags.msgrtn=0;
printmes(str31,100); // SHORT CKT FAULT
if(flags.swon==0)
__asm__ volatile ("reset");
}

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

//while(flags.ctfail==1)
//{
// flags.msgrtn=0;
//printmes(str28,100); // TRIP:C-T FAULT
//if(flags.swon==0)
//__asm__ volatile ("reset");
//}

while(flags.zincfail==1)
{
flags.msgrtn=0;
printmes(str35,100); // TRIP:SYNC FAULT!
if(flags.swon==0)
__asm__ volatile ("reset");
flags.fault=1;
}

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

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

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

while(flags.lbwarn==1)
{
flags.msgrtn=0;
printmes(str32,100); //"WARN:LOW-BATTERY";
if(flags.swon==0)
__asm__ volatile ("reset");

printmes(str20,1); // 0/0/"\fBATTERY V:";

if(flags.chon==1)
{
if(champs==0)
goto outmes;

printmes(str36,3); //"AC CHARGR:";


}
outmes:

if((key==upsmode)||(key==invmode))
{
if(flags.onflag==1)
{
printmes(str27,4); //"\fTOTAL LOAD:";
}
modedisp();

swdisp();

printmes(str25,2); //"\fMAINS VOLT:";

if(flags.onflag==1)
{
printmes(str26,0); //"\fINVERTER V:";
}

else
{
modedisp();
swdisp();
printmes(str25,2); //"\fMAINS VOLT:";
}

//printmes(str38,100); //"\fTITLE NAME


if(menudelay>=125)
{
flags.setup=1;
menudelay=0;
lcd_putc('\f');
printmes(str0,100); //"\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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy