#include <string.h>
#include <math.h>
#include "option.h"
#include "def.h"
#include "44b.h"
#include "44blib.h"
#define READ_COUNT2 (rTCNTO2 & 0xffff)
#define MaxBeepFreq 50000
#define MinBeepFreq 20
extern int Uart_GetIntNum(void);
void SetBeepPwm(unsigned short Freq, unsigned char HiRatio);
void StopBeepPwm(void);
void __irq TIMER2_int(void);
void Isr_Init(void);
void HaltUndef(void);
void HaltSwi(void);
void HaltPabort(void);
void HaltDabort(void);
int pwmcnt=0;
/****************************************
* pwm2 timer test *
****************************************/
void Main(void)
{
int beginf, endf;
int i, j;
rSYSCFG=SYSCFG_8KB;
#if (PLLON==1)
ChangePllValue(PLL_M,PLL_P,PLL_S);
#endif
Isr_Init();
Port_Init();
//Uart_Init(0,115200);
//Uart_Select(0);
//Uart_Printf("[PWM2 Test]\n");
//Uart_Printf("输入起始频率= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
beginf = 100;
//Uart_GetIntNum();
//while (beginf<MinBeepFreq ||beginf>=MaxBeepFreq){
//Uart_Printf("ERROR !!!! input begin f= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
//beginf = Uart_GetIntNum();
//}
//Uart_Printf("输入终止频率= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
endf =500;//改为200!!!!!
// Uart_GetIntNum();
//while (endf<MinBeepFreq ||endf>=MaxBeepFreq ||endf < beginf ){
//Uart_Printf("ERROR !!!! input end f> beginf (%d--%d) : ",MinBeepFreq,MaxBeepFreq);
//endf = Uart_GetIntNum();
//}
//Uart_Printf("起始频率= %d ; 终止频率=%d : ", beginf, endf );
rINTCON = 5;
rINTMOD = 0;
rINTMSK=~(BIT_GLOBAL|BIT_TIMER2);
pISR_TIMER2= (unsigned int)TIMER2_int; //(unsigned)
rPDATE = 0x157; //Beep = 10
rPCONE = 0x596b; // tout2
rPUPE = 0xff;
for(i=beginf; i<endf; i++){
pwmcnt=0;
SetBeepPwm( i, 1);
for (pwmcnt=0,j=0; pwmcnt<5;){
}
}
StopBeepPwm();
}
void SetBeepPwm(unsigned short Freq, unsigned char HiRatio)
{
if(Freq>MaxBeepFreq)
Freq = MaxBeepFreq;
if(HiRatio>100)
HiRatio = 100;
rTCON &= 0xffff0fff; // clear manual update bit, stop Timer2
rTCFG0 &= 0xffff00ff; // set Timer 2&3 prescaler 0
rTCFG1 &= 0xfffff0ff; // set Timer 2 MUX 1/16
rTCFG1 |= 0x00000300;
rTCNTB2 = MCLK/(Freq*16); //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
rTCMPB2 = (rTCNTB2*(100-HiRatio))/100; //if set inverter on, when TCNT2<=TCMP2, TOUT is low, TCNT2>TCMP2, TOUT is high
rTCON |= 0x00002000; // manual update
rTCON &= 0xffff0fff; // clear manal update bit
rTCON |= 0x0000d000; // auto reload, inverter on, start Timer 2
}
void StopBeepPwm(void)
{
rTCON &= ~0x1000;
}
void __irq TIMER2_int(void)
{
rI_ISPC=BIT_TIMER2;
pwmcnt++;
return;
}
void Isr_Init(void)
{
U32 i;
pISR_UNDEF=(unsigned)HaltUndef;
pISR_SWI =(unsigned)HaltSwi;
pISR_PABORT=(unsigned)HaltPabort;
pISR_DABORT=(unsigned)HaltDabort;
for(i=_RAM_STARTADDRESS;i<(_RAM_STARTADDRESS+0x20);i+=4)
{
*((volatile unsigned *)i)=0xEA000000+0x1FFE;
}
//rINTCON=0x1; // Vectored Int. IRQ enable,FIQ disable
rINTCON=0x5; // Non-vectored,IRQ enable,FIQ disable
rINTMOD=0x0; // All=IRQ mode
rINTMSK|=BIT_GLOBAL|BIT_EINT3; // All interrupt is masked.
}
void HaltUndef(void)
{
Uart_Printf("Undefined instruction exception!!!\n");
while(1);
}
void HaltSwi(void)
{
Uart_Printf("SWI exception!!!\n");
while(1);
}
void HaltPabort(void)
{
Uart_Printf("Pabort exception!!!\n");
while(1);
}
void HaltDabort(void)
{
Uart_Printf("Dabort exception!!!\n");
while(1);
} |