📄 0.#1
字号:
long power_calc_llds(long eds,long ids) //深侧向功率计算
{ long idata xp_llds;
long idata r_llds;
long idata typ_llds;
if((2450<=eds||eds<=0)||(2450<=ids||ids<=0)) //输入值超过2450mv或者低于0mV,PID输出项和DAC输出为0
{ return(0);}
else //计算深侧向实际功率对数
{eds=eds/20;
ids=ids/1820;
xp_llds=eds*ids;
xp_llds=log10(xp_llds);
ftp_llds=xp_llds<<13;
r_llds=eds/ids;
r_llds=k*r_llds;
if(40000<=r_llds||r_llds<=0.2) //深侧向电阻率超过控制范围,保持当前的功率(0.55uw)
{//PID_llds=0;
typ_llds=log10(40000);
}
else if(0.2<=r_llds<=68) //计算前段深侧向理论功率对数
{r_llds=log10(r_llds); //扩大8192倍(2^13)
r_llds=r_llds<<13;
typ_llds=1.114*r_llds+4252;
}
else //计算后段深侧向理论功率对数
{ r_llds=log10(r_llds);
r_llds=r_llds<<13;
typ_llds=36258-1.018*r_llds;
}
return(typ_llds);}
}
/*void power_calc_lls() //浅侧向功率计算
{ long idata xp_lls;
long idata r_lls;
if((2450<=es||es<=000)||(2450<=is||is<=000))//输入值超过2450mv或者低于0MV,实际功率和输出控制为零
{DAC_lls=0;}
else //计算浅侧向实际功率对数
{es=es/20;
is=is/1820;
xp_lls=es*is;
xp_lls=log10(xp_lls);
ftp_lls=xp_lls<<13;
r_lls=es/is;
r_lls=k*r_lls;
}
if(40000<=r_lls||r_lls<=0.2) //浅侧向电阻率超过控制范围,不进行恒功率控制,PID输出为零,保持当前的功率(0.55uw)
{PID_lls=0;
typ_lls=log10(40000);
}
else if (0.2<=r_lls<=68) //计算前段浅侧向理论功率对数
{ r_lls=log10(r_lls); //扩大8192倍(2^13)
r_lls=r_lls<<13;
typ_lls=1.114*r_lls+4252;
}
else//计算后段浅侧向理论功率对数
{r_lls=log10(r_lls);
r_lls=r_lls<<13;
typ_lls=36258-1.018*r_lls;
}
}*/
//输入: Setpoint(Setpoint) nextpoint(-2126<=nextpoint<=20975)
//输出:PID_lld
int PID_CRT(long Setpoint,long nextpoint) //PID计算
{ int PID_ll;
int En;
uchar idata PID_llds;
En=Setpoint-nextpoint;
if(En>=132) PID_llds=200;
else if(En<=-132) PID_llds=-200;
// En2=Setpoint-nextpoint1;
else
{ PID_P=(Kp*En)/PID_AREA*200; //比例项
// PID_D=Kd*(En2-En1); //微分项
PID_ll=Ki*En;
PID_Integral=PID_Integral+PID_ll; //积分累加
if(PID_Integral>=1638400) PID_Integral=1638400;
if(PID_Integral<=(-1638400)) PID_Integral=-1638400;
PID_I=PID_Integral>>13;
//积分项
if(PID_I>=200) PID_I=200;
if(PID_I<=(-200)) PID_I=-200;
PID_ll=PID_P+PID_I+PWMBASE; //控制输出
if(PID_ll<0) PID_ll=0;
if(PID_ll>=200) PID_ll=200;
PID_llds=PID_ll;
}
return(PID_llds);
}
void main(void)
{ uint i;
uint n;
long idata voltage;
DATA_lld=0;
ftp_llds=0;
SYSCLK_Init(); //系统配置
PORT_Init(); //端口配置
// UartInit(); //UART初始化
ADC0_Init();
DAC_Init();
loop: for(i=0;i<a;i++)
{ ADC0_Conver();
ADC0MD &= 0x80; //ADC处于空闲状态
printf("\n");
for (n=0;n<4;n++)
{ voltage = result[n] * 2450.0 / 16777215;
result[n] = (a-1)*result[n] +voltage;
result[n] = result[n]>>4;
}
}
DelayMs(5); //延时5ms
if(i!=a)
{ ADC0MD |= 0x82; //启动单次转换
DelayMs(10); //延时10ms
goto loop;
}
ADC0MD &= 0x00; //ADC处于禁止状态
DelayMs(10); //延时10ms
ed=result[0]; //读取深侧向电压值
id=result[1]; //读取深侧向电流值
es=result[2]; //读取浅侧向电压值
is=result[3]; //读取浅侧向电流值
DelayMs(5); //延时5ms
typ_lld=power_calc_llds(ed,id); //深侧向功率计算
if(typ_lld==0) DATA_lld=0; //深侧向采样值超过范围,DAC0输出为0
Setpoint=typ_lld; //理论功率
nextpoint=ftp_llds; //实际功率
// ftp_llds=0;
PID_lld=PID_CRT( Setpoint, nextpoint);
DATA_lld=DATA_lld+PID_lld*50; //mV
DAC0_Out_mV(DATA_lld);
DelayMs(5); //延时5ms
typ_lls=power_calc_llds(es,is); //浅侧向功率计算
if(typ_lls==0) DATA_lls=0; //浅侧向采样值超过范围,DAC1输出为0
DelayMs(10); //延时10ms
Setpoint=typ_lls; //理论功率
nextpoint=ftp_llds; //实际功率
// ftp_llds=0;
PID_lls=PID_CRT( Setpoint, nextpoint);
DATA_lls=DATA_lls+PID_lls*50; //mV
DAC1_Out_mV(DATA_lls);
// DelayMs(50);
// printf("\n");
// printf ("lld voltage is %dmV\n", DATA_lld);
// DAC0_Out_mV(DATA_lld);
// printf ("lls voltage is %dmV\n", DATA_lls);
// DAC1_Out_mV(DATA_lls);
}
/*void UartInit(void)
{ SCON0 = 0x10;//允许接收
if (SYSCLK/BAUDRATE/2/256 < 1)
{ TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
}
else if (SYSCLK/BAUDRATE/2/256 < 4)
{ TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
}
else if (SYSCLK/BAUDRATE/2/256 < 12)
{ TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
}
else
{ TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1;
TMOD &= ~0xf0;
TMOD |= 0x20;
TR1 = 1;
TI0 = 1;
}*/
/*void UartOut(unsigned char *SendByteAdd,unsigned char Count) //发送
{ unsigned char n;
for(n=0;n<Count;n++)
{ SBUF0=*SendByteAdd;
SendByteAdd++;
while(!TI0);
TI0 = 0;
}
}
unsigned char UartReceive(void) //接收
{ while(!RI0);
RI0 = 0; // 清除RI0
return SBUF0;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -