AnsweredAssumed Answered

PWM on FRDM-KL25

Question asked by Mohamad Mortada on Oct 28, 2015
Latest reply on Nov 1, 2015 by xiangjun.rong

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

 

}

*/ }

Outcomes