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 UART console communication using debug_console and fsl_gpio component implemented using 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
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 connection 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
And UART connections are:
Simple Instruction to execute the project
Step-1: Create a bare bone project
Step-2: Add the component debug_console, fsl_tpm to project
Step-3: Configure the hardware settings for UART and TPM components
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 uart” using same steps as earlier
Now the project looks like as shown below
Step-2: Add the component debug_console, fsl_tpm to project
Now i am going to add the component fsl_debug_console and fsl_tpm from component library
Adding fsl_debug_console
Step-3: Configure the hardware settings for UART and TPM components
Double click on the selected debug component “fsl_debug_console “ and select the baud rate of 19200 as shown below:
Now select the uart pins for these settings:
Settings for fsl_tpm
Double click on fsl_tpm component and make the below settings:
Enable to reload the counter on Trigger
Set the counter frequency to 1Hz with a Duty cyle of 50%
Allocate TPM0 Channel 4 to Pin LED2_RED (defined by board selection)
Select Clock source MCGIRCLK and choose the Prescaler Divide by 128
Enable the Initialization of TPM0 Channel 4
Save the configuration by pressing ctrl+s
Click on the generate button as shown below:
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 RED LED through Debug UART
The code is given below:
/* For example: for(;;) { } */
unsigned int led;
unsigned static int i=0;
tpmTmr1_ChnConfig0.uFrequencyHZ = 1000;
tpmTmr1_ChnConfig0.uDutyCyclePercent = 1;//inverted duty cycle %
TPM_DRV_PwmStart(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 4U);
while(1){
debug_printf("\npress the button 1 to Decrease the pwm control and 2 to Increase the pwm duty cycle\r\n");
if(debug_scanf("%s",&led)!=0);
switch(led){
case '1':
i=i+5;
if(i<=100){
TPM_DRV_PwmStop(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 4U);
tpmTmr1_ChnConfig0.uDutyCyclePercent = i;
TPM_DRV_PwmStart(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 4U);
debug_printf("You have pressed %c and inverted duty cycle %% value is %d%% \r\n",led,i);
}
else if (i>=101){
i=100;
debug_printf("You have pressed %c and inverted duty cycle %% value is %d%% \r\n",led,i);
}
break;
case '2':
i=i-5;
if(i<=100){
TPM_DRV_PwmStop(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 4U);
tpmTmr1_ChnConfig0.uDutyCyclePercent = i;
TPM_DRV_PwmStart(FSL_TPMTMR1,&tpmTmr1_ChnConfig0, 4U);
debug_printf("You have pressed %c and inverted duty cycle %% value is %d%% \r\n",led,i);
}
else if(i<=-5){
i=0;
debug_printf("You have pressed %c and inverted duty cycle %% value is %d%% \r\n",led,i);
}
break;
default:
debug_printf("You have entered wrong choice\r\n");
} //switch
} //while(1)
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
This code will ask user to enter choices ‘1’ or ‘2’ from keyboard to increase or decrease the PWM control for the Red LED connected on board through UART communication. 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:
The project compiles with with no error, we will proceed further in execution the project:
Step-5: Execute the project and see the result
Click on debug configuration and select the proper OpenSDA usb port from debugger tab as shown below:
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:
Now click on the resume button as shown below:
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 ‘1’ on keyboard 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 ‘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:
I have enclosed the project folder and executable for quick evaluation























