Use FTM0 / 1 with a one-shot timer.

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

Use FTM0 / 1 with a one-shot timer.

Jump to solution
1,009 Views
jun1
Contributor V

Hi.


I want to use the one-shot timer, but the CNT register does not seem to be reset even if the timer overflows.
So, remodeling to make CNT = 0 at FTM_StartTimer (), is there a problem?

Used for MODBUS silent interval timer.

Modify fsl_ftm.c (SDK_2.6.0_MKE04Z128xxx4)
static inline void FTM_StartTimer (FTM_Type * base, ftm_clock_source_t clockSource)
{
       uint32_t reg = base-> SC;

       if (base  ! = FTM2) {
              base-> CNT = 0u;       // 2019/09/14 add jun. 
       }
       reg & = ~ (FTM_SC_CLKS_MASK);
       reg | = FTM_SC_CLKS (clockSource);
       base-> SC = reg;
}

my application source is

// ================================================ =======
//        T3.5 character timer-timer start
// ================================================ =======
static inline void modbusT15Start (void)
{
       FTM_StopTimer (MODBUS_TMR_T15);
       FTM_StartTimer (MODBUS_TMR_T15, kFTM_SystemClock); // reset timer & start
}
// ================================================ =======
//        3.5 character timer-interrupt handler
// ================================================ =======
void MODBUS_TMR_T35_IRQ_HANDLER (void)
{
       / * Clear interrupt flag. * /
       FTM_ClearStatusFlags (MODBUS_TMR_T35, kFTM_TimeOverflowFlag);
       FTM_StopTimer (MODBUS_TMR_T35);
       modbusMatrix (E_MB_EV_T35_EXPIRED, 0);
}

jun.

Labels (1)
1 Solution
770 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi jun yamada,

  Your test result is correct, please know one key point: The counter will be cleared when the counter is overflow.

  That's why your cntdata[0] is nearly 0. After the overflow happens, the counter will counter again, that's why your other cntdata also has the data.

   If you want to clear the counter, you can set CNT = 0  directly.

  Reset clears the CNT register. Writing any value to COUNT updates the counter with its initial value, CNTIN. If it is FTM0/1, it should be updated to 0.

  But please note, even you write CNT=0, the counter will go on counting, if you want the counter stop, you can just disable the FTM  clock.

Wish it helps you!

Have a great day,
Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-----------------------------------------------------------------------------

View solution in original post

6 Replies
770 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi jun yamada ,

   1. Please check your counter mode: up counter, or up-down counting?

   2. Check your CNTIN

      If you want the counter is reset to 0 after it is overflow, you need to set CNTIN = 0x0000.

Wish it helps you!

If you still have questions about it, please kindly let me know.

Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
770 Views
jun1
Contributor V

thank you for your answer.

Isn't ke04 not having CNTIN register?

I saw the reference manual.

KE04 Sub-Family Reference Manual 

Table3-34. FTM modules features

jun.

0 Kudos
770 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Jun,

   Thanks for your updated information.

   Yes, you are right!

   MKE04 FTM0/1 doesn't have the CNTIN register, but I have tested the FTM0 overflow function, after the counter overflow happens, the CNT will be reset, please check my test result:

pastedImage_1.png

I enable the FTM0 timer overflow interrupt, when the overflow happens, the CNT is cleared.

I don't know how you test it, I think you also can enable the overflow interrupt, then debug the code, and add the break point in the interrupt, when you enter the interrupt, you can check the CNT data.

I am using the KDS PE to create the FTM0 code for KE04, I also attach the code for your reference.

If you still have questions about it, please kindly let me know.


Have a great day,
Kerry

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

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
770 Views
jun1
Contributor V

Thank you for your reply.

I checked with the test code.
I set 10500 to MOD. --- moddata
When an interrupt is generated, CNT of FTM1 becomes 0. --- cntdata [0]
If the timer is not stopped, the count continues. --- cntdata [1]
After the timer stops, CNT remains unchanged. --- cntdata [2] / [3]
Looking at the value of CNT after the timer starts, it seems that it starts counting from the previous value. --- cntdata [4] / [5]
So, the question is, is there a way to set CNT = 0 when the timer starts?
My idea is to write 0 directly to the CNT at the start of the timer.
Please let us know if there is a more appropriate method.

From there, it is natural, but if the timer does not stop, it continues counting.

Thank you.
junFTM1.GIF

0 Kudos
771 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi jun yamada,

  Your test result is correct, please know one key point: The counter will be cleared when the counter is overflow.

  That's why your cntdata[0] is nearly 0. After the overflow happens, the counter will counter again, that's why your other cntdata also has the data.

   If you want to clear the counter, you can set CNT = 0  directly.

  Reset clears the CNT register. Writing any value to COUNT updates the counter with its initial value, CNTIN. If it is FTM0/1, it should be updated to 0.

  But please note, even you write CNT=0, the counter will go on counting, if you want the counter stop, you can just disable the FTM  clock.

Wish it helps you!

Have a great day,
Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-----------------------------------------------------------------------------

770 Views
jun1
Contributor V

Thank you for your reply.
I understood it well.
It ’s really helpful because you can reply quickly.
I look forward to working with you.
jun

0 Kudos