傳送值有問題 |
缺席
|
saiki
一般會員 ![]() ![]() 發表:3 回覆:26 積分:5 註冊:2008-09-23 發送簡訊給我 |
首先我想先謝謝src大大教我會用串列接收
問題來了 但是我從電腦傳值進入51後 我七段上面顯示的值出現了亂碼 就連時間的計數也都亂了 不管我傳什麼值進去他都出現反的 1 @@? 為什麼 ascii 要解碼成數值不是就把它減 0x30嗎 怎麼我減完會有問題 [code c#] #include char ascii[4] = {0}; unsigned char pc_count = 0; unsigned char int0 = 0; unsigned char secclk = 0; unsigned char time[4] = {0,0,0,0}; //time[3] = min, time[2] = Min,time[1] = hour, time[0] = Hour unsigned char table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; // 0~9; bit rx_flag; bit tx_flag; void init_rs232(void); unsigned char decoder(unsigned char time); //void delay(unsigned int time); void int_s (void) interrupt 4 { if (RI) { pc_count = pc_count 1; if (pc_count == 4) { pc_count = 0; } ascii[pc_count] = SBUF; RI = 0; rx_flag = 1; time[pc_count] = ascii[pc_count] - 0x30; //ascii to integer } } void time0_int(void) interrupt 1 { TH0 = (65536-50000)/256; TL0 = (65536-50000)%6; int0 ; //50ms; = 0.05sec; if (int0 == 20) //1sec; { int0 = 0; secclk ; } if (secclk == 60) //60sec = 1 min { secclk = 0; time[3] ; if (time[3] == 10) //min = 10, Min { time[3] = 0; time[2] ; if (time[2] == 6 && time[3] == 0) //Min = 6, min = 10,hour ; { time[2] = 0; time[3] = 0; time[1] ; if (time[1] == 10) //hour = 10 , Hour { time[1] = 0; time[0] ; if (time[0] == 2 && time[1] == 4) //hour = 4,Hour = 2, { time[0] = 0; time[1] = 0; } } } } } } main(void) { init_rs232(); while (1) { unsigned char i; unsigned char com; com = 0x01; if (rx_flag == 1) { rx_flag = 0; } else { for (i = 0;i < 4;i ) { P0 = ~com; //output to P0 (scan) P1 = decoder(time[i]); com = com << 1; P1 = 0xff; } } } } void init_rs232(void) { TMOD = 0x21; //time1 mod 2, time0 mod 1 SCON = 0x50; // SM1 = 1, mod 1, REN = 1 ET0 = 1; //interrupt time start ES = 1; //serial interrupt start EA = 1; //inetrrupt start PS = 1; //serial interrupt is first TI = 1; TI = 0; TH0 = (65536-50000)/256; //time0 0.05s TL0 = (65536-50000)%6; TR0 = 1; //time0 start TH1 = 243; //bps 9600 TL1 = 243; //bps 9600 TR1 = 1; //time1 start } unsigned char decoder(unsigned char time) //decode output to 7-seg { switch (time) { case 0 : return table[0]; break; case 1 : return table[1]; break; case 2 : return table[2]; break; case 3 : return table[3]; break; case 4 : return table[4]; break; case 5 : return table[5]; break; case 6 : return table[6]; break; case 7 : return table[7]; break; case 8 : return table[8]; break; case 9 : return table[9]; break; } } /*void delay(unsigned int time) //delay 1ms { unsigned int n; while (time > 0) { n = 120; while (n>0) n--; time--; } } */ [/code] |
saiki
一般會員 ![]() ![]() 發表:3 回覆:26 積分:5 註冊:2008-09-23 發送簡訊給我 |
|
taishyang
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |