element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Freedom development platform
  • Products
  • Dev Tools
  • Freedom development platform
  • More
  • Cancel
Freedom development platform
Blog [FRDM-K64F + Kinetis Design Studio (KDS)] Tutorial: Toggling an RGB LED using interrupts
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Freedom development platform to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: bheemarao
  • Date Created: 3 Aug 2014 2:44 AM Date Created
  • Views 1350 views
  • Likes 0 likes
  • Comments 2 comments
  • interrupt
  • kinetis
  • kds
  • frdm-k64
  • kinetis-design-studio
  • rgb-led
  • pe
  • processor-expert
  • kinetis-k
Related
Recommended

[FRDM-K64F + Kinetis Design Studio (KDS)] Tutorial: Toggling an RGB LED using interrupts

bheemarao
bheemarao
3 Aug 2014

Here is a tutorial for toggling 3-led’s by pressing one switch using interrupt logic implemented using FRDM-K64F on Kinetis Design Studio.

 

Before i start my project make sure to have below requisites:

 

  1. KDS software installed in your PC You can download from Here
  2. FRDM-K64FFRDM-K64F board
  3. load Debug driver to K64F board and configuring it in KDS tool (refer to my earlier blog Here)
  4. Reference of FRDM-K64 user guide for schematic (download from Here)

 

So lets start in creating a new project using KDS IDE

 

Open KDS IDE by double clicking the KDS icon present in your desktop:

It will ask for the Workspace, give the path of your choice and click ‘ok’

 

image

 

After opening you can see the KDS welcome window as shown here


image

 

 

Create a New project as shown below:

 

File -> New  -> Kinetis Design Studio Project

 

image

 

Choose project name of your choice, i have given it as “RGB_Switch Interrupt-K64”

 

image

 

Select Device “MK64FN1M0XXX12” from the filter as shown

 

Kinetis K -> MK60->MK64F(120MHz) ->  MK64FN1M0XXX12

 

image

 

Now select the option “Use Processor Expert for configuration”  and uncheck “Use Kinetis SDK for headers, drivers” and click finish.

 

image

 

You can see the progress bar creating all required header files and libraries..Etc

 

image

 

After completion you can see below project window

 

image

 

Click on CPU, you can see the clock settings, i am using the default settings as shown below i.e the clock source is from internal oscillator with reference clock of 32.768 KHz and Fast internal reference clock is 4 MHz.

 

image

 

 

Now as per the requirement i am toggling the three LED’s i.e Red, Blue, Green using button press as an interrupt driven GPIO.

So i need to add 3- logic device driver components for LED as shown:

 

image

 

Now right click on Bit0_LDD and add to project 3 times as we need three LED components:

 

image

 

 

Rename the components as LED_R, LED_G, LED_B as shown

 

image

 

Refer to below schematic of K64 board the 3 LED’s are connected to Port-B and Port-E pins

 

image

 

LEDK64
REDPTB22/SPI2_SOUT/FB_AD29/CMP2_OUT
BLUEPTB21/SPI2_SCK/FB_AD30/CMP1_OUT
GREENPTE26/ENET_1588_CLKIN/UART4_CTS_b/RTC_CLKOUT/USB0_CLKIN

 

You can see these LED’s are connected in active-Low connection, i.e common Anode, we should provide Low signal to port pins to glow a LED.

 

Configure the below settings for 3 LED’s, Double click on the LED_R and provide the below setting

 

RED LED settings: connected to PTB22 port pin with initial value to be ‘1’ (i.e LED is in off condition)

 

image

 

Make sure under ‘Methods’ tab you select “Generate code” for ‘NegVal’ as shown below

 

image

 

Green LED’s setting: connected to PTE26 port pin with initial value to be ‘1’ (i.e LED is in off condition)

 

image

 

Select Generate code for NegVal as shown below:

 

image

 

Blue LED setting: connected to PTB21 port pin with initial value to be ‘1’ (i.e LED is in off condition)

 

image

 

“generate code” for NegVal as shown below:

image

 

 

Next we need to add component for switch acting as interrupt: external interrupt logic device driver (ExtInt_LDD) as shown below:

 

image

 

Select ExtInt_LDD and Double click or right click and add to project.

 

Configure the settings for this interrupt driver as shown below: as K64F board has the switch (SW2) which is connected to PTC6 port pin, we have assigned this port pin to the external interrupt source with rising edge.

image

 

The switch is connected to PTC6 and we have configured it as interrupt driven signal

 

Now we need to generate code as shown below:

 

image

 

Now all required driver files for LED and ExtInt are generated as shown below:

 

image

 

Now we need to write our code:

 

Our requirement here is to toggle all the three RGB LED’s one-by-one based on interrupt driven switch press

 

