新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于單片機的數碼管按鍵計算器程序

        基于單片機的數碼管按鍵計算器程序

        作者: 時間:2016-11-30 來源:網絡 收藏
        這是一個51hei論壇版主測試成功的程序。用的是stc單片機,電路比較簡單就不畫出來了按鍵接在P2口

        數碼管的位選口是:
        sbit ADDR0 = P1^0;
        sbit ADDR1 = P1^1;
        sbit ADDR2 = P1^2;
        sbit ADDR3 = P1^3;
        數碼管的段選口是P0
        還有一個led燈p1.4 一個喇叭P1.6
        /*
        * Easy calculator
        *
        * K4:+ K8:- K12:* K16:/ K14:Clear K15:=
        *數碼管按鍵計算器程序
        */
        #include
        typedef unsigned char uint8;
        typedef unsigned int uint16;
        typedef unsigned long uint32;
        typedef char int8;
        typedef int int16;
        typedeflong int32;
        sbit KeyIn1 = P2^4;
        sbit KeyIn2 = P2^5;
        sbit KeyIn3 = P2^6;
        sbit KeyIn4 = P2^7;
        sbit KeyOut1 = P2^3;
        sbit KeyOut2 = P2^2;
        sbit KeyOut3 = P2^1;
        sbit KeyOut4 = P2^0;
        sbit ADDR0 = P1^0;
        sbit ADDR1 = P1^1;
        sbit ADDR2 = P1^2;
        sbit ADDR3 = P1^3;
        sbit ENLED = P1^4;
        sbit BUZZ = P1^6;
        #define FADD 10
        #define FSUB 11
        #define FMUL 12
        #define FDIV 13
        #define FRES 14
        #define FEQU 15
        #define KEY_DELAY 300
        #define BUZ_DELAY 80
        code uint8 Ledcode[13]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0x86};
        uint8 Led_n=0;
        uint8 Led_buf[6];
        float Tmp1=0, Tmp2=0;
        int8 C_flag=0;
        /*
        * 延時
        */
        void delay(uint16 n)
        {
        while (n--);
        }
        /*
        *蜂鳴器發聲
        */
        void buzzer_sound(void)
        {
        uint16 i;
        for (i=0; i{
        BUZZ = ~BUZZ;
        delay(100);
        }
        BUZZ = 1;
        }
        /*
        *按鍵掃描
        */
        int8 scan_key(void)
        {
        int8 val=-1;
        KeyOut1 = 0;
        KeyOut2 = 1;
        KeyOut3 = 1;
        KeyOut4 = 1;
        if (KeyIn1 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn1 == 0)
        val = 1;
        }
        if (KeyIn2 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn2 == 0)
        val = 2;
        }
        if (KeyIn3 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn3 == 0)
        val = 3;
        }
        if (KeyIn4 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn4 == 0)
        val = FADD;
        }
        while ((KeyIn1 == 0)||(KeyIn2 == 0)||(KeyIn3 == 0)||(KeyIn4 == 0));
        KeyOut1 = 1;
        KeyOut2 = 0;
        KeyOut3 = 1;
        KeyOut4 = 1;
        if (KeyIn1 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn1 == 0)
        val = 4;
        }
        if (KeyIn2 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn2 == 0)
        val = 5;
        }
        if (KeyIn3 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn3 == 0)
        val = 6;
        }
        if (KeyIn4 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn4 == 0)
        val = FSUB;
        }
        while ((KeyIn1 == 0)||(KeyIn2 == 0)||(KeyIn3 == 0)||(KeyIn4 == 0));
        KeyOut1 = 1;
        KeyOut2 = 1;
        KeyOut3 = 0;
        KeyOut4 = 1;
        if (KeyIn1 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn1 == 0)
        val = 7;
        }
        if (KeyIn2 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn2 == 0)
        val = 8;
        }
        if (KeyIn3 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn3 == 0)
        val = 9;
        }
        if (KeyIn4 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn4 == 0)
        val = FMUL;
        }
        while ((KeyIn1 == 0)||(KeyIn2 == 0)||(KeyIn3 == 0)||(KeyIn4 == 0));
        KeyOut1 = 1;
        KeyOut2 = 1;
        KeyOut3 = 1;
        KeyOut4 = 0;
        if (KeyIn1 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn1 == 0)
        val = 0;
        }
        if (KeyIn2 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn2 == 0)
        val = FRES;
        }
        if (KeyIn3 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn3 == 0)
        val = FEQU;
        }
        if (KeyIn4 == 0)
        {
        delay(KEY_DELAY);
        if (KeyIn4 == 0)
        val = FDIV;
        }
        while ((KeyIn1 == 0)||(KeyIn2 == 0)||(KeyIn3 == 0)||(KeyIn4 == 0));
        if (val > 0)
        buzzer_sound();
        return val;
        }
        /*
        * 驗證數據有效性
        */
        bit check_num(float f_num)
        {
        if (f_num >= 100000)
        return 1;
        return 0;
        }
        /*
        * 制作數碼管錯誤標志
        */
        void make_led_error(void)
        {
        int8 i;
        for (i=0; i<5; i++)
        Led_buf[i] = Ledcode[10];
        Led_buf[5] = Ledcode[12];
        }
        /*
        *制作數碼管整數數據
        */
        void make_led_inumber(int32 i_num)
        {
        bit s_flag=0;
        int16 sit;
        int8 i;
        if (i_num < 0)
        {
        s_flag = 1;
        i_num = -i_num;
        }

        ET0 = 0;
        for (i=4, sit=10000; i>=1; i--, sit/=10)
        {
        if (i_num >= sit)
        break;
        Led_buf[i] = Ledcode[10];
        i_num -= i_num/sit*sit;
        }
        for (;i>=1; i--, sit/=10)
        {
        Led_buf[i] = Ledcode[i_num/sit];
        i_num -= i_num/sit*sit;
        }
        Led_buf[0] = Ledcode[i_num] & 0x7F;
        if (s_flag)
        Led_buf[5] = Ledcode[11];
        else
        Led_buf[5] = Ledcode[10];
        ET0 = 1;
        }


        上一頁 1 2 下一頁

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 巴彦淖尔市| 赤壁市| 连城县| 子洲县| 白山市| 乃东县| 当阳市| 彩票| 廊坊市| 西平县| 丹凤县| 耿马| 普兰店市| 池州市| 永定县| 奉化市| 辉县市| 铜陵市| 桦甸市| 昌黎县| 南江县| 衡东县| 延寿县| 平山县| 遂溪县| 汨罗市| 普陀区| 西乌珠穆沁旗| 宜兰县| 潮安县| 游戏| 肥西县| 张家界市| 左云县| 溆浦县| 深水埗区| 东阿县| 汤阴县| 朝阳区| 合肥市| 临颍县|