timer

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

timer

822 Views
deepaksimon
Contributor I

hi, i am a beginner in microcontroller programing.i have a freescale kl25z board.now i am trying to interface anseconds?? ultrasound sensor with this board.how can i check the time delay using a timer in microseconds??please give me a detailed answer with sampple programs if possible

Tags (1)
0 Kudos
5 Replies

604 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Deepak,

In the end, I know you want to know the delay time, if it is the case, you can use either the  system tick timer(SysTick) module or the TPM to finish it.

I think you can use the following code to get the delay based on TPM0 module. The delayTimerTick is the delay tick number , the tick is the frequency of FLL output clock.

BR

XiangJun Rong

static int i = 0;

void delay(void);

void TPM_Init(void);

uint16_t getTPMCounter(void);

void ResetTPM(void);

int main(void)

{

uint16_t delayTimerTick;

    /* Write your code here */

    /* This for loop should be replaced. By default this loop allows a single stepping. */

    TPM_Init();

    ResetTPM();

    delay();

    delayTimerTick=getTPMCounter();

    __asm("nop"); //set a break point here

    for (;;) {

        i++;

    }

    /* Never leave main */

    return 0;

}

void TPM_Init(void)

{

SIM_SCGC6|=0x03000000; //enable FTM0 and FTM0 module clock

SIM_SCGC5|=0x3E00;

SIM_SOPT2|=0x1000000; //select TPMSRC clock source as MCGIRCLK clock

TPM0_CONF=0x00; //set up BDM in 11

TPM0_MOD=0xFFFF;

TPM0_C0SC=0x14; //FTM output signal toggle when the FTM counter matches with //C0V registrer

TPM0_C0V=500;

TPM0_SC=0x08; // system clock driving, dividing by 1

}

void ResetTPM(void)

{

    TPM0_CNT=0x00;

}

uint16_t getTPMCounter(void)

{

     uint16_t temp;

     temp=TPM0_CNT;

     return temp;

}

void delay(void)

{

    uint16_t delayCounter;

    for(delayCounter=0; delayCounter<1000; delayCounter++)

    {

        __asm("nop");

        __asm("nop");

    }

}

0 Kudos

604 Views
adriancano
NXP Employee
NXP Employee

Hi Deepak Simon,

You will probably find the next post from colleague Erich Styger very useful.

Tutorial: Ultrasonic Ranging with the Freedom Board | MCU on Eclipse


Hope this information can help you

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

0 Kudos

604 Views
deepaksimon
Contributor I

Dear Adrian Sanchez Cano, Thank you for your help.I am using a software called coocox. i this i need to configure the timer manually.How can i do this? can you help me??

0 Kudos

604 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Deepak,

Frankly speaking, I do not know the signal pattern of the ultrasound sensor, anyway,If your ultrasound sensor can generate square-waveform signal, you can use FTM module to test the time of two edges  of the square-waveform signal, the FTM has capture function, which can detect the edge of external signal and trigger an interrupt.

If the ultrasound sensor can not generate the square-waveform signal, I think you can use Timer to generate interrupt, for example Timer can generate an interrupt in 1mS interval, in the ISR, you can increase a variable, you can get the time by  reading the variable.

Hope it can help you

BR

Xiangjun rong

0 Kudos

604 Views
deepaksimon
Contributor I

thank you

0 Kudos