Home

如何在LPC1768中使用PWM?LPC1768 PWM Tutorial

In this tutorial, I will show you how to configure and use the PWM in LPC1768 MCU. PWM is one of the commonly used techniques to control the amount of power delivered through a pin. The PWM technique allows you to control the brightness of an LED, speed of a Motor, position of a Servo etc. Using this LPC1768 PWM Tutorial, you can implement a simple LED Brightness control application.

重要的提示:我已经在上一个教程中提到了这一点,但是我将在每个LPC1768教程中重复此操作。从NXP官方网站下载LPC1768 MCU的数据表和用户手册。我无法详细解释 /讨论每个主题。您必须在这些文档中查找讨论主题,并收集其他信息。

介绍

PWM是脉冲宽度调制的缩写。这是最简单的生产方式使用数字系统的模拟值。the width of the Pulse i.e. the duration for with the pulse stays ON in a period is varied. Hence, it is called Pulse Width Modulation.

PWM循环的周期是脉冲高的持续时间和脉冲低的持续时间。这通常由t表示和T离开。因此,PWM = t的周期+ t离开

PWM信号的另一个重要参数是其占空比。占空比是脉冲高与PWM信号总周期的持续时间之比。

占空比= t/ (T+ t离开)

Duty cycle can also be represented as percentage.

Duty cycle % = T* 100 /(t+ t离开)

当脉冲的按时间和偏离时间相等时,占空比为50%,脉冲的形状为完美的方波。

An important classification ofPWM信号is:

  • 单边PWM
  • 双边PWM

单边缘脉宽调制的脉冲可以是在的beginning of the period at the end of the period. If the pulse is at the beginning of the period, then it is called as Left Aligned PWM or Trailing Edge PWM. If the pulse is at the end of the period, then it is called as Right Aligned PWM or Leading Edge PWM.

单边和双边PWM

Coming to Double Edge PWM, both the edges can be modulated and hence, the pulse is placed anywhere in the period. Double Edge PWM is generally used in multi-phase motor control applications.

One of the important applications of PWM is power delivery. Duty cycle of the PWM determines the average power of a PWM Signal and it is calculated using the following formula.

VAVG=占空比 * VH

Where VHis the maximum voltage level of the PWM Signal.

LPC1768中的PWM

The PWM peripheral in LPC1768 MCU is similar to that of a Timer Peripheral as it has a Timer Counter, Prescaler Match Registers and their functions are exactly same as what we discussed in the LPC1768 Timer Tutorial.

有6个称为PWM1.1的PWM输出,PWM1.2,…PWM1.6。使用这六个PWM通道,您可以生成6个单边的PWM信号或3个双层PWM信号。

共有七个比赛登记册。匹配寄存器0,即PWM1MR0用于设置PWM循环速率,而其余的匹配寄存器(即PWM1MR1 - PWM1MR6)对应于相应的PWM输出通道。PWM1.1对应于PWM1MR1,PWM1.2对应于PWM1MR2,等。

LPC1768 PWM Pins

下表显示了LPC1768 MCU中与PWM相关的引脚。

PWM1.1

P1.18 / P2.0

PWM1.2

P1.20 / P2.1 / P3.25

PWM1.3

P1.21 / P2.2 / P3.26
PWM1.4

P1.23 / P2.3

PWM1.5

P1.24 / P2.4
PWM1.6

P1.26 / P2.5

Registers Associated with LPC1768 PWM

There are totally 21 registers associated with PWM in LPC1768. If you take a look at the list of PWM Registers, you will find a lot of similar names as in Timer Peripheral. Let us now take a look at some of the important registers of PWM block.

PWM1TCR - PWM计时器控制寄存器:用于控制PWM计时器计数器的操作。

Bit 0

计数器启用 1时,启用了PWM计时器计数器和预定计数器。

Bit 1

计数器重置

当1时,PWM计时器计数器和预定计数器将在PCLK的下一个正边缘重置。

Bit 3 PWM Enable

When 1, PWM Mode is enabled.

PWM1PR – Prescale Register:它用于控制PWM输出的分辨率。PCLK的每个PR+1个周期都会增加TC。

PWM1MR0 – PWM1MR6 – Match Registers:如上所述,这些匹配寄存器包含脉冲宽度的值(pwm1tc ticks no。)。

PWM1MCR - PWM匹配控制寄存器:Used to control the operation to be performed when one of the match register matches the TC. Each Match Register has 3-bits of control. So, a total of 21-bits in the PWM1MCR register are used to control the operations for all the seven match registers.

Let us see one such set of bits for PWM1MR0 and all other register can be understood based on that.

