刚学单片机,请问如何改下列代码,使得蜂鸣器频率能从1khz开始,每5秒增加1khz一直到50khz,急急急,求大佬解答。
LQPWMmain.c
#include "derivative.h"
#include <stdio.h>
#include "LQprintp.h"
#include "LQinit.h"
#include"LQ12864.h"
byte u8_RCV_Ch=0xff;
//====================中断函数==================================
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 20 SCI0_ISR(void)
{
SCI0CR2_RIE=0;
//此处为串口中断需要处理的事情
uart_putchar(u8_RCV_Ch=uart_getkey());
//PORTB_BIT2=~PORTB_BIT2;
Beep();
SCI0CR2_RIE = 1;
}
//====================main()================
#pragma CODE_SEG DEFAULT
void main(void)
{
byte u8_beepflag=0;
char txtbuf[16]="";
Init_Dev();
LCD_Init();
PORTB=0x00;
LCD_Print(36,0,"龙丘科技");
LCD_Print(40,2,"开发板");
putstr("\nLongQiu s.&t.\n");
Beep();
Dly_ms(1000);
EnableInterrupts;
for(;;)
{
switch(u8_RCV_Ch)
{
case 'c':
Beep();
u8_RCV_Ch=0xff;
u8_beepflag=0; //关闭蜂鸣器测试
break;
case 'o':
u8_RCV_Ch=0xff;
u8_beepflag=1; //允许蜂鸣器连续鸣响
Beep();
break;
case 'u':
u8_RCV_Ch=0xff;
if(u8_beepflag)
{
PWME_PWME1 = 1; //开启PWM0波形输出
if(PWMPER1>15) //增大频率
PWMPER1-=5;
}
else
Beep();
break;
case 'd':
u8_RCV_Ch=0xff;
if(u8_beepflag)
{
PWME_PWME1 = 1;
if(PWMPER1<255) //减小频率
PWMPER1++;
if(PWMDTY1>PWMPER1+10) //减小占空比
PWMDTY1=PWMPER1*4/5; //20%
}
else
Beep();
break;
case 'l':
u8_RCV_Ch=0xff;
if(u8_beepflag)
{
PWME_PWME1 = 1;
if(PWMDTY1<PWMPER1) //增大占空比
PWMDTY1++;
}
else
Beep();
break;
case 'r':
u8_RCV_Ch=0xff;
if(u8_beepflag)
{
PWME_PWME1 = 1;
if(PWMDTY1<PWMPER1) //减小占空比
PWMDTY1--;
}
else
Beep();
break;
default:
break;
}//switch(u8_RCV_Ch)
Dly_ms(50);
//----------PWM0----------------------------------
sprintf(txtbuf,"F:%d0HZ ",PWMPER1);
LCD_P8x16Str(30,4,txtbuf);
printp("\n frequence: %d0Hz ",PWMPER1);
sprintf(txtbuf,"D:%d%% ",(PWMPER1-PWMDTY1)*100/PWMDTY1);
LCD_P8x16Str(30,6,txtbuf);
printp("\n duty circle: %d%%",(PWMPER1-PWMDTY1)*100/PWMDTY1);
PORTB_PB1=~PORTB_PB1;
}//for(;;)
} //main
LQinit.c
/********************************************
龙丘MC9S12XET256多功能开发板
Designed by Chiu Sir
E-mail:chiusir@163.com
软件版本:V1.1
最后更新:2010年5月21日
相关信息参考下列地址:
博客: http://longqiu.21ic.org
淘宝店:http://shop36265907.taobao.com
------------------------------------
Code Warrior 5.0
Target : MC9S12XET256
Crystal: 16.000Mhz
busclock:16.000MHz
pllclock:32.000MHz
============================================*/
#include "derivative.h"
void Dly_ms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
//for(jj=0;jj<1335;jj++); //16MHz--1ms
//for(jj=0;jj<4006;jj++); //48MHz--1ms
for(jj=0;jj<5341;jj++); //64MHz--1ms
}
//-----------------------------------------------------
//-----------------------------------------------------
void Beep(void)
{
word tmper=125,tmdty=125;
tmper=PWMPER1;
tmdty=PWMDTY1;
PWME_PWME1 = 1;
PWMPER1 =125;
PWMDTY1 =100;
Dly_ms(40);
PWMPER1 =tmper;
PWMDTY1 =tmdty;
PWME_PWME1 = 0;
}
//-----------------------------------------------------
static void SCI_Init(void)
{
SCI0CR2=0x2c; //enable Receive Full Interrupt,RX enable,Tx enable
SCI0BDH=0x00; //busclk 8MHz,19200bps,SCI0BDL=0x1a
SCI0BDL=0x68; //SCI0BDL=busclk/(16*SCI0BDL)
//busclk 8MHz, 9600bps,SCI0BDL=0x34
//busclk 8MHz, 9600bps,SCI0BDL=0x68
//busclk 24MHz, 9600bps,SCI0BDL=0x9C
} //busclk 32MHz, 9600bps,SCI0BDL=0xD0
//-----------------------------------------------------
static void Port_Init(void)
{
DDRB = 0xff; //LED PTB0--7,
PORTB= 0xff; //LEDs on
}
//-----------------------------------------------------
static void PWM_Init(void)
{
//SB,B for ch2367
//SA,A for ch0145
PWMPRCLK = 0X55; //clockA,CLK B 32分频:500khz
PWMSCLA = 0x02; //对clock SA 进行2*PWMSCLA=4分频;pwm clock=clockA/4=125KHz;
PWMSCLB = 0X02; //clk SB=clk B/(2*pwmsclb)=125KHZ
//pwm1
PWMCNT1 = 0;
PWMCAE_CAE1=0;
PWMPOL_PPOL1=0;
PWMPER1 =125;
PWMDTY1 =100;
PWMCLK_PCLK1 = 1;
PWME_PWME1 = 0;
}
//产生40ms的定式中断,读取IOC7的计数值
static void Timer_Init(void)
{
//TSCR1=0X80;//TIMER INT ENABLED
//TSCR1=0x90;//计数器使能TEN|快速清零TFFCA
ECT_TSCR1=0X00; //禁止TIM
//TSCR2=0X80;//DIV 1->2.5ms,enable time overflow interrrupt
//TSCR2=0X82;//DIV 4->10ms
//TSCR2=0X83;//DIV 8->20ms
//TSCR2=0X84;//DIV 16->40ms
ECT_TSCR2=0X85;//DIV 32->80ms
//TSCR2=0X86;//DIV 64->160ms
//TSCR2=0X87;//DIV 128->320ms,enable time overflow interrrupt
ECT_TCNT =0; //
ECT_PACTL=0X50; //PT7 PIN,PACN32 16BIT,FALLing edge,NOT INTERRUPT
//PBCTL=0X40;//PBCN10 16BIT,INT DISABLED
//ICPAR=0; //8BIT DISABLED
}
//-----------------------------------------------------
// PLL初始化子程序 BUS Clock=16M
void setbusclock(void)
{
CLKSEL=0X00; // disengage PLL to system
PLLCTL_PLLON=1; // turn on PLL
SYNR=0x00 | 0x01; // VCOFRQ[7:6];SYNDIV[5:0]
// fVCO= 2*fOSC*(SYNDIV + 1)/(REFDIV + 1)
// fPLL= fVCO/(2 × POSTDIV)
// fBUS= fPLL/2
// VCOCLK Frequency Ranges VCOFRQ[7:6]
// 32MHz <= fVCO <= 48MHz 00
// 48MHz < fVCO <= 80MHz 01
// Reserved 10
// 80MHz < fVCO <= 120MHz 11
REFDV=0x80 | 0x01; // REFFRQ[7:6];REFDIV[5:0]
// fREF=fOSC/(REFDIV + 1)
// REFCLK Frequency Ranges REFFRQ[7:6]
// 1MHz <= fREF <= 2MHz 00
// 2MHz < fREF <= 6MHz 01
// 6MHz < fREF <= 12MHz 10
// fREF > 12MHz 11
// pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
POSTDIV=0x00; // 4:0, fPLL= fVCO/(2xPOSTDIV)
// If POSTDIV = $00 then fPLL is identical to fVCO (divide by one).
_asm(nop); // BUS CLOCK=16M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
//-----------------------------------------------------
#pragma CODE_SEG DEFAULT
void Init_Dev(void)
{
setbusclock();
Port_Init();
SCI_Init();
PWM_Init();
}
//-----------------------------------------------------
LQinit.h
extern void Init_Dev(void);
extern void Dly_ms(int ms);
extern void Beep(void);