void Init_adc()
{
P1M0 = 0x00;
P1M1 = 0x08;
P_SW2|=0X80;
ADCTIM=0X3f; //2C
P_SW2&=0X7F;
ADCCFG=0X0f;
ADC_CONTR = 0x83; //p1.3ͨµÀ
_nop_();
_nop_();
EA=1;
EADC=1;
ADC_RES=0;
ADC_CONTR |= 0x40;
}
void main()
{
float Vout=0;
float Vout_D=0;
float V1=0,V2=0,V3=0;
float Vp=4.2057; //4.2057
float D=0.15;
float Kp=0.2,Ki=0.005;
Init_adc();
PWM_Init();
while(1)
{
ADC_CONTR |= 0x40;
_nop_();
_nop_();
while(!(ADC_CONTR&0x20));
ADC_CONTR &= ~0x20;
voltage2 = ADC_RES;//((unsigned int)ADC_RES<<8)+ADC_RESL;
Vout = voltage2*5/255;
//Vout_D = voltage1*5/255;
V1 = Vp-Vout;
//V3+=V1;
D = D + Kp*(V1-V2) + Ki*V1;
V2=V1;
if(D < 0 ) //|| Vout_D > 1.826*1.1)
{
D=0.0;
}
if(D > 0.45)
{
D=0.48;
}
pwm_value = D*120;
PWMB_CCR7H = 0;
PWMB_CCR7L = (unsigned char)(pwm_value);
soft_delay(2);
}
} |