📄 yinyue40.c
字号:
/******************************************************************************
*
* pm5050语音遥控输出程序 *
* 单片机 AT89C2051 *
* 功放为TDA2003 *
* 设计:叶设炉 *
* 时间:2006年9月24日完成 *
* *
******************************************************************************/
/* 加载头文件 */
#include <reg52.h>
#include <intrins.h>
/*定义宏变量*/
#define highfiltertime 1 //0.2S
#define highshorttime 1 //0.5S
#define highlongtime 5 //1S
/* 定义全局变量 */
unsigned long highkeycount_A;
unsigned long highkeycount_B;
unsigned long highkeycount_C;
unsigned long highkeycount_D;
unsigned long s_count;
unsigned long s10_count;
/* 子函数声明 */
void delayNms (unsigned long partnumx);
void outspk_ctr (unsigned char partnum);
void readkey (void);
void Count_S(void);
/* STC89C52RC内部EEPROM:END */
/******************************************************************************
* 定义全局变量 *
* *
* 空间名称 地址范围 说明 *
* DATA D:00H-D:7FH 片内RAM直接寻址区 *
* BDATA D:20H-D:2FH 片内RAM位寻址区 *
* IDATA I:00H-I:FFH 片内RAM间接寻址区 *
* XDATA X:0000H-X:FFFFH 64KB常规片外RAM数据区 *
* CODE C:0000H-C:FFFFH 64KB常规片外ROM代码区 *
******************************************************************************/
unsigned char bdata highkeyflgshort,highkeyflglong;//input station var
sbit highkeyflgshort_A = highkeyflgshort^0;
sbit highkeyflgshort_B = highkeyflgshort^1;
sbit highkeyflgshort_C = highkeyflgshort^2;
sbit highkeyflgshort_D = highkeyflgshort^3;
sbit highkeyflglong_A = highkeyflglong^0;
sbit highkeyflglong_B = highkeyflglong^1;
sbit highkeyflglong_C = highkeyflglong^2;
sbit highkeyflglong_D = highkeyflglong^3;
sbit busy_01 = highkeyflglong^4;
sbit play_busy = highkeyflglong^5;
/* 定义单片机管脚 */
sbit in_key_A = P3^2;
sbit in_key_B = P3^3;
sbit in_key_C = P3^4;
sbit in_key_D = P3^5;
sbit res = P1^3;
sbit in_spk_01 = P3^7;
sbit out_spk_k1 = P1^7;
sbit out_spk_k2 = P1^6;
//sbit out_power_12v = P1^4;
//sbit out_led_D1 = P3^7;
sbit out_led_D2 = P3^1;
/* 主程序 */
void main (void)
{
P1 = 0xFF; // 端口初始化
P3 = 0xFF;
highkeyflgshort = 0;
highkeyflglong = 0;
// 主程序初始化
for ( ; ; )
{
readkey ();
//if((in_key_A&&in_key_B&&in_key_C&&in_key_D)==1)out_power_12v = 0;//开工
//if((play_busy==0)&&(in_spk_01==0)) out_power_12v = 1 ,play_busy=0;//关工放
//if (play_busy==0) out_power_12v = 1 ;
// else out_power_12v = 0 ;
//if (play_busy==0 ?) out_power_12v = 1;out_power_12v = 0;
Count_S();
if (highkeyflgshort != 0x0)//if 1
{
switch (highkeyflgshort)//highkeyflgshort//switch 1
{
case 0x01: outspk_ctr(0x00);//S_A
break;
case 0x02: outspk_ctr(0x01);//S_B
break;
case 0x04: outspk_ctr(0x02);//S_C
break;
case 0x08: outspk_ctr(0x03);//S_D
break;
default: delayNms (100);//保证时基的均匀
break;
}//switch 1 end
highkeyflgshort = highkeyflgshort & 0xf0;//得到有效按键,只播放一次,故要清掉
}
else
{ if ((highkeyflglong & 0x0f) != 0x0)//if 2
{
switch ((highkeyflglong & 0x0f))//highkeyflgshort
{//switch 2
case 0x03: outspk_ctr(0x04);//L_A+B
break;
case 0x0c: outspk_ctr(0x05);//L_C+D
break;
case 0x01: outspk_ctr(0x06);//L_A
break;
case 0x02: outspk_ctr(0x07);//L_B
break;
case 0x04: outspk_ctr(0x08);//L_C
break;
case 0x08: outspk_ctr(0x09);//L_D
break;
default: delayNms (100);//保证时基的均匀
break;
}//switch 2 end
highkeyflglong =highkeyflglong & 0xf0;//得到有效按键,只播放一次,故要清掉
}// if 2 end
else//未进入两个SWITCH
delayNms (100);//保证时基的均匀
//if (in_spk_01== 0) out_power_12v = 1;
}//if 1 else is end
} //for end
}//main end
/****************************************************************/
void readkey (void) //扫描时间较确定
{
/**************************in_key_A*******************************/
if (in_key_A)
{ highkeycount_A++;}
else
{ /*
if ( highkeycount_A < highfiltertime ) //无效输入
{
highkeyflglong_A = 0;
highkeyflgshort_A = 0;
}
else
{ */
if(highkeycount_A>0)
{
if ( highkeycount_A > highlongtime )//长信号
{ highkeyflglong_A = 1;
highkeyflgshort_A = 0;
}
else
{ /*if ( (highkeycount_A < highshorttime) && (highkeycount_A > highfiltertime)) //短信号
{*/
highkeyflgshort_A = 1;
highkeyflglong_A = 0;
}
// }
}
highkeycount_A = 0; //计数清零
}
/****************************in_key_B*****************************/
if (in_key_B)
{ highkeycount_B++;}
else
{
/*if ( highkeycount_B < highfiltertime ) //无效输入
{
highkeyflglong_B = 0;
highkeyflgshort_B = 0;
}
else
{
*/
if(highkeycount_B>0)
{
if ( highkeycount_B > highlongtime )//长信号
{ highkeyflglong_B = 1;
highkeyflgshort_B = 0;
}
else
{ /*if ( (highkeycount_B < highshorttime) && (highkeycount_B > highfiltertime)) //短信号
{*/
highkeyflgshort_B = 1;
highkeyflglong_B = 0;
// }
}
}
highkeycount_B = 0; //计数清零
}
/*************************in_key_C********************************/
if (in_key_C)
{ highkeycount_C++;}
else
{
/*if ( highkeycount_C < highfiltertime ) //无效输入
{
highkeyflglong_C = 0;
highkeyflgshort_C = 0;
}
else
{ */
if(highkeycount_C>0)
{
if ( highkeycount_C > highlongtime )//长信号
{ highkeyflglong_C = 1;
highkeyflgshort_C = 0;
}
else
{/*if ( (highkeycount_C < highshorttime) && (highkeycount_C > highfiltertime)) //短信号
{*/
highkeyflgshort_C = 1;
highkeyflglong_C = 0;
// }
}
}
highkeycount_C = 0; //计数清零
}
/***********************in_key_D**********************************/
if (in_key_D)
{ highkeycount_D++;}
else
{ /*
if ( highkeycount_D < highfiltertime ) //无效输入
{
highkeyflglong_D = 0;
highkeyflgshort_D = 0;
}
else
{ */
if(highkeycount_D>0)
{
if ( highkeycount_D > highlongtime )//长信号
{ highkeyflglong_D = 1;
highkeyflgshort_D = 0;
}
else
{ /*if ( (highkeycount_D < highshorttime) && (highkeycount_D > highfiltertime)) //短信号
{*/
highkeyflgshort_D = 1;
highkeyflglong_D = 0;
}
// }
}
highkeycount_D = 0; //计数清零
}
}
/************************************************************/
void delayNms (unsigned long partnumx)
{
unsigned long i;
do{
for (i=0;i<10;i++); //实际测试设定
partnumx--;
} while (partnumx != 0);
}
/************************************************************/
void outspk_ctr (unsigned char partnum)//占用较长时间
{
unsigned char scount = 0;
unsigned long wcount = 0;
//out_power_12v = 0;//开工放
delayNms(800);
partnum = partnum|0x80;
//out_power_12v = 0;//开工放
//delayNms(20);
out_spk_k1 = 0;
delayNms(70); //60ms
out_spk_k1 = 1;
out_spk_k1 = 0;
out_spk_k1 = 1;
for (scount = 0;scount<8;scount++)//160ms
{
delayNms(10);
out_spk_k2 = 0;
if ((partnum&0x01) == 0x01)
out_spk_k1 = 1;
else
out_spk_k1 = 0;
partnum=partnum>>1;
delayNms(5);
out_spk_k2 = 1;
//if (in_spk_01== 0) out_power_12v =1;
//if((out_spk_k2||in_spk_01)== 1) out_power_12v =1;
}
delayNms(5);
// out_spk_k2 = 0
//out_led_D1 =0;//方音开始
//play_busy = 1;
//delayNms(10);//开10秒计时
//等待播放
/*do
{ //*wcount++;
delayNms(1);/*
}while((in_spk_01==1)||(wcount>60000)));//时间有多长会影响系统时钟?
if (wcount>60000)
busy_01 = 1;
else
busy_01 = 0;
//播放时间10S*/
//out_power_12v = 1;//关工放
}
/**********************************************************/
void Count_S(void)
{ s_count++;
if (s_count > 6)
{ out_led_D2 =0;//1秒
delayNms(5);
out_led_D2 =1;//1秒
s_count =0;
/*if (play_busy)
{
s10_count++;
if (s10_count>10)
{ out_power_12v = 1;//关工放
s10_count = 0;
play_busy = 0;//关10秒计时
out_led_D1 =1;//方音结束
}
}*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -