【发布时间】:2014-07-18 04:50:34
【问题描述】:
我在使用 SAM3X8E(cortex-m3) 微控制器的 arduino due 板上使用 PWM。当我在这块板上使用 PWM 启用和禁用时,波形在禁用通道时变为模拟,而不是停留在 0 或 1。请参阅附件波形。我也通过直接写入寄存器来尝试代码,但它是一样的。我还在 pwm 输出端尝试了一个 4.7k 下拉电阻,但得到了相同的结果。请告诉我如何在软件中修复它。
如果硬件解决方案也可以使用一些外部组件,那也可以。附上范围图像。蓝色示波器输出用于第 34 行 (PC02),黄色用于第 35 行 (PC03)。
// include all arduino libraries here.. these are only accepted from ino files.
#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
void setup() {
// put your setup code here, to run once:
pmc_enable_periph_clk (PWM_INTERFACE_ID) ; // turn on clocking to PWM unit
PWMC_ConfigureChannel (PWM, 0, 1, 0, PWM_CMR_CPOL) ; // PWM channel 0, clock = MCLK/2 = 42MHz
PWMC_SetPeriod (PWM, 0, 700) ; // period = 700 pwm clocks (60kHz)
PWMC_SetDutyCycle (PWM, 0, 80*700/100) ; // duty set to 80%
PWMC_EnableChannel (PWM, 0) ; // enable
// Configure pin 34 (PC2) to be driven by peripheral B (PWM channel 0 L)
// enable pin PC02 and PC03, they are complimentary
PIOC->PIO_PDR = 0xC ; // disable PIO control
PIOC->PIO_IDR = 0xC ; // disable PIO interrupts
PIOC->PIO_ABSR |= 0xC ; // switch to B peripheral
}
void loop() {
// put your main code here, to run repeatedly:
// From these settings, I got these numbers from the scope - 13.3us on time and 3.32us off time
//
PWMC_EnableChannel (PWM, 0) ; // enable
delayMicroseconds(100);
PWMC_DisableChannel (PWM, 0) ; // enable
delayMicroseconds(100);
}
这里是范围图片:
【问题讨论】:
-
您确定示波器上的 CH1 和 CH2 都处于直流耦合状态?
-
是的,两个通道都处于直流耦合状态。
-
为什么不直接将 PWM 占空比设置为最大值或最小值而不是禁用?