⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm.c

📁 s3c44b0功能测试源码
💻 C
字号:
#include <string.h>
#include "..\inc\44b.h"
#include "..\inc\44blib.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);
unsigned int pwmcnt=0;

/****************************************
 *	PWM (BEEP) test		*
 ****************************************/
void Test_Pwm(void)
{
    int  beginf, endf;
    int  i;
    
    Uart_Printf("input beginf= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
    beginf = Uart_GetIntNum();
     while (beginf<MinBeepFreq ||beginf>=MaxBeepFreq)
    {
     Uart_Printf("ERROR !!!!  input beginf= (%d--%d): ",MinBeepFreq,MaxBeepFreq);
     beginf = Uart_GetIntNum();
    }
    Uart_Printf("input endf= (%d--%d): ",MinBeepFreq,MaxBeepFreq);
     endf = Uart_GetIntNum();
     while (endf<MinBeepFreq ||endf>=MaxBeepFreq ||endf<beginf )
    {
     Uart_Printf("ERROR !!!!  input endf> beginf (%d--%d) : ",MinBeepFreq,MaxBeepFreq);
     endf = Uart_GetIntNum();
    }

    Uart_Printf("beginf= %d ; endf= %d : ", beginf, endf );
     	 
    rINTMSK=~(BIT_GLOBAL|BIT_TIMER2);
    pISR_TIMER2= (unsigned)TIMER2_Int;
    
   for (i=beginf; i<endf; i++)
    {
      pwmcnt=0;
      SetBeepPwm( i, 50);
      Delay(60);
 	}
	
    StopBeepPwm();
  }

void SetBeepPwm(unsigned short Freq, unsigned char HiRatio)
{
	if(Freq>MaxBeepFreq)
		Freq = MaxBeepFreq;
	if(HiRatio>100)
		HiRatio = 100;
	rTCFG0 	 = 0xffff00ff;					// set Timer 2&3 prescaler 0
	rTCFG1   = 0x0300;	                // set Timer2 MUX 1/16
	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	 = 0x02000;					// manual update
	rTCON    = 0x0d000;					// auto reload, inverter on, start Timer 2,clear manal update bit
}

void StopBeepPwm(void)
{
    rINTMSK=BIT_GLOBAL|BIT_TIMER2;
	rTCON=0x0;
}

 void __irq TIMER2_Int(void)
{
  rI_ISPC=BIT_TIMER2;//clear the pending condition
  pwmcnt++;
  //Uart_Printf("%d ",pwmcnt++);
  return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -