看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Windows MPLAB X 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) xc.h -->microchip libraries 問題(Question): 我現在有一個header -> INCLUDE.h 裡面引入多個header ---myLCD.h,INIT.h 我想要讓我的main可以更動INIT.h底下的INIT.c的一個變數 我使用extern 宣告該變數於INIT.h 並宣告該變數於INIT.c 但compile該專案卻跳出這行 (在main的地方)error: 'PWMValue' undeclared (first use in this function) 請問問題出在哪裡 或者是是否有更好的方法? 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): error: 'PWMValue' undeclared (first use in this function) 程式碼(Code):(請善用置底文網頁, 記得排版) 我只放部分 main.c: #include"INCLUDE.h" int main(){ ... sprintf( input1 , "PWM = %lld " , PWMValue ); ... } INCLUDE.h: #ifndef INCLUDE_H #define INCLUDE_H #include <xc.h> #include <stdio.h> #include <string.h> #include "INIT.h" #include "MY_LCD.h" #define Tcy 20000000 //10MHz oscillator with 8xPLL -> 20'000'000MIPS #endif /* INCLUDE_H */ INIT.h #ifndef INIT_H #define INIT_H void IO_init(); void PWM_init(); void Timer1_init(); void Interrupt_init(); void delay_us(unsigned int ); void delay_ms(unsigned int ); void _ISR _T1Interrupt(); void _ISR _INT1Interrupt(); extern unsigned int PWM_Value; extern unsigned char flag; #endif /* INIT_H */ INIT.c: #include "INCLUDE.h" unsigned int TIMER1_DELAY_VALUE; unsigned int PWMValue; ... void _ISR _INT1Interrupt() { PWMValue++; IFS0bits.INT0IF = 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.224.246.127 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1440740897.A.792.html ※ 編輯: tommycc (125.224.246.127), 08/28/2015 13:49:17 ※ 編輯: tommycc (125.224.246.127), 08/28/2015 13:52:22 ※ 編輯: tommycc (125.224.246.127), 08/28/2015 14:03:00 ※ 編輯: tommycc (125.224.246.127), 08/28/2015 14:03:35 ※ 編輯: tommycc (125.224.246.127), 08/28/2015 14:25:05
serikafan: code沒貼錯的話,init.h裡的是PWM_Value,其它是PWMValue 08/28 14:46
tommycc: oh no我在幹嘛 08/28 15:26
tommycc: 所以這種做法是比較好的做法瞜? 08/28 15:27
serikafan: 如果非要用global變數的話,我是會儘量避免就是了 08/28 15:52
tommycc: 盡量讓一個變數待在同一個檔案中做處理? 08/28 16:12
james732: 比較好的作法會寫一些function來操作變數吧? 08/28 16:24
james732: 譬如 void pwm_increment(int value); 來增加 之類的(?) 08/28 16:25
littleshan: 比較好的做法是需要什麼就用參數傳,不要有singleton 08/28 20:16
TeaEEE: 你應該只是忘了初始化 08/31 02:06
TeaEEE: unsigned int PWMValue = 0; 應該就可解決 08/31 02:09