新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 16鍵計(jì)算器 c51實(shí)現(xiàn)

16鍵計(jì)算器 c51實(shí)現(xiàn)

作者: 時(shí)間:2016-11-29 來源:網(wǎng)絡(luò) 收藏
1、定時(shí)顯示

開辟一顯示緩沖區(qū),顯示中斷程序定時(shí)讀取緩沖區(qū)顯示各燈位,每燈位顯示2.5ms,顯示4只燈需要10ms,也就是顯示的刷新頻率是100Hz,亮度為25%。這樣既保證亮度又不閃爍,同時(shí)CPU還有足夠時(shí)間做其他事情。

本文引用地址:http://butianyuan.cn/article/201611/323343.htm

2、鍵盤中斷掃描

利用鍵盤中斷掃描程序,讀出按鍵的鍵碼,存入鍵盤緩沖區(qū),供主程序讀區(qū)。

3、計(jì)算器的主程序讀取鍵盤緩沖區(qū)的鍵碼,驅(qū)動(dòng)計(jì)算器的運(yùn)行,計(jì)算器的運(yùn)行過程要清晰明了;

4、計(jì)算中需要十進(jìn)制與十六進(jìn)制(或二進(jìn)制)轉(zhuǎn)化。


#define LED_seg      XBYTE[0x6000]     //段碼地址#define LED_light    XBYTE[0x8000]      //燈位地址#include "reg51.h"#include "absacc.h"unsigned char read_key(unsigned char);unsigned char display(unsigned char,unsigned char);void delay(unsigned int);unsigned char light[4]={0xfe,0xfd,0xfb,0xf7};   //掃描值 燈位碼unsigned char seg[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //段碼unsigned char disp_buffer[5]={1,0,0,0,0};            //當(dāng)前燈位、 顯示區(qū)unsigned char key_buffer[2]={0,0xff};           //緩沖區(qū)滿標(biāo)志、鍵碼void main(){Unsigned char temp[5];unsigned char i;unsigned char operator=0;unsigned int tempa,tempb;bit first=1;TMOD=0x01;       //置T0為方式1TL0=0x0;          //延時(shí)2.5mS的定時(shí)器初始值TH0=0xf7;          //賦初值PT0=1;             //定時(shí)中斷0優(yōu)先設(shè)置TR0=1;           //啟動(dòng)定時(shí)ET0=1;           //允許定時(shí)IT1=0;        //電平觸發(fā)低電平有效EX1=1;       //允許外中斷PX1=1;       // 外部中斷1優(yōu)先級設(shè)定EA=1;        //中斷總允許while(1){P1=0xf0;                           //掃描if (key_buffer[0]){switch(key_buffer[1])              //緩沖區(qū)滿{case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9: if (first)  for(i=1;i<=4;i++)   //保存數(shù)據(jù),等待另一個(gè)數(shù)據(jù)輸入{temp[i]=disp_buffer[i];disp_buffer[i]=0;                                           }first=0;for(i=1;i<=3;i++)  disp_buffer[i]=disp_buffer[i+1];disp_buffer[4]=key_buffer[1];break;case 10:case 11:case 12:case 13:case 14: first=1;tempa=1000*disp_buffer[1]+100*disp_buffer[2]+10*disp_buffer[3]+disp_buffer[4];tempb=1000*temp[1]+100*temp[2]+10*temp[3]+temp[4];switch(operator){case 10:tempa+=tempb;break;case 11:tempa=tempb-tempa;break;case 12:tempa*=tempb;break;case 13:tempa=tempb/tempa;}tempa%=10000;disp_buffer[1]=tempa/1000;tempa%=1000;disp_buffer[2]=tempa/100;tempa%=100;disp_buffer[3]=tempa/10;disp_buffer[4]=tempa%10;operator=key_buffer[1];break;case 15: for(i=1;i<=4;i++)  disp_buffer[i]=temp[i]=0;first=1;operator=0;}key_buffer[0]=0;                     //緩沖區(qū)空}          }}
				
            
                
			
							
上一頁 1 2 下一頁

關(guān)鍵詞: 16鍵計(jì)算器c5

評論


技術(shù)專區(qū)

關(guān)閉