micros & millis

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

micros & millis

1,263 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bobi-one on Wed Nov 12 01:18:48 MST 2014
Have somebody implemented microseconds counter with the timers available on lpc1549.
I tried with the systick timer  but i am not sure if it can get under 1 ms.
Regards,
Boyko
Labels (1)
0 Kudos
9 Replies

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bobi-one on Mon Nov 17 09:16:00 MST 2014
Thanks R2 :D
0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Nov 17 08:36:31 MST 2014

Quote: bobi-one
Am I missing something?



:quest:

Could be useful to define an event and start the counter 

volatile uint32_t counter;

#ifdef __cplusplus
extern "C" {
#endif
void SCT0_IRQHandler(void)
{
 if (LPC_SCT0->EVFLAG & SCT_EVT_0)
 {
  LPC_SCT0->EVFLAG = SCT_EVT_0;
  Board_LED_Toggle(0);
  counter++;
 }
}
#ifdef __cplusplus
}
#endif


//main------------------------------------------------------------------------
int main(void)
{
 SystemCoreClockUpdate();
 Board_Init();

#define TICKRATE_MHZ (1000)
 Chip_SCT_Init(LPC_SCT0); /* The match/capture REGMODE defaults to match mode */
 Chip_SCT_Config(LPC_SCT0, (SCT_CONFIG_32BIT_COUNTER | SCT_CONFIG_CLKMODE_BUSCLK)); /* Configure the SCT as a 32bit counter using the bus clock */
 Chip_SCT_SetMatchCount(LPC_SCT0, SCT_MATCH_0, (SystemCoreClock/TICKRATE_MHZ)-1); /* Set the match count for match register 0 */
 Chip_SCT_SetMatchReload(LPC_SCT0, SCT_MATCH_0,(SystemCoreClock/TICKRATE_MHZ)-1); /* Set the match reload value */

 [color=#f00]LPC_SCT0->EVENT[0].STATE  = (SCT_EVT_0);// events
 LPC_SCT0->EVENT[0].CTRL = (0 << 0)  |  // use match register
                           (1 << 12) |  // COMBMODE[13:12] = match condition
                           (1 << 14) |  // STATELD[14]   = STATEV is loaded
                           (0 << 15);   // STATEV[19:15] = new state
 LPC_SCT0->LIMIT = (1<<0);[/color]
 Chip_SCT_EnableEventInt(LPC_SCT0, SCT_EVT_0); /* Enable an Interrupt on the Match Event */
 NVIC_EnableIRQ(SCT0_IRQn);
 [color=#f00]Chip_SCT_ClearControl(LPC_SCT0, SCT_CTRL_HALT_L | SCT_CTRL_HALT_H);//and start[/color]

 while(1)
 {
  //do something useful here....
 }
}

0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bobi-one on Mon Nov 17 03:10:09 MST 2014
I tried this code, but unfortunetly it never gets to the interrupt handler. Am I missing something?

void SCT0_IRQHandler(void)
{

Chip_GPIO_SetPinState(LPC_GPIO,0,BTN1_PIN,OFF);

/* Clear the Interrupt */
//Chip_SCT_ClearEventFlag(LPC_SCT0, SCT_EVT_0);
 LPC_SCT0->EVFLAG = SCT_EVT_0;
}

#define TICKRATE_MHZ (1000)
Chip_SCT_Init(LPC_SCT0); /* The match/capture REGMODE defaults to match mode */
Chip_SCT_Config(LPC_SCT0, ( SCT_CONFIG_32BIT_COUNTER | SCT_CONFIG_CLKMODE_BUSCLK)); /* Configure the SCT as a 32bit counter using the bus clock */
Chip_SCT_SetMatchCount(LPC_SCT0, SCT_MATCH_0, SystemCoreClock / TICKRATE_MHZ); /* Set the match count for match register 0 */
Chip_SCT_SetMatchReload(LPC_SCT0, SCT_MATCH_0, SystemCoreClock / TICKRATE_MHZ); /* Set the match reload value */
Chip_SCT_EnableEventInt(LPC_SCT0, SCT_EVT_0); /* Enable an Interrupt on the Match Event */

    /* Enable the IRQ for the SCT */
NVIC_EnableIRQ(SCT0_IRQn);

0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MarcVonWindscooting on Thu Nov 13 08:56:40 MST 2014
Hi Bobi,

here's my source code for the LPC810. I've never used LPCOPEN, but you'll easily figure out, how to use the SCT for this purpose. I haven't worked with LPC15 so far, so you'll have to make sure SCT is clocked (SYSAHBCLKCTRL on LPC800).
The rest  is quite simple, see usTimerInit().
Gettting the time (us) is usTimerUs().

The (hopefully descriptive) constants used can be found in sct.h's enums.

One thing may differ from your environment: I write SCT.someField while you probably will have to write SCT->someOtherField.
That's because I define SCT as a variable (not a pointer) in crt0.s . That's not the typical way. It's because I HATE that pointer syntax (and #define,  too). You should be able to port it to your environment/uC easily.

Hope this is helpful to you....
0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bobi-one on Thu Nov 13 04:00:24 MST 2014

Quote: 1234567890
Not with the Systick timer, because it wasn't neccessary for me yet.
I use StopWatch_DelayUS() from LPCOpen instead which uses the RIT timer.
Have a look at the documentation in the package for LPC15xx:
http://www.lpcware.com/system/files/lpcopen_2_08c_docs_15xx.zip


Unfortunately I am using the RIT timer for one other task.


Quote: MarcVonWindscooting
What's your aim?
1) Being able to measure us or sub-us delays? That enables you to create delays in that range easily, too. For this, I would use one SCT with 32bit counter, just free running. Reading out the timer value is simply looking at the clock (of PCLKs). Use 32bit math and you're fine.
.


I forgot about the SCT :D because i have only used the example for the PWM. I need only microseconds counting ( will not use it for delay)
Can you share the code how you use the SCT as a normal 32bit timer with the LPCOPEN? ( I am not sure if all the functions for it are in the library).
Regards,
Bobi
0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MarcVonWindscooting on Wed Nov 12 16:33:03 MST 2014
What's your aim?
1) Being able to measure us or sub-us delays? That enables you to create delays in that range easily, too. For this, I would use one SCT with 32bit counter, just free running. Reading out the timer value is simply looking at the clock (of PCLKs). Use 32bit math and you're fine.

