资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
主函数#include stm32f10x.h#include timer.h#include led.h#include delay.h#include oled.h#include sys.hint main(void) u16 t=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#include timer.h#include led.h#include usart.h#include oled.hvoid TIM3_PWM_Init(u16 arr,u16 psc) GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_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显示 /设置该引脚为复用输出功能,输出TIM3 CH2的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外部脉冲计数函数void TIM2_Excnt_Init(u16 arr) GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;NVIC_InitTypeDef NVIC_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);void TIM2_IRQHandler(void)if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)LED1=!LED1;TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update);
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号