Using a timer interrupt with KDS and K64F

Another demo that I saw while at the Freescale Tech Day was also pretty simple, but already moving a step beyond the traditional Arduino examples. It's just blinking a LED, but rather than the "brute force" approach of switching the pin then forcing a wait before switching it again (the algorithm in the classic Arduino "blink" example), this one is a bit smarter and uses a timer interrupt. Sure, you can use timers and interrupts on an AVR, but it's telling that this is considered an advanced feature.

I will say at this point, though, that I am becoming a bit concerned that the idea of "Processor Expert" might be missing something. It's obvious that the software understands pretty much everything about the abilities, configuration and use of Freescale's MCU chips, and provides property-style inspectors for every aspect, but it also seems to require users to know the ins and outs to a great level of detail in order to use it. Naively, I was hoping for something a bit more high level, perhaps with the ability to create, share, and use plugins for common tasks (driving a pin using a timer, reading or writing values using DMA, and so on) which gather together the required settings in one place.

Anyway, on to the steps to blink that LED.

I shall start from where I left off last time, with a LED attached to a named output pin, and the ability to switch it "manually" using the debugger.

Enable a function to toggle the I/O pin

  1. Double-click the BitIO component in the Components panel to open its Component Inspector
  2. Click the "Methods" tab to show the available methods
  3. Look down the list for "NegVal" and set it to "generate code"
  4. On the top "Project" menu, click "Generate Processor Expert Code"

Set up a repeating timer in Processor Expert

  1. In the Component Library find "TimerInt" and drag it to the Components list
  2. Double-click the new TimerInt component to open its inspector
  3. Click the "..." button to bring up the settings dialog and enter 500
  4. Click "OK" and see that the Interrupt Period is now set to 500ms

Use the timer to toggle the LED every half second

  1. Expand "Sources" in the Project Explorer panel
  2. Double-click Events.c to open it in the editor
  3. Look down for TI1_OnInterrupt
  4. Add a a call to toggle the LED where it says "Write your code here ...":
    LED_NegVal();
    
  5. Save all your changes (Ctrl-S or click the "Floppy Disc" icon at the top left) and re-build the project using the "hammer" icon

Now you should be able to re-run the debugger. Clear any breakpoints and click the "play forward" icon to see the LED toggle every 500ms.

Leave a Reply

Your email address will not be published. Required fields are marked *