新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 單片機鍵盤輸入及串口發送

        單片機鍵盤輸入及串口發送

        作者: 時間:2016-11-26 來源:網絡 收藏
        #include"reg52.h"

        typedef unsigned char uchar;
        typedef unsigned int uint;

        //數碼管端口控制
        sbit SG = P0^0;//控制個位
        sbit SS = P0^1;//控制十位
        unsigned char code rst[6]={0xe4,0xc0,0xe0,0xc0,0xe0,0x22};//定義一個code類型 實現軟件復位
        uchar tab0[] = {0x81,0xbd,0x92,0x98,0xac,0xc8,0xc0,0x9d,0x80,0x88}; //0~9共陽數碼管
        uchar tab1[2] ={0xfe,0xfe} ;
        uchar SendBuf[10];//發送的數
        uchar *p;
        uchar t1,t2;
        uchar channel;
        uchar SendLen=0,BufLen=7;
        Timer_0();//數碼動態掃描的定時函數
        Delay(uint t);//按鍵去抖的延時函數
        uint Key_1();//鍵盤掃描函數
        uint Key_2();//鍵盤返回值函數
        Delay();//延時函數
        Reset();//復位函數
        Affirm();//確認發送函數
        void init_ser1();//串口初始化函數
        void SendTo();
        void Coding(char,char,char,char,char);
        main()
        {
        ucharn;
        init_ser1();//串口初始化函數
        P2=0x00;
        P3=0x0f;
        p=tab1;
        Timer_0(); //定時器初始化函數
        SendTo();
        t1 = 0;
        t2 = 0;
        while(1)
        {

        n=Key_2();
        if(n!=0xff)
        {

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

        //Led=0;
        if(*p==0xfe)
        {
        if(p==tab1)
        {
        t1 = n;
        }
        else if(p==tab1+1)
        {
        t2 = n;
        }
        //Led=1;
        Delay(9000); //特別關鍵的延時
        //Led=0;

        *p=tab0[n];
        p++;
        }
        }

        }
        }
        uint Key_1()
        {
        uint i,j;
        if(P2!=0x00)
        {
        Delay(300);//去除按鍵抖動
        if(P2!=0x00)
        {
        i=P2;
        return(i);
        }
        }
        if(P3!=0xf0)
        {
        Delay(300);
        if(P3!=0xf0)

        j=P3;
        return(j);
        }

        }
        uint Key_2()
        {
        switch(key_1()) //根據掃描結果選取值
        {
        case 0x01:return 1;break;
        case0x02:return 2;break;
        case0x04:return 3;break;
        case0x08:return 4;break;
        case0x10:return 5;break;
        case0x20:return 6;break;
        case0x40:return 7;break;
        case0x80:return 8;break;
        case0x1f:return 9;break;
        case0x2f:Reset(); break;
        case0x4f:return 0;break;
        case0x8f:Affirm();break;
        default:return 0xff;break;
        }
        }
        Delay(uint t)//延時函數
        {

        while(--t);

        }
        Reset()//清除復位函數
        {
        Delay(20000);
        Coding(0,0,0,0,0);
        (*((void (*)())rst))();//復位
        }
        Affirm()//確認發送函數
        {


        Delay(20000);
        channel = t1*10 + t2;
        Coding(channel,0,0,0,0);
        SendTo();

        //flag=1;
        }
        Timer_0()//定時器0初始化
        {
        TMOD=TMOD&0xf0|0x01;
        TR0=1;
        ET0=1;
        //EA=1;
        }
        interrupt_0() interrupt 1//數碼管掃描
        {
        TH0=0xf8;
        TL0=0x30;

        P1=tab1[0];
        SG=1;
        SS=0;
        Delay(150);

        P1=tab1[1];
        SG=0;
        SS=1;

        }

        void Coding(char CHANNEL,char Command3,char Command4,char Command5,char Command6)
        {
        SendBuf[0]=0xFF;
        SendBuf[1]=CHANNEL;
        SendBuf[2]=Command3;
        SendBuf[3]=Command4;
        SendBuf[4]=Command5;
        SendBuf[5]=Command6;
        SendBuf[6]=Command6+Command5+Command4+Command3+CHANNEL;
        }

        void init_ser1()
        {

        // REN=1;//允許串行口接收數據
        //
        // SM0=0;
        // SM1=1;//串行口工作方式為1
        //
        // TMOD=TMOD&0xf0|0x20;//通過定時器1設置串口波特率
        // PCON|=0x0;
        // TH1=0xFD;
        // TL1=0xFD; //波特率9600
        // PS=1;
        //
        // TR1=1;//啟動定時器1
        // ES=1;//開串行口中斷
        // PS=1;
        PCON=0X00;
        SCON=0X50;
        TMOD|=0X20;
        TH1=0xFd;
        TL1=0xFd;
        TI=1;
        EA=1;
        TR1=1;
        ES=1;
        }
        void Serial(void)interrupt 4
        {
        if(RI)
        {
        RI=0;
        }
        if(TI)
        {
        TI=0;
        //Delay(20);
        if(SendLen >= BufLen) BufLen = 0;
        if(BufLen)
        {
        SBUF = SendBuf[SendLen++];
        }
        }
        }
        void SendTo(void)
        {
        BufLen = 7;
        SendLen = 0;
        SBUF = "" ;
        do{}
        while(BufLen);
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 佛学| 滁州市| 印江| 伊川县| 东辽县| 道孚县| 泗洪县| 通州区| 孝义市| 阿拉善右旗| 运城市| 宜城市| 博白县| 微博| 叶城县| 瓦房店市| 绥棱县| 梨树县| 朝阳县| 平乡县| 都安| 治县。| 辛集市| 左云县| 六枝特区| 邹平县| 临漳县| 阿巴嘎旗| 奎屯市| 株洲县| 区。| 绥宁县| 沙雅县| 南昌县| 潼南县| 横山县| 靖宇县| 白山市| 永城市| 浏阳市| 修文县|