Arduino TMILLIS() similar function?

cancel
Showing results for 
Search instead for 
Did you mean: 

Arduino TMILLIS() similar function?

42 Views
highlander
Contributor I

Hi.
Just wondering if there is a similar function to the TMILLIS() function or how to create one so I can do tasks on a millisecond basis timer. Even if its a 3.25ms counter it would work rather well.

 

Thanks

0 Kudos
1 Reply

25 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

Two things:

1) you have not mentioned MCU type

2) Such function is neither a part of the standard C language package nor assembler.

You can use ProcessorExpert  (pat of the CodeWarrior) for such purpose which enables you to set the timer on the base of your requirements and use some implemented higher level functions.

Otherwise, you should know what is a timer, how it depends on bus clock, how to set it, whether and how to use interrupt functions from it, program backup millisecond timer routine or only flag ms has gone. This is, however, application problem.

Short example follows which presents period generator which generates 3 periods by 3 different peripherals inside the S12XEP100 device and toggles pins each time when period finishes.

Best regards, Ladislav

//******************************************************************************
// periodic interrupt generator 1 : uses MDDC - modulus down counter
// Modulus down counter counts once with given period.
// The periodic interrupt is started by function StartMDC
// The periodic interrupt can be stopped by function StopMDC
// The pin PB0 toggled within interrupt.
//-----------
// periodic interrupt generator 2 : uses a timer
// interval = TCx * dt = TCx * (prescaler/fbus)
// TCx = interval / (prescaller/fbus) = interval * fbus / prescaler
// When 50MHz BUSCLK is used then we are able to set by means of standard prescaller:
// dt = 1/50,000,000 * prescaller
// let select prescaller = 8 and required internval is 0.01s =>
// => TCx = 0.01*50,000,000/8 = 62500
// Channel 4 is used to catch period. The timer is disconnected from PT4 pin.
// The interrupt is launched periodically each 10ms.
// The periodic interrupt is started by function StartTIM
// The periodic interrupt can be stopped by function StopTIM
// The pin PB1 toggled within interrupt.
//-----------
// periodic interrupt generator 3: uses a PIT - periodic interrupt timer
// Periodic Interrupt Timer 0 is used to generate 10ms periodic interrupt.
// time-out period0 = (PITMTLD + 1) * (PITLD + 1) / fBUS = (199+1)* (2499+1) / 50,000,000 = 0.01s
// The periodic interrupt is started by function StartPIT0
// The periodic interrupt can be stopped by function StopPIT0
// The pin PB2 toggled within interrupt.

//******************************************************************************

