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
Documents Trick & Tips for FRDM-KL25Z: Bits and Pins with Freescale Kinetis
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Freedom development platform to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: FreescaleTools_and_Software
  • Date Created: 8 Jan 2013 3:37 PM Date Created
  • Last Updated Last Updated: 12 Mar 2013 6:14 PM
  • Views 1393 views
  • Likes 0 likes
  • Comments 0 comments
Related
Recommended

Trick & Tips for FRDM-KL25Z: Bits and Pins with Freescale Kinetis

This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.

 

image image

  

In “Optimizing the Kinetis gcc Startup” I stripped down the fat of my startup code. Now time to add some useful things. And what does a microcontroller like the KL25Z on the Freedom FRDM-KL25ZFRDM-KL25Z board have: Pins! And this means I have bits to set and read :-) .

  

Fullscreen 7506.contentimage_22426.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

FRDM-KL25Z Board

 

 

 

So this post is about how to use digital I/O pins on the KL25Z to either turn on or off, or to read in values. In the LED tutorial I used my LED component to control them, but of course it is possible to do that kind of thing on a lower level: BitIO_LDD.

 

Note: I assume that you are familiar with the basics like creating a Processor Expert project, generating code and debugging it. If not, please see one of my earlier tutorials: Enlightening the Freedom Board

 

 

BitIO_LDD for an Output Pin

 

The BitIO_LDD is included in CodeWarrior for MCU10.3. Adding that component to the project is easy from them Components Library View:

  

Fullscreen 6011.contentimage_22427.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Adding BitIO_LDD Component

 

 

:!: There is as well a BitsIO_LDD component available (notice the ‘s’). This one is able to deal with multiple bits in a port, but we want here just to do it with a single bit.

 

This adds it to my project:

 

Fullscreen 0243.contentimage_22428.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

BitIO in Project

 

  

Time to configure it! I map it to the Red LED on my FRDM-KL25Z board which is connected to pin PTB18. For this I use the ‘Expert’ mode, give it a name (“RED”), assign it to PTB18, name the (optional) signal name and set it to output. I enable ‘Auto Initialization’ so the Processor Expert startup code initializes the pin for me:

 

Fullscreen 0647.contentimage_22429.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Configured Pin for Red LED

 

 

Time to try it out! For this I add two lines of code to the main() routine:

 

  

01 int main(void)

02 /*lint -restore Enable MISRA rule (6.3) checking. */

03 {

04 /* Write your local variable definition here */

05 

06 /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/

07 PE_low_level_init();

08 /*** End of Processor Expert internal initialization. ***/

09 

10 RED_SetVal(NULL);

11 RED_ClrVal(NULL);

12 

13 /*** Don't write any code pass this line, or it will be deleted during code generation. ***/

14 /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/

15 #ifdef PEX_RTOS_START

16 PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */

17 #endif

18 /*** End of RTOS startup code. ***/

19 /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/

20 for(;;){}

21 /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/

22 } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

 

  

:?: Wondering about the NULL parameter? The logical device drivers (LDD) components in Processor Expert need this. I will explore things how this can be optimized later on as the overhead caused by this is really not good even for such a powerful ARM Cortex-M0+ microcontroller. 

 

Time to create code (selecting the ProcessorExpert.pe file and context menu t ‘Generate Processor Expert Code’, build it and download it. The code should turn on the RED led, and then turn it off. Simple as that :-) .

 

The same way as of above, I add a BitIO_LDD for Green (pin PTB19) and Blue (pin PTD1):

 

Fullscreen 2100.contentimage_22430.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Red, Green and Blue LED as BitIO_LDD

   

   

BitIO_LDD for an Input Pin

  

Now I want to use a pin as input. As the FRDM-KL25Z has no dedicated button, I’m going to use the Reset button (SW1) instead which is connected to PTA20:

  

Fullscreen 3660.contentimage_22431.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

SW1 on the FRDM-KL25Z Board

  

   

Again I add a BitIO_LDD to my project and configure it:

  

Fullscreen 7802.contentimage_22432.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

SW1 connected to PTA20

  

  

Now I have a little problem, as PTA20 is configured as reset pin for the CPU. So I need to tell the CPU component that I’m using the reset pin for my purpose. For this I select the Cpu in the Components view and use the Inspector menu:

  

Fullscreen 6406.contentimage_22433.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

CPU in Components View

 

 

Here it tells me again that I’m ‘double booking’ that pin:

 

Fullscreen 4353.contentimage_22434.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Cpu with Component Inspector

  

 

What I need to do is to disable it:

 

Fullscreen 2210.contentimage_22435.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Disabled Reset Pin

 

   

:idea: Disabling the Reset Pin has other impact too, see “How (not) to Secure my Microcontroller”

 

But wait: there is one more setting:

   

Fullscreen 8156.contentimage_22436.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Reset Pin Function Disabled

  

  

Normally would need to configure an input pin with an internal pull-up resistor or similar. But SW1 already has a 10k Ohm pull-up resistor R4 attached:

 

Fullscreen 8561.contentimage_22437.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Target Reset and Bootloader Push Button

  

  

Time to test it in my code.

 

I change the above code in main() now to turn on the LED if I press the button, plus to prove that pressing the reset button does *not* cause a reset, I turn on the green LED for a second (using the Wait component):

 

