不晓得是怎么回事写了个串口中断接收程序,发生一次中断以后,然后返回主函数 然后串口就一直接收不到数据
主循环 while (1)
{
if(flag0==1) //判断是否时间到
{
caiji(P,I,D); //4.4秒调用一次 T=4.4 s
printf(" 输入电压=%dmV \r\n", SetPoint);
printf("实际电压= %6.2fmV \r\n", wd_dianya);
flag0=0; //开始采集标志清零
flag2=1;//为加热时间更新标志
}
}
float caiji(u8 P,u8 I,u8 D)
{
u8 w;
float sum_data=0;
while(w!=4)
{
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1)); //等待校准
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //启动ADC转换 软件启动功能
delay_us(5); //使AD连续转换
if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)==SET)
{
w++;
AD_value=ADC_GetConversionValue(ADC1);
AD_valu =AD_value;
sum_data+=AD_valu;
// AD_value= (AD_value/4096)*3300;
printf("The AD value = %d\r\n", AD_valu);
}
// ADC_Cmd(ADC1, DISABLE); //关闭AD
}
w=0;
sum_data=sum_data/4 ;//右移两位得到13位的数据
printf("The current AD wd_dianya = %d \r\n", (u8)sum_data);
wd_dianya = (sum_data /4096.0)*3300.0; //对应的温度电压值
Error = 500.0 - wd_dianya; //wd_dianya为AD采样电压 Error = PV[SetPoint] - wd_dianya; //wd_dianya为AD采样电压
e0=(P+I*T+(D/T)); // 对应为 A e0=(double)P+(double)(I*4.4)+(double)(D/4.4);
printf("The e0= %d \r\n",(u8)(1000*e0)); //
e1=(P+2.0*(D/T)); // B e1=(double)P+2.0*(double)(D/T);
e2=(D/T); // C e2=(double)(D/T);
du=0.0;
du=(u8)(e0*Error-e1*LastError+e2*BEFORError)*0.05; //为返回的增量
printf("The du= %d \r\n",(u8)du);
BEFORError=LastError;
LastError=Error;
u=u+du; //返回占空比值1/200
if(u>=128.0)
{u=128.0;}
if(u<0){u=0.0;}
printf("The u is = %d \r\n", (u8)u);
printf("The current AD and u is ok = %5.4fmV \r\n", wd_dianya);
return u;
}
void USART1_IRQHandler(void)
{
//处理接收到的数据
/*处理接收到的数据*/
if(USART_GetFlagStatus(USART1,USART_IT_RXNE)!= RESET)
{
ReBuffer[Recounter++]= USART_ReceiveData(USART1);
if(Recounter==6) //判断是否接收一个完整的数据帧
{
if(ReBuffer[0]==0x55) //判断收到的第一个字节是否为0x55 即协议的起始码
{
SetPoint=0;
SetPoint=ReBuffer[2]<<8; //左移八位
SetPoint=SetPoint+ReBuffer[1] ; //得到一个数据值 也就是上位机发来的数据值
printf("The SetPoint IS= %d \r\n", (u8)SetPoint);
P=ReBuffer[3] ;
printf("The is P= %d \r\n", (u8)P);
I=ReBuffer[4] ;
printf("The is I= %d \r\n", (u8)I);
D=ReBuffer[5] ;
printf("The is D= %d \r\n", (u8)D);
}
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //请中断标志
}
// USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
|