#include "user.h"
/******************************************************************************
· EEPROM
*******************************************************************************/
_FOSC(CSW_FSCM_OFF HS); //,
_FWDT(WDTPSB_16 WDTPSA_512 WDT_OFF); // Watchdog timer disabled
_FBORPOR(PWRT_64 BORV20 PBOR_ON MCLR_EN RST_PWMPIN PWMxH_ACT_HI PWMxL_ACT_HI);//
_FGS(GWRP_OFFCODE_PROT_OFF); //
_FICD(ICS_PGD);
unsigned int _EEDATA(2) user_data[EEPROM_DATA_LENGTH]=
{
2005, 329, 300, 100, 195, 0, 1080, 1500, //1
// 0 1 2 3 4 5 6 7
// PV
1080, 1200, 1600, 1350, 1400, 1440, 1200, 10000, //2
// 8 9 10 11 12 13 14 15
//
2200, 18000, 200, 450, 350, 1, 2, 0, //3
// 16 17 18? 19? 20 21 22 23
// 1 2 nc
276, 0, 0, 100, 937, 204, 0, 60, //4
// 24 25 26 27 28 29 30 31
// 0.17
0, 0, 0, 0, 0, 0, 0, 0 //5
// 32 33 34 35 36 37 38 39
//
};
volatile union Flag_REG _SYSCONTROL; //
volatile union Flag_REG _SYSSTATE; //
unsigned int Run_data[USER_DATA_LENGTH]={};//16
unsigned int DATA_Config[EEPROM_DATA_LENGTH];//40
//ADC
unsigned int ad_temp=0;
unsigned char AD_NUM=0;
unsigned char qiehuan_n=0;
unsigned int DC_V_PV_ARRAY[40]={0};
unsigned int DC_I_IN_ARRAY[40]={0};
unsigned int DC_V_OUT_ARRAY[40]={0};
unsigned int DC_I_OUT_ARRAY[40]={0};
unsigned int DC_V_PV_SUM=0;
unsigned int DC_I_IN_SUM=0;
unsigned int DC_V_OUT_SUM=0;
unsigned int DC_I_OUT_SUM=0;
unsigned int TEMPERTURE_SUM1;
unsigned int TEMPERTURE_SUM2;
unsigned int TEMPERTURE_ARRAY1[40];
unsigned int TEMPERTURE_ARRAY2[40];
//PWM
unsigned int DC_V_PV_last=0,Power_last=0,Power_now=0;
unsigned int duty_n=0;//
//
unsigned char Delay_1s_Count=50;// 0.5S
unsigned int recover_time1=0;//
unsigned int recover_time=0;//
//
//unsigned int Power_In_temp=0;
//unsigned int Power_Out_temp=0;
//
unsigned int BAUD;
// 2400 4800 9600 19200
const unsigned char BAUD_array[]={128,64,32,16};
/*********************************************************
**********************************************************/
void Init_IOPin(void) //
{
_TRISE2=0; discharge_EN=!enable_discharge;
_TRISB6=0; YELLOW=!light_led;
_TRISB7=0; GREEN=!light_led;
_TRISB8=0; RED=!light_led;
}
/******************************************************************************
** : Init_Timer1
** : 1
** :
** :
** :
** : 10ms
******************************************************************************/
void Init_Timer1(void)
{
T1CON=0; // Fosc/4
TMR1=0X0000; //
PR1=199; //10ms
T1CONbits.TCKPS=3;// Fosc/4 256 19531.25Hz
IPC0bits.T1IP=1;// 1
IFS0bits.T1IF=0;//
IEC0bits.T1IE=1;//
T1CONbits.TON=1;//
}
/******************************************************************************
** : Init_ADC
** : ADC
** :
** :
** :
** :
******************************************************************************/
void Init_ADC(void)
{
TRISBbits.TRISB0=1;ADPCFGbits.PCFG0=0; //AD
TRISBbits.TRISB1=1;ADPCFGbits.PCFG1=0; //AD
TRISBbits.TRISB2=1;ADPCFGbits.PCFG2=0; //AD
TRISBbits.TRISB3=1;ADPCFGbits.PCFG3=0; //AD
TRISBbits.TRISB4=1;ADPCFGbits.PCFG4=0 ;//AD
TRISBbits.TRISB5=1;ADPCFGbits.PCFG5=0; //AD
ADCON1=0x0064; //0000 0000 011x x100 //SSRC=011 PWM , ASAM=1
ADCON2=0x0000; //0000 x0xx 0x00 0000 AVDD AVSS / MUX A
ADCON3=0x0003; //xxx0 0110 0000 0111 0 TAD Tad=100ns 12Tad=1.2us 40 50us
ADCHS=0x0000; //0000 0000 0110 000 40 0
ADCSSL=0; // ANx
IPC2bits.ADIP=6;
IFS0bits.ADIF=0;
IEC0bits.ADIE=1;
ADCON1bits.ADON = 1; // ADC
}
/******************************************************************************
** : Init_PWM
** : PWM
** :
** :
** :
** : PWMF
******************************************************************************/
void Init_PWM(void)
{
PTCON =0x0040; // 1x0x xxxx 0000 0010 PWM , 1:1, ,PWM
PTMR=0X7FFF; //0111 1111 1111 1111 PTMER , 0
PTPER=PWM_F; // PWM / 1/(20M/4)*166
SEVTCMP = PTPER-8; // , AD, 1.6us
TRISE=0X0000;
TRISEbits.TRISE8=1;//FLTA
//PWMCON1 PWM 1
PWMCON1bits.PMOD1=1; //
PWMCON1bits.PMOD2=1; //
PWMCON1bits.PMOD3=1; //
PWMCON1bits.PEN1L=1; PWMCON1bits.PEN1H=0; // PWM
PWMCON1bits.PEN2L=0; PWMCON1bits.PEN2H=0; // PWM
PWMCON1bits.PEN3L=0; PWMCON1bits.PEN3H=0; // PWM
PWMCON2 = 0x0002; // 1:1,。 PDC PWM , OVDCON, PWM ,
DTCON1=0X0000 ; // A Tcy
FLTACON=0x0000; //0000 0000 xxxx 1111 PWM1 A
VDCON = 0x0000; //PWM Poutxx , PWM
PDC1=0;// 0,
IEC2bits.PWMIE=1; // PWM
IFS2bits.PWMIF=0; //CLEAR THE PWM INTERRUPT FLAG
IPC9bits.PWMIP=7; // 7
/*
IEC2bits.FLTAIE=1; //PWM
IFS2bits.FLTAIF=0;
IPC10bits.FLTAIP=7;
*/
PTCONbits.PTEN=1; // PWM
}
/******************************************************************************
** : Read_EEPROM_Data
** : EEPROM
** :
** :
** :
** : EEPROM DATA_Config
******************************************************************************/
void Read_EEPROM_Data(void)
{
unsigned char i=0,j=0;
unsigned int temp;
unsigned int page,offset, offset_x;
page=__builtin_tblpage(user_data[0]);
ffset=__builtin_tbloffset(user_data[0]);
for (i=0,j=0;iLOW_V_R)(DC_V_OUTHIGH_V)
{
SYSSTATE=0xFF80; //
SYSSTATEbits.BOV=1; //
}
if(DC_I_OUT>=PRO_I) //
SYSSTATEbits.IOV=1;
else
SYSSTATEbits.IOV=0;
if (
(SYSSTATEbits.BUV) ||//
//(SYSSTATEbits.BOV) ||
(SYSSTATEbits.IOV) //
//(SYSSTATEbits.SHC)//
)
SYSSTATEbits.FLTD=1;// 1
else
SYSSTATEbits.FLTD=0;
if(SYSSTATEbits.FLTD)
recover_time1=500;// 5S
if(recover_time1)
SYSCONTROLbits.DISCHREN=0;
else
SYSCONTROLbits.DISCHREN=1;
//----------------------- -----------------------------
//---------------------- ---------------------------
if(DC_V_PVPV_HH) //
SYSSTATEbits.POV=1;
else
SYSSTATEbits.POV=0;
if(DC_V_OUTBAT_HH) //
SYSSTATEbits.OHV=1;
else
SYSSTATEbits.OHV=0;
/* if(TEMPERTURE1>SETTEMP1) //,
SYSSTATEbits.TOV=1;
else
SYSSTATEbits.TOV=0;
*/
if(DC_I_IN>=(PRO_I+DC_I_OUT))//
SYSSTATEbits.ICV=1;
else SYSSTATEbits.ICV=0;
if(SYSSTATE0x1B00)//
SYSSTATEbits.FLTC=1;
else
SYSSTATEbits.FLTC=0;
if(SYSSTATEbits.FLTC)
recover_time=500;
if( recover_time)
SYSCONTROLbits.CHREN=0;
else
SYSCONTROLbits.CHREN=1;
//------------------------- -----------------------------
RUN_STATE=SYSSTATE;//
}
/*********************************************************
END
**********************************************************/
/*********************************************************
**********************************************************/
void InOutput_Control(void)
{
if((Delay_1s_Count>=30)((SYSSTATEbits.FLTD)||(SYSSTATEbits.FLTC)))
RED=light_led; //
else
RED=!light_led;
if( SYSCONTROLbits.DISCHREN)
{
discharge_EN=enable_discharge;//
YELLOW=light_led;//
}
else
{
discharge_EN=!enable_discharge;//
YELLOW=!light_led;
}
//------------ ------------------------
if(SYSCONTROLbits.CHREN)
{
SYSCONTROLbits.MPPTEN=1;
VDCON=0X0100;//, PWM
GREEN=light_led;//
}
else
{
SYSCONTROLbits.MPPTEN=0;
VDCON=0;
GREEN=!light_led;
}
}
/*********************************************************
END
**********************************************************/
extern unsigned char USART_BUF[];
extern void USART_SEND(unsigned char *p,unsigned char len);
/*********************************************************
PWM START
**********************************************************/
void MPPT_Solar_Duty_Adgust(void)
{
unsigned long mppt_temp;
if(SYSCONTROLbits.MPPTCLK) //1S 1
{
SYSCONTROLbits.MPPTCLK=0;
//USART_BUF[0]=1;
//USART_SEND(USART_BUF,1);
if(SYSCONTROLbits.MPPTEN)
{
mppt_temp=(unsigned long)DC_I_IN;
mppt_temp*=DC_V_OUT;
Power_now=(unsigned int)(mppt_temp/100);//
if((DC_V_OUT>FLT_V) ||(SYSSTATEbits.ICV))//
{ if(DUTY>DUTY_MIN)
DUTY--;
else DUTY=DUTY_MIN;
}
else //--------MPPTF-----------
{
if(Power_now>=Power_last)
{
if(DC_V_PV>3) DUTY=DUTY+8; //7.5
else
if(DC_I_IN>2) DUTY=DUTY+6; //7.5--15
else
if(DC_I_IN>1) DUTY=DUTY+4; //15--30
else DUTY=DUTY+1; //30--60
}
else
{
if(DC_I_IN>3) DUTY=DUTY-7;
else
if(DC_I_IN>2) DUTY=DUTY-5;
else
if(DC_I_IN>1) DUTY=DUTY-3;
else DUTY=DUTY-1;
}
}
else //if(Power_now=DC_V_PV_last)
{
if(DC_I_IN>3) DUTY=DUTY+8;
else
if(DC_I_IN>2) DUTY=DUTY+6;
else
if(DC_I_IN>1) DUTY=DUTY+4;
else DUTY=DUTY+1;
}
else
{
if(DC_I_IN>3) DUTY=DUTY-7;
else
if(DC_I_IN>2) DUTY=DUTY-5;
else
if(DC_I_IN>1) DUTY=DUTY-3;
else DUTY=DUTY-1;
}
}
}
Power_last=Power_now; //
DC_V_PV_last=DC_V_PV;
if(DUTY=DUTY_MAX) DUTY=DUTY_MAX;// 0.9
}
else
{
DUTY=DUTY_FIRST;// MPPTEN , PWM , IO
}// EEROM ,,
//if(GREEN==0) GREEN=light_led;
//else if(GREEN==1) GREEN=!light_led;
// duty_n=DUTY;
PDC1=DUTY; //
//USART_BUF[0]=duty_n;
// USART_SEND(USART_BUF,1);//
}
}
/*********************************************************
PWM END
**********************************************************/
/*********************************************************
AD START
**********************************************************/
void AD_Process(void)//10
{
unsigned long ad_buf=0;
unsigned char i;
unsigned int ad_temp;
switch (ADCHSbits.CH0SA)
{
case AD_PV_CHANNEL :
DC_V_PV_SUM=0;
for(i=0;i>1;
break;
case AD_I_IN_CHANNEL :
DC_I_IN_SUM=0;
for(i=0;i>1;
break;
case AD_V_OUT_CHANNEL :
DC_V_OUT_SUM=0;
for(i=0;i>1;
break;
case AD_T1_TEMP_CHANNEL :
TEMPERTURE_SUM1=0;
for(i=0;i>1;
break;
case AD_I_OUT_CHANNEL :
DC_I_OUT_SUM=0;
for(i=0;i>1;
break;
case AD_T2_TEMP_CHANNEL :
TEMPERTURE_SUM2=0;
for(i=0;i>1;
break;
default:
break;
}
/*ad_buf=(unsigned long)DC_V_OUT;//,
ad_buf*=DC_I_IN;
Power_In_temp=(unsigned int)(ad_buf/100);
POWER_IN=Power_In_temp;
*///
}
/*********************************************************
AD END
**********************************************************/
/*Interrupt*/
void __attribute__((__interrupt__,auto_psv))_T1Interrupt(void)
{
IFS0bits.T1IF=0;
if(--Delay_1s_Count==0)
{
Delay_1s_Count=50;// MPPT , 10mS
SYSCONTROLbits.MPPTCLK=1;//
}
if(recover_time1)
{
recover_time1--;
}
if(recover_time)
{
recover_time--;
}
}
void __attribute__((__interrupt__,auto_psv)) _ADCInterrupt (void)
{
if(ADCHSbits.CH0SA== AD_PV_CHANNEL)
{
DC_V_PV_ARRAY[AD_NUM]=ADCBUF0;
}
else if(ADCHSbits.CH0SA== AD_I_IN_CHANNEL)
{
DC_I_IN_ARRAY[AD_NUM]=ADCBUF0;
}
else if(ADCHSbits.CH0SA== AD_V_OUT_CHANNEL)
{
DC_V_OUT_ARRAY[AD_NUM]=ADCBUF0;
}
else if(ADCHSbits.CH0SA== AD_T1_TEMP_CHANNEL)
{
TEMPERTURE_ARRAY1[AD_NUM]=ADCBUF0;
}
else if(ADCHSbits.CH0SA== AD_I_OUT_CHANNEL)
{
DC_I_OUT_ARRAY[AD_NUM]=ADCBUF0;
}
else if(ADCHSbits.CH0SA==AD_T2_TEMP_CHANNEL)
{
TEMPERTURE_ARRAY2[AD_NUM]=ADCBUF0;
}
else;
//------------------------------
IFS0bits.ADIF=0; //clear interrupt flag
}
void __attribute__((__interrupt__,auto_psv)) _FLTAInterrupt (void)
{
}
void __attribute__((__interrupt__,auto_psv)) _PWMInterrupt (void)
{
AD_NUM++;//
if(AD_NUM>=40)
{
AD_NUM=0;
AD_Process();
//---AD ---
qiehuan_n++; if(qiehuan_n>5) {qiehuan_n=0; ; }
if (qiehuan_n==0) ADCHSbits.CH0SA=AD_I_IN_CHANNEL ;
else if(qiehuan_n==1) ADCHSbits.CH0SA=AD_I_OUT_CHANNEL ;
else if(qiehuan_n==2) ADCHSbits.CH0SA=AD_PV_CHANNEL;
else if(qiehuan_n==3) ADCHSbits.CH0SA=AD_V_OUT_CHANNEL ;
else if(qiehuan_n==4) ADCHSbits.CH0SA=AD_T1_TEMP_CHANNEL;
else if(qiehuan_n==5) ADCHSbits.CH0SA=AD_T2_TEMP_CHANNEL;
else;
}
IFS2bits.PWMIF= 0;
}
extern void Usart_Communication(void);
extern void Init_Timer4(void);
extern void Init_UART2(void);
int main(void)
{
Clear_Run_data();
Read_EEPROM_Data();
Init_IOPin();
Init_Timer1();
Init_Data();
Init_ADC();
Init_PWM();
Init_Timer4();
Init_UART2();
while(1)
{
Sys_Station();
InOutput_Control();
Usart_Communication();
MPPT_Solar_Duty_Adgust();
}
}