CTIMER2 PWM LPC54018JBD208

cancel
Showing results for 
Search instead for 
Did you mean: 

CTIMER2 PWM LPC54018JBD208

Jump to solution
326 Views
Contributor III

Hi

Somebody could help me I'm using PWM on CTIMER2 but it doesn't work. I'm using this configuration:

#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_ctimer.h"
#include "pin_mux.h"
#include <stdbool.h>
/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define CTIMER CTIMER2                 /* Timer 1 */
#define CTIMER_MAT_OUT kCTIMER_Match_3 /* Match output 1 */
#define CTIMER_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk)
/*******************************************************************************
 * Prototypes
 ******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
volatile uint32_t g_pwmPeriod = 0U;
volatile uint32_t g_pulsePeriod = 0U;
/*******************************************************************************
 * Code
 ******************************************************************************/
status_t CTIMER_GetPwmPeriodValue(uint32_t pwmFreqHz, uint8_t dutyCyclePercent, uint32_t timerClock_Hz)
{
    /* Calculate PWM period match value */
    g_pwmPeriod = (timerClock_Hz / pwmFreqHz) - 1;
    /* Calculate pulse width match value */
    if (dutyCyclePercent == 0)
    {
        g_pulsePeriod = g_pwmPeriod + 1;
    }
    else
    {
        g_pulsePeriod = (g_pwmPeriod * (100 - dutyCyclePercent)) / 100;
    }
    return kStatus_Success;
}
/*!
 * @brief Main function
 */
int main(void)
{
    ctimer_config_t config;
    uint32_t srcClock_Hz;
    uint32_t timerClock;
   
    /* Init hardware*/
    /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
    CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
    /* Enable the asynchronous bridge */
    SYSCON->ASYNCAPBCTRL = 1;
    /* Use 12 MHz clock for some of the Ctimers */
    CLOCK_AttachClk(kFRO12M_to_ASYNC_APB);
    BOARD_InitPins();
    BOARD_BootClockFROHF96M();
    BOARD_InitDebugConsole();
    /* CTimer0 counter uses the AHB clock, some CTimer1 modules use the Aysnc clock */
    srcClock_Hz = CTIMER_CLK_FREQ;
    PRINTF("CTimer example to generate a PWM signal\r\n");
    CTIMER_GetDefaultConfig(&config);
    timerClock = srcClock_Hz / (config.prescale + 1);
    CTIMER_Init(CTIMER, &config);
    /* Get the PWM period match value and pulse width match value of 20Khz PWM signal with 20% dutycycle */
    CTIMER_GetPwmPeriodValue(20000, 20, timerClock);
    CTIMER_SetupPwmPeriod(CTIMER, CTIMER_MAT_OUT, g_pwmPeriod, g_pulsePeriod, false);
    CTIMER_StartTimer(CTIMER);
    while (1)
    {
    }
}
Which is the example code of simple_pwm
Using other CTIMER like CTIMER3 or CTIMER2 it works, I have to use CTIMER1, CTIMER2, and CTIMER3 for 9 PWMs. Any idea?
Best Regards
Labels (1)
0 Kudos
1 Solution
2 Views
NXP TechSupport
NXP TechSupport

Hi Oscar,

I saw that for the CTIMER2 you're using the MATCH3 register, in the reference manual 17.6.12 in the Table.340 there's 

the next note:

pastedImage_1.png

And also in the SDK, if you check the CTIMER_SetupPwmPeriod if the MATCH 3 is used the function return fail.

pastedImage_2.png

In the OM4006 there's no output pin for the PWM, so if you want to have more PWM, I suggest to check the SCTIMER. There's also an example about it in the SDK.

I hope this will help you.

Best Regards,

Alexis Andalon

View solution in original post

6 Replies
2 Views
NXP TechSupport
NXP TechSupport

Hi Oscar Niño,

Overall your code seems fine, but could you please share how are you configuring the pin?

Also are you using the OM40006?

If you plan to use the multiple channels of each timer as outputs to the pwm, you need to know that the channels using the same timer will have the same frequency, you can only change the duty cycle.

Best Regards,

Alexis Andalón

0 Kudos
2 Views
Contributor III

Hi Alexis

I'm using my own board but I got the OM40006 board too. I need to use 6 or 7 PWM outputs, I have use TIMER 1 Match 0,1,2 and Timer 3 Match 0, 1. Justo Timer 2 doesn't work.

Here how I configured pins:

Conf_Pins&#125;.jpg

 and:

CTIMER_GetPwmPeriodValue(20000, (duty_cycle_blue_SL*single_led_state), timerClock);
 CTIMER_SetupPwmPeriod(CTIMER1, kCTIMER_Match_0, g_pwmPeriod, g_pulsePeriod, false);
 CTIMER_StartTimer(CTIMER1);
 CTIMER_GetPwmPeriodValue(20000, (duty_cycle_green_SL*single_led_state), timerClock);
 CTIMER_SetupPwmPeriod(CTIMER1, kCTIMER_Match_1, g_pwmPeriod, g_pulsePeriod, false);
 CTIMER_StartTimer(CTIMER1);
 CTIMER_GetPwmPeriodValue(20000, (duty_cycle_red_SL*single_led_state), timerClock);
 CTIMER_SetupPwmPeriod(CTIMER1, kCTIMER_Match_2, g_pwmPeriod, g_pulsePeriod, false);
 CTIMER_StartTimer(CTIMER1);
 CTIMER_GetPwmPeriodValue(20000, duty_cycle_blue, timerClock);
 CTIMER_SetupPwmPeriod(CTIMER2, kCTIMER_Match_3, g_pwmPeriod, g_pulsePeriod, false);
 CTIMER_StartTimer(CTIMER2);
 CTIMER_GetPwmPeriodValue(20000, duty_cycle_red, timerClock);
 CTIMER_SetupPwmPeriod(CTIMER3, kCTIMER_Match_0, g_pwmPeriod, g_pulsePeriod, false);
 CTIMER_StartTimer(CTIMER3);
 CTIMER_GetPwmPeriodValue(20000, duty_cycle_green, timerClock);
 CTIMER_SetupPwmPeriod(CTIMER3, kCTIMER_Match_1, g_pwmPeriod, g_pulsePeriod, false);
 CTIMER_StartTimer(CTIMER3);
Thanks so much for your time.
Best Regards
Oscar Niño
0 Kudos
3 Views
NXP TechSupport
NXP TechSupport

Hi Oscar,

I saw that for the CTIMER2 you're using the MATCH3 register, in the reference manual 17.6.12 in the Table.340 there's 

the next note:

pastedImage_1.png

And also in the SDK, if you check the CTIMER_SetupPwmPeriod if the MATCH 3 is used the function return fail.

pastedImage_2.png

In the OM4006 there's no output pin for the PWM, so if you want to have more PWM, I suggest to check the SCTIMER. There's also an example about it in the SDK.

I hope this will help you.

Best Regards,

Alexis Andalon

View solution in original post

2 Views
Contributor III

Hi Alexis Andalon

Thank you so much, I didn't see that part of code,  so I supposse there si no way to use match 3 Chanel on my own board rigth? My interest is because that means we have to do a modification of PCB.

Best Regards

0 Kudos
2 Views
NXP TechSupport
NXP TechSupport

That's correct, the CTIMER_MATCH3 is used to store the PWM cycle, so it can't be used a a PWM ouput.

Best Regards,

Alexis Andalon

0 Kudos
2 Views
Contributor III

Thank you so much Alexis.

0 Kudos