高级会员
- 积分
- 833
- 威望
- 514
- 贡献
- 147
- 兑换币
- 6
- 注册时间
- 2012-2-21
- 在线时间
- 86 小时
- 毕业学校
- 昆明理工大学
|
#include "lptmr.h"
//==========================================================================
//函数名称: lptmr_internal_ref_input
//函数返回: 无
//参数说明: 无
//注 意: 内部参考时钟4分频作为模块时钟,使用ptc5号引脚,alt2通道
//功能概要: 清上次寄存器的值便于本次设置
//==========================================================================
void lptmr_internal_ref_input()
{
unsigned int compare_value=15625; //4 seconds with prescale=8 and 2Mhz fast clock
//Reset LPTMR module
lptmr_clear_registers();
/* Enable LPT Module */
SIM_SCGC5|=SIM_SCGC5_LPTIMER_MASK;
//打开c口5号引脚的相应功能
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTA clock
PORTC_PCR5=PORT_PCR_MUX(0x4); //Use ALT2 on PTC 5
// /* Ensure Internal Reference Clock is Enabled * /
MCG_C1|=MCG_C1_IRCLKEN_MASK;
//Enable fast internal ref clock by setting MCG_C2[IRCS]=1
//If wanted to use 32Khz slow mode, set MCG_C2[IRCS]=0 instead
MCG_C2|=MCG_C2_IRCS_MASK;
/* Configure LPTMR */
LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value
LPTMR0_PSR=LPTMR_PSR_PCS(0x0)|LPTMR_PSR_PRESCALE(0x1); //Use internal clock prescaled by8
LPTMR0_CSR=LPTMR_CSR_TPS(0x2)|LPTMR_CSR_TMS_MASK; //use 2模块(LPT0_ALT2)(c口5引脚) 计数模式
LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //开始计数 //Turn on LPT with default settings
}
//==========================================================================
//函数名称: lptmr_pulse_counter
//函数返回: 无
//参数说明: 输入值为要使用的引脚号
//注 意: 使用1khz的LPO时钟进行计数,使用与低速状态
//功能概要: 清上次寄存器的值便于本次设置
//==========================================================================
void lptmr_pulse_counter(char pin_select)
{
unsigned int compare_value=1000;
//Reset LPTMR module
lptmr_clear_registers();
//Set up GPIO
if(pin_select==LPTMR_ALT1)
{
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; //Turn on PORTA clock
PORTA_PCR19=PORT_PCR_MUX(0x6); //Use ALT6 on PTA19
//printf("Testing ALT1 pin on PORTA19\n");
//printf("\tTWR-K70F120M: ALT1 is conected to pin 18 on J15\n");
}
else if(pin_select==LPTMR_ALT2)
{
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTC clock
PORTC_PCR5=PORT_PCR_MUX(0x4); //Use ALT2 on PTC5
}
LPTMR0_PSR=LPTMR_PSR_PCS(0x1)|LPTMR_PSR_PBYP_MASK; //Use LPO clock but bypass glitch filter
LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value); //Set compare value
LPTMR0_CSR=LPTMR_CSR_TPS(pin_select)|LPTMR_CSR_TMS_MASK; //Set LPT to use the pin selected, and put in pulse count mode, on rising edge (default)
//printf("Press any key to start pulse counter\n");
//in_char(); //wait for keyboard press
LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPT
}
//=================================================================================
//函数名: get_counter_value
//功能 : 读编码器的计数值
//注意 : 电压不稳,频率过低可能丢脉冲
//输入 : 无
//输出 : 编码器的计数值
// On each read of the LPTMR counter register, software must first write to the *
//LPTMR counter register with any value. This will synchronize and register the *
// current value of the LPTMR counter register into a temporary register. The *
//contents of the temporary register are returned on each read of the LPTMR *
// counter register.
//===============================================================================
uint16 get_counter_value()
{
LPTMR0_CNR=0x1;
return LPTMR0_CNR;
}
//==========================================================================
//函数名称: lptmr_clear_registers
//函数返回: 无
//参数说明:
//功能概要: 清上次寄存器的值便于本次设置
//==========================================================================
void lptmr_clear_registers()
{
LPTMR0_CSR=0x00;
LPTMR0_PSR=0x00;
LPTMR0_CMR=0x00;
}
|
|