我想使用16位定时器写入应用程序以50Hz频率和1ms占空比控制PWM。这是我的代码。
#include "board.h"
#include "../../static/inc/ct.h"
#include <cr_section_macros.h>
#define PRESCALE_VAL 36000 /* f = 72MHz, output T = 0.5ms => output f = 2kHz, 72MHz / 2kHZ = 36000 */
#define TIMER LPC_TIMER16_0
#define SYSCTL LPC_SYSCTL
#define IOCON LPC_IOCON
#define PWM_GPIO_PORT 0 /* PWM output port */
#define PWM_GPIO_PIN 8 /* PWM output pin */
#define IOCON_FUNC_PWM IOCON_FUNC2
#define PWM_MATCH_CHANNEL 0
#define PWM_RESET_CHANNEL 3
int main(void) {
// Generic initialization
SystemCoreClockUpdate();
Board_Init();
IOCON->PIO0[PWM_GPIO_PIN] = IOCON_FUNC_PWM;
SYSCTL->SYSAHBCLKCTRL |= CT16B0_CLOCK_MASK;
Timer_Reset(TIMER);
TIMER->PR = PRESCALE_VAL - 1;
TIMER->MR[PWM_MATCH_CHANNEL] = 38; // 38 * 0.5 = 19ms
TIMER->MR[PWM_RESET_CHANNEL] = 40; // 40 * 0.5 = 20ms
TIMER->MCR |= TIMER_RESET_ON_MATCH(PWM_RESET_CHANNEL);
TIMER->EMR |= _BIT(PWM_MATCH_CHANNEL);
TIMER->PWMC |= _BIT(PWM_MATCH_CHANNEL) | _BIT(PWM_RESET_CHANNEL);
TIMER->TCR |= TIMER_ENABLE_MASK;
while(1) {}
return 0 ;
}
我使用连接到pwm引脚的逻辑状态分析器。结果我看到高状态需要1.5ms(?)而低状态需要19ms(这没关系)。我的问题是为什么占空比需要1.5ms而不是1ms?