![]() |
PCDVD數位科技討論區
(https://www.pcdvd.com.tw/index.php)
- 七嘴八舌異言堂
(https://www.pcdvd.com.tw/forumdisplay.php?f=12)
- - 請教Keli C語法和UART I/O的問題
(https://www.pcdvd.com.tw/showthread.php?t=862853)
|
|---|
請教Keli C語法和UART I/O的問題
我把UART I/O的程式模組化了,
可是在輸出的時候遇到亂碼的情況, 程式如下: 代碼:
void uart_read_char(char *ch){ //這是從UART讀一個char的副程式
char in;
REN=1;
while(RI!=1);
RI=0;
//in=SBUF;
//uart_write_char(in);
*ch=SBUF; //這樣寫出就會 NAK(0x15)的亂碼
uart_write_char(*ch); //從UART寫一個char,在這呼叫因為在debug
}
目前的問題就是,如果寫成 in=SBUF; uart_write_char(in); 就可以正確的輸出輸入(用軟體終端機測試) 但是如果寫成 *ch=SBUF; uart_write_char(*ch); 就會讀到NAK(0x15) 難道Keli C不支援指標傳遞嗎? 主程式: 代碼:
#ifdef TEST_UART
void main(){
char keyin;
uart_init(UART_MODE_1,BUAT_RATE_9600);
while(1){
uart_read_char(&keyin);
}
}
#endif
|
Interrupt ??
|
引用:
*ch=SBUF;// 設定ch的位址=SBUF內容值. uart_write_char(*ch);//肯定列印出一堆奇怪東西. 丁丁大學果然人材擠擠.:laugh: |
改成這樣看看(多此一舉的寫法... :laugh: )...
in=SBUF; *ch=in; uart_write_char(*ch); Keil有支援指標,但是有時會錯... 改用IAR吧!!!寫多工時很好用(但是51不適合多工 :jolin: )... |
引用:
嗯...錯誤, Debug 2小時終於確認問題了... 引用:
:jolin: 引用:
謝謝你,我試試看 |
引用:
還是不懂哪裡錯了 代碼:
void uart_read_char(char *ch){
...
*ch=SBUF;
uart_write_char(*ch);
}
|
引用:
對齁 不過interrupt function的sign為 void FUNC(void) interrupt NO(), 這下得refactoring囉... |
引用:
"*ch=SBUF" ch is pointing to SBUF which I assume is a memory-mapped register mapped to the output of the UART input buffer. Once you read it the buffer pushes out the valid data, so the output now is the next item in the buffer (invalid data?) uart_write_char(*ch) then obviously ends up writing the wrong value. 我很久沒寫embedded c了..好像是這樣 |
改了一下寫法,
read UART副程式的prototype由pass by address value改成return char void main(){ uart_init(UART_MODE_1,BUAT_RATE_9600); uart_write_char(uart_read_char()); } 這樣就正常工作了。 不過有點土法煉鋼就是了,如果中斷的方式比較正規 |
| 所有的時間均為GMT +8。 現在的時間是12:06 PM. |
vBulletin Version 3.0.1
powered_by_vbulletin 2025。