Fullscreen 2703.contentimage_22438.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Added Wait Component

 

 

And my code looks like this:

 

  

01 GREEN_ClrVal(NULL); /* turn on green LED */

02 WAIT1_Waitms(1000);

03 GREEN_SetVal(NULL); /* turn off green LED */

04 for(;;) {

05 if (SW1_GetVal(NULL)==0) { /* button low level => pressed */

06 RED_ClrVal(NULL); /* LED cathode is connected to microcontroller pin: low level turns it on */

07 BLUE_SetVal(NULL); /* turn off blue led */

08 } else {

09 RED_SetVal(NULL); /* turn off red led */

10 BLUE_ClrVal(NULL); /* turn on blue led */

11 }

12 }

 

 

While reset/SW1 is not pressed, the blue LED is on:

  

Fullscreen 1207.contentimage_22439.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Blue LED on if button is not pressed

    

    

Pressing the reset/SW1 button turns the red LED on:

  

Fullscreen 8154.contentimage_22440.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Pressing SW1 turns Red LED on

     

    

Hardware Settings

   

I know now how to configure pins for input or output, and how to set or read them. But there is more you typically can configure. Things like pull-up or pull-down resistors, slew rates or driving strength. All these settings are missing in the above components. But they are available in the non-LDD (Logical Device Driver) land :-( .

  

:idea: For other Processor Expert supported (non-LDD) cores (like the S08), such hardware settings can be configured in the BitIO component.

   

So it looks like these important settings are missing here. I was lucky that my reset pin in the example above had a pull-up resistor on the board. But I know that the hardware supports an internal pull-up (or pull-down) resistor. So it should be possible to establish a ‘resistor-less’ switch between GND and pin 14 on the J2 header on the FRDM-KL25Z board:

  

Fullscreen 3386.contentimage_22441.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

J2 with switch connection (Source: Freescale FRDM-KL25Z Schematics)

  

  

D15 is connected to the PTE1 pin on the KL25Z. And I want it as an input with an internal pull-up. So how to configure it? The trick is to use Init_GPIO with pin sharing.

 

 

Pin with Pin Sharing

  

First, I add a normal BitIO_LDD for my new switch SW2, and configure it for PTE1:

 

Fullscreen 4857.contentimage_22442.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

SW2 Configured

   

   

I’m no going to configure the hardware (internal pull-up) in an Init_GPIO.

 

 

Init_GPIO

 

Init_GPIO is a component which only performs initialization, nothing else. So I add it to my project:

  

Fullscreen 3362.contentimage_22443.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Init_GPIO Component Added to Project

  

  

Next, I configure it to use PTE1 with enabled pull-up resistor:

  

Fullscreen 2100.contentimage_22444.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Init of PTE1 with Error

  

  

But: it reports an error as PTE1 is already used by my BitIO_LDD component. How to solve this? The solution is to configure the pin for ‘pin sharing’: this means that several Processor Expert components can share that pin. To enable pin sharing I select the context menu on that pin:

  

Fullscreen 0045.contentimage_22445.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Enabling Pin Sharing

  

 

Now notice the subtle color change of that icon which can easily be missed:

 

Fullscreen 8551.contentimage_22446.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Pin Sharing Enabled Icon

  

 

But: it means that it is enabled for sharing, and the error from above is fixed :mrgreen: .

 

Time to generate Processor Expert code and to update my source:

 

 

01 GREEN_ClrVal(NULL); /* turn on green LED */

02 WAIT1_Waitms(1000);

03 GREEN_SetVal(NULL); /* turn off green LED */

04 for(;;) {

05 if (SW1_GetVal(NULL)==0) { /* button low level => pressed */

06 RED_ClrVal(NULL); /* LED cathode is connected to microcontroller pin: low level turns it on */

07 } else {

08 RED_SetVal(NULL); /* turn off red led */

09 }

10 if (SW1_GetVal(NULL)==0) { /* button low level => pressed */

11 BLUE_SetVal(NULL); /* turn off blue led */

12 } else {

13 BLUE_ClrVal(NULL); /* turn on blue led */

14 }

15 }

 

  

Time again to try it out on the board: it will turn on the green LED for a second and then turn it off. Then the LED will be off. If I press SW1, the led will be red (as above).

  

Fullscreen 3683.contentimage_22448.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

LED off with open switch

  

  

If make put PTE1 to ground with a wire (emulating a switch), the LED will turn blue:

 

Fullscreen 2287.contentimage_22449.html Download
<html><head><title>Jive SBS</title></head>
<body><font face="arial,helvetica,sans-serif">
<b>Error</b><br><font size="-1">
An general error occurred while processing your request.
</font></font></body></html>

Blue LED on with switch closed

 

 

Summary

 

Doing Bit I/O really is easy with the Eclipse based CodeWarrior for MCU10.3. The project and source files created and discussed is available here.

 

Happy Biting :-)

  

PS: As noticed earlier: The LDD approach comes with some avoidable overhead. I preparing a follow-up article on this how to get things optimized, so it fits my needs and expectations. Stay tuned …

  • pins
  • bits
  • processor_expert
  • freescale
  • kinetis
  • codewarrior
  • frdm-kl25z
  • kinetis-l
  • freedom
  • Share
  • History
  • More
  • Cancel
  • Sign in to reply
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 © 2026 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