[問題] 字串傳輸時,LCD會跑出上一筆的資料?

看板C_and_CPP (C/C++)作者 (milano)時間12年前 (2014/01/21 16:07), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) keil c, 89S51, RS232 問題(Question): 我用RS232連續傳三筆長度不同的資料給89S51控制LCD(2*16)顯示 執行時螢幕上有上一筆的資料 例如:x ='P1:11112222333344445555'; ->P1:1111222233334 4445555 x1 ='P2:111122223333'; ->P2:111122223333 4445555 x2 ='P3:11112222'; ->P3:11112222 333 4445555 我知道後面有1格空白是字串最後有個'\0'的code. 但我明明在程式裡面有清除LCD螢幕及清空字串的code 我覺得應該是清空字串時有錯...但應該怎麼寫??? 程式碼(Code):(請善用置底文網頁, 記得排版) 先宣告buf[32] 程式流程是串列中斷後字串(SBUF)丟給buf[] index=1時,LCD初始化,清除LCD螢幕,設定LCD游標位置,寫入字串顯示,把buf[0]字串清空? int index=0; char buf[32]; main() { SCON=0x50; /*0101-0000 */ TMOD=0x20; /*0010-0000 */ TH1=243; TL1=243; TR1=1; IE=0x90; /*1001-0000 */ while(1) { if(index) { init(); 初始化 delayms(3); 延遲3ms wrins(0x01); // clear monitor//清除LCD螢幕 delayms(3); 延遲3ms position(1,1); 設定游標位置 display(buf); 顯示buf[]字串 buf[0]='\0'; // clear buf[] string varible// } else { P0=0x00; delayms(1000); P0=0xff; delayms(1000); } } } void SerialISR(void) interrupt 4 //ISR { if( RI ) { buf[index] = SBUF; index++; RI=0; } } void position(char line,column) //LCD position// { unsigned char instruction; line--; column--; instruction=0x80+(0x40*line + column); wrins(instruction); } void display(char *string) //當字串數字寫到16時游標換第2列第1行開始寫... { char k=0; while(string[k] !=0x00) { if(k==16) { position(2,1); wrdata(string[k]); k++; } else { wrdata(string[k]); k++; } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 120.126.8.123

01/21 17:45, , 1F
clear 時 index 要歸零 ?
01/21 17:45, 1F

01/23 10:20, , 2F
那邊我在這漏打了..index=0;在buf[0]='\0'下一行
01/23 10:20, 2F
文章代碼(AID): #1ItYhTiW (C_and_CPP)
文章代碼(AID): #1ItYhTiW (C_and_CPP)