rms c语言 函数,用C可以更快地计算RMS值吗?

博主正在为8位微控制器编写C代码来读取电流互感器的ADC值并计算RMS电流。由于浮点运算的高计算负荷和编译器不支持sqrt()函数,导致代码执行效率低下。他们寻求一种更快的算法来替代浮点计算和sqrt()操作。
摘要由CSDN通过智能技术生成

我正在用C编写一个用于小型8位微控制器的软件。部分代码是读取电流互感器(ZCT)的ADC值,然后计算RMS值。流过ZCT的电流是正弦波,但会失真。我的代码如下:

float adc_value, inst_current;

float acc_load_current; // accumulator = (I1*I1 + I2*I2 + ... + In*In)

double rms_current;

// Calculate the real instantanous value from the ADC reading

inst_current = (adc_value/1024)*2.5; // 10bit ADC, Voltage ref. 2.5V, so formula is: x=(adc/1024)*2.5V

// Update the RMS value with the new instananous value:

// Substract 1 sample from the accumulator (sample size is 512, so divide accumulator by 512 and substract it from the accumulator)

acc_load_current -= (acc_load_current / 512);

inst_current *= inst_current; // square the instantanous current

acc_load_current += inst_current; // Add it to the accumulator

rms_current = (acc_load_current / 512); // Get the mean square value. (sample size is 512)

rms_current = sqrt(rms_current); // Get RMS value

// Now the < rms_current > is the real RMS current

但是,它具有许多浮点计算。这给我的小型MCU增加了很大的负担。而且我发现该sqrt()功能在我的编译器中不起作用。

有没有可以运行得更快的代码?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值