|
做了个简单的计算器练手
单片机计算器仿真原理图:
单片机源程序预览:
- #include<reg52.h>
- sbit in1 = P2^0; //按键P2.0输出
- sbit in2 = P2^1;
- sbit in3 = P2^2;
- sbit in4 = P2^3;
- sbit out1 = P2^4; //动态行变换
- sbit out2 = P2^5; //动态行变换
- sbit out3 = P2^6; //动态行变换
- sbit out4 = P2^7; //动态行变换
- unsigned char code LedChar[]={
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
- 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
- };
- unsigned char KeySta[4][4] = {
- {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
- };
- unsigned char LedBuff[6] = {
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
- };
- unsigned char code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表
- { 0x31, 0x32, 0x33, 0x26 }, //数字键1、数字键2、数字键3、向上键
- { 0x34, 0x35, 0x36, 0x25 }, //数字键4、数字键5、数字键6、向左键
- { 0x37, 0x38, 0x39, 0x28 }, //数字键7、数字键8、数字键9、向下键
- { 0x30, 0x1B, 0x0D, 0x27 } //数字键0、ESC键、 回车键、 向右键
- };
- void KeyDriver();
- void main()
- {
- EA = 1; //使能总中断
- ET0 = 1; //使能T0中断
- TMOD = 0x01; //设置T0为模式1
- TH0 = 0xDC;
- TL0 = 0x00;
- TR0 = 1; //启动T0
- P1=0xFF;
- LedBuff[0] = LedChar[0]; //上电显示0
-
- while (1)
- {
- KeyDriver(); //调用按键驱动函数
- }
- }
- void ShowNumber(unsigned long num)
- {
- signed char i;
- unsigned char buf[6];
- for(i=0; i<6; i++)
- {
- buf[i] = num % 10;
- num = num / 10;
- }
- for(i=5; i>=1; i--)
- {
- if(buf[i] == 0)
- {
- LedBuff[i] = 0xFF;
- }
- else
- break;
- }
-
- for(; i>=0; i--)
- {
- LedBuff[i] = LedChar[buf[i]];
- }
- }
- void KeyAction(unsigned char keycode)
- {
- static unsigned long result = 0;
- static unsigned long addend = 0;
-
- if((keycode >= 0x30) &&(keycode <= 0x39))
- {
- addend = (addend *10) + (keycode - 0x30);
- ShowNumber(addend);
- }
-
- else if(keycode == 0x26)
- {
- result += addend;
- addend = 0;
- ShowNumber(result);
- }
- else if(keycode == 0x0D)
- {
- result += addend;
- addend = 0;
- ShowNumber(result);
- }
- else if(keycode == 0x1B)
- {
- addend = 0;
- result = 0;
- ShowNumber(addend);
- }
-
- }
- void KeyDriver()
- {
- unsigned char i, j;
- static unsigned char backup [4][4] = {
- {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
- };
- for(i=0; i<4; i++)
- {
- for(j=0; j<4; j++)
- {
- if(backup[i][j] != KeySta[i][j])
- {
- if(backup[i][j] == 0)
- {
- KeyAction(KeyCodeMap[i][j]);
- }
- backup[i][j] = KeySta[i][j];
- }
- }
- }
-
- }
- /* 按键扫描函数,需在定时中断中调用,推荐调用间隔1ms */
- void KeyScan()
- {
- unsigned char i;
- static unsigned char keyout = 0; //矩阵按键扫描输出索引
- static unsigned char keybuf[4][4] = { //矩阵按键扫描缓冲区
- {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
- {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
- };
- //将一行的4个按键值移入缓冲区
- keybuf[keyout][0] = (keybuf[keyout][0] << 1) | in1;
- keybuf[keyout][1] = (keybuf[keyout][1] << 1) | in2;
- keybuf[keyout][2] = (keybuf[keyout][2] << 1) | in3;
- keybuf[keyout][3] = (keybuf[keyout][3] << 1) | in4;
- //消抖后更新按键状态
- for (i=0; i<4; i++) //每行4个按键,所以循环4次
- {
- if ((keybuf[keyout][i] & 0x0F) == 0x00)
- { //连续4次扫描值为0,即4*4ms内都是按下状态时,可认为按键已稳定的按下
- KeySta[keyout][i] = 0;
- }
- else if ((keybuf[keyout][i] & 0x0F) == 0x0F)
- { //连续4次扫描值为1,即4*4ms内都是弹起状态时,可认为按键已稳定的弹起
- KeySta[keyout][i] = 1;
- }
- }
- //执行下一次的扫描输出
- keyout++; //输出索引递增
- keyout = keyout & 0x03; //索引值加到4即归零
- switch (keyout) //根据索引,释放当前输出引脚,拉低下次的输出引脚
- {
- case 0: out1 = 0; out2 = 1; out3 = 1;out4 = 1; ; break; //当我按下第一行的按键,只有keyout==0时符合条件
- case 1: out1 = 1; out2 = 0; out3 = 1;out4 = 1; ; break;
- case 2: out1 = 1; out2 = 1; out3 = 0;out4 = 1; ; break;
- case 3: out1 = 1; out2 = 1; out3 = 1;out4 = 0; ; break;
- default: break;
- }
- }
- /* 数码管动态扫描刷新函数,需在定时中断中调用 */
- void LedScan()
- {
- static unsigned char i = 0; //动态扫描的索引
-
- P0 = 0xFF; //显示消隐
- switch (i)
- {
- case 0:P1=0xFE; i++; P0=LedBuff[0];break;
- case 1:P1=0xFD; i++; P0=LedBuff[1];break;
- case 2:P1=0xFB; i++; P0=LedBuff[2];break;
- case 3:P1=0xF7; i++; P0=LedBuff[3];break;
- case 4:P1=0xEF; i++; P0=LedBuff[4];break;
- case 5:P1=0xDF; i=0; P0=LedBuff[5];break;
- default:break;
- }
- }
- /* T0中断服务函数,用于数码管显示扫描与按键扫描 */
- void InterruptTimer0() interrupt 1
- {
- TH0 = 0xDC;
- TL0 = 0x00;
- LedScan();
- KeyScan();
- }
复制代码
所有资料打包下载:
简单的计算器加法程序.zip
(79.82 KB, 下载次数: 21)
|
评分
-
查看全部评分
|