蜂鸣器这块不太懂,怎么添加上按键以后无源蜂鸣器长鸣的功能,求教
- #include <reg52.h>
- #include <intrins.h>
- typedef unsigned char u8
- typedef unsigned int u16;
- #define KEY_PORT P1
- #define DIG_PORT P0
- sbit gA = P2^2;
- sbit gB = P2^3;
- sbit gC = P2^4;
- sbit gBuz = P1^5;
- unsigned char code gDuanMa[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- void delay10ms(void);
- u8 KeyScan(void);
- void KeyValueDisplay(u8 val);
- void BuzzerOnOff(void);
- void delay1ms(void);
- void main(void)
- {
- u8 key = 0;
- u8 key1 = 0;
- while (1)
- {
- key = KeyScan();
- if (key != 0)
- {
- key1 = key;
- beep = 1;
- }
- KeyValueDisplay(key1);
-
- }
- }
- u8 KeyScan(void)
- {
- u8 val = 0;
-
- KEY_PORT = 0x0f;
- if (KEY_PORT != 0x0f)
- {
- delay10ms();
- if (KEY_PORT != 0x0f)
- {
- switch (KEY_PORT)
- {
- case 0x07 : val = 1; break;
- case 0x0b : val = 2; break;
- case 0x0d : val = 3; break;
- case 0x0e : val = 4; break;
- default : break;
- }
- KEY_PORT = 0xf0;
- switch (KEY_PORT)
- {
- case 0x70: val = val + 0; break;
- case 0xb0: val = val + 4; break;
- case 0xd0: val = val + 8; break;
- case 0xe0: val = val + 12; break;
- }
- return val;
- }
- }
- return 0;
- }
- void BuzzerOnOff(void)
- {
- if()
- gBuz = 1;
- delay1ms();
- gBuz = 0;
- delay1ms();
- }
- void delay10ms(void) //误差 0us
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
- void delay1ms(void) //误差 0us
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=142;b>0;b--)
- for(a=2;a>0;a--);
- }
- void KeyValueDisplay(u8 val)
- {
- gA = 1; gB = 1; gC = 1; // 位选数码管0
- DIG_PORT = gDuanMa[val / 10];
- delay10ms(); // 间隔一段显示时间
- DIG_PORT = 0x00; // 消隐,注意不要漏掉了
- gA = 0; gB = 1; gC = 1;
- DIG_PORT = gDuanMa[val % 10];
- delay10ms(); // 间隔一段显示时间
- DIG_PORT = 0x00; // 消隐,注意不要漏掉了
- }
复制代码
|