我正在为 MSP430 设备编写固件,该设备使用 LED 和光电二极管来检测墨水上的特定类型。该设备以大约 155us 的速度扫描,扫描仪下的样本速度范围为 0.1m/s 至 3.3m/s。该设备的目标是测试墨水并测量墨水(通过)与测试(未通过)的比率,并在比率介于相应值之间时打开绿色 LED,而不是在相应值之间打开红色 LED。我正在使用静态整数数组将连续通过的值和测试值存储到每个数组的相同索引号。在数组的最后一个索引之后,索引被设置回零并覆盖旧值。
绿色_LED_ON;和类似的定义是我的 MCU 的端口定义,并且经过验证是正确的。
event 是测试结果。如果检测到墨水,则 event=DETECTED,反之亦然
test 将是 GUI 设置的平均值,但现在它什么都不是,因为我的这部分功能没有工作
通常情况下,我不会有 GREEN_LED_ON;等在 if(event) 循环中,但我把它们放在那里可以看到我的代码出错的地方。代码似乎卡在了甚至开始的循环中。例如,如果我从没有墨水的设备开始,LED 保持红色,当设备墨水过多时,无论如何设备都会保持绿色。有谁知道我做错了什么以及如何解决它?
注意:
*我也尝试将 while(event)s 更改为 if 语句,我得到相同的结果
*当我在 if 语句中注释数组时,代码按预期工作
*顶部版本是代码的当前部分,底部是我开始使用的
void display(char event, char test) {
static int size=6;
static int array[6]={0}; //array with number of passes for each n
static int n=0;
static float sum=0;//total number of passes
static float average=0;//average pass rate over n
static int consecpass=0; //consecutive passes
static int consecfail=0; //consecutive fails
static int totalnumberoftests[6]={0}; //total number of tests conducted. Counts the number of passing or failing tests for the nth value
static float counter=1; //used to count the total number of tests
static int flag=0;
if(n==size) n=0;
if (event == DETECTED)
{
if (flag==0)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=0;
totalnumberoftests[n]=consecfail;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
n++;
}
consecfail=0;
consecpass++;
//GREEN_LED_ON;
//RED_LED_OFF;
flag=1;
} if (event==NOT_DETECTED){
if(flag==1)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=consecpass;
totalnumberoftests[n]=consecpass;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
n++;
}
//array[n]=consecpass;
//totalnumberoftests[n]=consecpass;
consecpass=0;
consecfail++;
flag=0;
//GREEN_LED_OFF;
//RED_LED_ON;
}
if (consecpass>8000)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=consecpass;
totalnumberoftests[n]=consecpass;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
n++;
}
if(consecfail>30000)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=0;
totalnumberoftests[n]=consecfail;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
n++;
}
average=sum/counter;
if(average<1 && average >0 )
{
GREEN_LED_ON;
RED_LED_OFF;
}else{
GREEN_LED_OFF;
RED_LED_ON;
}
}
这是我最初的开始:
void display(char event, char test) {
static int size=6;
static int array[6]={0}; //array with number of passes for each n
static int n=0;
static int sum=0;//total number of passes
static double average=0;//average pass rate over n
static int consecpass=0; //consecutive passes
static int consecfail=0; //consecutive fails
static int totalnumberoftests[6]={0}; //total number of tests conducted. Counts the number of passing or failing tests for the nth value
static float counter=0; //used to count the total number of tests
while(n<=size)
{
sum=sum-array[n]; //subtacts the nth value from the total sum of passing tests
counter=counter-totalnumberoftests[n]; //subtracts the nth value of the total number of tests run
if(event == DETECTED)
{
array[n]=0;
totalnumberoftests[n]=consecfail;
consecfail=0;
consecpass++;
GREEN_LED_ON;
RED_LED_OFF;
} if(event==NOT_DETECTED){
array[n]=consecpass;
totalnumberoftests[n]=consecpass;
consecpass=0;
consecfail++;
GREEN_LED_OFF;
RED_LED_ON;
}
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
average=sum/counter;
/*if(average<1)
{
GREEN_LED_ON;
RED_LED_OFF;
}else{
GREEN_LED_OFF;
RED_LED_ON;
}*/
n++;
}
if(n>size) n=0;
}