PWM on FRDM-KL25

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

PWM on FRDM-KL25

4,310 次查看
mohamadmortada
Contributor I

Hello Everyone, I am using FRDM-KL25 to create PWM signal that controls DC motor. I am using PTB3 and configured it as ALT3 in order to use TPM2-CH1. After many unseccessfull attempts, I feel I need the help of someone. The code below, did not generate a PWM signal (completely dead) on PTB3. Could you please help?

#include <MKL25Z4.H>

//-----------------------------------------//

// PWM connection with DC Motor     //

// //

//  PWM Output:       //

//  Output --> PTB3 //

//  PTB3: TPM2_CH1 ALT3 //

//-----------------------------------------//

#define PWM_Dir PTB->PDDR

#define PWM_Set PTB->PSOR

#define PWM_Clear PTB->PCOR

#define PWM_Out 3

//Functions Signatures

void Registers_Configuration(void);

int main (void){

  Registers_Configuration();

  while(1){

  }

}

//Functions definition

void Registers_Configuration(){

  SystemInit();

  MCG->C1 |= 1<<1;

  MCG->C2 |= 1<<0; //MCG_C2_IRCS_MASK;

  SystemCoreClockUpdate();

  SIM->SCGC5 |= 1<<10; //Enable PORTB clocking

  PORTB->PCR[3] = (1<<9)|(1<<8); //Configure PORTB PIN 3 as ALT3 (Chip specific: TPM2_CH1)

  //PTB->PDOR |= 1<<3;

  //PTB->PSOR |= 1<<3;

  SIM->SCGC6 |= 1<<26; //Enable TPM2 clocking

  //SIM->SOPT2 |= 1<<16;

  SIM->SOPT2 |= 1<<25 | 1<<24; //Choose TPM clock source (MCGIRCLK)

  TPM2->SC = TPM_SC_CMOD(0x00);

  TPM2->SC |= TPM_SC_PS(0x01);

  TPM2->MOD = TPM_MOD_MOD(10);//1000*1µs = 1ms

  TPM2->SC |= TPM_SC_TOF_MASK;//Reset TOF bit

  TPM2->SC |= TPM_SC_CMOD(0x01);//LPTPM activate internal counter

  TPM2->CONTROLS->CnSC = 0x0028; //MSnB:MSnA = 10; ELSnB:ELSnA = 10; Edge Aligned PWM;

  TPM2->CONTROLS->CnV = 0x0FFF; // Pulse widh duty cycle

}

*/ }
0 项奖励
回复
5 回复数

2,762 次查看
mjbcswitzerland
Specialist V

Hi

Attached is a binary which you can load to your FRDM-KL25Z.

It outputs the following three PWM signals:

- Timer 2 - channel 0 1kHz 20% high/80% low [PTB18]

- Timer 2 - channel 1 1kHz 29.4% high/70.6% low [PTB3]

- Timer 0 - channel 1 1kHz 29.4% high/70.6% low [PTD1]

Using the Virtual COM UART connection (115'200 Baud) you can use the memory debugging interface (in I/O menu) to check the TPM register settings and then correct anything that is not right  in your code.

Below is the simulation in the uTasker Kinetis simulator showing the PTB3 setting.

pastedImage_3.png

PWM interface used as described in http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF

Regards

Mark

Kinetis: http://www.utasker.com/kinetis.html

KL25: http://www.utasker.com/kinetis/FRDM-KL25Z.html / http://www.utasker.com/kinetis/TWR-KL25Z48M.html

For the complete "out-of-the-box" Kinetis experience and faster time to market

:smileyinfo: Out-of-the-box support for 46 Kinetis boards and 10 IDEs (460 combinations from a single code source with no porting required)

0 项奖励
回复

2,762 次查看
mohamadmortada
Contributor I

Hi Mark,

Sorry forgot to add my code to my previous email. Here's the code.

Please note that I am not experienced in Microcontrollers.

#include <MKL25Z4.H>

//

0 项奖励
回复

2,762 次查看
mjbcswitzerland
Specialist V

Hi

I don't see any code.

Please use one of the following methods:

1. If you are a student please post the complete project as an attachment (activate "Use advanced editor" and then the attach button will apear in the bottom right hand corner of the form. Attach the complete project (zipped) because code clippings are often not enough to identify the root cause of problems).
2. If your work is commercial and requires a fast fix please arrange a support session at  http://www.utasker.com/support.html

3. If you require free but unsupported working project code (with KL25 PWM driver) download it from http://www.utasker.com/forum/index.php?topic=1721.msg6697#msg6697

4. In case you would like to use an optimal solution in a real KL25 project, consider a complete project solution with personal support from http://www.utasker.com/Licensing/request.html

Regards

Mark

0 项奖励
回复

2,762 次查看
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Mohamad,

Regarding your question, I seems that your code is wrong:

1)Anyway, the TPM2->MOD value should be greater than   TPM2->CONTROLS->CnV, in your code,     TPM2->CONTROLS->CnV = 0x0FFF;   TPM2->MOD = TPM_MOD_MOD(10); it is wrong.

2)it is correct   TPM2->SC |= TPM_SC_CMOD(0x01);//LPTPM activate.  If you set the CMOD bits as "10",  you should input an external  clock from TPM_CLKIN1 or TPM_CLKIN2, and you have to write the TPM2CLKSEL bits in SIM_SOPT4 to select the external clock pins.

Hope it can help you.

BR

Xiangjun Rong

0 项奖励
回复

2,762 次查看
mohamadmortada
Contributor I

Hello Mark,

Thanks for your answer. However, I'm still unable to get the PWM working.

Actually, I made some advancement, I now can see the counter counting in

debug mode. Meanwhile, no output is generated on PTB3 pin. I am using

oscilloscope.

I downloaded your .bin code and I can see the PWM signal on the scope along

with RGB led ob FRDM-KL25 board changing colors.

I don't have the u Tasker you suggested.

Could you please tell me what's is missing in my code? I am using keil.

Note that, I can't see the C1SC register to set it up; however (as shown in

the code below) I could have access to CnSC as: TPM2->Controls->CnSC.

Since I am using PTB3, then, as I understood, have to use TPM2_Ch1. But, I

can't have access to C1SC.

Here is my code, could you please help?

Best regards,

M.M.,

On Thu, Oct 29, 2015 at 6:46 PM, mjbcswitzerland <

0 项奖励
回复