Pulse Width Modulator

smallcracker 2021-01-29 00:00:00
Categories: Tags:

PWM

概述

Each instance of a Tiva PWM module provides up to four instances of a PWM generator block, and an output control block. Each generator block has two PWM output signals, which can be operated independently or as a pair of signals with dead band delays inserted. Each generator block also has an interrupt output and a trigger output. The control block determines the polarity of the PWM signals and which signals are passed through to the pins.

每个PWM提供了最多4个PWM发生器和一个输出控制模块,每一个发生器能产生两个PWM输出信号,可以独立操作,也可以作为一对插入了死区延迟的信号运行。每一个生成器块还有一个中断输出和一个触发输出,控制块确定PWM信号的极性以及哪些信号传递到引脚。

Some of the features of the Tiva PWM module are:
Up to four generator blocks, each containing
• One 16-bit down or up/down counter
• Two comparators
• PWM generator
• Dead band generator

至多4个产生模块,每一个产生模块包括

一个十六位的向上/向下计数器

两个比较器

PWM产生器

死区产生器

Control block
• PWM output enable
• Output polarity control
• Synchronization
• Fault handling
• Interrupt status

控制模块有

PWM输出使能

输出极性控制

错误处理

中断状态

This driver is contained in driverlib/pwm.c, with driverlib/pwm.h containing the API declarations for use by applications.

细节描述

These functions perform high-level operations on PWM modules.

这些模块表现了高水平的对PWM的操作

The following functions provide the user with a way to configure the PWM for the most common operations, such as setting the period, generating left- and center-aligned pulses, modifying the pulse width, and controlling interrupts, triggers, and output characteristics. However, the PWM module is very versatile and can be configured in a number of different ways, many of which are beyond the scope of this API. In order to fully exploit the many features of the PWM module, users are advised to use register access macros.

接下来的函数为用户提供了一种配置PWM做最普通操作的方式,这些操作有设定周期,产生左对齐/中心对齐的脉冲,调节脉冲宽度,控制中断/触发/输出特征等功能。然而,PWM模块非常的强大,可以用很多种方式设置,许多用的都不是这些API。为了能充分利用PWM模块的诸多功能,我们推荐用户使用寄存器。

When discussing the various components of a PWM module, this API uses the following labeling convention:

The generator blocks are called Gen0, Gen1, Gen2 and Gen3.

产生器模块被称为Gen0, Gen1, Gen2 and Gen3.

The two PWM output signals associated with each generator block are called OutA and OutB.

与产生器模块相关的两个PWM输出信号被称为OutA和OutB

The output signals are called PWM0, PWM1, PWM2, PWM3, PWM4, PWM5, PWM6 and PWM7.

输出信号被称为PWM0, PWM1, PWM2, PWM3, PWM4, PWM5, PWM6 and PWM7.

PWM0 and PWM1 are associated with Gen0, PWM2 and PWM3 are associated with Gen1, PWM4 and PWM5 are associated with Gen2 and PWM6 and PWM7 are associated with Gen3.

PWM0和PWM1与Gen0相关联,PWM2和PWM3与Gen1相关联,PWM4和PWM5与Gen2相关联,PWM6和PWM7与Gen3相关联。

Also, as a simplifying assumption for this API, comparator A for each generator block is used exclusively to adjust the pulse width of the even numbered PWM outputs (PWM0, PWM2, PWM4 and PWM6). In addition, comparator B is used exclusively for the odd numbered PWM outputs (PWM1, PWM3, PWM5 and PWM7).
Note that the number of generators and PWM outputs supported varies depending upon the Tiva part in use. Please consult the datasheet for the part you are using to determine whether it supports 1 or 2 modules with 3 or 4 generators each and 6 or 8 outputs each.

另外,作为对此API的简化假设,每个生成器模块的比较器仅用于调整偶数个PWM输出的脉冲高度,比较器A仅用于计数PWM输出

请注意,支持的生成器和 PWM 输出的数量因使用的 Tiva 部件而异。请参阅您使用的部件的数据表,以确定它是否支持 1 个或 2 个模块,每个模块有 3 个或 4 个生成器,每个模块有 6 个或 8 个输出。

我们这款单片机包括两个PWM模块,总共有16个输出

这次我们直接看例程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// Enable the PWM0 peripheral
// 使能PWM外设
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
//
// Wait for the PWM0 module to be ready.
// 等待PWM模块准备好
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_PWM0))
{
}
//
// Configure the PWM generator for count down mode with immediate updates
// to the parameters.
// 配置PWM产生器,并设置器为向下计数模式,和非同步模式
//
PWMGenConfigure(PWM_BASE, PWM_GEN_0,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
//
// Set the period. For a 50 KHz frequency, the period = 1/50,000, or 20
// microseconds. For a 20 MHz clock, this translates to 400 clock ticks.
// Use this value to set the period.
// 设置周期。
//
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, 400);
//
// Set the pulse width of PWM0 for a 25% duty cycle.
// 脉冲宽度设置
//
PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 100);
//
//
// Set the pulse width of PWM1 for a 75% duty cycle.
//
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 300);
//
// Start the timers in generator 0.
//
PWMGenEnable(PWM_BASE, PWM_GEN_0);
//
// Enable the outputs.
//
PWMOutputState(PWM_BASE, (PWM_OUT_0_BIT | PWM_OUT_1_BIT), true);

