2014-05-04 28 views
0

我目前正在學習如何將HC08系列freescale微控制器與codewarrior IDE(ver。6.3)一起使用。 我寫了一個簡單的程序,但它無法編譯。C代碼中的飛思卡爾微控制器錯誤

#include <hidef.h> 
#include "derivative.h" 

void main(void) { 
    EnableInterrupts; 

    /* include your code here */ 
    DDRA |= 0x03; 
    PTA |= 0x01; 
    unsigned int counter; << error here "Error : C2801: '}' missing 
    counter = 50000; 
    while(counter--); 
    PTA ^= 0x03; 

    for(;;) { 
     __RESET_WATCHDOG(); /* feeds the dog */ 
    } 

} 

任何想法可能是什麼問題呢?所有括號匹配。也許這是微控制器具體的東西?

+4

我已經使用IDE和編譯器,但不記得它是否支持1990或1999 C標準。在C99之前,函數中的可執行代碼之後的變量聲明不合法。嘗試將變量聲明移動到main的開頭。 –

+0

@AviBerger請將此評論添加爲答案,這樣我就可以接受它,並且您將得到您當之無愧的觀點:) – Lukasz

回答

5

我用的是IDE &編譯器,但如果它支持1990年或1999年C標準不記得。

一個塊中的可執行代碼之後的變量定義在C99之前是不合法的。由於這正是您收到錯誤消息的地方,因此看起來編譯器不支持C99。嘗試將變量定義移動到main的開頭。

void main(void) {  
    unsigned int counter;  
    EnableInterrupts; 

    // the rest of the code 
} 

您還可以引入一個新塊。這並不常見&對您的代碼示例不是特別有用。值得指出的是,它適用於循環的括號內(或if語句),並且有時可用於爲此塊提供一個可變局部變量。

void main(void) { 
    EnableInterrupts; 

    DDRA |= 0x03; 
    PTA |= 0x01; 

    { // start of nested scope 
     unsigned int counter; 
     counter = 50000; 
     while(counter--); 
    } // "counter" ceases to exist here 

    PTA ^= 0x03; 

    for(;;) { 
     __RESET_WATCHDOG(); /* feeds the dog */ 
    } 

} 
3

您的編譯器可能是C89編譯器或類似的。這意味着變量定義必須是在範圍的頂部,所以:

void main(void) { 
    unsigned int counter; 
    EnableInterrupts; 

    /* include your code here */ 
    DDRA |= 0x03; 
    PTA |= 0x01; 
相關問題