LPC824: Simultaneous Timer and PWM

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC824: Simultaneous Timer and PWM

Jump to solution
2,267 Views
tiagofernandes
Contributor III

In the LPC824 micro is possible using at the same time a SCT_Timer and SCT PWM? I ask this because I need to have one Timer (with a interrupt at each 10ms) and two PWM outputs independent among them and running in the same program.

 

Best regards,

Tiago

Labels (2)
Tags (4)
0 Kudos
1 Solution
1,276 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Tiago,

I'm afraid the kind of demand beyond the LPC824 which only has one SCTtimer module.

I'm sorry for misunderstanding your question.

I'd like to suggest that you can LPC11E6x MCU which contains the two SCTtimer modules.

And you can learn the more information about them through the link below.

http://cache.nxp.com/documents/user_manual/UM10732.pdf?fpsp=1&WT_TYPE=Users%20Guides&WT_VENDOR=FREES...
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
11 Replies
1,276 Views
tiagofernandes
Contributor III

Hello,

Thanks for the help and sorry for the inconvenience.

Best Regards,

Tiago

0 Kudos
1,276 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Tiago,

After investigate deeper, I'd like to suggest you to follow the approach.

1. At first, Clear the UNIFY bit to focus the SCT operates as two 16-bit counters.

2. On counter is used to generate the periodical interruption. Another counters is used to generate the two PWMs.

3. The other SCT registers configurations are almost as the same as the previous.

As the 16-bit counters are independent, the periodical interruption operation will not disturb the PWM outputs.

Hope it helps.
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,276 Views
tiagofernandes
Contributor III

Hello Jeremy,

With the configuration that you presented, I'm capable of have Timer and 2PWM, but this 2 PWM can have a different frequencies? I have tried and testing with successful results the example where I have 2 PWM independet each other (differents frequencies and duty cycle) (example PWM at _L and _H of the AN11538), but what I noticed was that I need one timer to each PWM that have different frequencies (I know, and proved in tests, that I can have multiple PWM with the same frequency and different duty Cycle with the same timer). That's why I do the question of, is possible with your configuration have 1 timer, and 2 PWMs (first PWM with frequency X and second PWM with frequency Y) using the SCT register?

Best Regards,

Tiago

0 Kudos
1,277 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Tiago,

I'm afraid the kind of demand beyond the LPC824 which only has one SCTtimer module.

I'm sorry for misunderstanding your question.

I'd like to suggest that you can LPC11E6x MCU which contains the two SCTtimer modules.

And you can learn the more information about them through the link below.

http://cache.nxp.com/documents/user_manual/UM10732.pdf?fpsp=1&WT_TYPE=Users%20Guides&WT_VENDOR=FREES...
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,276 Views
tiagofernandes
Contributor III

Hello,

No problem for the misunderstanding. I know that the Systick is now exactly a timer like the SCTimer/PWM, but can be used like one right? The precision of this Systick is good as SCT or is precision will diversify (what is bad)?

Best regards,

Tiago

0 Kudos
1,276 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Tiago,

My Gosh, It's great to choose the Systick timer to generate the periodic interrupt, and we usually use the Systick to set the precision of delay time.

I think your approach can work it out.
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,276 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Tiago,

Definitely, the LPC824 can work it out.

And you can learn the more information about the SCT_Timer by though the link as below.

cache.nxp.com/documents/application_note/AN11538.zip?fpsp=1&WT_TYPE=Application%20Notes&WT_VENDOR=FREESCALE&WT_FILE_FORMAT=zip&WT_ASSET=Documentation&fileExt=.zip


Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,276 Views
tiagofernandes
Contributor III

Hello,

Thanks for answering to my question. When I configure the SCT for timer, and following I configure to PWM, the first configuration(timer) will not be changed? I saw all the examples that you send in that link, but I think that will change the configuration of the SCT Timer when I config the PWM. Do I need to set differents SCT? if yes how can I do that?

Best regards,

Tiago

0 Kudos
1,276 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Tiago,

