stm32F1外部脉冲计数(库函数实现)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

主函数#includestm32f10x.h#includetimer.h#includeled.h#includedelay.h#includeoled.h#includesys.hintmain(void){u16t=0;delay_init();LED_Init();TIM3_PWM_Init(9999,719);TIM2_Excnt_Init(10);OLED_Init();NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);OLED_ShowString(20,0,EXCNT_TEST,16);OLED_Refresh_Gram();TIM_SetCompare2(TIM3,1000);while(1){delay_ms(1);t=TIM_GetCounter(TIM2);OLED_ShowNum(20,30,t,6,16);OLED_Refresh_Gram();}}timer.c#includetimer.h#includeled.h#includeusart.h#includeoled.hvoidTIM3_PWM_Init(u16arr,u16psc){GPIO_InitTypeDefGPIO_InitStructure;TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_OCInitTypeDefTIM_OCInitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);//使能GPIO外设和AFIO复用功能模块时钟使能GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//Timer3部分重映射TIM3_CH2-PB5//用于TIM3的CH2输出的PWM通过该LED显示//设置该引脚为复用输出功能,输出TIM3CH2的PWM脉冲波形GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//TIM_CH2GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);//GPIO_WriteBit(GPIOA,GPIO_Pin_7,Bit_SET);//PA7上拉TIM_TimeBaseStructure.TIM_Period=arr;//设置在下一个更新事件装入活动的自动重装载寄存器周期的值80KTIM_TimeBaseStructure.TIM_Prescaler=psc;//设置用来作为TIMx时钟频率除数的预分频值不分频TIM_TimeBaseStructure.TIM_ClockDivision=0;//设置时钟分割:TDTS=Tck_timTIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//TIM向上计数模式TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//选择定时器模式:TIM脉冲宽度调制模式2TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//比较输出使能TIM_OCInitStructure.TIM_Pulse=0;//设置待装入捕获比较寄存器的脉冲值TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//输出极性:TIM输出比较极性高TIM_OC2Init(TIM3,&TIM_OCInitStructure);//根据TIM_OCInitStruct中指定的参数初始化外设TIMxTIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能TIMx在CCR2上的预装载寄存器TIM_ARRPreloadConfig(TIM3,ENABLE);//使能TIMx在ARR上的预装载寄存器TIM_Cmd(TIM3,ENABLE);//使能TIMx外设}//定时器2外部脉冲计数函数voidTIM2_Excnt_Init(u16arr){GPIO_InitTypeDefGPIO_InitStructure;TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;NVIC_InitTypeDefNVIC_InitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能GPIOA和TIM2的时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);TIM_DeInit(TIM2);GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseStructure.TIM_Period=arr;TIM_TimeBaseStructure.TIM_Prescaler=0;TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,10);//ExtTRGFilter:外部触发滤波器。该参数取值在0x0和0xF之间。在我所在的环境下设置为10经检测最准确。NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;NVIC_Init(&NVIC_InitStructure);//中断优先级设置,抢占优先级为1,响应优先级为3TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC1,ENABLE);//使能TIM2中断TIM_SetCounter(TIM2,0);TIM_Cmd(TIM2,ENABLE);}voidTIM2_IRQHandler(void){if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET){LED1=!LED1;TIM_ClearITPendingBit(TIM2,TIM_IT_CC1|TIM_IT_Update);}}

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功