www.pudn.com > hf44b0_TEST.rar > Pwm.C


#include  
#include "..\inc\44b.h" 
#include "..\inc\44blib.h" 
#include "..\inc\timer.h" 
 
#define READ_COUNT2 (rTCNTO2 & 0xffff) 
#define	MaxBeepFreq	900 
#define	MinBeepFreq	100 
 
extern int  Uart_GetIntNum(void); 
void SetBeepPwm(unsigned short Freq, unsigned char HiRatio); 
void StopBeepPwm(void); 
void __irq TIMER2_int(void); 
 //volatile int isWdtInt; 
unsigned int pwmcnt=0; 
 
/**************************************** 
 *	PWM (BEEP) test		* 
 ****************************************/ 
void Test_Pwm(void) 
{ 
    int  beginf, endf; 
    int  i; 
    Uart_Printf("[pwm (beep)  Test]\n"); 
    Uart_Printf("input begin f= (%d--%d): ", MinBeepFreq,MaxBeepFreq); 
    beginf = Uart_GetIntNum(); 
     while (beginfMaxBeepFreq) 
    { 
     Uart_Printf("ERROR !!!!  input begin f= (%d--%d): ", MinBeepFreq,MaxBeepFreq); 
     beginf = Uart_GetIntNum(); 
    } 
    Uart_Printf("input end f= (%d--%d): ", MinBeepFreq,MaxBeepFreq); 
    endf = Uart_GetIntNum(); 
     while (endfMaxBeepFreq ||endf < beginf ) 
    { 
     Uart_Printf("ERROR !!!!  input end f> beginf   (%d--%d) : ",MinBeepFreq,MaxBeepFreq); 
     endf = Uart_GetIntNum(); 
    } 
 
   Uart_Printf("beginf= %d ; endf=%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; iMaxBeepFreq) 
		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 ; 
}