How to configure timer to generate 1 ms .Help me with a sample code for MC9S12ZVCRMV1 16 bit controller.

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

How to configure timer to generate 1 ms .Help me with a sample code for MC9S12ZVCRMV1 16 bit controller.

1,007 Views
sathishkannan
Contributor III

I need step by step instruction to configure register for timer and generate delay of 1 ms .Please explain to see the delay in debugger.

Labels (1)
0 Kudos
4 Replies

632 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

Easily in the attached project you can find an example for 250ms period. The definition of value TIM0_TC0_INCREMENT defines period of TC0 interrupt. If you require anothe period just recalculate on the basis of you BUSCLK.

period = (PRS+1)* TIM0_TC0_INCREMENT/ BUSCLK =

                      = 250 * BUSCLK*1000 /  BUSCLK = 250,000 us = 0.25s

Of course the project contains more examples on different peripherals but you should look only for TIM0_Channel0_ISR, TIM0_Init (TIM0.c, TIM0.h) and possible principle of interrupt functions definition in the InterruptVectorTable.c

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

#pragma CODE_SEG NON_BANKED

/* added -ansi off to compiler option; Project->Properties->Compiler->General*/

interrupt void TIM0_Channel0_ISR(void) // vector defined in the prm file

{

  TIM0TC0      = TIM0TCNT + TIM0_TC0_INCREMENT;

  TIM0TFLG1    = 0B00000001; // clear flag from channel 0

  PTS_PTS4    = ~PTS_PTS4; // do anything

}

#pragma CODE_SEG DEFAULT

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

Function Name : TIM0_Init

Engineer      : xxxx

Date          : Dec-14-2013

Parameters    : NONE

Modifies      : TIM0 setup registers

Returns      : NONE

Notes        : period 0.25sec

                period = (PRS+1)* TIM0_TC0_INCREMENT/ BUSCLK =

                      = 250 * BUSCLK*1000 /  BUSCLK = 250,000 us = 0.25s

Issues        : NONE

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

void TIM0_Init(void)

{

SET_INTERRUPT_PRIORITY(TIM0_Channel0_VEC_OFF, 3); // priority 3 FOR TIM_CH0 OC

TIM0TIOS    = 0B00000001; // Channel0 output compare

TIM0TCTL2    = 0B00000000; // no output compare action at pin

TIM0TFLG1    = 0B11111111; // clear all flags

TIM0TIE      = 0B00000001; // enable OC from Channel0

TIM0PTPSR    = 249;        // prescaler 250

TIM0TC0      = TIM0TCNT + TIM0_TC0_INCREMENT;

TIM0TSCR1    = 0B11101000; // Timer enable, stop in wait and BDM mode,

                            // no fast flag clear, precise prescaler

}

Best regards,

Ladislav

0 Kudos

632 Views
sathishkannan
Contributor III

Hi,

I got 1 ms delay .

derive me the calculation and explanation how i got 1 ms delay.

busclock =16 Mhz

TIM0_TC0_INCREMENT=10000

prescaler=16

Is it correct or I am getting by mistake ?

0 Kudos

632 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

period = (PRS+1)* TIM0_TC0_INCREMENT/ BUSCLK

TIM0_TC0_INCREMENT = period * BUSCLK / (PRS+1) = 0.001 * 16,000,000 / (15 + 1) = 1000              [ -; s, MHz, -]

TIM0_TC0_INCREMENT = 1000

TIM0PTPSR = 15

Best regards,

Ladislav

0 Kudos

632 Views
sathishkannan
Contributor III

Hi,

As per your calculcation you are passing TIM0_TC0_INCREMENT = 1000,but in my program i am passing as 10000 and prescaler as 16 .whats wrong with it ?

0 Kudos