【问题标题】:PWM Issue with arduino due (cortex-m3)由于 arduino 的 PWM 问题(cortex-m3)
【发布时间】: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 占空比设置为最大值或最小值而不是禁用?

标签: arduino arm cortex-m3


【解决方案1】:

禁用频道需要一些时间。 禁用后尝试添加等待循环:

void loop() {
  PWMC_EnableChannel (PWM, 0) ;   // enable
  delayMicroseconds(100);
  PWMC_DisableChannel (PWM, 0) ;   // disable
  while ((PWM->PWMC_SR & 1) != 0); //add this
  delayMicroseconds(100);
}

【讨论】:

  • 我试着按照你说的检查状态寄存器,我得到了相同的波形。延迟是多余的。即使没有任何延迟,它也可以正常工作。根据文档,在 pwm disable 完成后,它应该完成该循环并停止。这就是这里正在发生的事情,但是当它结束时,输出不是像石头一样下降,而是开始下降。输出接近 0v 需要一个很好的毫秒。
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多