【发布时间】:2016-12-02 02:38:48
【问题描述】:
我正在编写固件代码,我将中断时间设置为 10us,但现在循环每 5ms 运行一次,这要慢得多。任何关于如何加速循环的想法都非常感谢!
unsigned long lastTime;
double BAT_I_FB, Output, Setpoint= 8 ;
double errSum=0, lastErr,error=99;
double kp=0.1, ki=15, kd=0;
double KdVal,KpVal,KiVal,PID,BUS_V;
double SampleTime = 10;
void loop() {
unsigned long now = micros();
int timeChange = (now-lastTime);
if(timeChange >= SampleTime)
{
/*Compute all the working error variables*/
BAT_I_FB = analogRead(pins[15].arduinoPinNumber);
BAT_I_FB = float(BAT_I_FB * (5.0/1024)) * pins[15].multiplier;
error = Setpoint - BAT_I_FB;
errSum += error;
double dErr = (error - lastErr);
BUS_V = analogRead(pins[18].arduinoPinNumber);
BUS_V = float(BUS_V * (5.0/1024)) * pins[18].multiplier;
/*compute PID Output*/
PID = kp * error +ki/10000 * errSum + kd * 1000 * dErr;
Output = (PID-100) * (-2.5);
analogWrite(2, Output);
/*Remember some variable for next time*/
lastErr = error;
lastTime = now;
}
【问题讨论】:
-
这个处理器有多快,中断处理程序中有多少代码?您可能只能在 10 微秒内执行少量指令 - 在这种情况下,解决方案是“少做事或获得更快的处理器”。
标签: c arduino interrupt firmware