2) If you want exactly 1us per tick (in some special register, NOT one or even multiple variables in RAM incremented) I would try to use one SCT split into 2 16-bit counters and use one part as a pre-scaler and the other as the clock counter. Use 16bit math here and you're fine.

A very special case of course could be a clock of 1 < < n MHz, using approach 1) but dividing by > > n and applying special overflow handling when calculating differences t1-t0 .

Just theory, I find myself doing timings with MRT and SYSTICK instead of the simple approach with T0.TC+T0.PR on the 'old' uCs. ARM had better includes a prescaler into SYSTICK, really. Saved a few gates at the expense of umpteen words of code  :~

EDIT: forget about 2) !
There [color=#f00]is[/color] a pre-scaler in the SCT and it provides enough range (1..256) to scale to 1 MHz (1us). I'm working on something very similar for a LPC810 right now.
0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by 1234567890 on Wed Nov 12 11:35:22 MST 2014
Not with the Systick timer, because it wasn't neccessary for me yet.
I use StopWatch_DelayUS() from LPCOpen instead which uses the RIT timer.
Have a look at the documentation in the package for LPC15xx:
http://www.lpcware.com/system/files/lpcopen_2_08c_docs_15xx.zip
0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sandy_493 on Wed Nov 12 04:42:07 MST 2014
Hello bobi,

Here is the sample code that i have implemented to generate 1 msec using systick interrupt.

sysTickRate = Chip_Clock_GetSysTickClockRate();

// sysTickRate  = 72000000

/* Enable and setup SysTick Timer at a periodic rate */
SysTick_Config(sysTickRate/ 1000 ); @ 1msec
0 Kudos

1,081 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sandy_493 on Wed Nov 12 04:40:35 MST 2014
Hello bobi,

Here is the sample code that i have implemented to generate 1 msec using systick interrupt.

sysTickRate = Chip_Clock_GetSysTickClockRate();

// sysTickRate  = 72000000

/* Enable and setup SysTick Timer at a periodic rate */
SysTick_Config(sysTickRate/ 1000 ); @ 1msec
0 Kudos