SW press Red ‘on’ and Green, Blue ‘off’

Next time SW press Green ‘on’ and Red, Blue ‘off’

Next time SW press Blue ‘on’ and Red, Green ‘off’

And carry on the sequence.

 

‘Events.c’ file in our project has the Interrupt service routine function:

ExtInt_LDD will generate a function as shown:

 

void EInt1_OnInterrupt(LDD_TUserData *UserDataPtr)

{

/* Write your code here ... */

}

We need to handle ISR

We will assign one global variable ‘x’ in the ‘Events.c’ file and increment this every time when SW is pressed i.e interrupt occurs.

 

int x=0; // global variable assigned for interrupt

 

void EInt1_OnInterrupt(LDD_TUserData *UserDataPtr)

{

/* Write your code here ... */

       x++; // increment the value when SW is pressed interrupt occurs

}

Now we need to write our code in ‘main.c’ file

 

extern int x; // we are using the variable declared in ‘Events.c’

 

Below are lines of codes written

 

int y;

LDD_TDeviceData *Red_Ptr, *Green_Ptr, *Blue_Ptr; // declaring pointer variable for Red, Blue, Green LED’s

 

for(;;) {

y=x % 3; // in order to detect the key press we are using modulo operator

if(y==1){

LED_G_SetVal(Green_Ptr); // switch off Green LED

LED_B_SetVal(Blue_Ptr); // switch off Blue LED

LED_R_NegVal(Red_Ptr);  // Toggle RED LED

}

if (y==2)

{

LED_R_SetVal(Red_Ptr); // switch off Red LED

LED_B_SetVal(Blue_Ptr); // switch off Blue LED

LED_G_NegVal(Green_Ptr); // Toggle Green LED

}

if (y==0)

{

LED_R_SetVal(Red_Ptr); // switch off RED LED

LED_G_SetVal(Green_Ptr); // switch off Green LED

LED_B_NegVal(Blue_Ptr); // Toggle Blue LED

}

}

 

Code explanation:

 

Whenever key press is detected a global variable(x) is incremented inside ISR.

We do modulo operation for this incremented value(x) based on the modulo operation output we can decide on the key press occurrence, this is explained below:

 

 

x

y=x%3

Y

Red Led

Green Led

Blue Led

0

0 % 3

0

off

off

Toggle

1

1 % 3

1

Toggle

off

off

2

2 % 3

2

off

Toggle

off

3

3 % 3

0

off

off

on

4

4 % 3

1

Toggle

off

off

5

5 % 3

2

off

Toggle

off

6

6 % 3

0

off

off

on

7

7 % 3

1

Toggle

off

off

8

8 % 3

2

off

Toggle

off

 

You can clean the project once and Build the project to check that your project configuration is done properly and build successfully.

 

image

 

Build progress as shown:

 

image

 

Build success as shown below:

 

image

 

Now we need to setup for Debug configuration:

As a pre-requisite the board is loaded with “JLink_OpenSDA_V2.bin” OpenSDA_V2 application driver file. And same will be used for connecting hardware to PC.

 

image

 

Select ‘GDB SEGGER J-Link Debugging' and double click or right click and select ‘New’

 

image

 

You can see a default name as project name as shown below: make sure under ‘Main’ tab C/C++ application there is *.elf file name of your project.

 

image

 

Next click on Debuger tab check ‘Device name’ field by default it is blank.

 

image

 

Now specify device name “MK64FN1M0xxx12” and click on apply and close the Debug configuration window as shown below:

 

image

 

Now connect FRDM-K64F board to PC through USB mini connector connecting to “SDA USB” port of board as shown:

 

image

 

Click on Debug icon as shown and select Debug Configuration and proceed to debug your project:

 

image

 

Click on Debug

 

image

 

proceed further y clicking 'accept' for J-Link connection Terms of use.

image

 

Now you can see the Debug window opens ready for single stepping or Running the project as shown:

 

image

Click on Resume button or press ‘F8’ button for executing the project.

 

You can see the output as shown


image

Attached is the video recording of the project execution and also the project folder for quick reference.


You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image


Attachments:
You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image
RGB_Switch Interrupt-K64.rar
  • Sign in to reply
  • Former Member
    Former Member over 11 years ago

    Never mind.  I found that I had to use "GDB OpenOCD Debugging" instead of "GDB Segger J-Link Debugging"

    And this discussion talks about using the -f kinetis.cfg options and power cycling twice when using OpenOCD:

    https://community.freescale.com/message/426107#426107

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 11 years ago

    This worked for me up to the point of starting debugging.

    Instead of a "terms of use" screen, I got an error message:

    image

    And then it timed out and gave another error message:

    image

    Any idea what is going on?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube