Tutorial: Timer (LED) with Processor Expert for Kinetis

In the ‘Pre-LDD age’, setting up a periodic timer event with Processor was really easy. With the concept of LED’s (see “There is a Time and Date for both Worlds”) things are a bit different. But once things are clear, it is not that hard. So here I’m doing a very exciting thing: blinking an LED using a timer! 😎

So I’m going to have an LED blinking every second. I’m using the TWR-K60N512 with an ARM Cortex-M4 and the Eclipse based CodeWarrior for MCU10.2. But it is easy to adopt it to any other Kinetis board.

Adding an LED

❗ The steps below are for an older version of the LED component. See “Tutorial: Enlightning the Freedom KL25Z Board” for an updated version.

First, I set up my LED. For this I add the LED component in combination with a GPIO_LDD component:

LED and GPIO_LDD

LED and GPIO_LDD

The GPIO pin I configure as below:

GPIO_LDD Properties

GPIO_LDD Properties

This means I set it up for port PTA, on pin PTA10 (according to the schematics of the board), and name it LED_E4 (I will need that name later). The Pin signal name is used for documentation in the generated Documentation\ProcessorExpert_SIGNALS.txt file. As this is an output pin, I it set it to Output 😀. I set Auto initialization to yes so things are automatically initialized during execution of PE_low_level_init().

In the LED component itself I link it to the GPIO1 and the LED_E4 name I used in the previous step:

LED Properties

LED Properties

As I have configured GPIO_LDD to initialize automatically, so I can keep the initialization in the LED to no.

Adding a Timer

I’m adding the TimerUnit_LDD:

TimerUnit_LDD added

TimerUnit_LDD added

Now I have to configure the timer properties:

TimerUnit Properties

TimerUnit Properties

Selecting the counter Module Name (FTM0_CNT in above) is easy. A bit complicated is to set up the Counter frequency. Clicking into that field gives me a browse button:

Browse Button for Counter Frequency

Browse Button for Counter Frequency

This gives me the following dialog with a set of frequencies:

Timer Dialog

Timer Dialog

Well, fine. Except that I cannot set a frequency of 1 Hz?!? Hmm, that’s strange, as this timer should really offer something like this? The magic is that this is not the timer frequency I need for toggling my LED at 1 Hz: this is the ‘input clock source frequency‘ of that timer. Or basically the frequency after the pre-scaler. Once this clear, it is OK that I set it up to 128 Hz (as this would be enough afterwards for 1 Hz):

128 Hz for my timer

128 Hz for my timer

Hint: Instead of using the ‘browse’ button, I can simply enter “128 Hz” into the field.

The logical next step is to enable interrupts for the timer:

Enabled Interrupts for Timer

Enabled Interrupts for Timer

So far so good. But we are not quite there yet. What we have now is a timer which is counting at a frequency of 128 Hz. It will count from 0 to 65536, do an interrupt then and start counting again. Not yet what we want. We want an interrupt after 1 second. For this I need to change it to ‘On-match‘ and specify the 1 Hz Period:

On-Match and 1 Hz Period

On-Match and 1 Hz Period

I make sure that component gets initialized PE_low_level_init() too:

Auto Initialization

Auto Initialization

Note: If I keep that setting to ‘no’, then I need to make sure my application calls the component Init() function somewhere:

TimerUnit_LDD Init Function

TimerUnit_LDD Init Function

Toggling the LED

Now it is time to generate code with the menu Project > Generate Processor Expert Code; or using the tool button in the Project Panel view.

Finally I can double-click on the OnCounterRestart() Event:

OnCounterRestart Event

OnCounterRestart Event

and add the code to toggle my LED:

