📄 lpc2132pwm.txt
字号:
/******************************************************************/
PWM控制程序:
#include "config.h"
#include "new_key.h"
#include "lcd.h"
extern signed char num;
extern char keep_code[];
extern char info[];
extern char expect[];
/******************************************/
void PWM_Init()
{
PINSEL0 = (PINSEL0&0xfffcffff)|(0x02 << 16); // P0.8选择PWM4功能
/* PWM初始化 */
PWMPR = 0x00; // 不分频,计数频率为Fpclk
PWMMCR = 0x02; // 设置PWMMR0匹配时复位PWMTC
PWMPCR = 0x1000; // 允许PWM2输出,单边PWM
PWMMR0 = Fpclk / 10000;
PWMMR4 = PWMMR0 / 2; // 50%占空比
PWMLER = 0x11; // PWM0和PWM2匹配锁存
PWMTCR = 0x02; // 复位PWMTC
PWMTCR = 0x09; // 启动PWM输出
}
/*********************************/
void PWM_Change(void)
{
uint32 tmp=0;
if(keep_code[num-1]==0x0f)
{
info[6]=keep_code[0]+'0';
info[7]=keep_code[1]+'0';
info[8]='.';
info[9]=keep_code[3]+'0';
tmp=keep_code[0]*10000+keep_code[1]*1000+keep_code[2]*100+keep_code[3]*10;
PWMMR0=Fpclk/tmp;
PWMMR4=PWMMR0/2;
}
if(keep_code[1]==0x0e)
{
expect[0]=keep_code[0];
expect[2]=keep_code[2];
expect[3]=keep_code[3];
}
else
{
info[13]=keep_code[0]+'0';
info[14]=keep_code[1]+'0';
tmp=keep_code[0]*10+keep_code[1];
PWMMR4 = PWMMR0*tmp/100; // 设置输出频率
}
PWMLER = 0x11; // 更新匹配值后,必须锁存
}
/********************************/
void PWM_Status()
{ char status;
status= PWMMR4*10/PWMMR0;
info[13]=status+'0';
info[14]=PWMMR4*10%PWMMR0*10/PWMMR0+'0';
// info[13]=codes[0]+'0';
// info[14]=codes[1]+'0';
// data_assic[2]=data/343+48;
// data=data%343*10;
}
/****************************/
void PWM_Up(uint16 up)
{
PWMMR4=(PWMMR4+(PWMMR0*up)/1000);
if(PWMMR4>(PWMMR0-5))
PWMMR4=PWMMR0-5;
// num=-1;
PWMLER=0x11;
}
/****************************/
void PWM_Down(uint16 down)
{
if(PWMMR4!=0)
{
PWMMR4=(PWMMR4-(down*PWMMR0)/1000);
if(PWMMR4<5)
PWMMR4=0;
}
PWMLER= 0x11;
// num=-1;
}
#include "config.h"
#include "music.h"
const uint32 HCMM[] =
{ _LA,_SO,_MI,_LA,_SO,_MI,
_LA,_LA,_SO,_LA,
_LA,_SO,_MI,_LA,_SO,_MI,
_RE,_RE,_DO,_RE,
_MI,_MI,_SO,_LA,_DO1,_LA,_SO,
_MI,_MI,_SO,_DO,
_MI,_MI,_MI,_MI,_MI,
_1LA,_1LA,_1SO,_1LA
};
const uint32 HCMM_L[] =
{ _4,_8,_8,_4,_8,_8,
_8,_4,_8,_2,
_4,_8,_8,_4,_8,_8,
_8,_4,_8,_2,
_4,_8,_8,_8,_8,_8,_8,
_8,_4,_8,_2,
_4,_4,_4,_8,_8,
_8,_4,_8,_2
};
void Delay(uint8 dly)
{
uint32 i;
for(;dly>0;dly--)
for(i=0;i<0x7FFFF;i++);
}
int main(void)
{
uint8 i;
PINSEL0 = 0x02<<14;
PWMPR = 0x00;
PWMMCR = 0x02;
PWMPCR = 0x0400;
PWMMR0 = Fpclk/1000;
PWMMR2 = PWMMR0/2;
PWMLER = 0x05;
PWMTCR = 0x02;
PWMTCR = 0x09;
while(1)
{
for(i=0;i<sizeof(HCMM);i++)
{
PWMMR0 = Fpclk/HCMM;
PWMLER = 0x05;
Delay(HCMM_L);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -