【问题标题】:PIC (dsPIC33EP 512MU810) : High Speed PWM modulePIC (dsPIC33EP 512MU810):高速 PWM 模块
【发布时间】:2014-07-27 03:07:56
【问题描述】:
  • 我正在尝试使用内置(硬件)高速 PWM 生成 10% 占空比 - 1kHz PWM,时钟频率为 80Mhz (F_osc)。

  • 根据文档, 基期(PTPER 的数字)计算公式为

    PTPER = F_osc / [(F_pwm) x (PWM_Clk_Prescale)]

代入 F_osc = 80Mhz、F_pwm = 1khz 和 Prescale = 8,我得到的计数是 PTPER=10,000。

但是,从我在示波器上的输出中,我可以看到我的 PWM 频率约为 2khz。。

我做错了吗?下面是我的代码--

#include<p33EP512MU810.h>
#include<p33Exxxx.h>

//---------------- compiler directives------------------
_FOSCSEL(FNOSC_FRC);
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_XT);    // Clock Switching Enabled and Fail Safe Clock Monitor is disabled
                                                    // OSC2 Pin Function: OSC2 is Clock Output
                                                    // Primary Oscillator Mode: XT Crystal
_FWDT(FWDTEN_OFF);                                  // Watchdog Timer Enabled/disabled by user software
//------------------------------------------------------


//function to initialise oscillator and set up clock
void Init_Oscillator(void)
{
   // Configure Oscillator to operate the device at 40Mhz
    // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
    // Fosc= 12M*40/(3*2)=80Mhz for 12M input clock
    // 0.5M < Fin/N1 < 8MHz
    // 100M < Fin*M/N2 < 200M
    // Fcy  = Fosc / 2 = 40 MIPS
    PLLFBD       = 38;  // M=80
    CLKDIVbits.PLLPOST = 0;   // N2=2
    CLKDIVbits.PLLPRE  = 1;   // N1=3
    OSCTUN             = 0;   // Tune FRC oscillator, if FRC is used
    RCONbits.SWDTEN=0;     // Disable Watch Dog Timer

    // Clock switch to incorporate PLL
    __builtin_write_OSCCONH(0x03);              // Initiate Clock Switch to
                                                                                            // Primary Oscillator with PLL (NOSC=0b011)
    __builtin_write_OSCCONL(0x01);              // Start clock switching
    while (OSCCONbits.COSC != 0b011);           // Wait for Clock switch (to XT w/ PLL) to occur
    while(OSCCONbits.LOCK!=1) {}; // Wait for PLL to lock
}

void Init_PWM(void)
{
    /**** PTCON: PWM Time Base Control Register ****/
    PTCONbits.PTEN   = 0;   // Timer Enable bit:    DISABLE MCPWM

        //---------------------------------------------------------------
        //Prescaler 1:8
        PTCON2bits.PCLKDIV = 0b011; // PCLKSEL: 1,2,4,8,16,32,64
        //mode selection
        PWMCON1 = 0x0000;          //PTPER holds period count of PWM
        //Independent mode enable
        IOCON1 = 0xCC00; //fig 14-35 of PWM manual
        // 0xCC00 => PENH high, PENL high, PMOD: 11- True Independent PWM o/p

        //---------------------------------------------------------------
        /**** PTPER: PWM Time Base Period Register ****/
    PTPER = 10000; // Period Value bits
        //Enable PWM timer
        PTCONbits.PTEN   = 1;   // Timer Enable bit:    ENABLE MCPWM
}
//--- extra settings:
// 1) Stop in IDLE MODE :: PTCONbits.PTSIDL = 1 (yes)
// 2) Setting a particular pin as output ==> IOCONxbits.PENH(/L) = 1(enabled)
// 3) MDC,PDC,SDC et al depend on the PWM MODE.
//    Resp, selection of PTPER,PHASE,SPHASE.
//---


int main(void)
{
        Init_Oscillator();
    Init_PWM();// Initialize PWM module
        PDC1 = 1000; // This sets the duty cycle.
    while(1); //loop forever //while

return 0;
}

【问题讨论】:

    标签: embedded microcontroller pic pwm


    【解决方案1】:

    尽管有你的cmets,我相信你有

    PLLDIV = 78 // M = 80
    

    所以,

    FOSC = FIN * PLLDIV / ((PLLPRE + 2) * 2 * (PLLPOST + 1))
    
    FOSC = 8MHz * 80 / ((1 + 2) * 2 * (0 + 1))
    
    FOSC = 8MHz * 80 / 6
    
    FOSC = ~ 107 MHz
    

    【讨论】:

    • 您好 Doug,感谢您指出错误的 cmets(它们来自我编写的另一个代码)。我在 cmets 中进行了必要的编辑。但是,“F_osc = (F_in * M)/ (N1*N2)”和我的值,我得到 F_osc = 80MHz。我已经尝试对带有 OC(输出比较)模块的 PWM 使用相同的配置,并且在那里得到了正确的波形。我想知道这里是否还有其他错误。
    • 啊!我刚刚得到错误。让我试着纠正它。谢谢!
    【解决方案2】:

    您的 80MHZ 的 pwm 配置是正确的,但请确保您的输入频率为 12MHZ ...我已经使用相同的 PWM 配置测试了 80mhz 的 pwm,并且它会根据您需要 1KHZ 生成正确的输出。所以检查你的 PLL 和 Fcrystal 配置。我认为您正在生成 pll 160MHZ。如果您使用的是 M=80,那么您必须将其替换为 M=40 (PLLFBD = 38;),更正您的评论或代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多