📄 pwm.c
字号:
#include <string.h>
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\timer.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 TIMER0_int(void);
//volatile int isWdtInt;
unsigned int pwmcnt=0;
/****************************************
* PWM (BEEP) test *
****************************************/
void Test_Pwm(void)
{
int beginf, endf;
int i, j;
Uart_Printf("[pwm (beep) Test]\n");
/*Uart_Printf("input begin f= (%d--%d): ", MinBeepFreq,MaxBeepFreq);
beginf = Uart_GetIntNum();
while (beginf<MinBeepFreq ||beginf>=MaxBeepFreq)
{
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 (endf<MinBeepFreq ||endf>=MaxBeepFreq ||endf < beginf )
{
Uart_Printf("ERROR !!!! input end f> beginf (%d--%d) : ",MinBeepFreq,MaxBeepFreq);
endf = Uart_GetIntNum();
}*///edit by chenzhi
beginf=2700;
endf=3000;
Uart_Printf("beginf= %d ; endf=%d : ", beginf, endf );
rINTCON = 5; //允许IRQ使用非向量模式,允许IRQ中断
rINTMOD = 0; //中断以IRQ模式来处理
rINTMSK=~(BIT_GLOBAL|BIT_TIMER2);//开放TIMER2中断
pISR_TIMER0= (unsigned int)TIMER0_int; //(unsigned)TIMER0中断函数入口地址
rPDATE = 0x157; //Beep = 10
rPCONE = 0x55ab; // tout0
rPUPE = 0xff; //禁止上拉电阻连接到对应脚
for (i=beginf; i<endf; i+=50)
{
pwmcnt=0;
SetBeepPwm( i, 50);
for (pwmcnt=0,j=0; pwmcnt<5;)
{
Uart_Printf("j=%d\n",j++);
}
/* for (j=0; j<5;)
{
while (READ_COUNT2>=1)
{
}
j++;
} */
}
StopBeepPwm();
}
void SetBeepPwm(unsigned short Freq, unsigned char HiRatio)
{
if(Freq>MaxBeepFreq)
Freq = MaxBeepFreq;
if(HiRatio>100)
HiRatio = 100;
rTCON &= 0xfffffff0; // clear manual update bit, stop Timer0
rTCFG0 &= 0xffffff00; // set Timer 0&1 prescaler 0
rTCFG1 &= 0xfffffff0; // set Timer 0 MUX 1/16 edit by chenzhi
rTCFG1 |= 0x00000003; //edit by chenzhi
rTCNTB0 = MCLK/(Freq*16); //if set inverter off, when TCNT0<=TCMP0, TOUT is high, TCNT0>TCMP0, TOUT is low
rTCMPB0 = (rTCNTB0*(100-HiRatio))/100; //if set inverter on, when TCNT0<=TCMP0, TOUT is low, TCNT0>TCMP0, TOUT is high
rTCON |= 0x00000002; // manual update
rTCON &= 0xfffffff0; // clear manal update bit
rTCON |= 0x0000000d; // auto reload, inverter on, start Timer 0
}
void StopBeepPwm(void)
{
rTCON &= ~0x0001; //stop timer0
}
void __irq TIMER0_int(void)
{
rI_ISPC=BIT_TIMER0;
pwmcnt++;
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -