送一段在LPC2148上定时器模拟的PWM波的相关代码,由于ARM运行速度快,对主程序影响较小,可实现两路模拟PWM输出和音乐方波输出
//#define SIM_PWM5 1<<21 //P0.21
//#define SIM_PWM6 1<<9 //P0.9
#define SIM_PWM5 1<<18 //P0.18
#define SIM_PWM6 1<<19 //P0.19
uint uiPWM5_RatioSetValue;
uint uiPWM6_RatioSetValue;
uint uiPWMPlusLength;
#define SIM_PWM_Plus_MaxLength 1200 //控制波形的周期
uint uiSIM_PWM5_Temp;
uint uiSIM_PWM6_Temp;
void SIM_PWM_PLUS_CTRL(void)
{
if(uiPWMPlusLength)
{
uiPWMPlusLength--;
}
else
{
uiPWMPlusLength = SIM_PWM_Plus_MaxLength;
// ucSIM_PWM_Temp = 98; //占空比控制值
// ucSIM_PWM_Temp = 70; //占空比控制值
uiSIM_PWM5_Temp = uiPWM5_RatioSetValue;
uiSIM_PWM6_Temp = uiPWM6_RatioSetValue;
// LightContol(2,1);
IO0SET = SIM_PWM5;
IO0SET = SIM_PWM6;
}
if(uiSIM_PWM5_Temp)
{
uiSIM_PWM5_Temp--;
}
else
{
// LightContol(2,0);
IO0CLR = SIM_PWM5;
// IO0CLR = SIM_PWM6;
}
if(uiSIM_PWM6_Temp)
{
uiSIM_PWM6_Temp--;
}
else
{
// LightContol(2,0);
IO0CLR = SIM_PWM6;
}
}
bool bEnableMusicOutFlag;
uint uiDivValue; //分频值
uint uiMusicFrequencyCount; //生成指定音乐频率的计数终值
void SetMusicOn(void)
{
IO0DIR |= Beep_Pin;
bEnableMusicOutFlag = 0xff;
uiDivValue = 0;
}
void SetMusicOff(void)
{
bEnableMusicOutFlag = false;
IO0SET = Beep_Pin;
}
//定时器1的中断频率是120K Hz
//音乐波控制函数调用
void SIM_Music_Plus_CTRL(void)
{
if(bEnableMusicOutFlag)
{
uiDivValue++;
if(uiDivValue == (uiMusicFrequencyCount/2))
{
IO0CLR = Beep_Pin;
}
if(uiDivValue == uiMusicFrequencyCount)
{
IO0SET = Beep_Pin;
uiDivValue = 0;
}
}
}
uchar ucFlag;
void IRQ_Timer1(void) __irq
{
if(ucFlag)
{
ucFlag =0;
}
else
{
ucFlag =1;
}
/*
if(ucFlag)
{
LightContol(1,1);
}
else
{
LightContol(1,0);
}
*/
//频率固定,占空比可调的软件PWM波生成函数调用。
SIM_PWM_PLUS_CTRL();
//占空比固定,频率可调的,音乐波控制函数调用
SIM_Music_Plus_CTRL();
T1IR = 0x01;
VICVectAddr = 0x00;
}
//初始化系统中断
void Init_SystemInterrupt(void)
{
/* 设置定时器0中断IRQ */
VICIntSelect = 0x00; /* 所有中断通道设置为IRQ中断 */
VICVectCntl0 = 0x20 | 0x05; /* 设置定时器1中断通道分配最高优先级 */
VICVectAddr0 = (unsigned int)IRQ_Timer1; /* 设置中断服务程序地址 */
VICIntEnable = 1 << 0x05; /* 使能定时器1中断 */
} |