c - 在 Arduino 中中断

标签 c arduino interrupt firmware

我正在处理固件代码,我将中断时间设置为 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;

}

最佳答案

请注意,Ardunio 开发板无法运行那么快。例如,对于 analogRead,读取模拟输入大约需要 100 微秒(参见:https://www.arduino.cc/en/Reference/AnalogRead)。对于 analogWrite,它会生成大约 490 Hz (2ms) 的波。并且您未在此处显示的代码的其他部分可能会有更多延迟。

关于c - 在 Arduino 中中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623906/

相关文章:

c - 当子进程通过 ctrl-\中止时,父进程未运行

c - 多线程 C 应用程序框架或模式

c# - 代码运行时退出 Interop 控制的 Excel

c - 汇编器到 C 的转换 INCF 和 CPFSLT

c - 如何使用地址分配一个值?

c - 静态与 Malloc

c++ - 我如何创建一个类并声明另一个类的变量

c++ - 由于此错误,无法使用 microcoap 库。任何修复?

c - PCF8574a 没有响应 - TWI

assembly - 皮质-M3 : Changing Interrupt Return Address