//******************************************************************************
#include <hidef.h> /* common defines and macros */
#include <mc9s12xep100.h> /* derivative information */
//******************************************************************************
#pragma LINK_INFO DERIVATIVE "mc9s12xep100"
//******************************************************************************
#define UBYTE unsigned char
#define UWORD unsigned int
#define ULONG unsigned long
//******************************************************************************
//******************************************************************************
// local function prototypes
//******************************************************************************
static void PLL_Init(unsigned char synr, unsigned char refdv, unsigned char postdiv);
//--------------------------
static void MDC_Init(void);
static void StartMDC(void);
static void StopMDC(void);
//--------------------------
static void TIM_Init(void);
static void StartTIM(void);
static void StopTIM(void);
//--------------------------
static void PIT0_Init(void);
static void StartPIT0(void);
static void StopPIT0(void);
//******************************************************************************
#pragma CODE_SEG DEFAULT
//------------------------------------------------------------------------------
static void MDC_Init(void)
{
// Time interval = Tbus / Prescaller {1;4;8;16} * MCCNT
// Enable ISR from MDC, Continue MDC when reaches 0, prescaler 8,
ECT_MCCTL = ECT_MCCTL_MCZI_MASK | ECT_MCCTL_MODMC_MASK | 2;
}
//------------------------------------------------------------------------------
static void StartMDC(void) // interval [us] Modullus Down Counter
{
ECT_MCFLG_MCZF = 1; // clear interrupt flag
ECT_MCCTL_MCEN = 1;
ECT_MCCNT = 62500; // 10ms period at 50MHz BUSCLK
ECT_MCCTL_FLMC = 1;
}
//------------------------------------------------------------------------------
static void StopMDC(void) // interval [us] Modullus Down Counter
{
ECT_MCCTL_MCEN = 0; // stop MDC
}
//------------------------------------------------------------------------------
#pragma CODE_SEG NON_BANKED
//------------------------------------------------------------------------------
interrupt 26 void ModulusDownCounterIsr(void)
{
ECT_MCFLG_MCZF = 1; // clear interrupt flag
PORTB_PB0 = ~PORTB_PB0;
}
//******************************************************************************
#pragma CODE_SEG DEFAULT
//------------------------------------------------------------------------------
static void TIM_Init(void)
{
TIM_TSCR2 = 0x03; // prescaller = 8
//--- channel 4 setup ---------------
TIM_TIOS_IOS4 = 1; // channel 4 is output compare
TIM_TCTL1 = 0x00; // no output compare action at channel 4 on compare
TIM_OCPD_OCPD4 = 1; // channel 4 disconnected from output pin logic
//-----------------------------------
TIM_TIE_C4I = 0; // disable interrupt from channel 4
//-----------------------------------
TIM_TSCR1 = 0xE0; // enable timer,stop in wait,stop in freeze, no fast flag clear
}
//------------------------------------------------------------------------------
static void StartTIM(void) // 10ms period at 50MHz BUSCLK
{
TIM_TFLG1 = 0x10; // clear interrupt flag from channel 4
TIM_TC4 = TIM_TCNT + 62500; // set new value and clear interrupt flag
TIM_TIE_C4I = 1; // enable interrupt from channel 4
}
//------------------------------------------------------------------------------
static void StopTIM(void) //
{
TIM_TIE_C4I = 0; // disable interrupt from channel 4
}
//------------------------------------------------------------------------------
#pragma CODE_SEG NON_BANKED
//------------------------------------------------------------------------------
interrupt 89 void TC4_Isr(void)
{ // 10ms period at 50MHz BUSCLK
TIM_TFLG1 = 0x10; // clear interrupt flag from channel 4
TIM_TC4 = TIM_TCNT + 62500; // set new value and clear interrupt flag
PORTB_PB1 = ~PORTB_PB1;
}
//******************************************************************************
#pragma CODE_SEG DEFAULT
//------------------------------------------------------------------------------
static void PIT0_Init(void)
{
//--- PIT0 -------------------------------
// clock period is set 10ms
// time-out period0 = (PITMTLD + 1) * (PITLD + 1) / fBUS = (199+1)* (2499+1) / 50,000,000 = 0.01s
PITMUX_PMUX0 = 0; // microtimer 0 for PIT0
PITMTLD0 = 199; // microtimer (prescaller) value
PITLD0 = 2499; // PIT0 timer interval
PITCFLMT = 0xE0; // Enable PIT module,stop in wait,stop in freeze


PITINTE_PINTE0 = 0; // Disable PIT0 interrupt to generate 10 ms
PITCE_PCE0 = 1; // Enable PIT0
}
//------------------------------------------------------------------------------
static void StartPIT0(void)
{
PITTF = PITTF_PTF0_MASK; // Clear interrupt flag from PIT0
PITCFLMT_PFLMT0 = 1; // reload microtimer 0
PITFLT_PFLT0 = 1; // reload PIT0 counter
PITINTE_PINTE0 = 1; // Enable PIT0 interrupt to generate 10 ms
}
//------------------------------------------------------------------------------
static void StopPIT0(void)
{
PITINTE_PINTE0 = 0; // Disable PIT0 interrupt to generate 10 ms
}
//------------------------------------------------------------------------------
#pragma CODE_SEG NON_BANKED
//------------------------------------------------------------------------------
interrupt 66 void PIT0_Isr(void)
{
PORTB_PB2 = ~PORTB_PB2;
PITTF = PITTF_PTF0_MASK; // Clear interrupt flag PIT0
}
//******************************************************************************
#pragma CODE_SEG DEFAULT
//------------------------------------------------------------------------------
static void PLL_Init(unsigned char synr, unsigned char refdv, unsigned char postdiv)
{
PLLCTL = 0B00000001; // CME=0,PLLON=0,FM1=0,FM2=0,FSTWKP=0,PRE=0,PCE=0,SCME=1
CLKSEL = 0B00000011; // PLLSEL=0,PSTP=0,PLLWAI=0,RTIWAI=1,COPWAI=1
SYNR = synr; // Set the multiplier register
REFDV = refdv; // Set the divider register
POSTDIV = postdiv; // Set the post divider register
PLLCTL_PLLON = 1; // Enable the Phase Lock Loop
while(!CRGFLG_LOCK); // Wait till the PLL VCO is within tolerance
CLKSEL_PLLSEL = 1; // Select clock source from PLLCLK
//ECLKCTL_NECLK=0; // Enable the BusClk output at ECLK pin
}
//******************************************************************************

void main(void)
{
ULONG i;
//..................................
PLL_Init(0xC4,0x80,0x00);
//..................................
DDRB = 0x07; // PB0,1,2 an output
PORTB = 0x00; // inactive state of PB0
//..................................
MDC_Init();
TIM_Init();
PIT0_Init();
//..................................
EnableInterrupts;
//..................................
StartMDC();
// StopMDC();
StartTIM();
// StopTIM();
StartPIT0();
// StopPIT0();
for(;;)
{
for(i=0;i<1000000UL;i++) // SW delay
{
asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;
}
StopMDC();
StopTIM();
StopPIT0();

for(i=0;i<1000000UL;i++) // SW delay
{
asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;asm nop;
}

StartMDC();
StartTIM();
StartPIT0();
}
//........................................
}
//******************************************************************************
// Services performed by FREESCALE in this matter are performed AS IS and without any warranty. CUSTOMER retains
// the final decision relative to the total design and functionality of the end product. FREESCALE neither
// guarantees nor will be held liable by CUSTOMER for the success of this project.
// FREESCALE DISCLAIMS ALL WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY INCLUDING, BUT NOT LIMITED TO, IMPLIED
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ON ANY HARDWARE, SOFTWARE ORE ADVISE SUPPLIED
// TO THE PROJECT BY FREESCALE, AND OR NAY PRODUCT RESULTING FROM FREESCALE SERVICES . IN NO EVENT SHALL FREESCALE
// BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT.
//
// CUSTOMER agrees to hold FREESCALE harmless against any and all claims demands or actions by anyone on account of
// any damage, or injury, whether commercial, contractual, or tortuous, rising directly or indirectly as a result of
// the advise or assistance supplied CUSTOMER in connection with product, services or goods supplied under this Agreement.
//******************************************************************************

 

 

 

 

0 Kudos