Bit 0

PWMMR0I 如果将此位设置为1,则当PWMMR0与PWMTC匹配时会生成中断。
Bit 1 PWMMR0R

如果将此位设置为1,则当PWMMR0与PWMTC匹配时,PWMTC是重置的。

Bit 2

PWMMR0S

如果将此位设置为1,则停止PWMTC和PWMPC,然后将PWMTCR [0]设置为0,当PWMMR0与PWMTC匹配时。

Similarly, Bits [5:3] for PWMMR1, Bits [8:6] for PWMMR2 etc.

PWMLER - PWM闩锁启用登记册:用于控制PWM匹配寄存器的更新。当一个值写入匹配寄存器中时,它不会立即发挥作用。只有在设置了PWMler寄存器中的相应“闩锁”位时,匹配寄存器中的值才能有效。

PWMler的位0用于启用PWM匹配0闩锁,PWMler的位1用于启用PWM匹配1闩锁,依此类推,直到匹配寄存器6的位6。

PWM1PCR – PWM Control Register:用于启用PWM通道,并选择PWM的类型,即单个边缘PWM或双边PWM。

Bit 2 is called PWMSEL2 and when this bit 0, PWM2 output is single edged. When this bit is 1, PWM2 output is double edged. Similarly Bit 3 for PWM3, Bit 4 for PWM4 until Bit 6 for PWM6.

Bits 9 in PWM1PCR is called PWMENA1 and when this bit is 1, PWM1 output is enabled, otherwise disabled. Similarly, Bit 10 for PWM2 output, Bit 11 for PWM3 output and so on until Bit 14 for PWM6 output.

Initialize PWM Block

If you observe the registers of PWM, you can find a lot of similarities with that of a Timer. And the initialization is also very similar. Additionally, some PWM related initializations like PWM pin selection, enabling PWM channel, selecting type of PWM etc. must be done.

Before that, let us make some calculation related to the PWM PR.

如果PR是PWM预定值,并且PCLK是PWM外围时钟,然后可以使用以下公式计算PWM分辨率。

LPC1768中的PWMImage 1

重新安排上述方程式,我们可以将PR的价值作为:

pr =(pwmRES* PCLK) – 1

For PCLK of 25 MHz and PWMRES1毫米秒,PR的值为

pr =(25 * 106* 1 * 10-3) – 1 = 24999.

如果PWMRESis 1 micro second, then PR = 24.

例子

让我们使用上述PR值并编写一个程序来进行LED调光应用程序。

#include
#define PWMPRESCALE (25-1)
#define TIMPRESCALE (25000-1)

void pwm_init(void);
void pulsewidth_update(unsigned int pulsewidth);
void延迟(未签名的int毫秒);
void Timer0_init(void);

int main(void)
{
timer0_init();
pwm_init();

int count = 0;

而(1)
{
while(count < 1000)
{
count+=10;
pulsewidth_update(count);
延迟(1);
}
而(计数> 0)
{
计数 - = 10;
pulsewidth_update(count);
延迟(1);
}

}
return 0;
}

void PWM_Init(void)
{
LPC_PINCON->PINSEL3 |= (1<<5);
LPC_PWM1->PCR = 0x0;
lpc_pwm1-> pr = pwmprescale;
LPC_PWM1-> MR0 = 1000;
LPC_PWM1-> MR1 = 0;
lpc_pwm1-> mcr =(1 << 1);
lpc_pwm1-> ler =(1 << 1)|(1 << 0);
lpc_pwm1-> pcr =(1 << 9);
lpc_pwm1-> tcr =(1 << 1);

LPC_PWM1->TCR = (1<<0) | (1<<3);

}

void PulseWidth_Update(unsigned int pulseWidth)
{
LPC_PWM1->MR1 = pulseWidth;
LPC_PWM1->LER = (1<<1);
}

void delaym(未签名的INT毫秒)
{
LPC_TIM0->TCR = 0x02;
LPC_TIM0->TCR = 0x01;

while(LPC_TIM0->TC < milliseconds);

lpc_tim0-> tcr = 0x00;
}

void Timer0_init(void)
{
lpc_tim0-> ctcr = 0x0;
lpc_tim0-> pr = timprescale;

LPC_TIM0->TCR = 0x02;
}

结论

A simple tutorial on using PWM in LPC1768 MCU. Using this LPC1768 PWM Tutorial, you can configure the PWM block to generate PWM signals for LED Control, Motor Control, etc.

发表评论

您的电子邮件地址不会被公开。

电子产品hub Favicon
<\/i>","library":""}}" data-widget_type="nav-menu.default">
Baidu