The SCT has the several match registers, event and state mechanism , it can make the SCT perform some complicated timer operations.

So I was wondering if you can tell the exactly trouble you stuck in right now, it can accelerate the investigation.

I'm looking forward to your reply.
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,276 Views
tiagofernandes
Contributor III

Hello,

I tried the following code in the LPCxpresso824 board, and the signal of the PWM (pins 12 and 16) is alright, however the signal of the timer (pin 27) is always high when what I want is toogle the output. I tryied with diferent match, diferent events the result is always the same, the pwm works the timer not. I following by the examples in the application note that are in the previous link of this conversation. What I'm doing wrong?

Code_main function:

Config_inputs_outputs();

//timer

Chip_SCT_Init(LPC_SCT);

Chip_SCT_Config(LPC_SCT, SCT_CONFIG_32BIT_COUNTER ); /* UNIFIED */

Chip_SCT_SetMatchCount(LPC_SCT, SCT_MATCH_1, (SystemCoreClock/100)); /* match_1_ */

Chip_SCT_SetMatchReload(LPC_SCT, SCT_MATCH_1, (SystemCoreClock/100));

  /* EVENT registers */

Chip_SCT_EventState(LPC_SCT, SCT_EVENT_0, ENABLE_STATE0); /* U: --> state U_ENTRY */

Chip_SCT_EventControl(LPC_SCT, SCT_EVENT_0, (CHIP_SCT_EVENTCTRL_T)  ( SCT_EVECTRL_MATCH1 | SCT_COMBMODE_MATCH | SCT_STATELD_1    | SCT_STATEEV_0    ));

Chip_SCT_EnableEventInt(LPC_SCT, SCT_EVT_0);

LPC_SCT->LIMIT_L = 0x00000001;

NVIC_EnableIRQ(SCT_IRQn); /* enable SCT interrupt  */

Chip_SCT_ClearControl(LPC_SCT , SCT_CTRL_HALT_L);

//PWM

  /* Initialize the SCT as PWM and set frequency */

Chip_SCTPWM_Init(LPC_SCT);

Chip_SCTPWM_SetRate(LPC_SCT, 10000); /* PWM frequency 10 KHz */

  /* Setup Board specific output pin */

  /* Enable SWM clock before altering SWM */

Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);

     /* Connect SCT output 0 to LED pin PIO12, SCT output 1 to LED pin PIO16 */

Chip_SWM_MovablePinAssign(SWM_SCT_OUT1_O, 12);

Chip_SWM_MovablePinAssign(SWM_SCT_OUT0_O, 16);

Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);

/* Use SCT0_OUT1 pin */

Chip_SCTPWM_SetOutPin(LPC_SCT, 1, 1);

Chip_SCTPWM_SetOutPin(LPC_SCT, 2, 0);

  /* Start with 0% duty cycle */

Chip_SCTPWM_SetDutyCycle(LPC_SCT, 1, Chip_SCTPWM_GetTicksPerCycle(LPC_SCT) / 2);

Chip_SCTPWM_SetDutyCycle(LPC_SCT, 2, 0);

Chip_SCTPWM_Start(LPC_SCT);

IRQ_function()

void SCT_IRQHandler (void)

{                                                

/* SCT Interrupt Handler (10 msec) */

  static uint8_t cnt = 0;

       Chip_GPIO_SetPinToggle(LPC_GPIO_PORT,0, 27);     /* toggle pin P0_27                */

    Chip_SCT_ClearEventFlag(LPC_SCT, SCT_EVT_0); /* clear event 0 flag             */

}

0 Kudos
1,276 Views
tiagofernandes
Contributor III

Hello,

I have running more tests and I realize that in the previous code I have overlapping of match and events, so I update my code to the following. Even with this code I have a problem because if I have the line code "Chip_SCT_Config(LPC_SCT, SCT_CONFIG_32BIT_COUNTER | SCT_CONFIG_AUTOLIMIT_L);" the PWM works right and the timer not, but if I have the code line "Chip_SCT_Config(LPC_SCT, SCT_CONFIG_32BIT_COUNTER);" the timer works fine and the PWM doesn't work. How can I reconcile Timer and PWM? What I'm doing wrong?

