Esempio n. 1
0
void FTM_init  (FTM_MemMapPtr base)
{
	// Habilito el pin PTC1 como salida del canal 0
	PORTC_PCR1 = (PORT_PCR_ISF_MASK |
	              PORT_PCR_MUX(0x04));
	PORTB_PCR18 = (PORT_PCR_ISF_MASK |
		              PORT_PCR_MUX(0x03));
	
	// Habilito la llave general de las interrupciones
	//NVICISER1 |= NVIC_ISER_SETENA(0x0400);
	
	// Habilito el clock gating
	SIM_SCGC6 |= SIM_SCGC6_FTM0_MASK;
	SIM_SCGC6 |= SIM_SCGC6_FTM2_MASK;
	
	// Deshabilito la proteccion de escritura
	FTM_MODE_REG(base) 		= FTM_MODE_WPDIS_MASK;
	
	// Apago los flag de interrupciones
	FTM_STATUS_REG(base) 	= (FTM0_STATUS &
				       0x00);
	
	// Deshabilito los clocks y borro la configuracion
	FTM_SC_REG(base) 		= 0x00;
	
	// Configuro el canal 0 como nada
	FTM_CnSC_REG(base,0) 		= 0x00;
	
	// Configuro la salida inicial en alto (los LEDs son activo-bajo)
	FTM_MODE_REG(base) 		= FTM_MODE_INIT_MASK;
	
	// Configuro el PWM a duty 25% y reinicio el contador
	FTM_CnV_REG(base,0) 		= 0x0000;
	FTM_MOD_REG(base) 	    = 0x0000;
	FTM_CNTIN_REG(base) 		= 0x0000;
	FTM_CNT_REG(base) 		= 0x0000;
	
	FTM_PWMLOAD_REG(base) 	= (FTM_PWMLOAD_LDOK_MASK | 
					   FTM_PWMLOAD_CH0SEL_MASK);
	
	// Habilito las interrupciones y los clocks, y configuro el prescaler
	FTM_SC_REG(base) 		= (FTM_SC_TOIE_MASK |
			   		   FTM_SC_CLKS(0x01) |
			           FTM_SC_PS(0x0F));
	// Habilito los contadores del modulo
	FTM_CONF_REG(base) 		= FTM_CONF_BDMMODE(0x03);
}
Esempio n. 2
0
void FTM_PWMEdgeAligned(FTM_MemMapPtr base, bool outputTrue){
	FTM_setPeriod(base, 0x0000);
	FTM_setDuty(base, 0x0000);
	FTM_CnSC_REG(base,0) 	&=	~FTM_CnSC_MSA_MASK;
	FTM_CnSC_REG(base,0) 	|=	FTM_CnSC_MSB_MASK;
	FTM_SC_REG(base) &=	~FTM_SC_CPWMS_MASK;
	FTM_COMBINE_REG(base) &=	~FTM_COMBINE_COMBINE0_MASK;
	FTM_COMBINE_REG(base) &=	~FTM_COMBINE_DECAPEN0_MASK;
	if(outputTrue == HIGHTRUE){
		FTM_CnSC_REG(base,0) 	|=	FTM_CnSC_ELSB_MASK;
		FTM_CnSC_REG(base,0) 	&=	~FTM_CnSC_ELSA_MASK;
	}
	else{
		FTM_CnSC_REG(base,0) 	|=	FTM_CnSC_ELSA_MASK;
		FTM_CnSC_REG(base,0) 	&=	~FTM_CnSC_ELSB_MASK;
	}
	
	FTM_SC_REG(base) &=	~FTM_SC_CPWMS_MASK;
}
Esempio n. 3
0
void FTM_PWMCenterAligned(FTM_MemMapPtr base, bool outputTrue){
	FTM_CnSC_REG(base,0) 	&=	~FTM_CnSC_MSA_MASK;
	FTM_CnSC_REG(base,0) 	|=	FTM_CnSC_MSB_MASK;
	FTM_SC_REG(base) |= FTM_SC_CPWMS_MASK;
	FTM_COMBINE_REG(base) &=	~FTM_COMBINE_COMBINE0_MASK;
	FTM_COMBINE_REG(base) &=	~FTM_COMBINE_DECAPEN0_MASK;
	
	if(outputTrue == HIGHTRUE){
		FTM_CnSC_REG(base,0) 	|=	FTM_CnSC_ELSB_MASK;
		FTM_CnSC_REG(base,0) 	&=	~FTM_CnSC_ELSA_MASK;
	}
	else{
		FTM_CnSC_REG(base,0) 	|=	FTM_CnSC_ELSA_MASK;
		FTM_CnSC_REG(base,0) 	&=	~FTM_CnSC_ELSB_MASK;
	}
}
Esempio n. 4
0
File: ftm_pwm.c Progetto: aarzho/k60
/**
 *    @brief pwm2模块相关寄存器配置
 *
 *    @param   channel  pwm2所选的通道  
 */
