2014-12-29 37 views
0

我有電子卡上的端口(Atmel卡與8051微控制器)。該端口名爲p3_0。端口得到'1'或'0'邏輯。我需要知道如何讀取頻率

1  0  1  0 
'''''''  ''''''''  
'  '  '  ' 
'  '''''''  ''''''  And so on.... 

我需要使用C爲8051(這實際上是原始的C),並編寫一個代碼,將知道頻率是什麼。最後我需要在字符數組中的頻率,因爲我需要將它打印到僅知道打印char array[];的LCD上。我使用「itoa」函數將int數字「count」轉換爲char array [];。頻率可以在16Hz到90Hz之間。

這是我做過什麼:

void main(void) 
{ 
    init_lcd()///Print command 
    ;print_lcd(1,"The Project"); //Print command 
    ;print_lcd(2,"is starting:"); //Print command 
    for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);} 
    for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);} 

while(1) 
    { 
    count = 0; 
    while(P3_0 == 1) 
     { 
     count ++; 
      init_lcd() //Print command 
      ;print_lcd(1,"Counting..."); //Print command 
       for(i=0; i<1; i++){delay(1000);delay(1000);delay(1000);}//delay... 
     } 

    init_lcd()///Print command 
    ;print_lcd(1,"Done Counting!"); //Print command 
    for(i=0; i<1; i++){delay(1000);delay(1000);delay(1000);} 

    ;print_lcd(2,itoa(count)); //Print command 
    for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);} 
    for(i=0; i<20; i++){delay(1000);delay(1000);delay(1000);} 
    } 
} 

的想法是我檢查端口和我算每一次的「1」。最後,我會決定一個時間,每'1'計數和(做頻率計算)

問題是:我怎麼知道究竟是如何在頻率上,我怎麼能?

在計數「從頻率取」的時候也有與達賴有關係的東西。 我該如何解決這個問題?

感謝所有誰閱讀輸入代碼herethis! 我真的很感謝任何幫助!, 謝謝大家!

+0

任何答案?請?! –

回答

0

它們是測量頻率的邏輯。 8051有2個定時器/計數器,配置一個作爲定時器,另一個作爲計數器,並將頻率輸入到計數器引腳,然後同時啓動計數器和定時器(無論如何,它們在激活定時器時會有1個機器週期延遲,但您可以通過計算進行補償),然後在此負載計數器之前輸入合適的值,以便模塊可以測量低頻。當計數器溢出停止計時器並且知道否。的計數和時間流逝,你可以計算出頻率。 希望這個邏輯有效。