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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Atmega32單片機(jī)串口驅(qū)動(dòng)程序

          Atmega32單片機(jī)串口驅(qū)動(dòng)程序

          作者: 時(shí)間:2013-12-12 來源:網(wǎng)絡(luò) 收藏

          /* ******************************************************************
          * Filename: uart.c
          * Author: lstzixing
          * Mail: blievethink@gmail.com
          * Date: 2009-5-26
          * Description: 串口數(shù)據(jù)收發(fā)接口. For
          * ****************************************************************** */

          #include "app.h"

          // 對(duì)發(fā)送緩沖,信號(hào)計(jì)數(shù)為空閑字符數(shù)
          // 對(duì)接收緩沖,計(jì)數(shù)為緩沖已有計(jì)數(shù)
          typedef struct _FIFO
          {
          INT8U * buf; // FIFO緩沖區(qū)
          INT8U * in, * out; // FIFO讀寫指針
          OS_EVENT * sem; // FIFO讀寫同步信號(hào)量
          }FIFO;

          static INT8U UartTxBuf[ UART_TX_LEN ]; // 發(fā)送緩沖
          static INT8U UartRxBuf[ UART_RX_LEN ]; // 接收緩沖
          static FIFO UartTxFifo, UartRxFifo; // 收發(fā)緩沖控制FIFO結(jié)構(gòu)

          OS_SEM_DATA SemData;

          static INT8U UartPutRxChar( INT8U c );
          static INT8U UartGetTxChar( INT8U * err );

          #define UartStartTx() { UCSRB |= 1UDRIE; }
          #define UartStopTx() { UCSRB = ~(1UDRIE); }
          #define UartStartRx() { UCSRB |= 1RXCIE; }
          #define UartStopRx() { UCSRB = ~( 1RXCIE ); }
          /* ****************************************************************
          * UartFlush()
          * 功能:緩沖清空
          * 參數(shù): isTxBuf ------ 是否為發(fā)送緩沖
          * 返回值:None
          * 說明:清空收發(fā)緩沖
          * *************************************************************** */
          void UartFlush( INT8U isTxBuf )
          {
          #if OS_CRITICAL_METHOD == 3
          OS_CPU_SR cpu_sr;
          #endif
          INT8U cnt;

          OS_ENTER_CRITICAL();

          if( isTxBuf )
          {
          UartTxFifo.buf = UartTxBuf; // 復(fù)位發(fā)送緩沖讀寫指針
          UartTxFifo.in = UartTxBuf;
          UartTxFifo.out = UartTxBuf;
          OSSemQuery( UartTxFifo.sem, SemData );
          cnt = UART_TX_LEN - SemData.OSCnt; // 在其它地方必須保證SemData.OSCnt UART_TX_LEN
          while( cnt-- )
          OSSemPost( UartTxFifo.sem ); // 復(fù)位發(fā)送信號(hào)量值為UART_TX_LEN
          }

          else
          {
          UartRxFifo.buf = UartRxBuf; // 復(fù)位接收緩沖讀寫指針
          UartRxFifo.in = UartRxBuf;
          UartRxFifo.out = UartRxBuf;
          while( OSSemAccept( UartRxFifo.sem ) ); // 復(fù)位接收信號(hào)量值為0

          }

          OS_EXIT_CRITICAL();
          }

          void UartPutStr( char * str )
          {
          char * ptr;

          ptr = (char *)str;

          while(*ptr != '看屁屁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); })();