1、void InterruptTimer0() interrupt 1 TH0 = 0xFC; /重新加载初值 TL0 = 0x67; LedScan(); /调用数码管显示扫描函数 KeyScan(); /调用按键扫描函数 ShowPhoto(tx); /调用点阵显示函数/* T1中断服务函数,用于蜂鸣器 */ void InterruptTimer1() interrupt 3 TH1 = T1RH; /重新加载重载值 TL1 = T1RL; BUZZ = BUZZ; /反转蜂鸣器控制电平2、宏定义头文件sys.c#ifndef _sys_h_#define _sys_h_#include
2、sys.h#define uint unsigned int#define uchar unsigned charsbit ADDR0 = P10;sbit ADDR1 = P11;sbit ADDR2 = P12;sbit ADDR3 = P13;sbit ENLED = P14;sbit KEY_IN_1 = P24;sbit KEY_IN_2 = P25;sbit KEY_IN_3 = P26;sbit KEY_IN_4 = P27;sbit KEY_OUT_1 = P23;sbit KEY_OUT_2 = P22;sbit KEY_OUT_3 = P21;sbit KEY_OUT_4
3、= P20;sbit BUZZ = P16; /蜂鸣器控制引脚unsigned char T1RH = 0; /T0重载值的高字节unsigned char T1RL = 0; /T0重载值的低字节unsigned char tx = 0; /矩阵led显示unsigned char code LedChar = /数码管显示字符转换表 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E;unsigned char LedBuff6 = /数码管显示缓冲区
4、0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFFunsigned char code KeyCodeMap44 = /矩阵按键编号到标准键盘键码的映射表 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键、等号键、 除键unsigned char KeySta44 = /全部矩阵按键的当前状态 1,
5、 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1unsigned char code image68 = /符号的字模表 0xFF,0xBD,0x5A,0xFF,0xFF,0xBD,0xDB,0xE7,/笑脸 0xE7,0xE7,0xE7,0x00,0x00,0xE7,0xE7,0xE7,/加号 0xFF,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0xFF, /减号 0x3C,0x18,0x81,0xC3,0xC3,0x81,0x18,0x3C, /乘号 0xE7,0xE7,0xFF,0x00,0x00,0xFF,0xE7,0xE7,
6、/除号 0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF /等号void Init();void ShowNumber(unsigned long num);void KeyAction(unsigned char keycode);void KeyDriver();void KeyScan();void LedScan();void OpenBuzz(unsigned int frequ);void StopBuzz();void Delay();void Buzz(unsigned int frequ);void ShowPhoto(unsigned int
7、a);#endif3、函数头文件fun.c#ifndef _fun_h_#define _fun_h_fun.hvoid Init() EA = 1; /使能总中断 TMOD = 0x11; /设置T0为模式1 /为T0赋初值0xFC67,定时1ms ET0 = 1; /使能T0中断 TR0 = 1; /启动T0 /* 将一个无符号长整型的数字显示到数码管上,num-待显示数字 */void ShowNumber(unsigned long num) signed char i; unsigned char buf6; for (i=0; i=1; i-) /从最高位起,遇到0转换为空格,遇到
8、非0则退出循环 if (bufi = 0) LedBuffi = 0xFF; else break; for ( ;=0; i-) /剩余低位都如实转换为数码管显示字符 LedBuffi = LedCharbufi;/* 按键动作函数,根据键码执行相应的操作,keycode-按键键码 */void KeyAction(unsigned char keycode) static unsigned long result = 0; /用于保存运算结果 static unsigned long add2 = 0; /用于保存输入的数字 static unsigned int sign2 = 0; /
9、用作统计前一次和当前运算符号,03依次代表加减乘除 static unsigned int flag = 0; /用于统计次数 switch(keycode) /不同按键蜂鸣器发出不同频率声音 case 0x30 : Buzz(1000);break; case 0x31 : Buzz(1500); case 0x32 : Buzz(2000); case 0x33 : Buzz(2500); case 0x34 : Buzz(3000); case 0x35 : Buzz(3500); case 0x36 : Buzz(4000); case 0x37 : Buzz(4500); case
10、0x38 : Buzz(5000); case 0x39 : Buzz(5500); case 0x26 : Buzz(6000); case 0x28 : Buzz(6500); case 0x25 : Buzz(7500); case 0x27 : Buzz(8000); case 0x0D : Buzz(8500); case 0x1B : Buzz(9000); default : if (keycode=0x30) & (keycode=0x39) /输入0-9的数字 if(flag = 0) add0 = (add0*10)+(keycode-0x30); /整体十进制左移,新数字
11、进入个位 ShowNumber(add0); /运算结果显示到数码管 else add1 = (add1*10)+(keycode-0x30); ShowNumber(add1); else if (keycode = 0x26) /向上键用作加号,执行加法或连加运算 sign0 = sign1; /保存前一次的运算符号 sign1 = 0; /保存当前运算符号 flag = 1; tx = 1; switch(sign0) /用于连加 case 0:add0= add0+add1; case 1:add0= add0-add1; case 2:add0= add0*add1; case 3:
12、add0= add0/add1; default : add1=0; /清零add1; else if (keycode = 0x28) /向下键用作减号,执行减法 sign0 = sign1; sign1 = 1; tx = 2; switch(sign0) ShowNumber(add0); else if (keycode = 0x25) /向下键用作乘号,执行乘法 sign1 = 2; tx = 3; else if (keycode = 0x27) /向下键用作除号,执行除法 sign1 = 3; tx = 4 ; else if (keycode = 0x0D) /等号键,执行运算
13、 switch(sign1) result = add0+add1; /进行加法运算 ShowNumber(result); break; result = add0-add1; /进行减法运算 result = add0*add1; /进行乘法运算 result = add0/add1; /进行除法运算 tx = 5 ; flag = 0; add0 = 0; add1 = 0; sign0=0; sign1=0; else if (keycode = 0x1B) /Esc键,清零结果 add0 = 0; result = 0; tx = 0 ; /清零后的加数显示到数码管/* 按键驱动函数
14、,检测按键动作,调度相应动作函数,需在主循环中调用 */void KeyDriver() unsigned char i, j; static unsigned char backup44 = /按键值备份,保存前一次的值 ;4; i+) /循环检测4*4的矩阵按键 for (j=0; j j+) if (backupij != KeyStaij) /检测按键动作= 0) /按键按下时执行动作 KeyAction(KeyCodeMapij); /调用按键动作函数 backupij = KeyStaij; /刷新前一次的备份值/* 按键扫描函数,需在定时中断中调用,调用间隔1ms */void
15、KeyScan() unsigned char i; static unsigned char keyout = 0; /矩阵按键扫描输出索引 static unsigned char keybuf44 = /矩阵按键扫描缓冲区 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF /将一行的4个按键值移入缓冲区 keybufkeyout0 = (keybufkeyout0 1) | KEY_IN_1; keybufkeyout1 = (keybufkeyout1 1) | KEY_IN_2; keybufkeyout2 = (keybufkeyout2 1) | KEY_IN_3; keybufkeyout3 = (keybufkeyout3 8); /16位重载值分解为高低两个字节 T1RL = (unsigned char)reload; TH1 = 0xFF; /设定一个接近溢出的初值,以使定时器马上投入工作 TL1 = 0xFE; ET1 = 1; TR1 = 1; /启动T0/* 蜂鸣器停止函数*/void StopBuzz() ET1 = 0; /禁用T0中断 TR1 = 0; /停
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1