#include "timer.h"
#include "led.h"
void TIM3_Int_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); ///ê1ÄüTIM3ê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_TIM3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉ
TIM_TimeBaseInitStructure.TIM_Period = 359*4; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseInitStructure.TIM_Prescaler=0; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//3õê¼»ˉTIM3
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //¶¨ê±Æ÷3ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //ÇàÕ¼óÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter=6;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM3->CNT = 0;
TIM_DeInit(TIM3);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);//Çå3yTIMμĸüD±ê־λ
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
int main(void)
{
u16 count;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200);
LED_Init(); //3õê¼»ˉLED¶Ë¿ú
TIM3_Int_Init(); //¶¨ê±Æ÷ê±Öó84M£¬·ÖÆμÏμêy8400£¬ËùòÔ84M/8400=10KhzμļÆêyÆμÂ꣬¼Æêy5000′ÎÎa500ms
while(1)
{
LED0=!LED0;//DS0·-×a
// count=Read_Encoder(1)/4;
count=TIM3 -> CNT/4;
printf("count = %d
",count);
delay_ms(500);//Ñóê±200ms
};
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>