流程图
代码
void main()
{
unsigned int count = 0;
float sum_ik_x_ik = 0;
float temp;
float ik;
float ik_filter;
init_LCD();
init_timer();
while (1)
{
display();
//读取采样点的值ik
ik = (float)(A_to_D() * 1.0 / 51 - A);
if (ik < 0)
{
ik = -ik;
}
if (flag == 1)
{
//ik加权平均滤波
ik_filter = filter(ik);
//ik^2缓存
temp = (ik_filter * ik_filter);
sum_ik_x_ik += temp;
count++;
flag = 0;
//一周期采样结束
if (count == N)
{
count = 0;
//计算电流(电压)有效值
I = sqrt(sum_ik_x_ik / N);
sum_ik_x_ik = 0;
}
}
}
}
void timer0_isr() interrupt 1
{
TH0 = (65536 - 20000 / N) / 256;
TL0 = (65536 - 20000 / N) % 256;
flag = 1;
}