static void __irq Key1_ISR(void)
//暂停键,关闭蜂鸣器
{
rSRCPND = rSRCPND | (0x1<<1);
rINTPND = rINTPND | (0x1<<1);
rTCON &= ~0x8;
//禁止定时器自动重载,即关闭定时器
stop = TRUE;
}
void __irq Key4_ISR(void)
//重启键,开启蜂鸣器
{
rSRCPND = rSRCPND | 0x1;
rINTPND = rINTPND | 0x1;
stop = FALSE;
}
void delay(int a)
{
int k;
for(k=0;k<a;k++)
;
}
void Main(void)
{
int freq;
rGPBCON = 0x155556;
//B0为TOUT0,B5~B8为输出,给LED
rGPBUP
= 0x7ff;
rGPFCON = 0xaaaa;
//F口为EINT,给按钮
//按钮的一些必要配置
rSRCPND = 0x07;
rINTMSK = ~0x07;
rINTPND =0x07;
rEXTINT0 = 0x22;
freq = 2500;
rTCFG0 &= 0xFFFF00;
rTCFG0 |= 0x31;
//prescal 是49
rTCFG1 &= ~0xF;
//1/2,因为PCLK为50MHz,所以50MHz/50/2=500kHz
rTCNTB0 = 5000;
rTCMPB0 = freq;
rTCON &= ~0x1F;
rTCON |= 0xf;
//死区无效,自动装载,电平反转,手动更新,定时器开启
rTCON &= ~0x2 ;
//手动更新位清零,PWM开始工作
pISR_EINT0 = (U32)Key4_ISR;
pISR_EINT1 = (U32)Key1_ISR;
stop = FALSE;
rGPBDAT = ~0x60;
//两个LED亮
while(1)
{
//频率递增
for ( ; freq<4950 ; )
{
freq+=10;
rTCMPB0 = freq;
//重新赋值
delay(20000);
while (stop == TRUE)
//暂停
{
delay(1000);
if (stop ==FALSE)
//判断是否重启
{
rTCON &= ~0x1F;
rTCON |= 0xf;
rTCON &= ~0x2 ;
//恢复PWM功能
}
}
//4个LED随着频率的高低,时灭时亮
if(freq == 100)
rGPBDAT = ~0x1e0;
if(freq == 1300)
rGPBDAT = ~0xe0;
if(freq == 2500)
rGPBDAT = ~0x60;
if(freq == 3700)
rGPBDAT = ~0x20;
if(freq == 4900)
rGPBDAT = ~0x0;
}
//频率递减
for( ; freq>50 ; )
{
freq-=10;
rTCMPB0 = freq;
delay(20000);
while (stop == TRUE)
{
delay(1000);
if (stop ==FALSE)
{
rTCON &= ~0x1F;
rTCON |= 0xf;
rTCON &= ~0x2 ;
}
}
if(freq == 100)
rGPBDAT = ~0x1e0;
if(freq == 1300)
rGPBDAT = ~0xe0;
if(freq == 2500)
rGPBDAT = ~0x60;
if(freq == 3700)
rGPBDAT = ~0x20;
if(freq == 4900)
rGPBDAT = ~0x0;
}
}
} |