static void pwm_mod2_set(ID channel)
{
	/*使能ftm0模块时钟*/
	SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK;
	
	/*选择时钟源,分频系数,以及计数器计数方式*/
	FTM_SC_REG(FTM2_BASE_PTR) = FTM_SC_CLKS(FTM2_PWM_CLK) | FTM_SC_PS(FTM2_PWM_PRESCALE) | ((FTM2_PWM_MODE & 0x10)<<1); 
	
	/*设置计数器计数初值*/
	FTM_CNTIN_REG(FTM2_BASE_PTR) = CNTIN_VALUE;
	
	/*初始化计数器*/
	FTM_CNT_REG(FTM2_BASE_PTR) = FTM2_INIT_VALUE;
	
	/*设置计数器最大模值*/
	FTM_MOD_REG(FTM2_BASE_PTR) = FTM2_PWM_CYCLE;
	
	/*设置FTM模块的工作方式*/
	FTM_CnSC_REG(FTM2_BASE_PTR,channel) = ((FTM2_PWM_MODE & 0x0f)<<2);
	
	/*设置初始化占空比*/
	FTM_CnV_REG(FTM2_BASE_PTR,channel) = PWM_INIT_VALUE;
}
Esempio n. 5
0
void Qd::setMode ()
{
	//===Setings pin===//
	//pha
	pha.settingPinPort(QdDef::PhaPort);
	pha.settingPin(QdDef::PhaPin, QdDef::PhaAlt);

	//phb
	phb.settingPinPort(QdDef::PhbPort);
	phb.settingPin(QdDef::PhbPin, QdDef::PhbAlt);

	//===Settings timer===//
	FTM_SC_REG(ftm_ptr[num_ftm]) = 0;
	setPeriod(FTM_MOD_MOD_MASK);
	setInitValue(0);
	FTM_MODE_REG (ftm_ptr[num_ftm]) |= FTM_MODE_WPDIS_MASK;
	FTM_MODE_REG (ftm_ptr[num_ftm]) |= FTM_MODE_FTMEN_MASK;

	FTM_CnSC_REG(ftm_ptr[num_ftm], 0) = 0;
	FTM_CnSC_REG(ftm_ptr[num_ftm], 1) = 0;
	FTM_QDCTRL_REG(ftm_ptr[num_ftm]) |= FTM_QDCTRL_QUADEN_MASK|FTM_QDCTRL_PHAFLTREN_MASK|FTM_QDCTRL_PHBFLTREN_MASK;
	FTM_FILTER_REG (ftm_ptr[num_ftm]) |= FTM_FILTER_CH0FVAL(2) | FTM_FILTER_CH1FVAL(2) ;
	start ();
}
Esempio n. 6
0
void FTM_setPrescaler(FTM_MemMapPtr base, bool prescaler){
	FTM_SC_REG(base) |= FTM_SC_PS(prescaler);
}
Esempio n. 7
0
/**   
 *    @brief    编码器初始化函数
 *    
 *    @param    port     端口号,由宏定义声明
 *    
 *    @return   ER       错误码
 *      
 *    @note    系统时钟频率最好是大于输入信号频率,可同过分频系数DIVIDE对系统进行1~128分频(寄存器FTMx_SC的后三位)
*/
ER ftm_decoder_init(PORT port)
{
    switch (port)
    {
        case PTA8_9:                            
            SIM_SCGC6 |= SIM_SCGC6_FTM1_MASK; /* 使能FTM时钟 */

            /* 选择引脚复用功能 */
            PORTA_PCR8 = PORT_PCR_MUX(6);
            PORTA_PCR9 = PORT_PCR_MUX(6);

            /* 配置成正交编码功能  */
            FTM_CNTIN_REG(FTM1_BASE_PTR) = 0x0000; /* 初始值 */
            FTM_MOD_REG(FTM1_BASE_PTR)   = 0xffff; /* 可根据需要设定 */

            FTM_FMS_REG(FTM1_BASE_PTR)   = 0x00;   /* write protect disable */
            FTM_MODE_REG(FTM1_BASE_PTR)  = 0x05;   /* 不使能write protect 功能,缺省值;FTMEN=1; */

            /* 0001,1000  CLKS=11:extern Clock;PS=000|DIVIDE:system clock divided by 1or2or4or8or16or32or64or128; */
            FTM_SC_REG(FTM1_BASE_PTR)     = 0x18 | FTM_DEC_DEV1; 
            /* 0000,1101 不是能过滤,QUADMODE=1:count and direction mode;QUADIR=1:increasing;QUADEN=1 */
            FTM_QDCTRL_REG(FTM1_BASE_PTR) = 0x0d; 
            break;
            
        case PTA10_11:
            SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK; /* 使能FTM时钟 */

            /* 选择引脚复用功能 */
            PORTA_PCR10 = PORT_PCR_MUX(6);
            PORTA_PCR11 = PORT_PCR_MUX(6);

            /* 配置成正交编码功能  */
            FTM_CNTIN_REG(FTM2_BASE_PTR) = 0x0000; /* 初始值 */
            FTM_MOD_REG(FTM2_BASE_PTR)   = 0xffff; /* 可根据需要设定 */

            FTM_FMS_REG(FTM2_BASE_PTR)   = 0x00;   /* write protect disable */
            FTM_MODE_REG(FTM2_BASE_PTR)  = 0x05;   /* 不使能write protect 功能,缺省值;FTMEN=1; */

            /* 0001,1000   CLKS=11:extern Clock;PS=000|DIVIDE:system clock divided by 1or2or4or8or16or32or64or128; */
            FTM_SC_REG(FTM2_BASE_PTR)     = 0x18 | FTM_DEC_DEV1;
            /* 0000,1101 不是能过滤,QUADMODE=1:count and direction mode;QUADIR=1:increasing;QUADEN=1 */
            FTM_QDCTRL_REG(FTM2_BASE_PTR) = 0x0d;
            break;
            
        case PTA12_13: 
            SIM_SCGC6 |= SIM_SCGC6_FTM1_MASK; /* 使能FTM时钟 */

            /* 选择引脚复用功能 */
            PORTA_PCR12 = PORT_PCR_MUX(7);
            PORTA_PCR13 = PORT_PCR_MUX(7);

            /* 配置成正交编码功能  */
            FTM_CNTIN_REG(FTM1_BASE_PTR) = 0x0000; /* 初始值 */
            FTM_MOD_REG(FTM1_BASE_PTR)   = 0xffff; /* 可根据需要设定 */

            FTM_FMS_REG(FTM1_BASE_PTR)   = 0x00;   /* write protect disable */
            FTM_MODE_REG(FTM1_BASE_PTR)  = 0x05;   /* 不使能write protect 功能,缺省值;FTMEN=1; */

            /* 0001,1000   CLKS=11:extern Clock;PS=000|DIVIDE:system clock divided by 1or2or4or8or16or32or64or128; */
            FTM_SC_REG(FTM1_BASE_PTR)     = 0x18 | FTM_DEC_DEV1; 
            /* 0000,1101 不是能过滤,QUADMODE=1:count and direction mode;QUADIR=1:increasing;QUADEN=1 */
            FTM_QDCTRL_REG(FTM1_BASE_PTR) = 0x0d; 
            break;
            
        case PTB0_1:
            SIM_SCGC6 |= SIM_SCGC6_FTM1_MASK; /* 使能FTM时钟 */

            /* 选择引脚复用功能 */
            PORTB_PCR0 = PORT_PCR_MUX(6);
            PORTB_PCR1 = PORT_PCR_MUX(6);

            /* 配置成正交编码功能  */
            FTM_CNTIN_REG(FTM1_BASE_PTR) = 0x0000; /* 初始值 */
            FTM_MOD_REG(FTM1_BASE_PTR)   = 0xffff; /* 可根据需要设定 */

            FTM_FMS_REG(FTM1_BASE_PTR)   = 0x00;   /* write protect disable */
            FTM_MODE_REG(FTM1_BASE_PTR)  = 0x05;   /* 不使能write protect 功能,缺省值;FTMEN=1; */

            /* 0001,1000   CLKS=11:extern Clock;PS=000|DIVIDE:system clock divided by 1or2or4or8or16or32or64or128; */
            FTM_SC_REG(FTM1_BASE_PTR)     = 0x18 | FTM_DEC_DEV1; 
            /* 0000,1101 不是能过滤,QUADMODE=1:count and direction mode;QUADIR=1:increasing;QUADEN=1 */
            FTM_QDCTRL_REG(FTM1_BASE_PTR) = 0x0d; 
            break;
            
        case PTB18_19:
            SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK; /* 使能FTM时钟 */

            /* 选择引脚复用功能 */
            PORTB_PCR18 = PORT_PCR_MUX(6);
            PORTB_PCR19 = PORT_PCR_MUX(6);

            /* 配置成正交编码功能  */
            FTM_CNTIN_REG(FTM2_BASE_PTR) = 0x0000; /* 初始值 */
            FTM_MOD_REG(FTM2_BASE_PTR) = 0xffff; /* 可根据需要设定 */

            FTM_FMS_REG(FTM2_BASE_PTR) = 0x00; /* write protect disable */
            FTM_MODE_REG(FTM2_BASE_PTR) = 0x05; /* 不使能write protect 功能,缺省值;FTMEN=1; */

            /* 0001,1000   CLKS=11:extern Clock;PS=000|DIVIDE:system clock divided by 1or2or4or8or16or32or64or128; */
            FTM_SC_REG(FTM2_BASE_PTR) = 0x18 | FTM_DEC_DEV1;
            /* 0000,1101 不是能过滤,QUADMODE=1:count and direction mode;QUADIR=1:increasing;QUADEN=1 */
            FTM_QDCTRL_REG(FTM2_BASE_PTR) = 0x0d;
            break;
            
        default:
            return E_OBJ;
    }
    
    return E_OK;
}