nuvoton - PWM

PWM 的特性

  • clock frequency up to 144MHz
  • 16 bit resolution counter
    • up counting mode
    • down counting mode
    • up/down counting mode 
  • 2 PWM modules
    • Each module with 6 output channel
    • Each module with 3 complementary pair
  • Interrupt event
    • Zero
    • Period
    • Compared value
    • Brake condition happied
  • Capture Timer function
    • 12 capture input channels with 16 bit resolution
    • rising / falling capture
    • input rising / falling capture interrupt
    • rising / falling capture with counter 
    • PDMA transfer for PWM all channels

Working mode

  • independent mode architecture

  • complementary mode architecture

Counter mode 


Double buffering mode

  • PERIOD, CMPDAT, 和 FTCMPDAT 是由開發者設計,但寫入並不會立刻啟動.
  • PBUF, CMPBUF, 和 FTCMPBUF 硬體實際動作的值
  • 當週期完成後,才會載入開發者寫入的新值.
  • Counter loading mode
    • period loading in up-count mode
      • 預設的載入模式
      • 當 PWM Counter 上數到 PERIOD 或者下數到 0 時,會啟動載入
    • immediately loading
      • PWM channel n 的 IMMLDENn (PWM_CTL0[21:16]) = 1
      • 立刻載入, 當 PERIOD, CMPDAT and FTCMPDAT 被修改.
      • 當 PWM Counter 的數值大於寫入的值,會立刻回到啟點.
    • window loading
      • PWM channel n 的 IMMLDENn (PWM_CTL0[13:8]) = 1
      • 當週期結束,  PERIOD 的載入就會啟動.
      • CMPDAT 的載入需要 Load window 開啟, 並且計數結束.
    • center loading
      • PWM channel n 的 IMMLDENn (PWM_CTL0[5:0]) = 1
      • 當週期結束,  PERIOD 的載入就會啟動
      • CMPDAT 的載入在計數到中心點時載入.

PWM Pulse generation

  • PWM pulse generation 動作的條件
    • Zero point event
    • Period point event
    • center point event (上數計數模式)
    • PWM counter 符合 PWM compare value
  • PWM pulse generation 的動作 
    • do nothing (X)
    • set High (H)
    • set Low (L)
    • toggle (T)

PWM Output Mode

  • 支援 2 種輸出模式
    • Independent mode - 用於 DC motor system
    • Complementary mode - 用於 AC induction motor and permanent magnets synchronous motor.
    • Independent mode
      • 預設模式
      • Channel n 的 OUTMODEn (PWM_CTL1[26:24]) = 0
      • PWM_CH0 - PWM_CH5 使用獨立的週期(period) 和 百分比(duty).
    • Complementary mode
      • Channel n 的 OUTMODEn (PWM_CTL1[26:24]) = 1
      • PWM 的輸入變成一對, 例如 : PWM_CH1 是 PWM_CH0 的反向.

PWM Output Function

  • 支援 2 種輸出功能
    • Group function
    • Synchronous function
    • Group function
      • GROUPEN (PWM_CTL0[24]) = 1
      • 工作模式可以是 Independent 或 Complementary mode
      • 範例 : 所有偶數 PWM channel 由 PWM_CH0 控制, 所有奇數 PWM channel 由 PWM_CH1 控制. 
    • Synchronous function
      • 僅能工作在 Complementary mode
      • 當接收到 SYNC 輸入訊號, PWM 輸出就會回到設定值(比較值), 再依計數方向上數或下數.

PWM Output Control

在 PWM pulse generation 之後, 由 PWM output control 來控制輸出訊號.輸出控制可分為數個階段如下:
  • Independent mode
    • Mask
      • 用於 Electrically Commutated Motor (ECM) 的訊號控制
      • PWM Mask Enable Control Register MSKENn(PWM_MSKEN[5:0]) 選擇那幾個 channel 輸出訊號會被 Mask 掉.
      • PWM Masked Data Register MSKDATn(PWM_MSK[5:0]) 決定被 Mask PWM channel 的輸出是 High 還是 Low.
    • Brake
      • 支援兩個 external input 的 brake 功能
      • Brake function flow
        • edge detect brake source 
        • 產生 BRK_INT
        • BRKLSTSn (PWM_INTSTS1[29:24]) = 1
        • 當 brake 發生時, PWM 輸出保持為預設值
        • software clear state
    • Pin Polarity
      • 使用 PWM Negative Polarity Control Register (PWM_POLCTL) 來設定輸出的極性.
    • Output Enable
  • Complementary mode
    • Complementary channels
    • Dead-Time Insertion
      • 在 Complementary mode 可以使用 external 訊號來驗動, Dead-Time 的設計就如 de-boune 的功能一樣,檢測輸入訊號變化是否有持續 Dead-Time 以上,才會變更輸出訊號.
      • Dead-Time 由程式設計者設定
        • 啟動 channel n DTEN (PWM_DTCTLn[16]) =1 
        • 設定 Dead-Time 的長度於 DTCNT (PWM_DTCTLn[11:0])

PWM Interrupt generation

PWM 發生的中斷訊號有
  • Zero point Interrupt Flag ZIFn (PWM_INTSTS0[5:0])
  • Period point Interrupt Flag PIFn (PWM_INTSTS0[13:8])
  • Different Interrupt 
    • 當 PWM counter 和 PWM_CMPDATn 的值相同時觸發.
  • Up Interrupt Flag CMPUIFn (PWM_INTSTS0[21:16]) 
    • 當 PWM counter 上數到 Up-counter 設定值
  • Down Interrupt Flag CMPDIFn (PWM_INTSTS0[29:24]) 
    • 當 PWM counter 下數到 Down-counter 設定值 

PWM Code Flow

  • Enable Clock of PWM module
  • CLK_EnableModuleClock(PWM1_MODULE);
  • Select Clock Source of PWM module
  • CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0);
  • Configuration PWM1 GPIO and module reset
  • /* Set PC9~PC11 multi-function pins for PWM1 Channel0~2  */
    SYS->GPC_MFPH &= ~(SYS_GPC_MFPH_PC9MFP_Msk | SYS_GPC_MFPH_PC10MFP_Msk | SYS_GPC_MFPH_PC11MFP_Msk);
    SYS->GPC_MFPH |= SYS_GPC_MFPH_PC9MFP_PWM1_CH0 | SYS_GPC_MFPH_PC10MFP_PWM1_CH1 | SYS_GPC_MFPH_PC11MFP_PWM1_CH2;
    
    GPIO_SetMode(PC, BIT13, GPIO_MODE_INPUT); 
    SYS->GPC_MFPH &= ~SYS_GPC_MFPH_PC13MFP_Msk ;
    SYS->GPC_MFPH |= SYS_GPC_MFPH_PC13MFP_PWM1_CH4;
    
    /* Reset PWM1 channel 0~5 */
    SYS_ResetModule(PWM1_RST);
    
  • Write PWM1 CH0 output value
  • void writePWMDAC(unsigned char Enable, unsigned char ch0_dut)
    {
        /* set PWMB channel 0 output configuration */
        PWM_ConfigOutputChannel(PWM1, 4, 1000, ch0_dut);
    
        // Start PWM COUNT
        PWM_Start(PWM1, 1 << 4);
    
        if(Enable == 0)
            /* Enable PWM Output path for PWMB channel 0 */
            PWM_DisableOutput(PWM1, 1 << 4);
        else
            /* Diable PWM Output path for PWMB channel 0 */
            PWM_EnableOutput(PWM1, 1 << 4);
    }
範例程式 :
在 FreeRTOS 中,加入一個定時啟動的 Task.在 Task 的初始化時,同時初始化 ADC 和 PWM,把 PWM 的輸出接到 ADC 的輸入。在定時啟動的部分,調整 PWM 的輸出再讀取 ADC 的值。


範例程式位於 Github 的 nuvotoon-M451-Sample 的 FreeRTOS-PWM-Sample branch

Reference document :

  • nuvoton NuMicro-M451-Series-Training-Material
  • nuvoton TRM_M451_Series_EN_Rev2.08 

留言

這個網誌中的熱門文章

EC 所需知識 - SMBUS

EC 所需知識 - LPC

EC 所需知識 - KBC