Other question, is possible have 2 pwm with 2 different frequencies? For this I just need to have one match to each frequencies and 1 match to each duty cycle? Or I can use the example PWM at _L and _H of the AN11538 version 5.0? If the solution pass for this last example I have a doubt, the code line "LPC_SCT->MATCH[0].H = PWM1_PERIOD - 1; // match 0 @ PWM0 freq" presented in High part of the SCT is refered to the PWM1 or 0. I ask this because the comment talk about PWM0 but are configurate the PWM1.

//*****timer + pwm********
Chip_SCT_Init(LPC_SCT);

//--------pwm---------
rate = Chip_Clock_GetSystemClockRate() / 10000;;
/* Stop the SCT before configuration */
/* Stop SCT */
Chip_SCT_SetControl(LPC_SCT, SCT_CTRL_HALT_L | SCT_CTRL_HALT_H);
/* Clear the counter */
Chip_SCT_SetControl(LPC_SCT, SCT_CTRL_CLRCTR_L | SCT_CTRL_CLRCTR_H);

/* Set MATCH0 for max limit */
LPC_SCT->REGMODE = 0;
Chip_SCT_SetMatchCount(LPC_SCT, SCT_MATCH_0, 0);
Chip_SCT_SetMatchReload(LPC_SCT, SCT_MATCH_0, rate);
LPC_SCT->EVENT[0].CTRL = 1 << 12;
LPC_SCT->EVENT[0].STATE = 1;

/* Set SCT Counter to count 32-bits and reset to 0 after reaching MATCH0 */
//Chip_SCT_Config(LPC_SCT, SCT_CONFIG_32BIT_COUNTER | SCT_CONFIG_AUTOLIMIT_L);
Chip_SCT_Config(LPC_SCT, SCT_CONFIG_32BIT_COUNTER);

//-------timer---------
Chip_SCT_SetMatchCount(LPC_SCT, SCT_MATCH_4, (SystemCoreClock/100));/* match_4_ */
Chip_SCT_SetMatchReload(LPC_SCT, SCT_MATCH_4, (SystemCoreClock/100));
/* EVENT registers */
Chip_SCT_EventState(LPC_SCT, SCT_EVENT_4, ENABLE_STATE0);/* U: --> state U_ENTRY */
Chip_SCT_EventControl(LPC_SCT, SCT_EVENT_4, (CHIP_SCT_EVENTCTRL_T)  ( SCT_EVECTRL_MATCH4|
SCT_COMBMODE_MATCH|
SCT_STATELD_1|
SCT_STATEEV_0));

Chip_SCT_EnableEventInt(LPC_SCT, SCT_EVT_4);
LPC_SCT->LIMIT_L = 0x10;

Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
/* Connect SCT output 0 to LED pin PIO12, SCT output 1 to LED pin PIO16 */
Chip_SWM_MovablePinAssign(SWM_SCT_OUT1_O, 12);
Chip_SWM_MovablePinAssign(SWM_SCT_OUT0_O, 16);
Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);

/* Use SCT0_OUT1 pin */
Chip_SCTPWM_SetOutPin(LPC_SCT, 1, 1);
Chip_SCTPWM_SetOutPin(LPC_SCT, 2, 0);

NVIC_EnableIRQ(SCT_IRQn);/* enable SCT interrupt  */

//----set duty cycle----
Chip_SCT_SetMatchReload(LPC_SCT, 1, Chip_SCTPWM_PercentageToTicks(LPC_SCT, 50));//duty cycle=50%
Chip_SCT_SetMatchReload(LPC_SCT, 2, Chip_SCTPWM_PercentageToTicks(LPC_SCT, 90));//duty cycle=90%

Chip_SCT_ClearControl(LPC_SCT , SCT_CTRL_HALT_L | SCT_CTRL_HALT_H);

Best Regards,

Tiago

0 Kudos