新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > USRAT通信測試

        USRAT通信測試

        作者: 時間:2016-11-11 來源:網絡 收藏
        估計是接口連線的問題,及晶振的問題,我的實驗實際發送接收顯示有一定錯誤率。

        嘗試了短接測試,錯誤率還比較小,通過rs232連接到電腦中的串口助手顯示的信息不準確。

        本文引用地址:http://www.104case.com/article/201611/316844.htm




        #include
        #include
        #include "delay.h"
        #define uchar unsigned char
        #define BAUD 9600 //波特率采用9600bps
        #define CRYSTAL 4000000//系統時鐘4MHz
        //計算和定義波特率設置參數
        #define BAUD_SETTING (unsigned int)((unsigned long)CRYSTAL/(16*(unsigned long)BAUD)-1)
        #define BAUD_H (unsigned char)(BAUD_SETTING>>8)
        #define BAUD_L (unsigned char)(BAUD_SETTING)
        // USART控制和狀態寄存器的標志位定義
        #define RXC 7 // UCSRA位定義
        #define TXC 6
        #define UDRE 5
        #define FE 4
        #define DOR 3
        #define PE 2
        #define U2X 1
        #define MPCM 0
        #define RXCIE 7 // UCSRB位定義
        #define TXCIE 6
        #define UDRIE 5
        #define RXEN 4
        #define TXEN 3
        #define UCSZ2 2
        #define RXB8 1
        #define TXB8 0
        #define URSEL 7 // UCSRC位定義
        #define UMSEL 6
        #define UPM1 5
        #define UPM0 4
        #define USBS 3
        #define UCSZ1 2
        #define UCSZ0 1
        #define UCPOL 0
        #define FRAMING_ERROR (1<#define PARITY_ERROR (1<#define DATA_OVERRUN (1<#define DATA_REGISTER_EMPTY (1<uchar led7[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共陽7段數碼管顯示0-9對應的8bit
        #pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
        void uart0_rx_isr(void)
        {
        uchar status,data;
        status=UCSRA;
        data=UDR;
        if((status&(FRAMING_ERROR|PARITY_ERROR|DATA_OVERRUN))==0)
        PORTB = led7[data];
        }

        void USART_Transmit(uchar data)
        {
        while(!(UCSRA & DATA_REGISTER_EMPTY)); // 等待發送寄存器空
        UDR=data; // 發送數據
        }

        void inti_port(void)
        {
        PORTB=0x00; // LED段碼輸出
        DDRB=0xff;
        PORTD=0x03; // TXD(PD1)輸出
        DDRD=0x02; // RXD(PD0)輸入,上拉有效
        }

        void inti_usart(void)
        {
        UCSRA = 0x00; // USART初始化
        UCSRB = (1< //UCSRB = 0x98;
        UCSRC = (1< //UCSRC = 0x86;
        UBRRH = BAUD_H; // 設置波特率
        //UBRRL = BAUD_L;
        SEI(); // 開中斷
        }

        void main(void)
        {
        uchar i=0;
        inti_port();
        inti_usart();
        while(1)
        {
        USART_Transmit(i);
        if (++i>=10)i=0;
        delay_ms(500);
        }
        }



        關鍵詞: USRAT通信測

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 定西市| 恩施市| 平潭县| 会东县| 哈密市| 呈贡县| 石狮市| 成安县| 夏津县| 成武县| 吴旗县| 泾川县| 武威市| 皮山县| 八宿县| 易门县| 锦屏县| 察雅县| 轮台县| 榕江县| 会理县| 常宁市| 大安市| 布尔津县| 台南市| 静宁县| 清流县| 三都| 太和县| 鄯善县| 资溪县| 剑河县| 伊金霍洛旗| 利辛县| 惠东县| 张掖市| 浦城县| 双柏县| 桦南县| 莒南县| 博白县|