我这个程序能实现这个功能吗???
#include "define.h"
#include "delay.h"
#include "play.h"
#include "motor.h"
#include "mic.h"
/********请先在define.h中调整晶振频率与周期配置**************/
u8 mode = 0; //速度级别
void change_mode()
{
static u8 i=0,j=0; //计时变量约为8ms
if(mic_flag==2)
{
i++;
if(i>10&&mic_flag==2)
{
//开机确认
if(mode==0)
{
Play_music(PCM8sfkj,sizeof(PCM8sfkj));
j=50;
mic_flag=0;
while(j--)
{
delay_ms(100);
if(mic_flag==2)
{
Play_music(PCM8zstz,sizeof(PCM8zstz));
mode=1;
mic_flag=0;
break;
}
}
}
else if(mode==1)
{
mode=2;
}
else if(mode==2)
{
Play_music(PCM8zstz,sizeof(PCM8zstz));
mode=1;
}
mic_flag=0;
i=0;
}
}
else if(mic_flag==3)
{
i++;
if(i>15&&mic_flag==3)
{
mode=0;
mic_flag=0;
i=0;
}
}
}
void change_PWM()
{
if(mode==0)
{
PCA_PWM0 = 0x03;
CCAP0H = CCAP0L = 0xff;
}
else if(mode==1)
{
PCA_PWM0 = 0x00;
CCAP0H = CCAP0L = CCAP0L-1;
}
else if(mode==2)
{
PCA_PWM0 = 0x00;
CCAP0H = CCAP0L = CCAP0L;
}
}
void main()
{
Timer1Init();
IT0Init();
PWMInit();
Play_init();
delay_ms(1000);
while(1)
{
change_mode();
change_PWM();
Digital_Display_One(CCAP0L/16);
delay_ms(50);
}
}
void delay_ms(uint ms)
{
unsigned char i, j;
do
{
i = TIME_I;
j = TIME_J;
do
{
while (--j);
} while (--i);
}while(--ms);
}
#if Machine_Cycle==12UL
void delay5us() //@12.000MHz
{
_nop_();
}
#elif Machine_Cycle==2UL
void delay5us() //@12.000MHz
{
unsigned char i;
i = 12;
while (--i);
}
void IT0Init(void) //100微秒@11.0592MHz
{
IT0=1;
EX0=1;
EA=1;
}
void Timer1Init(void) //50毫秒@11.0592MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TMOD |= 0x10; //设置定时器模式
TL1 = 0x66; //设置定时初值
TH1 = 0xFC; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
EA = 1;
ET1 = 1;
}
u32 times=0;
void T1INT() interrupt 3
{
TL1 = 0x66; //设置定时初值
TH1 = 0xFC; //设置定时初值
times++;
}
u8 mic_flag=0;
void EX0INT(void) interrupt 0
{
static u32 last_time=0;
u32 differ;
differ=times-last_time;
last_time=times;
if(differ>1000)
{
mic_flag=1;
}
else if(mic_flag>0&&differ>100&&differ<500)
{
mic_flag++;
}
}
void PWMInit()
{
CCON = 0; //清除PCA控制寄存器
CL = 0; //复位PCA定时器计数值
CH = 0;
CMOD=0x0a; //工作模式:频率=Sysclk/4/256=10800Hz 无中断
CCAP0H = CCAP0L = 0xff; //PWM0设置占空比50%
PCA_PWM0 = 0x03;
CCAPM0 = 0x42; //PCA模块0 工作在8比特方式 无中断
CR = 1; //PCA定时器运行
}
|