新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 單片機學習——獨立按鍵之三

        單片機學習——獨立按鍵之三

        作者: 時間:2016-11-23 來源:網絡 收藏
        題目:按下16個矩陣鍵盤依次在數碼管上顯示1-16的平方。如按下第一個顯示1,第二個顯示4...
        解決方案:(C語言編程)
        #include
        #include
        #define uint unsigned int
        #define uchar unsigned char
        uchar bai,shi,ge,num,temp;
        uint shu;
        sbit le=P3^6;
        void delay(uint a);
        void dis();
        void display(uchar ba,uchar sh,uchar g);
        uchar code tabledu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
        0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0x86,0xc7,0xc7,0xc0,0xff};
        uchar code tablewe[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
        void main()
        {
        while(1){
        P1=0xfe;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        delay(5);
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        temp=P1;
        switch(temp)
        {
        case 0x7e:shu=16 ;break;
        case 0xbe:shu=15 ;break;
        case 0xde:shu=14 ;break;
        case 0xee:shu=13 ;break;
        }
        while(temp!=0xf0)
        {
        temp=P1;
        temp=temp&0xf0;
        }
        shu=shu*shu;
        }
        }
        bai=shu/100;
        shi=shu%100/10;
        ge=shu%10;
        display(bai,shi,ge);
        P1=0xfd;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        delay(5);
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        temp=P1;
        switch(temp)
        {
        case 0x7d:shu=12 ;break;
        case 0xbd:shu=11 ;break;
        case 0xdd:shu=10 ;break;
        case 0xed:shu=9 ;break;
        }
        while(temp!=0xf0)
        {
        temp=P1;
        temp=temp&0xf0;
        }
        shu=shu*shu;
        }
        }
        bai=shu/100;
        shi=shu%100/10;
        ge=shu%10;
        display(bai,shi,ge);
        P1=0xfb;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        delay(5);
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        temp=P1;
        switch(temp)
        {
        case 0x7b:shu=8 ;break;
        case 0xbb:shu=7 ;break;
        case 0xdb:shu=6 ;break;
        case 0xeb:shu=5 ;break;
        }
        while(temp!=0xf0)
        {
        temp=P1;
        temp=temp&0xf0;
        }
        shu=shu*shu;
        }
        }
        bai=shu/100;
        shi=shu%100/10;
        ge=shu%10;
        display(bai,shi,ge);
        P1=0xf7;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        delay(5);
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
        temp=P1;
        switch(temp)
        {
        case 0x77:shu=4 ;break;
        case 0xb7:shu=3 ;break;
        case 0xd7:shu=2 ;break;
        case 0xe7:shu=1 ;break;
        }
        while(temp!=0xf0)
        {
        temp=P1;
        temp=temp&0xf0;
        }
        shu=shu*shu;
        }
        }
        bai=shu/100;
        shi=shu%100/10;
        ge=shu%10;
        display(bai,shi,ge);
        }
        }
        void display(uchar ba,uchar sh,uchar g)
        {
        le=1;
        P2=tablewe[1];
        le=0;
        le=1;
        P0=tabledu[ba];
        le=0;
        delay(5);
        le=1;
        P2=tablewe[2];
        le=0;
        le=1;
        P0=tabledu[sh];
        le=0;
        delay(5);
        le=1;
        P2=tablewe[3];
        le=0;
        le=1;
        P0=tabledu[g];
        le=0;
        delay(5);
        }
        void delay(uint a) {
        uint i,j;
        for(j=a;j>0;j--)
        for(i=250;i>0;i--);
        }


        關鍵詞: 單片機獨立按

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 舞阳县| 成武县| 长治市| 嘉善县| 奉贤区| 江津市| 阿克陶县| 绥德县| 抚宁县| 瓦房店市| 吴江市| 揭东县| 达州市| 大理市| 定结县| 雅安市| 廉江市| 来安县| 乌拉特中旗| 红安县| 齐齐哈尔市| 杭锦旗| 延长县| 偃师市| 锡林郭勒盟| 南宫市| 洞口县| 扎鲁特旗| 建德市| 芦溪县| 子洲县| 余姚市| 铜梁县| 叶城县| 新建县| 安乡县| 夏邑县| 汕头市| 波密县| 长武县| 轮台县|