新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 51單片機外部中斷程序

        51單片機外部中斷程序

        作者: 時間:2016-11-18 來源:網絡 收藏
        /*以下是能在keilC 中變異的源碼,直接粘貼過去就行,外部中斷0(INT0)和外部中斷1都有(INT1)*/

        /*程序很簡單,但是對新手來說還是很實用的*/

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

        /*******************************************************************/
        /* */
        /* 單片機開發(fā)系統演示程序 - INT0 INT1 中斷計數 */
        /* */
        /* 6位數碼管顯示 */
        /* */
        /* */
        /*******************************************************************/

        #include < reg51.h >
        #include

        #define uchar unsigned char
        #define uint unsigned int

        unsigned char code LEDData[ ] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,
        0x82,0xF8,0x80,0x90,0xff};

        unsigned char data display[8] = {0x00,0x00,0x00,0xff,0xff,0x00,0x00,0x00};

        unsigned char code scan_bit[8] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};

        unsigned char count0,count1,temp=0 ;

        /********************************************************
        * *
        * 延時函數 *
        * *
        ********************************************************/
        void delay(uint ms)
        // 延時子程序
        {
        uchar k;
        while(ms--)
        {
        for(k = 0; k < 100; k++);
        }
        }

        /********************************************************
        * *
        * 數據處理與顯示函數 *
        * *
        ********************************************************/
        void disp_count()
        {
        char n;

        temp=count0;

        display[2]=temp/100; //數據處理
        temp=temp%100;
        display[1]=temp/10;
        display[0]=temp%10;

        if(display[2]==0) //高位為0,不顯示
        {
        display[2]=0x0a;
        if(display[1]==0)
        display[1]=0x0a;
        }

        temp=count1;

        display[7]=temp/100; //數據處理
        temp=temp%100;
        display[6]=temp/10;
        display[5]=temp%10;

        if(display[7]==0) //高位為0,不顯示
        {
        display[7]=0x0a;
        if(display[6]==0)
        display[6]=0x0a;
        }

        for(n=0;n<8;n++)
        {
        P0 =LEDData[display[n]] ; //顯示段碼
        P2 =scan_bit[n]; //輸出位碼
        delay(1);
        P2 = 0xff; //關閉顯示
        }
        }

        /********************************************************
        * *
        * 主程序 *
        * *
        ********************************************************/
        void main(void)
        {
        P0=0xff;
        P1=0xff;
        P2=0xff;

        IT0=0; //低電平觸發(fā)
        // IT0=1; //下降沿觸發(fā)
        IT1=0; //低電平觸發(fā)
        // IT1=1; //下降沿觸發(fā)
        PX0=1;
        EA=1;
        EX1=1;
        EX0=1;

        while(1)
        {
        disp_count();
        }
        }

        /********************************************************
        * *
        * INT0中斷函數 *
        * *
        ********************************************************/
        void counter0(void) interrupt 0
        {
        uchar x;
        EX0=0;
        count0++;

        for(x=0;x<10;x++)
        {
        disp_count();
        }
        EX0=1;
        }

        /********************************************************
        * *
        * INT1中斷函數 *
        * *
        ********************************************************/
        void counter1(void) interrupt 2
        {
        uchar x;
        EX1=0;
        count1++;

        for(x=0;x<10;x++)
        {
        disp_count();
        }
        EX1=1;
        }

        /********************************************************/



        關鍵詞: 51單片機外部中

        評論


        技術專區(qū)

        關閉
        主站蜘蛛池模板: 陆川县| 睢宁县| 花垣县| 垫江县| 郯城县| 和龙市| 封丘县| 商都县| 古蔺县| 甘孜县| 盐池县| 金沙县| 故城县| 永定县| 灌云县| 苍南县| 安泽县| 永清县| 博湖县| 右玉县| 甘肃省| 化州市| 大城县| 靖安县| 任丘市| 罗甸县| 安徽省| 清远市| 武安市| 宿迁市| 武威市| 池州市| 隆回县| 迁西县| 中阳县| 天津市| 肥乡县| 海原县| 中宁县| 重庆市| 雅江县|