| 
 
新手入门 
 
	积分14金钱14 注册时间2019-12-18在线时间2 小时 | 
 
| #include <reg52.h> #include <intrins.h>
 #define FOSC 11059200L //晶振设置,默认使用11.0592M Hz
 //#define FOSC 12000000L //晶振设置,使用12M Hz
 //#define FOSC 24000000L //晶振设置,使用24M Hz
 //IO接口定义
 #define LED_PORT P0
 #define KEY_PORT P3
 sbit wela_1 = P2^4;
 sbit wela_2 = P2^5;
 sbit wela_3 = P2^6;
 sbit wela_4 = P2^7;
 //全局变量定义
 unsigned char key_value,key_value_disp;
 //LED显示字模 0-F 共阳模式
 unsigned code table[]= {0Xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
 /*******************************************************************************
 * 函 数 名 :Delayms
 * 函数功能 :实现 ms级的延时
 * 输    入 :ms
 * 输    出 :无
 *******************************************************************************/
 void Delayms(unsigned int ms)
 {
 unsigned int i,j;
 for(i=0;i<ms;i++)
 #if FOSC == 11059200L
 for(j=0;j<114;j++);
 #elif FOSC == 12000000L
 for(j=0;j<123;j++);
 #elif FOSC == 24000000L
 for(j=0;j<249;j++);
 #else
 for(j=0;j<114;j++);
 #endif
 }
 void Exit0Init()
 {
 EX0 = 1; //使能 INT1 外部中断
 IT0 = 1; // 触发方式为脉冲负边沿触发
 EA = 1;//总中断
 }
 /*******************************************************************************
 * 函 数 名 :LEDdisplay
 * 函数功能 :循环显示各个位上的数据
 * 输    入 :num要显示的数据
 * 输    出 :无
 *******************************************************************************/
 void LEDdisplay(unsigned int num)
 {
 unsigned char qian,bai,shi,ge;
 qian=num/1000;
 bai=num%1000/100;
 shi=num%100/10;
 ge=num%10;
 
 wela_1 = 1;   //关闭所有数码管
 wela_2 = 1;
 wela_3 = 1;
 wela_4 = 1;
 wela_4=0;  //显示千位
 LED_PORT=table[qian];
 Delayms(1);
 LED_PORT = 0xff;
 wela_4=1;
 
 wela_3=0;  //显示百位
 LED_PORT=table[bai];
 Delayms(1);
 LED_PORT = 0xff;
 wela_3=1;
 
 wela_2=0;  //显示十位
 LED_PORT=table[shi];
 Delayms(1);
 LED_PORT = 0xff;
 wela_2=1;
 
 wela_1=0;  //显示个位
 LED_PORT=table[ge];
 Delayms(1);
 LED_PORT = 0xff;
 }
 
 /*******************************************************************************
 * 函 数 名 :KeyScan
 * 函数功能 :4*4键盘扫描
 * 输    入 :无
 * 输    出 :num 获取的键值,若没有键值则返回 0xff
 *******************************************************************************/
 unsigned char KeyScan()
 {
 unsigned char temp,num;
 num = 0xff;
 KEY_PORT=0xfe;//1111 1110
 temp=KEY_PORT;
 temp=temp&0xf0;
 while(temp!=0xf0)
 {
 Delayms(5); //延时消抖
 temp=KEY_PORT;
 temp=temp&0xf0;//1111 0000
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 switch(temp)
 {
 case 0xee:num=0;
 while(temp)
 {
 P1=0Xe7;//11100111
 Delayms(100);
 P1=0Xc3;//11000011
 Delayms(100);
 P1=0X81;//10000001
 Delayms(100);
 P1=0X00;//00000000
 Delayms(100);
 P1=0X81;//00000000
 Delayms(100);
 P1=0Xc3;//00000000
 Delayms(100);
 P1=0Xe7;//00000000
 Delayms(100);
 P1=0Xff;//00000000
 Exit0Init;
 
 }
 break;
 
 case 0xde:num=1;
 
 P1=0x7f;
 Delayms(100);
 P1=0xbf;
 Delayms(100);
 P1=0xdf;
 Delayms(100);
 P1=0xef;
 Delayms(100);
 P1=0xf7;
 Delayms(100);
 P1=0xfb;
 Delayms(100);
 P1=0xfd;
 Delayms(100);
 P1=0xfe;
 Delayms(100);
 
 
 
 
 break;
 
 case 0xbe:num=2;
 
 break;
 
 case 0x7e:num=3;
 P1=0xff;
 Delayms(100);
 break;
 }
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 temp=temp&0xf0;
 }
 }
 }
 KEY_PORT=0xfd;
 temp=KEY_PORT;
 temp=temp&0xf0;
 while(temp!=0xf0)
 {
 Delayms(5); //延时消抖
 temp=KEY_PORT;
 temp=temp&0xf0;
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 switch(temp)
 {
 case 0xed:num=4;
 break;
 case 0xdd:num=5;
 break;
 case 0xbd:num=6;
 break;
 case 0x7d:num=7;
 break;
 }
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 temp=temp&0xf0;
 }
 }
 }
 KEY_PORT=0xfb;
 temp=KEY_PORT;
 temp=temp&0xf0;
 while(temp!=0xf0)
 {
 Delayms(5); //延时消抖
 temp=KEY_PORT;
 temp=temp&0xf0;
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 switch(temp)
 {
 case 0xeb:num=8;
 break;
 case 0xdb:num=9;
 break;
 case 0xbb:num=10;
 break;
 case 0x7b:num=11;
 break;
 }
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 temp=temp&0xf0;
 }
 }
 }
 KEY_PORT=0xf7;
 temp=KEY_PORT;
 temp=temp&0xf0;
 while(temp!=0xf0)
 {
 Delayms(5); //延时消抖
 temp=KEY_PORT;
 temp=temp&0xf0;
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 switch(temp)
 {
 case 0xe7:num=12;
 break;
 case 0xd7:num=13;
 break;
 case 0xb7:num=14;
 break;
 case 0x77:num=15;
 break;
 }
 while(temp!=0xf0)
 {
 temp=KEY_PORT;
 temp=temp&0xf0;
 }
 }
 }
 return num;
 }
 
 
 /*******************************************************************************
 * 函 数 名 :main
 * 函数功能 :主函数
 * 输    入 :无
 * 输    出 :无
 *******************************************************************************/
 void main()
 {
 key_value_disp = 0;
 key_value = 0;
 while(1)
 {
 key_value=KeyScan();
 if(key_value != 0xff)
 {
 
 key_value_disp = key_value;
 }
 LEDdisplay(key_value_disp);
 }
 
 }
 void Exit0Int() interrupt 0
 {
 unsigned char temp;
 EX0 = 0;        //关中断
 Delayms(30);      //延时30ms,去掉这行会出现按一下中断几次的情况
 if(key_value != 0xff)
 {
 temp=0;
 }
 EX0 = 1;
 }
 
 想用距阵按键控制流水灯但是现在一直在流水灯第一个花样死循环跳不出去,按其他按键没反应,怎样能安下别的按键跳出case里的while循环,实现另一个花样
 
 | 
 |