调试9:定时器应用——超声波模块测距
1、超声波模块原理介绍:
2、设置定时器:
利用Applilet3来设置定时器:
得到代码:
void TAU0_Init(void)
{
TAU0EN = 1U; /* supplies input clock */
TPS0 = _0000_TAU_CKM0_FCLK_0 | _0000_TAU_CKM1_FCLK_0 | _0000_TAU_CKM2_FCLK_1 | _0000_TAU_CKM3_FCLK_8;
/* Stop all channels */
TT0 = _0001_TAU_CH0_STOP_TRG_ON | _0002_TAU_CH1_STOP_TRG_ON | _0004_TAU_CH2_STOP_TRG_ON | _0008_TAU_CH3_STOP_TRG_ON | _0010_TAU_CH4_STOP_TRG_ON | _0020_TAU_CH5_STOP_TRG_ON | _0040_TAU_CH6_STOP_TRG_ON | _0080_TAU_CH7_STOP_TRG_ON | _0200_TAU_CH1_H8_STOP_TRG_ON | _0800_TAU_CH3_H8_STOP_TRG_ON;
/* Mask channel 0 interrupt */
TMMK00 = 1U; /* disable INTTM00 interrupt */
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
/* Mask channel 1 interrupt */
TMMK01 = 1U; /* disable INTTM01 interrupt */
TMIF01 = 0U; /* clear INTTM01 interrupt flag */
/* Mask channel 1 higher 8 bits interrupt */
TMMK01H = 1U; /* disable INTTM01H interrupt */
TMIF01H = 0U; /* clear INTTM01H interrupt flag */
/* Mask channel 2 interrupt */
TMMK02 = 1U;
/* disable INTTM02 interrupt */
TMIF02 = 0U; /* clear INTTM02 interrupt flag */
/* Mask channel 3 interrupt */
TMMK03 = 1U; /* disable INTTM03 interrupt */
TMIF03 = 0U; /* clear INTTM03 interrupt flag */
/* Mask channel 3 higher 8 bits interrupt */
TMMK03H = 1U; /* disable INTTM03H interrupt */
TMIF03H = 0U; /* clear INTTM03H interrupt flag */
/* Mask channel 4 interrupt */
TMMK04 = 1U;
/* disable INTTM04 interrupt */
TMIF04 = 0U; /* clear INTTM04 interrupt flag */
/* Mask channel 5 interrupt */
TMMK05 = 1U; /* disable INTTM05 interrupt */
TMIF05 = 0U; /* clear INTTM05 interrupt flag */
/* Mask channel 6 interrupt */
TMMK06 = 1U; /* disable INTTM06 interrupt */
TMIF06 = 0U; /* clear INTTM06 interrupt flag */
/* Mask channel 7 interrupt */
TMMK07 = 1U; /* disable INTTM07 interrupt */
TMIF07 = 0U; /* clear INTTM07 interrupt flag */
/* Set INTTM00 high priority */
TMPR100 = 0U;
TMPR000 = 0U;
/* Channel 0 used as interval timer */
TMR00 = _0000_TAU_CLOCK_SELECT_CKM0 | _0000_TAU_CLOCK_MODE_CKS | _0000_TAU_COMBINATION_SLAVE | _0000_TAU_TRIGGER_SOFTWARE | _0000_TAU_MODE_INTERVAL_TIMER | _0001_TAU_START_INT_USED;
TDR00 = _001F_TAU_TDR00_VALUE;
TO0 &= ~_0001_TAU_CH0_OUTPUT_VALUE_1;
TOE0 &= ~_0001_TAU_CH0_OUTPUT_ENABLE;
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function starts TAU0 channel 0 counter.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void TAU0_Channel0_Start(void)
{
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
TMMK00 = 0U; /* enable INTTM00 interrupt */
TS0 |= _0001_TAU_CH0_START_TRG_ON;
}
/*
**-----------------------------------------------------------------------------
**
** Abstract:
** This function stops TAU0 channel 0 counter.
**
** Parameters:
** None
**
** Returns:
** None
**
**-----------------------------------------------------------------------------
*/
void TAU0_Channel0_Stop(void)
{
TT0 |= _0001_TAU_CH0_STOP_TRG_ON;
/* Mask channel 0 interrupt */
TMMK00 = 1U; /* disable INTTM00 interrupt */
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
}
3、关键程序:
主程序中只需启动定时器:TAU0_Channel0_Start();
主循环中:
S=change_distence();
LCD_Accel_Value2(4,0,S);
其中:
void delay1(void)
{
uchar i;
for(i=11;i>0;i--);
}
void Get_distance(void)
{
unsigned long int i=162400; //3.5m测距所需的时间
Trig_1; //触发
delay1();
Trig_0; //停止触发
//检测输入信号
while(!(EchoIN==1)&&i>0)
{
i--;
}
if (i>0)
{
tick_1us=0;
while(EchoIN==1);
temp1=tick_1us;
}
}
unsigned int change_distence()
{
unsigned long int i;
Get_distance();
k=temp1/58.00;
i=(uint)(k*100);
return i;
}
而关键的 tick_1us
是在定时中断:
#pragma vector = INTTM00_vect
__interrupt void MD_INTTM00(void)
{
/* Start user code. Do not edit comment generated here */
TM00_flag = TRUE;
tick_1us++;
/* End user code. Do not edit comment generated here */
}
得到的!
4、图片:
5、视频: