本程序利用4个按键来控制蜂鸣器的输出频率,
第一个按键为蜂鸣器鸣叫开始。
第二个按键为蜂鸣器鸣叫停止。
第三个按键为蜂鸣器鸣叫频率增加。
第四个按键为蜂鸣器鸣叫频率降低。
程序如下,,需要注意的是按键输入,外部没有上拉电阻,所以必须允许片内上拉电阻。
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include <string.h> /* include peripheral declarations */
unsigned int temp=500;
int begin=1;
void SYS_INIT(void)
{
SOPT1 = 0x63; //disable cop. enable stop.
SPMSC1 = 0x00; //LVD disabled
IRQSC_IRQPE =1;
TODC_TODCLKS = 0;
TODC_TODPS = 1;
TODC_TODEN =1;
PTADD =0X00;
PTBDD = 0x00;
PTCDD = 0xFF;
PTBD = 0xff;
PTCD = 0xff;
PTAPE=0XFF;
//允许上拉电阻
PTBPE=0XFF;
PTCPE=0XFF;
}
//----------------------------------
void DELAY(unsigned int i)
{
while(i)i--;
}
//////////////// Main//////////////////////////////////////////////
void main(void)
{
SYS_INIT();
for(;;)
{
if((PTAD&0x40)==0){begin=1;DELAY(5000);} //KEY
1 BEGIN
if((PTAD&0x80)==0){begin=0;DELAY(5000);} //KEY
2 STOP
if((PTAD&0x01)==0)
{if(temp>200)temp-=20;DELAY(5000);} //+
if((PTBD&0X40)==0)
{if(temp<800)temp+=20;DELAY(5000);} //-
if(begin==1)
{
DELAY(temp);
PTCD=~PTCD;
}
}
}