我正在爲MSP430器件編寫固件,該器件使用LED和光電二極管檢測墨水上的特定類型。該裝置掃描約155us,掃描儀下的樣品速度範圍爲.1m/s至3.3m/s。該設備的目標是測試墨水並測量墨水(通過)以測試(不通過)比率,並且當比率介於相應值之間時打開綠色LED,當打開紅色LED時打開綠色LED。我正在使用靜態整數數組來將連續傳遞和測試值的值存儲到每個數組的相同索引編號。在數組的最後一個索引之後,將索引設置回零,並將舊值寫入。圓形陣列卡住if語句
GREEN_LED_ON;和類似的定義是我的MCU的端口定義,並經過驗證是正確的。
事件是測試結果。如果檢測墨水,事件=檢測,反之亦然
測試將平均組由GUI,但現在它是什麼,因爲我沒有我的工作功能
通常的這一部分,我將不會有GREEN_LED_ON;等等在if(event)循環中,但是我把它們放在那裏來看看我的代碼出錯了。代碼似乎陷入了循環甚至開始的地步。例如,如果我從墨水開始使用設備,LED會保持紅色,並且當設備墨水過多時,無論如何設備都保持綠色。有沒有人知道我做錯了什麼,以及如何解決它?
注:
*我也試圖改變,而(事件)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;
}
雖然部分會「卡住」,因爲事件不會改變裏面的值。我想你的意思是「如果」而不是「在那裏」。 – Anty
@And我實際上試圖用ifs替換while並得到相同的確切結果 –
您仍然錯過了一點 - 「事件」在顯示執行過程中不會改變值。如何調用顯示以及如何讀取事件? – Anty