标签:stm32
最近这几周各种考试,各种课程设计,唉,我得复习复习了,所以啦,博客暂停直到6-25之后继续更新,欢迎阅读。
STM32F103 输入捕获信号:
/*--------------------------------------------------------------------------- * Project: Time.h * Name: zwp * Date: 2014/6 *---------------------------------------------------------------------------*/ #ifndef TIME_H_ #define TIME_H_ #include "stm32f10x.h" /* ** ¶¨Ê±Æ÷2ͨµÀ1ÊäÈ벶»ñÅäÖà */ void TIM2_Cap_Init(u16 arr, u16 psc); /* ** ¶¨Ê±Æ÷5ÖжϷþÎñ³ÌÐò */ void TIM2_IRQHandler(void); #endif
/*------------------------------------------------------------------------------- * Project: Time.c * Name: zwp * Date: 2014/6 *-----------------------------------------------------------------------------*/ #include "Time.h" TIM_ICInitTypeDef TIM2_ICInitStructure; /* ** ¶¨Ê±Æ÷2ͨµÀ1ÊäÈ벶»ñÅäÖà */ void TIM2_Cap_Init(u16 arr, u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; /* ʹÄÜTIM2 ʱÖÓ */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* ʹÄÜGPIOAʱÖÓ */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* ÉèÖÃÒý½Å */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOA, &GPIO_InitStructure); /* PA0 ÏÂÀ */ GPIO_ResetBits(GPIOA, GPIO_Pin_0); /* ³õʼ»¯¶¨Ê±Æ÷2TIM2 */ TIM_TimeBaseStructure.TIM_Period = arr; // É趨¼ÆÊýÆ÷×Ô¶¯ÖØ×°Öµ TIM_TimeBaseStructure.TIM_Prescaler = psc; // Ô¤·ÖƵÆ÷ TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;// ÉèÖÃʱÖÓ·Ö¸î TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // ÏòÉϼÆÊý TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // ³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î» /* ³õʼ»¯TIM2 ÊäÈ벶»ñ²ÎÊý */ TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1; // Ñ¡ÔñÊäÈë¶ËIC1ÒþÉäµ½TI1ÉÏ TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // ÉÏÉýÑز¶»ñ TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;// ÒþÉäµ½TI1ÉÏ TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // ÅäÖÃÊäÈë·ÖƵ£¬²»·ÖƵ TIM2_ICInitStructure.TIM_ICFilter = 0x00; // ÅäÖÃÊäÈëÂ˲¨Æ÷ ²»Â˲¨ TIM_ICInit(TIM2, &TIM2_ICInitStructure); /* ÖжϷÖ×é³õʼ»¯ */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; // TIM2 ÖÐ¶Ï NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // ÏÈÕ¼ÓÅÏȼ¶2 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ×ÓÓÅÏȼ¶0¼¶ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQͨµÀ±»Ê¹ÄÜ NVIC_Init(&NVIC_InitStructure); // ³õʼ»¯ÍþÉåNVIC¼Ä´æÆ÷ /* ÔÊÐí¸üÐÂÖжÏCC1IE²¶»ñÖÐ¶Ï */ TIM_ITConfig(TIM2, TIM_IT_Update|TIM_IT_CC1, ENABLE); /* ʹÄܶ¨Ê±Æ÷ 2 */ TIM_Cmd(TIM2, ENABLE); } u8 TIM2CH1_CAPTURE_STA = 0; // ÊäÈ벶»ñ״̬ u16 TIM2CH1_CAPTURE_VAL; // ÊäÈ벶»ñÖµ /* ** ¶¨Ê±Æ÷5ÖжϷþÎñ³ÌÐò */ void TIM2_IRQHandler(void) { if((TIM2CH1_CAPTURE_STA & 0x80) == 0) // »¹Î´³É¹¦²¶»ñ { if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { if(TIM2CH1_CAPTURE_STA & 0x40) // ÒѾ²¶»ñµ½¸ßµçƽ { if((TIM2CH1_CAPTURE_STA & 0x3f) == 0x3F) // ¸ßµçƽ̫³¤ÁË { TIM2CH1_CAPTURE_STA |= 0x80; TIM2CH1_CAPTURE_VAL = 0xFFFF; } else TIM2CH1_CAPTURE_STA++; } } if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) // ²¶»ñ1·¢Éú²¶»ñʼþ { if(TIM2CH1_CAPTURE_STA & 0x40) // ²¶»ñµ½Ò»¸öϽµÑØ { TIM2CH1_CAPTURE_STA |= 0x80; // ´Ó±ê¼Ç³É¹¦²¶»ñµ½Ò»´ÎÉÏÉýÑØ TIM2CH1_CAPTURE_VAL = TIM_GetCapture1(TIM2); /* CC1P = 0 ÉèÖÃΪÉÏÉýÑز¶»ñ */ TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Rising); } else /* »¹Î´¿ªÊ¼µÚÒ»´Î²¶»ñÉÏÉýÑØ */ { TIM2CH1_CAPTURE_STA = 0; TIM2CH1_CAPTURE_VAL = 0; TIM_SetCounter(TIM2, 0); TIM2CH1_CAPTURE_STA |= 0x40; // ±ê¼Ç²¶»ñµ½ÁËÉÏÉýÑØ TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Falling); // ÉèÖÃΪϽµÑز¶»ñ } } } TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update); // Çå³ýÖжϱê־λ }
注释字体乱码了,可能是编码格式不同吧!认真看,因该能看懂。
加油封尘浪!!!
标签:stm32
原文地址:http://blog.csdn.net/qqzwp/article/details/28908695