欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標(biāo)題: 串口發(fā)送數(shù)據(jù)給單片機(jī),單片機(jī)接收后在返回,但結(jié)果沒有顯示,請問哪里有問題呢? [打印本頁]

作者: 明日香    時(shí)間: 2021-1-4 14:58
標(biāo)題: 串口發(fā)送數(shù)據(jù)給單片機(jī),單片機(jī)接收后在返回,但結(jié)果沒有顯示,請問哪里有問題呢?
求教:我用串口助手發(fā)送數(shù)據(jù)給單片機(jī),但再讓單片機(jī)把接收到的數(shù)據(jù)發(fā)送給串口助手,但卻沒有結(jié)果,請問是怎么回事呢?

請問這是出了什么問題呢?
以下為源程序:
  1. #include <stc12c5a.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <intrins.h>
  7. #define uchar unsigned char
  8. #define uint  unsigned int

  9. uchar rec[7]={0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  10. uchar table[2]={0x00,0x00};//接收

  11. uchar num=0;
  12. void UART_1Interrupt();//串口1接收字符串
  13. void UART_1send();//串口發(fā)送
  14. int flag2=0;//接收標(biāo)志位

  15. void delay(uint z)//延時(shí)函數(shù)1ms
  16. {
  17.         uint x,y;
  18.         for(x=z;x>0;x--);
  19.                 for(y=110;y>0;y--);
  20. }


  21. void init()  //系統(tǒng)初始化
  22. {
  23. TMOD |=0X20;//定時(shí)器T1,方式2,波特率由PCON寄存器的SMOD決定
  24. SCON=0x50; //REN RI TI,RI為0,TI為0
  25. //串行口1方式1 SCON是串行口1的串行控制寄存器,REN為1,允許接收
  26. PCON=0x00;//各工作方式波特率加倍
  27. TH1=0xfD;//9600bps@11.0592
  28. TL1=0xfD;
  29. TR1=1; //定時(shí)器1中斷打開
  30. EA=1;//cpu總中斷允許位,1為開放中斷
  31. ES=1;// 1允許串行口中斷
  32. }

  33. void main()
  34. {
  35.         init();
  36.         while(1)
  37.         {
  38.         if(flag2==1)
  39.           {
  40.            UART_1send();
  41.           }
  42.         }
  43. }

  44. void UART_1send()  //串口發(fā)送函數(shù)
  45. {
  46.    SBUF=table[0];//發(fā)送table[0],串口助手應(yīng)顯示
  47.          while(TI==0);
  48.        {
  49.                                          }                        //數(shù)據(jù)發(fā)送結(jié)束時(shí)TI自動置1
  50.                          TI=0;//清除數(shù)據(jù)傳送標(biāo)志
  51.                         delay(500);
  52. }

  53. void UART_1interrupt() interrupt 4  //串口1接收函數(shù)
  54. {
  55.         if(RI)
  56.         {
  57.                 if(flag2==0)//如果flag2=0,
  58.                 {
  59.                   if(num==0)
  60.                         {        
  61.              if(SBUF==0x01)//如果SUBF=0x01
  62.                          {
  63.                          rec[num++]=SBUF;
  64.                                  RI=0;
  65.                          }
  66.                      else
  67.                            {
  68.           rec[num++]=SBUF;
  69.                              if(num>=6)7個(gè)為一組,發(fā)送完一組數(shù)據(jù)
  70.                               {
  71.                               flag2=1;//接收完一組數(shù)據(jù)置1
  72.                                     num=0;
  73.                                                 table[0]=rec[4];//rec[4]給table[0]
  74.                                }
  75.                                                 RI=0;
  76.                      }
  77.             }
  78.      }
  79.   }
  80. }
復(fù)制代碼

捕獲1.PNG (13.67 KB, 下載次數(shù): 70)

捕獲1.PNG





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