/*
** ===================================================================
**     Event       :  TU1_OnCounterRestart (module Events)
**
**     Component   :  TU1 [TimerUnit_LDD]
**     Description :
**         Called if counter overflow/underflow or counter is
**         reinitialized by modulo or compare register matching.
**         OnCounterRestart event and Timer unit must be enabled. See
**         <SetEventMask> and <GetEventMask> methods. This event is
**         available only if a  is enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. The pointer passed as
**                           the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void TU1_OnCounterRestart(LDD_TUserData *UserDataPtr)
{
  LED1_Neg();
}

I build, download and run: Now my LED shall trigger every second from the 1 Hz timer interrupt. Hopefully.

Happy Timing 🙂

Advertisements

18 thoughts on “Tutorial: Timer (LED) with Processor Expert for Kinetis

  1. Pingback: Sports Timing System in a Lunch Box | MCU on Eclipse

  2. Pingback: Tutorial: Freedom with FreeRTOS and Kinetis-L | MCU on Eclipse

  3. Excellent! You help me in first steps with Kinetis K60 board.
    I have a question:
    I cannot find the “LED” component in my Codewarrior (v10.2), so looking in your blog, I’ve found the component in: http://steinerberg.com/EmbeddedComponents/LED/home.htm
    After importing it, I saw that it haven’t the same Properties screen that you show (It haven’t the LDD and BitIO subfields). So, I assign the LED component to the GPIO pin via the LEDpin1:BitIO “subcomponent” from LED1:LED.
    So, I cannot compile my project because the Linker give me the error: >Undefined : “LED1_Neg”
    And when I look for “LED1_Neg();” from the events menu below LED1:LED, it redirects me to this section:
    /*
    void LED1_Neg(void)
    {
    *** This method is implemented as macro in the header file
    }
    */
    As we see, this function is between comments, but the message says that the function is implemented.

    I am doing something bad?
    Can you help me with this?

    Thank you so much, your blog is very helpfull.

    PS: Sorry about my english.

    Like

    • The ‘Neg’ method for the underlying BitIO needs to be enabled. Click on the LED component to ‘unfold’ it with the small triangle on the left, then unfold the underlying BitIO component to list the methods of it. Probably the NegVal() method is disabled (with a small black cross). Right click on it and toggle the Enable/Disable flag. Then it should be enabled. Generate code again and you should be fine. I need to to check the LED component (well, the template to the BitIO) so that method is enabled automatically. Seems I have missed this with the changes for the interface.
      Let me know if this helps (or not),.

      Like

      • Thanks for your answer.
        The method Neg() was enabled (I check that before).
        I resolve the problem loading a different version of the LED component (the first one uploaded before this post).
        Now the program works fine and I’m able to start testing my board.

        Thanks a lot.

        Like

  4. The LED component link after the sub title “Adding an LED” links to a post on scalability. Not sure that was the intended effect, hehe. Anyway, I know where you keep the components 🙂

    Like

  5. Hi Erich,
    I don’t know if it’s the best topic for my question but seems to be okay :p.
    I want to know if you have some tips or code to do a variable timer.
    My purpose is to put a value on DAC every X µs but X depends on a variable (the frequency of the sine I try to output)… So X is variable and I tried to use the TimerInt because I had seen the setPeriod() method but seems to be unavailable in the help on component… Any suggest to do it ?
    I use the FRDM KL25Z with Processor Expert. My actual researchs are about finding how Processor Expert deals with the Clock Frequency in the files TU1.c & T1.c and make it variable…

    Like

    • Hi Alex,
      Instead of changing the period of the timer, I rather would count the timer ticks and then put the value out. Say set the timer to 1 MHz and then count the ticks.
      Then you have 1 µs resolution, and could wait 1, 2, 3, 4, etc µs. My only concern is interrupt overhead: having an interrupt every µs is a heavy workload, and the DA conversion needs some time too.
      I have not done this, but someone else had a similar problem. The solution was to set up a table with the values to put on the DA converter, and using DMA to transfer the data to the converter.
      That reduces the interrupt load, and should be doable. I do not have a ‘ready-to-use’ example, but I think that would be doable idea?

      Like

      • Yep, I have defined a table of values and I have imported its pointer, its size and I have created a counter(static one of course) with the combinaison of “volatile” in the ProcessorExpert.c & “extern” in events.c and it works good enough for me to have a little “sine” (accurancy on amplitude does not matter in my case so I’ve reduced the size of the table to make it accurate on frequency ;)).
        DMA should be the right way for more complicated application :p.
        Thank you again for your response ;).

        Like

  6. Hi,
    It is posible to make equivalent of arduino millis() function for KL25?

    for example:

    starttime = millis();
    … do something…
    endtime = millis();
    difference = endtime-starttime;

    I would be grateful for any suggestions

    Like

    • Hi misiek81,
      yes, I would create a timer with a Processor Expert component, and then subtract the timer values as in your code. If you are using FreeRTOS, it is even easier, as you have this time information from the RTOS.
      I hope this helps,
      Erich

      Like

  7. Hi Erich,
    I’m working with kinetis KL15 and I’d like enable PIT interrupt on channel 0 and channel 1.

    About channel 0 I haven’t got any problem, but about channel 1 the software doesn’t fall in the interrupt function. I configured all using processor expert.

    Have you got any idea about this problem?

    Please, let me know.

    Many thanks.

    Best regards,

    Stefano

    Like

      • Hi Erich,

        I verified the interrupt flag of each timer’s channel and they work correctly. I checked them in the main function. But if I want to develop an interrupt function where I want clear the channel interrupt flag, the software doesn’t reach the interrupt function.

        Thanks.

        Like

      • Hi Stefano,
        I’m not sure why it is not working for you. What I can think of is that if your interrupt frequency is too high, you won’t have the needed time to enter the interrupt service routine. Can you try things with a very slow timer interrupt period (say every 100 ms)?

        Erich

        Like

  8. Hi Erich,

    I tryed it, but the final result is the same. I’d like send you the processor expert configuration for this peripheral. How can I do it? Do you think there is a problem in the microcontroller?

    Many Thanks.

    Like

    • No, I don’t think there is an issue with your microcontroller.

      You could send me that project to the email address listed on the About page of this blog.

      But I cannot commit that I will find time in the near future to look at it.

      Erich

      Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s