標(biāo)題: 單片機(jī)串口通訊程序變量賦值問(wèn)題 [打印本頁(yè)]

作者: Thunder.    時(shí)間: 2019-4-8 09:43
標(biāo)題: 單片機(jī)串口通訊程序變量賦值問(wèn)題
如圖 我想把這三個(gè)數(shù)取過(guò)來(lái) 231 是定義三個(gè)數(shù)組呢  還是直接使用變量賦值 這下面是我寫(xiě)的 但是取不出來(lái)
  1. #include<stc12.h>//頭文件
  2. sbit DU=P2^6;
  3. sbit WE=P2^7;
  4. unsigned int date[6],value,one,two,three;
  5. unsigned char num=0;
  6. unsigned int  code sz[4]={0x3f,0x06,0x5b ,0x4f};         
  7. void delay(unsigned char xms)                  //xms等于幾就延遲幾毫秒
  8. {
  9.    unsigned int i,j;
  10.         for(i=xms;i>0;i--)
  11.             for(j=112;j>0;j--);
  12. }
  13. void UartConfiguration()
  14. {
  15.         
  16.            TMOD=0x20;    //定時(shí)器工作方式,選擇了定時(shí)器1,工作方式2 八位初值自動(dòng)重裝的8位定時(shí)器。         
  17.      TH1=0xfd;     //定時(shí)器1初值  ,設(shè)置波特率為9600 晶振11.0529MHZ?  
  18.      TL1=0xfd;  
  19.            AUXR |= 0x40;                //定時(shí)器1時(shí)鐘為Fosc,即1T
  20.            AUXR &= 0xFE;                //串口1選擇定時(shí)器1為波特率發(fā)生器
  21.      TR1=1;        //開(kāi)啟定時(shí)器1  
  22.            SCON=0X50;
  23.            PCON=0X00;
  24.            


  25.            ES=1;         //允許串口中斷  
  26.      EA=1;         //允許中斷(總閘)   
  27.         }

  28. void main()
  29. {
  30.   UartConfiguration();
  31. while(1)
  32. {
  33. //   one=date[0]%10;
  34. //   two=date[2]%10;
  35. //   three=date[4]%10;
  36.         
  37.           one=value%10;
  38.     two=(value%100)/10;
  39.     three=value/100;
  40.     P0=sz[one];
  41.                 DU=1;
  42.                 DU=0;
  43.                 P0=0x7f;
  44.                 WE=1;
  45.                 WE=0;
  46.                 delay(2);               

  47.                 P0=sz[two];        
  48.                 DU=1;
  49.                 DU=0;
  50.                 P0=0xbf;
  51.                 WE=1;
  52.                 WE=0;
  53.                 delay(2);

  54.                 P0=sz[three];        
  55.                 DU=1;
  56.                 DU=0;
  57.                 P0=0xdf;
  58.                 WE=1;
  59.                 WE=0;
  60.                 delay(2);
  61.         
  62. }
  63.         }
  64. void Uart() interrupt 4
  65. {
  66.         if(RI==1)
  67.         {
  68.                         RI=0;
  69. //                 date[num]=SBUF;
  70. //                 num++;
  71. //                 if(num==7)
  72. //                 {
  73. //                  num=0;
  74. //                  }
  75.                  value=SBUF;
  76.          
  77.         }
  78. }
復(fù)制代碼

QQ圖片20190408094138.jpg (195.71 KB, 下載次數(shù): 159)

串口發(fā)送的內(nèi)容

串口發(fā)送的內(nèi)容

作者: 孤劍酷    時(shí)間: 2019-4-10 19:54
提供一個(gè)思路,因?yàn)榇谳敵鲞^(guò)來(lái)的都是有空格來(lái)斷開(kāi)的,當(dāng)你需要這些數(shù)據(jù)的時(shí)候,你可以在你獲取數(shù)據(jù)的程序里按照順序以空格來(lái)判斷這個(gè)數(shù)據(jù)是不是完整的,類似于C語(yǔ)言的那個(gè)提取字符串。
作者: chtyise    時(shí)間: 2019-4-10 23:14
如果按你寫(xiě)的程序,當(dāng)串口接收數(shù)據(jù)是0xe7,這樣可以的.
如果按你電腦上的數(shù)據(jù)是三個(gè)十六進(jìn)制數(shù),程序可以用數(shù)組接收,
注意電腦收到是6個(gè)數(shù)據(jù)0x32 0x20 0x33 0x20 0x31 0x20
作者: louis333    時(shí)間: 2019-4-11 08:52
串口接受的一般是字符串發(fā)送,發(fā)送的是ASCII碼,需要轉(zhuǎn)換成自己需要的數(shù)字,去掉空格,用數(shù)組儲(chǔ)存起來(lái)比較合適!




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1