借用了论坛里给出的模块,先把按键调通了,为了方便大家看,没用模块化编程,都在一个大程序里,直接贴上来了。
#include<reg52.h>
#include<intrins.h>
#define UINT8 unsigned char
sbit LED = P1^5; //指示灯
sbit IR_INT0 = P3^2; //红外
//调音按键
sbit KEY_SUB = P1^2;
sbit KEY_ADD = P1^3;
sbit KEY_MUTE = P1^1;
sbit CHECK = P1^0; //检测音频线有没有接入
sbit MUTE = P1^4; //功放静音
//MCU调音量
sbit CLOCK = P3^3;
sbit UPDOWN = P3^6;
//还有几个全局变量,主要是存功放和按键状态的
unsigned char uup=0; //有连续按键时用
unsigned char IRtimer;//
unsigned char phonejack = 1;// //有没有音频线状态
unsigned char MUTEstate = 0; //MUTE状态
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
//控制音量加
void Volume_add(void)
{//
UINT8 iadd = 5;
UPDOWN = 1;
while(--iadd)
Delay1ms();
CLOCK = 0;
iadd = 5;
while(--iadd)
Delay1ms();
CLOCK = 1;
}
// 控制音量减
void Volume_sub(void)
{
UINT8 isub = 5;
UPDOWN = 0;
while(--isub)
Delay1ms();
CLOCK = 0;
isub = 5;
while(--isub)
Delay1ms();
CLOCK = 1;
}
void flash_LED(UINT8 a)
{
LED = 0;
while(a--)
{ Delay1ms(); }
LED = 1;
}
//关于按键,软件上一定要做延时处理
//下面是给大家一个延时的方法,高手请略过;
void Scan_key(void)
{
int loop;
if(KEY_SUB == 0)
{LED = 1;
loop = 10;
while((KEY_SUB == 0) && (loop >0))
{--loop;Delay1ms();}
if(KEY_SUB == 0)
{
Volume_sub();
// u_sstr("-1\n\r");
flash_LED(20);
loop = 1500;
while((KEY_SUB == 0) && (loop >0))
{--loop;Delay1ms();}
}
while(KEY_SUB == 0)
{
Volume_sub();
// u_sstr("-1\n\r");
flash_LED(20);;
loop = 200;
while((KEY_SUB == 0) && (loop >0))
{--loop;Delay1ms();}
}
}
if(KEY_ADD == 0)
{LED = 1;
loop = 10;
while((KEY_ADD == 0) && (loop >0))
{--loop;Delay1ms();}
if(KEY_ADD == 0)
{
Volume_add();
// u_sstr("+1\n\r");
flash_LED(20);
loop = 1500;
while((KEY_ADD == 0) && (loop >0))
{--loop;Delay1ms();}
}
while(KEY_ADD == 0)
{
Volume_add();
// u_sstr("+1\n\r");
flash_LED(20);
loop = 200;
while((KEY_ADD == 0) && (loop >0))
{--loop;Delay1ms();}
}
}
if(KEY_MUTE == 0)
{
LED = 1;
loop = 10;
while((KEY_MUTE == 0) && (loop >0))
{--loop;Delay1ms();}
if(KEY_MUTE == 0)
{
if(MUTEstate == 1)
{
MUTEstate = 0;
// MUTE = 0;
// u_sstr("MUTE\n\r");
}
else
{
MUTEstate = 1;
// MUTE = 1;
// u_sstr("CLOSE MUTE\n\r");
}
flash_LED(20);
}
while(KEY_MUTE==0);
}
}
void checkphone(void)
{
if(CHECK == 0) //没有耳机插入
{
MUTE = 1;
phonejack = 0; //没有耳机
}
else //有耳机
{
if(phonejack == 0)
{
flash_LED(20);
}
phonejack = 1;
if(MUTEstate == 1)
{
MUTE = 1;
}
else
{
MUTE = 0;
}
}
}
void main()
{
while(1)
{
checkphone();
Scan_key();
}
}