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 Kinetis Design Studio 3.0 Kinetis SDK 1.2 Processor-Expert using FRDM-KL43Z: Quick tutorial on dimming control (varying PWM duty cycle) using user push buttons
  • 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: 28 Aug 2015 2:11 PM Date Created
  • Views 804 views
  • Likes 0 likes
  • Comments 2 comments
  • pwm
  • pwm_using_push_button
  • kl43z
Related
Recommended

Kinetis Design Studio 3.0 Kinetis SDK 1.2 Processor-Expert using FRDM-KL43Z: Quick tutorial on dimming control (varying PWM duty cycle) using user push buttons

bheemarao
bheemarao
28 Aug 2015

Here is a Project created using new KDS version 3.0 and Kinetis software development kit 1.2 on processor expert platform. This project controls the brightness of LED i.e varying the duty cycle using two user push button, one button to increase the brightness and other to decrease the brightness of the LED connected through PWM of the KL43Z freedom board.

 

Requirements

To run successfully this exercise, you need first to download following packages (link enclosed):

- IDE toolchain Kinetis Design Studio (KDS) min v3.0
- microcontroller Library
Kinetis Software Development Kit (KSDK) min 1.2
-
FRDM-KL43Z materials (schematics, Quick Start Package and Sample Code Package)
- latest
P&E Micro windows drivers and SDA Applications

