2017-02-08 99 views
0

我正在爲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; 


    } 
+1

雖然部分會「卡住」,因爲事件不會改變裏面的值。我想你的意思是「如果」而不是「在那裏」。 – Anty

+0

@And我實際上試圖用ifs替換while並得到相同的確切結果 –

+0

您仍然錯過了一點 - 「事件」在顯示執行過程中不會改變值。如何調用顯示以及如何讀取事件? – Anty

回答

1

*當我評論陣列內的if語句,代碼按預期方式工作

static int size=6; 
static int array[6]={0}; //array with number of passes for each n 
static int totalnumberoftests[6]={0}; 

while(n<=size) 

當n = 6你傳遞數組對於那些(最小指數= 0),邊界 - 最大指數是5而不是6。

array[n]=0; 
    totalnumberoftests[n]=consecfail; 

這就是UB,這可能會產生無效的行爲。

將條件改爲n <大小。

無論如何,這段代碼對我來說似乎很「怪異」。

+0

while循環是否必要?另一位評論者描述說,事件調用基本上是作爲while循環工作的,而另一個可能會導致我想要發生的問題 –

0

要闡述我的意見,如果你是在一個事件驅動的系統,我希望有一些代碼(通常稱爲「事件循環」)的地方,看起來是這樣的:

event_loop() 
{ 
    while (TRUE) 
    { 
     event = get_event_from_someplace(...); 

     display(...); 
    } 
} 

它可能不是直接調用display,而是有一些過程,其中註冊事件處理程序。但結果是,在某些庫代碼中可能會有無止境的循環,一遍又一遍地調用你的函數。所以你不需要代碼中的while()

您的代碼應該是狀態機,它跟蹤內部狀態(使用static變量,就像您一樣),然後執行每次調用所需的任何更新。

事情是這樣的:

void display(char event, ...) 
{ 
    static int consecutive_passes = 0; 
    static int consecutive_fails = 0; 


    if (event == DETECTED) { 
     ++consecutive_passes; 
    } 
    else if (event == NOT_DETECTED) { 
     ++consecutive_fails; 
    } 
    else { 
     // What else is there? 
    } 
} 

的想法是,這段代碼被調用每一個都有一個事件時,它只是更新任何東西集需要更新。但是沒有while循環,因爲這些調用來自事件循環,這就是你需要的所有循環。

+0

我明白了。謝謝你的澄清! while循環的意義在於數組在n> size(數組)之後重寫數據,我使用while循環來設置數組的索引。這可以用if語句和標誌來完成嗎?我做了一個編輯,顯示我對代碼做了什麼,但它仍然沒有按照我想要的方式工作。 –

+0

是的!你的更新,'if(n == size)n = 0;'是完美的。管理數組的「循環」部分。然而,我不得不承認,你的代碼的其餘部分讓我感到困惑。你可以添加一個更新來解釋你正在測試的內容,以及你試圖收集什麼類型的統計數據? –