高级会员
- 积分
- 505
- 威望
- 263
- 贡献
- 140
- 兑换币
- 169
- 注册时间
- 2017-2-19
- 在线时间
- 51 小时
- 毕业学校
- 哈尔滨佛学院、
|
/**
* @BRIEF PIT3³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
static void PIT3_DIS_Init(void)
{
//³õʼ»¯Ê±ÖÓ
SIM->SCGC6|=SIM_SCGC6_PIT_MASK;
//¿ªÆôÄ£¿éʱÖÓ
PIT->MCR&=~PIT_MCR_MDIS_MASK;
PIT->MCR &= ~PIT_MCR_FRZ_MASK;
//¼ÆÊýÆ÷³õʼֵ¸ø×î´ó
(PIT->CHANNEL[3].LDVAL)=0xFFFFFFFF;
//¶¨Ê±Æ÷ÖÜÆÚ½áÊøʱÖÃÒ»Ö»ÓÐд1ʱÇåÁã
(PIT->CHANNEL[3].TFLG)|=PIT_TFLG_TIF_MASK;
PIT->CHANNEL[3].TCTRL|=PIT_TCTRL_TIE_MASK;
}
/**
* @brief Ô§Ñì²â¾àÄ£¿éÒý½Å³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
static void GPIO_DIS_Init(void)
{
/*PC0*/
GPIO_InitTypeDef GPIO_InitStructure;
//ÉÏÀ
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_MUX=Mode1;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
//ÉÏÉýÑØ´¥·¢ÍⲿÖжÏ
GPIO_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising_Falling;
GPIO_Init(PTC,&GPIO_InitStructure);
}
/**
* @brief PIT3ÖжÏÓÅÏȼ¶ÅäÖÃ
* @param ÎÞ
* @retval ÎÞ
*/
void PIT3_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//ÖжÏÓÅÏȼ¶·Ö×é
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
//ÖжÏÔ´£ºTIM3
NVIC_InitStructure.NVIC_IRQChannel=PIT3_IRQn ;
/*Õâ¸öÓÅÏȼ¶±ØÐë×î¸ß*/
//ÇÀÕ¼ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
//ÏìÓ¦ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
//ʹÄÜ
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE ;
//³õʼ»¯½á¹¹Ìå
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Ô§Ñì²â¾à³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
void Distance_Init(void)
{
GPIO_DIS_Init();
PIT3_DIS_Init();
PIT3_NVIC_Configuration();
}
中断
//ABÁ¾³µ
uint16_t ABDistance=0;
uint16_t Old_ABDistance=0;
void PORTC_IRQHandler(void)
{
//³¬Éù²¨¼Æʱʱ¼ä
float chaoshengboTime=0;
//È·±£ÊÇ·ñ²úÉúÁËORTC11 ¼´³¡ÖжÏVSYNCµÄÖжÏ
if(EXTI_GetFlagStatus(PORTC,GPIO_Pin_11) == SET)
{
//Çå³ýÖжϱê־λ
EXTI_ClearFlag(PORTC,GPIO_Pin_11);
//¿ªÆôDMA´«Êä ÔÚÓ²¼þDMAÇëÇó¸Ãλ±ØÐëÖÃ1
DMA0->ERQ |= DMA_ERQ_ERQ0_MASK;
}
//ÓÃÓÚÔ§Ñì²â¾àʹÓÃ
if(EXTI_GetFlagStatus(PORTC,GPIO_Pin_0) == SET)
{
//Çå³ýÖжϱê־λ
EXTI_ClearFlag(PORTC,GPIO_Pin_0);
//¸ßµçƽ
if((PTC->PDIR & (0x01<<GPIO_Pin_0)))
{
//Í£Ö¹¶¨Ê±Æ÷
(PIT->CHANNEL[3].TCTRL)&=~PIT_TCTRL_TEN_MASK;
//32λ¶¨Ê±Æ÷£¬×°×î´óµÄÊý
(PIT->CHANNEL[3].LDVAL)=0xFFFFFFFF;
//Æô¶¯¶¨Ê±Æ÷
(PIT->CHANNEL[3].TCTRL)|=PIT_TCTRL_TEN_MASK;
}
//µÍµçƽ
else
{
//50M×ÜÏßʱÖÓ£¬¼ÆËãµÃµ½Ê±¼ä£¬µ¥Î»Î¢Ãë
chaoshengboTime = (0xFFFFFFFF - (PIT->CHANNEL[3].CVAL))/50;
/*Æäʵ²»ÓüÆËã*/
//Ò»ÃëÖÓµÄÉùÒôËÙ¶ÈÉèΪ340Ã×£¬ÓÉÓÚchaoshengboTimeµ¥Î»ÊÇ΢Ãî,/1000ºóµ¥Î»ÊÇmm
ABDistance = chaoshengboTime * 340/1000;
if(ABDistance>=2500)
{
ABDistance=Old_ABDistance;
}
Old_ABDistance=ABDistance;
// if(ABDistance<2000)
// {
// ABDistance=(Old_ABDistance*5+ABDistance)/6;
// }
// else if(ABDistance>=2000)
// {
// ABDistance=2000;
// }
// Old_ABDistance=ABDistance;
//Í£Ö¹¶¨Ê±Æ÷
(PIT->CHANNEL[3].TCTRL)&=~PIT_TCTRL_TEN_MASK;
}
}
} |
|