关注如下函数

PWMGenConfigure

用于配置PWM

This function is used to set the mode of operation for a PWM generator. The counting mode, synchronization mode, and debug behavior are all configured. After configuration, the generator is left in the disabled state.

这个函数用于设置PWM产生器的操作模式。计数模式,同步模式,调试模式都在这里设置。设置之后,产生器处于关闭状态。

A PWM generator can count in two different modes: count down mode or count up/down mode. In count down mode, it counts from a value down to zero, and then resets to the preset value, producing left-aligned PWM signals (that is, the rising edge of the two PWM signals produced by the generator occur at the same time). In count up/down mode, it counts up from zero to the preset value, counts back down to zero, and then repeats the process, producing centeraligned PWM signals (that is, the middle of the high/low period of the PWM signals produced by the generator occurs at the same time).

PWM产生器可以用两种计数模式计数,向下计数模式和向上/向下计数模式。

在向下计数模式中,它从一个值向下数到零,然后恢复到预设值,产生一个左对齐的PWM信号(也就是说,两个产生器产生的信号的升边同时出现)在向上向下计数模式中,它从零数到预设值,再数回到零,反复这个过程,产生的是中心对齐的PWM信号(也就是说,产生器产生的PWM信号的周期的中间时刻是同时发生的)每个生成器不是能产生两个PWM信号嘛

When the PWM generator parameters (period and pulse width) are modified, their effect on the output PWM signals can be delayed. In synchronous mode, the parameter updates are not applied until a synchronization event occurs. This mode allows multiple parameters to be modified and take effect simultaneously, instead of one at a time. Additionally, parameters to multiple PWM generators in synchronous mode can be updated simultaneously, allowing them to be treated as if they were a unified generator. In non-synchronous mode, the parameter updates are not delayed until a synchronization event. In either mode, the parameter updates only occur when the counter is at zero to help prevent oddly formed PWM signals during the update (that is, a PWM pulse that is too short or too long).

当PWM脉冲产生器的参数(周期和脉冲宽度)被修改时,它们对PWM信号的影响可能会延迟。

在同步模式下,直到发生同步时间时应用参数才被更新。此模式允许多个参数同时修改并生效,而不是一次一个参数。此外,同步模式下多个 PWM 生成器的参数可以同时更新,从而可以将它们视为统一生成器。

在非同步模式下,参数 更新不会延迟到同步事件,在任意模式下,参数更新仅在计数器为零时在会发生,用以防止更新期间产生奇怪的PWM信号。

The PWM generator can either pause or continue running when the processor is stopped via the debugger. If configured to pause, it continues to count until it reaches zero, at which point it pauses until the processor is restarted. If configured to continue running, it keeps counting as if nothing had happened.

当调试器使处理器停止时,PWM产生器可以停止/继续运行如果配置为暂停,则它将继续计数,直到达到零,此时它会暂停,直到重新启动处理器。如果配置为继续运行,则继续计数,就像什么都没发生一样。

image-20201212211525865

这是这个函数的参数,第一部分是PWM模块的基址,第二部分是PWM产生器,究竟是GEN几,最后是配置参数。

那么有哪些参数呢?

PWM_GEN_MODE_DOWN or PWM_GEN_MODE_UP_DOWN to specify the counting
mode

计数模式,就是向上模式还是向上向下模式

PWM_GEN_MODE_SYNC or PWM_GEN_MODE_NO_SYNC to specify the counter load
and comparator update synchronization mode

不晓得

PWM_GEN_MODE_DBG_RUN or PWM_GEN_MODE_DBG_STOP to specify the debug
behavior

配置了debug的行为

PWM_GEN_MODE_GEN_NO_SYNC, PWM_GEN_MODE_GEN_SYNC_LOCAL, or
PWM_GEN_MODE_GEN_SYNC_GLOBAL to specify the update synchronization mode
for generator counting mode changes

不晓得

PWM_GEN_MODE_DB_NO_SYNC, PWM_GEN_MODE_DB_SYNC_LOCAL, or
PWM_GEN_MODE_DB_SYNC_GLOBAL to specify the deadband parameter synchronization
mode

指定死区参数同步模式

PWM_GEN_MODE_FAULT_LATCHED or PWM_GEN_MODE_FAULT_UNLATCHED to
specify whether fault conditions are latched or not

指定是否锁定故障条件

PWM_GEN_MODE_FAULT_MINPER or PWM_GEN_MODE_FAULT_NO_MINPER to
specify whether minimum fault period support is required

确定是否需要最小错误周期支持

PWM_GEN_MODE_FAULT_EXT or PWM_GEN_MODE_FAULT_LEGACY to specify
whether extended fault source selection support is enabled or not

指定是否启用扩展故障源选择支持