我在使用SAM3X8E(cortex-m3)微控制器的arduino应用板上使用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);
}
这里是答案范围图片:
答案 0 :(得分:0)
禁用频道需要一些时间。 尝试在禁用后添加等待循环:
void loop() {
PWMC_EnableChannel (PWM, 0) ; // enable
delayMicroseconds(100);
PWMC_DisableChannel (PWM, 0) ; // disable
while ((PWM->PWMC_SR & 1) != 0); //add this
delayMicroseconds(100);
}