新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > AVR和51單片機串口通信簡單實驗

        AVR和51單片機串口通信簡單實驗

        作者: 時間:2016-11-30 來源:網絡 收藏
        //AVR作為發送機,控制51的喇叭發聲和2極管發光
        //AVR發送方代碼 //采用2400波特率
        #include
        #include
        #define uchar unsigned char
        #define uint unsigned int
        #define k1 PIND_Bit4 //設置為喇叭
        #define k3 PIND_Bit6 //設置為發光管
        #define k4 PIND_Bit7 //發送控制命令
        #define UDRE 5
        #define RXC 7
        uchar dat=48;
        void delay(uint k)
        {
        uint i,j;
        for(i=0;ifor(j=0;j<1140;j++);
        }
        #include "1602.h"
        void port_init()
        {
        PORTA=0xff;
        DDRA=0xff;
        PORTB=0xff;
        DDRB=0xff;
        PORTC=0xff;
        DDRC=0xff;
        PORTD=0xff;
        DDRD=0x0f;
        }
        void serial_init()
        {
        UCSRB=0x00;
        UCSRA=0x02; //雙倍速
        UCSRC=0x06; //8位字符模式
        UBRRH=0x01; //2400波特率
        UBRRL=0xa0;
        UCSRB=0x18;
        }
        void send(uchar k) //串口發送函數
        {
        while(!(UCSRA&(1<UDR=k;

        }
        void key1()
        {
        static uint i,j;
        if(k1==0)
        {
        if(i==0)
        {
        j++;
        if(j>10)
        {
        if(k1==0)
        {
        i=1;j=0;
        dat=49;
        }
        }
        }
        }
        else
        {
        i=0;j=0;
        }
        }
        void key3()
        {
        static uint i,j;
        if(k3==0)
        {
        if(i==0)
        {
        j++;
        if(j>10)
        {
        if(k3==0)
        {
        i=1;j=0;
        dat=50;
        }
        }
        }
        }
        else
        {
        i=0;j=0;
        }
        }

        void main()
        {
        port_init();
        serial_init();
        init1602();
        delay(1000);
        while(1)
        {
        displayonechar(1,6,dat);
        if(k4==0)
        {
        send(dat);
        }
        key1(); //喇叭發聲
        key3(); //2極管發光
        }
        }
        //----------1602庫函數-----------
        #define RS PORTB_Bit0
        #define RW PORTB_Bit1
        #define EN PORTB_Bit2
        #define DATA PORTA
        #define busy 0x80

        void wait()
        {
        uchar val;
        DATA=0xff;
        RS=0;
        RW=1;
        __no_operation();
        __no_operation();
        EN=1;
        __no_operation();
        __no_operation();
        DDRA=0x00;
        val=PINA;
        while(val&busy)
        {
        val=PINA;
        }
        EN=0;
        DDRA=0xff;
        }

        void writecmd(uchar cmd)
        {
        wait();
        RS=0;
        RW=0;
        __no_operation();
        DATA=cmd;
        __no_operation();
        EN=1;
        __no_operation();
        __no_operation();
        EN=0;
        }
        上一頁 1 2 下一頁

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 红原县| 淮滨县| 花莲县| 思南县| 延安市| 平江县| 扎鲁特旗| 平安县| 乐安县| 金堂县| 大英县| 黄龙县| 伽师县| 秦安县| 左贡县| 乐东| 攀枝花市| 大新县| 南安市| 婺源县| 尚志市| 赤城县| 香港| 闽清县| 龙泉市| 遂平县| 炎陵县| 曲周县| 雅江县| 海安县| 镇安县| 克山县| 资源县| 临猗县| 平度市| 江门市| 黄龙县| 鲁甸县| 昂仁县| 左贡县| 陆良县|