<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于51單片機(jī)的串口檢測(cè)程序

          基于51單片機(jī)的串口檢測(cè)程序

          作者: 時(shí)間:2012-07-07 來(lái)源:網(wǎng)絡(luò) 收藏

          #i nclude reg51_STC.H
          #define uchar unsigned char
          #define uint unsigned int

          //--------------------------------------------------------------------------
          //函數(shù)名稱: UART_Init()
          //函數(shù)功能: 初始化函數(shù),在系統(tǒng)時(shí)鐘為11.059MHZ時(shí),設(shè)定波特率為9600bit/s
          //其他說(shuō)明: 接收中斷允許,發(fā)送中斷禁止
          //--------------------------------------------------------------------------

          void UART_Init(void)
          {
          SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
          TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
          PCON |= 0x80 ; //SMOD=1;
          TH1 = 0xFA ; //Baud:9600 fosc=11.0592MHz
          ES=1;
          TR1 = 1 ; // timer 1 run
          EA=1;
          }


          //--------------------------------------------------------------------------
          //函數(shù)名稱: main(void)
          //函數(shù)功能: 主函數(shù)
          //其他說(shuō)明: 無(wú)
          //--------------------------------------------------------------------------

          void main(void)
          {
          UART_Init();
          while(1);
          }


          //--------------------------------------------------------------------------
          //函數(shù)名稱: Uart_SendData()
          //函數(shù)功能: 串口發(fā)送一個(gè)字節(jié)的數(shù)據(jù)
          //其他說(shuō)明: 此供中斷調(diào)用
          //--------------------------------------------------------------------------

          void Uart_SendData(uchar dat)
          {
          SBUF=dat; //寫SBUF,開始發(fā)送
          while(TI==0); //等待發(fā)送
          TI=0; //清發(fā)送標(biāo)志位
          }


          //--------------------------------------------------------------------------
          //函數(shù)名稱: INT_UartRcv()
          //函數(shù)功能: 串口接收中斷函數(shù)
          //其他說(shuō)明: 無(wú)
          //--------------------------------------------------------------------------

          void INT_UartRcv(void) interrupt 4
          {
          uchar Rcv;
          if(RI)
          {
          RI=0; //
          Rcv=SBUF;
          Uart_SendData(Rcv); //返回接收數(shù)據(jù),可以改為其他函數(shù)
          }
          }

          本文引用地址:http://www.ex-cimer.com/article/171192.htm

          NO。2

          #i ncludereg52.h>
          #i nclude string.h>
          #define INBUF_LEN 4 //數(shù)據(jù)長(zhǎng)度
          unsigned char inbuf1[INBUF_LEN];
          unsigned char checksum,count3;
          bit read_flag= 0 ;
          void init_serialcomm( void )
          {
          SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
          TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
          PCON |= 0x80 ; //SMOD=1;
          TH1 = 0xF4 ; //Baud:4800 fosc=11.0592MHz
          IE |= 0x90 ; //Enable Serial Interrupt
          TR1 = 1 ; // timer 1 run
          EA=1;
          }

          //向串口發(fā)送一個(gè)字符


          上一頁(yè) 1 2 下一頁(yè)

          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();