以上两个程序,都是驱动数码管的程序
程序一为动态数码管,程序二为矩形按键控制数码管显示字符
在这里我有一个问题不明白
两个程序都用switch函数
break了之后不是就跳出了么
那么为什么程序一里,字符显示之后会熄灭
但是程序二里,字符显示了之后就一直显示
程序一:
- #include "reg52.h"
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- void delay(u16 i)
- {
- while(i--);
- }
- void DigDisplay()
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- switch(i)
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;
- case(1):
- LSA=1;LSB=0;LSC=0; break;
- case(2):
- LSA=0;LSB=1;LSC=0; break;
- case(3):
- LSA=1;LSB=1;LSC=0; break;
- case(4):
- LSA=0;LSB=0;LSC=1; break;
- case(5):
- LSA=1;LSB=0;LSC=1; break;
- case(6):
- LSA=0;LSB=1;LSC=1; break;
- case(7):
- LSA=1;LSB=1;LSC=1; break;
- }
- P0=smgduan[i];
- delay(10000);
- P0=0x00;
- }
- }
- void main()
- {
- while(1)
- {
- DigDisplay();
- }
- }
复制代码
程序二:
- #include "reg52.h"
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- #define GPIO_KEY P1
- #define GPIO_DIG P0
- u8 KEYOUT;
- u8 code smg[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- void delay(u16 i)
- {
- while(i--);
- }
- void KEYDOWN(void)
- {
- char a;
- GPIO_KEY=0X0F;
- if(GPIO_KEY!=0X0F)
- {
- delay(1000);
- if(GPIO_KEY!=0X0F)
- {
- GPIO_KEY=0X0F;
- switch(GPIO_KEY)
- {
- case(0x07):KEYOUT=0;P0=smg[KEYOUT];break;
- case(0x0b):KEYOUT=1;P0=smg[KEYOUT];break;
- case(0x0d):KEYOUT=2;P0=smg[KEYOUT];break;
- case(0x0e):KEYOUT=3;P0=smg[KEYOUT];break;
- }
- GPIO_KEY=0XF0;
- switch(GPIO_KEY)
- {
- case(0x70):KEYOUT=KEYOUT;P0=smg[KEYOUT];break;
- case(0xb0):KEYOUT=KEYOUT+4;P0=smg[KEYOUT];break;
- case(0xd0):KEYOUT=KEYOUT+8;P0=smg[KEYOUT];break;
- case(0xe0):KEYOUT=KEYOUT+12;P0=smg[KEYOUT];break;
- }
- while((a<40)&&(GPIO_KEY!=0X0F))
- {
- delay(100);
- a++;
- }
- }
- }
-
- }
- void main()
- {
- while(1)
- {
- LSA=0;
- LSB=0;
- LSC=0;
- KEYDOWN();
-
- }
- }
复制代码
|