学前班
最后登录1970-1-1
在线时间 小时
注册时间2017-4-30
|
本人最近在做一个光电编码器脉冲捕获的实验,按照一些资料和手册做了相应IO口的配置,下面是我个人移植的一些代码。
“encorder.c”文件代码:
- #include "encoder.h"
- TIM_ICInitTypeDef TIM5_ICInitStructure;
- void TIM5_Cap_Init(u16 arr,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //ê1Äü TIM5 ê±Öó
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ê1Äü GPIOB ê±Öó
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PB0 Çå3t֮ǰéèÖÃ
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PB0 êäèë
- GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉ GPIOB.0
- GPIO_ResetBits(GPIOB,GPIO_Pin_0); //PB0 ÏÂà-
- TIM_TimeBaseStructure.TIM_Period = arr; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //Ô¤·ÖÆμÆ÷
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM ÏòéϼÆêyÄ£ê½
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMx
- TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; // Ñ¡Ôñêäèë¶Ë IC1 ó3éäμ½ TI1 éÏ
- TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
- TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½ TI1 éÏ
- TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
- TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
- TIM_ICInit(TIM5, &TIM5_ICInitStructure);//3õê¼»ˉ TIM5 êäèë2¶»ñí¨μà 1
- NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM5 ÖD¶Ï
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏèÕ¼óÅÏ輶¶t¼¶
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //′óóÅÏ輶0¼¶
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ í¨μà±»ê1Äü
- NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉ NVIC
- TIM_ITConfig( TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//ÔêDí¸üDÂÖD¶Ï2¶»ñÖD¶Ï
- TIM_Cmd(TIM5,ENABLE );} //ê1Äü¶¨ê±Æ÷5
- u8 TIM5CH1_CAPTURE_STA=0; //êäèë2¶»ñ×′ì¬
- u16 TIM5CH1_CAPTURE_VAL; //êäèë2¶»ñÖμ
- //¶¨ê±Æ÷5·tÎñÖD¶Ï3ìDò
- void TIM5_IRQHandler(void)
- {
- if((TIM5CH1_CAPTURE_STA&0X80)==0)//»1Î′3é1|2¶»ñ
- {
- if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
- {
- if(TIM5CH1_CAPTURE_STA&0X40)//òѾ-2¶»ñμ½¸ßμçƽáË
- {
- if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)//¸ßμçƽì«3¤áË
- {
- TIM5CH1_CAPTURE_STA|=0X80;//±ê¼Ç3é1|2¶»ñáËò»′Î
- TIM5CH1_CAPTURE_VAL=0XFFFF;
- }else TIM5CH1_CAPTURE_STA++;
- }
- }
- if (TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)//2¶»ñ1·¢éú2¶»ñê¼t
- {
- if(TIM5CH1_CAPTURE_STA&0X40) //2¶»ñμ½ò»¸öϽμÑØ
- {
- TIM5CH1_CAPTURE_STA|=0X80; //±êÖ¾3é1|2¶»ñμ½ò»′ÎéÏéyÑØ
- TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);
- TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //éèÖÃÎaéÏéyÑØ2¶»ñ
- }else //»1Î′¿aê¼£¬μúò»′Î2¶»ñéÏéyÑØ
- {
- TIM5CH1_CAPTURE_STA=0; //Çå¿Õ
- TIM5CH1_CAPTURE_VAL=0;
- TIM_SetCounter(TIM5,0);
- TIM5CH1_CAPTURE_STA|=0X40; //±ê¼Ç2¶»ñμ½éÏéyÑØ
- TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling); //éèÖÃÎaϽμÑØ2¶»ñ
- }
- }
- }
- TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
- }
复制代码
“encoder.h"文件代码:
- #ifndef _ENCODER_H
- #define _ENCODER_H
- #include "stm32f10x.h"
- #include "misc.h"
- #include "stm32f10x_tim.h"
- #endif
- void TIM5_Cap_Init(u16 arr,u16 psc);
- void TIM5_IRQHandler(void);
复制代码
想利用定时器做一个1s的中断,统计1s内输入的高电平数并将该数值打印,但不知道该如何写相应的程序,有大神能帮我看看嘛?
|
|