Here is a Project created using new KDS version 3.0 and Kinetis software development kit 1.2 on processor expert platform. This project demonstrates dimming the LED (i.e varying the duty cycle from 0-100% and 100 to 0%) using FTM module of PE implemented using freedom board FRDM-K22F.
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-K22F materials (schematics, Quick Start Package and Sample Code Package)
- latest P&E Micro windows drivers and SDA Applications
Before connecting your FRDM-K22F 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).
Tutorial instructions
The tutorial shows how to vary the duty cycle of the FTM module connected to LED with KSDK 1.2.0 in KDS 3.0 and Processor Expert using a Flex timer module Output for FRDM-K22F freedom board.
Guide is prepared for Green LED which is connected to PTA2/FTM Module 0 (FTM0), channel 7 (according to FRDM-K22F schematics).
Create new project
Create new project in KDS 3.0 with KSDK 1.2.0
Type the project name e.g. ‘FRDM-K22 dimming using FTM’, choose the board FRDM-K22F, mark off options Kinetis SDK and Processor Expert
Now, your project structure looks like this in the project explorer and Processor Expert windows:
Set Processor Expert Settings
Now, go to Components Library, find fsl_ftm component using filters KSDK 1.2.0 and Applicable to Project and by double click add the component to the Processor Expert Component View of your project.
Rename the component flexTimer1:fsl_ftm to Diming_LED
Before we proceed for changing the settings in component inspector go to CPU component and make the below changes
This is required as the pin PTA-2 is in conflict with TDO signal of Debug interface (JTAG)
Double click on Diming_LED in Components View and Component Inspector view should open automatically
Configure the FTM module as shown below:
Set frequency = 1 Hz and Duty cycle = 50% and Mode = Edge aligned PWM
Click on the Fault pin tab as shown below where it is in red mark indicating error
Now uncheck the Fault pin 2 check button so that error will be disappear
Next select the “initialisation” tab and do the settings as shown below
Select
FTM clock source = system clock
Clock source prescaler = 128
PWM channel initialisation:
Check box the channel no:7 and assign the pin for that channel
As we know from schematic Green LED is connected to PTA-2 / FTM channel-7 hence we will be choosing this option from pop down
Now we have completed all the hardware settings for the FTM module we will generate the code for this by clicking on the ‘generate ’ button
Build your project by choosing the right toolbar shortcut
Debugging the FRDM-K22F board for the first time, you need to define a Debug Configuration by selecting the appropriate option from the toolbar
For this example, we are going to select the PEMicro OpenSDA Debug mode.
Select the Debugger tab to choose the Interface OpenSDA Embedded Debug - USB Port
You can now start the Debug session!!
Launch the configuration
You should now able to see the Green LED blink with a 1Hz frequency
We will proceed further from here as our requirement is dimming the led i.e its brightness has to vary from 0 to 100% ,
Now we need to add some lines of code to perform this activity i.e we need to stop the running PWM and load its duty cycle value from 0 to 100% inside a while loop.
Below is line of code:
while(1){ FTM_DRV_CounterStart(FSL_DIMING_LED,kCounting_FTM_UP, 0, 0xFFFF, false); //start the counter //for Green led dimming assigned to channel-7 config-0 Diming_LED_ChnConfig0.uFrequencyHZ = 1000; FTM_DRV_PwmStart(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U); for(int i=100;i>=1;i--){ FTM_DRV_PwmStop(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U); Diming_LED_ChnConfig0.uDutyCyclePercent = i; FTM_DRV_PwmStart(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U); for(int x=0;x<=200000;x++){} // delay in between for the brightness to stay for a while } for(int x=0;x<=1000000;x++){} // software delay to stay while after the brightness reaches to 100% for(int i=1;i<=100;i++){ FTM_DRV_PwmStop(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U); Diming_LED_ChnConfig0.uDutyCyclePercent = i; FTM_DRV_PwmStart(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U); for(int x=0;x<=200000;x++){} // delay in between for the brightness to stay for a while } for(int x=0;x<=2000000;x++){} // software delay to stay while after the brightness reaches to 100% }
Explanation of code:
FTM_DRV_CounterStart(FSL_DIMING_LED,kCounting_FTM_UP, 0, 0xFFFF, false);//start the counter
This will load the counter with all the required parameters and start the counter.
Diming_LED_ChnConfig0.uFrequencyHZ = 1000;
This API changes the PWM running frequency to 1kHz
FTM_DRV_PwmStart(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U);
this will start the channel-7 PWM
for(int i=100;i>=1;i--){
FTM_DRV_PwmStop(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U);
Diming_LED_ChnConfig0.uDutyCyclePercent = i;
FTM_DRV_PwmStart(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U);
for(int x=0;x<=200000;x++){} // delay in between for the brightness to stay for a while
}
The above loop will stop the pwm and loads the duty cycle value from 0 to 100 % (raising brightness) through the variable ‘i’ and starts the channel-7th PWM which is connected to Green Led, there is some software delay for the brightness to stay.
This process is carried out for lowering the brightness of LED by loading the duty cycle value from 100 to 0 %
for(int i=1;i<=100;i++){
FTM_DRV_PwmStop(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U);
Diming_LED_ChnConfig0.uDutyCyclePercent = i;
FTM_DRV_PwmStart(FSL_DIMING_LED,&Diming_LED_ChnConfig0,7U);
for(int x=0;x<=200000;x++){} // delay in between for the brightness to stay for a while
}
Now, save the main.c file and build again as shown below:
After building, execute the project by launching the Debug configuration as shown:
As we had configured earlier the debug configuration settings for P&E micro device driver
Click on the resume button to start the execution
You can see the execution as shown in video output
I have enclosed the project folder and binaries for quick reference and evaluation.