[http://www.pemicro.com/downloads/main_downloads_temp/201507291203137773819/Pemicro_OpenSDA_Debug_MSD_Update_Apps_2015_03_12.zip ]


Before connecting your FRDM-KL43Z for the first time to the USB port of your computer, install the P&E Micro windows driver to ensure a correct detection of the board.

Install first the IDE toolchain KDS and when the installation ended successfully, install the MCU Library KSDK keeping the proposed installation path c:\freescale\kds or ksdk unchanged.


Launch KDS and define your workspace path, then select Help, Install New Software, Add, Archive, C:\Freescale\KSDK_1.2.0\tools\eclipse_update, select KSDK_1.2.0_Eclipse_Update.zip and press Open, OK, select KSDK Eclipse Update, press Next, Accept the Licence Agreement and press Finish. The KSDK libraries are now directly available in the IDE toolchain.


The automated code generator Processor Expert is already included in the IDE Toolchain (available separately as Processor Expert Driver Suite for other IDE toolchains).

 

The Hardware connections of the KL43Z from schematic are:

GPIO pin connection of the board:

LED1 is connected to PTD-5 Green LED

LED2 is connected to PTE31 RED LED


image

And UART connections are:

image

Push button schematic connection are:

image

Button-1: PTC-3

Button-2: PTA-4


Simple Instruction to execute the project

Step-1: Create a bare bone project

Step-2: Add the component debug_console, fsl_tpm, fsl_gpio to project

Step-3: Configure the hardware settings for UART and TPM user buttons

Step-4: Add the software code and compile/Build the project

Step-5: Execute the project and see the result.

 

Step-1: Create a bare bone project

I have created a project by name “KL43Z PWM using push button” using same steps as earlier

image

image


image

image

image

Now the project looks like as shown below

image

Step-2: Add the component debug_console, fsl_tpm, fsl_gpio to project

Now i am going to add the component fsl_debug_console and fsl_tpm from component library

image

Adding fsl_debug_console


image

Adding fsl_gpio component

image

Step-3: Configure the hardware settings for UART, TPM and GPIO components


Double click on the selected debug component “fsl_debug_console “ and select the baud rate of 19200 as shown below:

image

Now select the uart pins for these settings:

image

Settings for fsl_tpm

Double click on fsl_tpm component and make the below settings:

image

Enable to reload the counter on Trigger


Set the counter frequency to 1Hz with a Duty cyle of 50%
Allocate TPM0 Channel 5 to Pin LED2_Green (defined by board selection)


Select Clock source MCGIRCLK and choose the Prescaler Divide by 128
Enable the Initialization of TPM0 Channel 5

image

image

Save the configuration by pressing ctrl+s

 

Settings for fsl_gpio for connecting user push buttons

Double click on fsl_gpio component and make the below settings:

image

image

Now we need to enable the Interrupts for Port-A  and Port-C in Events tab of the fsl_gpio component

Button-1: PTC-3, Button-2: PTA-4

image

Next we can see the interrupts will get enabled automatically in properties tab of the component as shown below:

image

Click on the generate button as shown below:

image

Step-4: Add the software code and compile/Build the project

 

Now write the below lines of codes inside the main file to control the PWM of Green LED through user push button.

First we need to write interrupt handler for Port-A and Port-C which is connected to user push buttons.

image

The code is given below:


/* For example: for(;;) { } */
int i=0;
extern unsigned char flag_up, flag_down;
 tpmTmr1_ChnConfig0.uFrequencyHZ = 1000;
 TPM_DRV_PwmStop(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 5U);
 tpmTmr1_ChnConfig0.uDutyCyclePercent = i;
 TPM_DRV_PwmStart(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 5U);
 debug_printf("LED  brightness %% value is %d \r\n",100-i);


while(1){
        if (flag_up == 1){
                flag_up = 0;
                i=i+5;
                    if(i<=100 && i>=0){
                    TPM_DRV_PwmStop(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 5U);
                    tpmTmr1_ChnConfig0.uDutyCyclePercent = i;
                    TPM_DRV_PwmStart(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 5U);
                    debug_printf("You have pressed button-down and brightness %% value is %d \r\n",100-i);
                    }

              if(i>100){
                    i=100;
                    debug_printf("You have pressed button-down and brightness %% value is %d Press the Down button to decrease brightness\r\n",100-i);
                      }



            }
        if (flag_down == 1){
                flag_down = 0;
                i=i-5;

                if(i<=100 && i>=0){
                    TPM_DRV_PwmStop(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 5U);
                    tpmTmr1_ChnConfig0.uDutyCyclePercent = i;
                    TPM_DRV_PwmStart(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 5U);
                    debug_printf("You have pressed button-down and brightness %% value is %d \r\n",100-i);
                    }
                    if(i<=0){
                          i=0;
                          debug_printf("You have pressed button-down and brightness %% value is %d Press the UP button to increase brightness \r\n",100-i);
                          }
                }
 } //while(1)

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

 

 

This code will write the PWM value to the TPM module connected to RED LED. The brightness of LED (varying the duty cycle) is increased/decreased using two user push button, one button to increase the brightness and other to decrease the brightness of the LED connected. And the result can be seen on board with response on hyper terminal.

 

Now compile/build the project by clicking the generate button as shown below:


image

The project compiles with with no error, we will proceed further in execution the project:

image

Step-5: Execute the project and see the result


image


Click on debug configuration and select the proper OpenSDA usb port from debugger tab as shown below:

image

image

Before clicking on the resume button make sure to open the hyper terminal of your choice with baud rate of 19200, 8N1

I have opened putty with the said settings:

image

Now click on the resume button as shown below:


image

Working logic of the project:


The PWM will be varied for every 5%. i.e PWM increases from 0 to 100% for every 5% increase by pressing the button-1  and reaches to 100% (as led is connected in active-low led fades down from 100% to 0%) then reaches to 100% (led is switched off).

Again by pressing the button-2 from keyboard the PWM varies from 100% tp 0% duty cycle (brightness of the RED LED slowly increases from 0 to 100% as led is connected in active-low connection).


Below is my video output seen on the hyper terminal window:

 

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

 

 

I have enclosed the project folder and executables for quick evaluation


Happy working on PWM using push button image image image image image

Attachments:
KL43Z PWM using push button.rar
KL43Z PWM using push button-srec.rar
  • Sign in to reply
  • sdorf
    sdorf over 10 years ago

    Hello Bheemarao;

     

    Very nice post!!!

     

    Doing this post reproduction I found two issues that I want to share with you.

     

    1. If I select all three components (debug console, gpio and tpm) and then start configuration there is a pin conflict between debugger and gpio. Conflict disappears after uart and gpio configuration. I think it is good to make a note on it.

    2. I think that:

      1. Pull up must be selected

      2. Interrupt on rising or falling edge must be selected (screen shot is attached).

    image

    Regards

    Shaul

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 10 years ago

    Nice detailed post.

     

    DAB

    • 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 © 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