之前学校比赛的时候要求我们测量pwm波的频率和占空比,于是我就用了输入捕获来实现这一功能。 但是我在使用库函数写程序的时候不知道为什么每次捕获都只能捕获到一次上升沿和下降沿,无法捕获第二个上升沿。也就是只能捕获到一次高电平的时间而无法捕获到整个周期的时间,后来我在尝试使用寄存器编写的时候发现竟然完全没有出现这个问题,原因不明,推测是库函数在编写时加入了过多限制或者部分代码有问题。下面给出我的寄存器版本的初始化,调用结果的函数以及计时器中断服务函数。
//首先就是对于时钟和对应I/O口的初始化 //这部分如果需要换I/O口的话需要自己重新配置时钟和通道以及对应I/O口 //不过对着手册修改起来也不是很麻烦,只是要仔细一点不要漏掉什么就行了。 TIM_ICInitTypeDef TIM5_ICInitStructure; //定时器5通道1输入捕获配置 //arr:自动重装值(TIM2,TIM5是32位的!!) //psc:时钟预分频数 void TIM5_CH1_Cap_Init(u32 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5时钟使能 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA0 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA0 GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //PA0复用位定时器5 TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure); //初始化TIM5输入捕获参数 TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上 TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获 TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上 TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波 TIM_ICInit(TIM5, &TIM5_ICInitStructure); TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断 TIM_Cmd(TIM5,ENABLE ); //使能定时器5 NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、 } //接下来就是对于发生捕获的中断处理函数了 u8 TIM5CH1_CAPTURE_STA=0; //输入捕获状态 u32 TIM5CH1_CAPTURE_VAL; //输入捕获值(TIM2/TIM5是32位) double in1=0,in2=0; //定时器5中断服务程序 void TIM5_IRQHandler(void) { u16 tsr; tsr=TIM5->SR; if((TIM5CH1_CAPTURE_STA&0X80)==0)//还未成功捕获 { if(tsr&0x02)//捕获1发生捕获事件 { if(TIM5CH1_CAPTURE_STA==0X40) //捕获到一个下降沿 { TIM5CH1_CAPTURE_STA=0X10; //标记成功捕获到一次高电平脉宽 TIM5CH1_CAPTURE_VAL=TIM5->CCR1; //获取当前的捕获值. in1=TIM5->CCR1+1; TIM5->CCER&=~(1<<1); //CC1P=0 设置为上升沿捕获 } else if(TIM5CH1_CAPTURE_STA==0x10) { TIM5CH1_CAPTURE_STA=0X80; TIM5CH1_CAPTURE_VAL=TIM5->CCR1; in2=TIM5->CCR1+1; TIM5->CCER&=~(1<<1); //CC1P=0 设置为上升沿捕获 } else //还未开始,第一次捕获上升沿 { TIM5CH1_CAPTURE_STA=0; //清空 TIM5CH1_CAPTURE_VAL=0; TIM5CH1_CAPTURE_STA=0X40; //标记捕获到了上升沿 TIM5->CR1&=~(1<<0); //使能定时器2 TIM5->CNT=0; //计数器清空 TIM5->CCER|=1<<1; //CC1P=1 设置为下降沿捕获 TIM5->CR1|=0x01; //使能定时器2 } } } TIM5->SR=0;//清除中断标志位 } //最后就是在main函数里的使用了 //这里面还用到了LCD屏幕的输出(其实也只需要简单用库就行了) #include "usart.h" #include "delay.h" #include "led.h" #include "sys.h" #include "beep.h" #include "key.h" #include "lcd.h" #include "remote.h" #include "exti.h" void _show_info(void); void pwm_measure(void); extern double frequency,duty; extern double in1,in2; extern u32 TIM5CH1_CAPTURE_VAL; extern u8 TIM5CH1_CAPTURE_STA; int main(void) { int key_status,remote_status; int mode=0; delay_init(168); LED_Init(); KEY_Init(); Remote_Init(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); uart_init(115200); LCD_Init(); POINT_COLOR=BLUE; PWMMODE_84psc(frequency,duty); TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1); _show_info(); LCD_ShowString(30,190,200,16,16,"Duty change mode"); while(1) { remote_status=Remote_Scan(); key_status=KEY_Scan(0); printf("%d",key_status); if(key_status) { if(key_status==3) { mode++; if(mode==3)mode=0; if(mode==0) { _show_info(); LCD_ShowString(30,190,240,16,16,"Duty change mode"); } else if(mode == 1) { _show_info(); LCD_ShowString(30,190,240,16,16,"Frequency change mode"); } else if(mode ==2) { _show_info(); LCD_ShowString(30,190,240,16,16,"Now is input mode!"); LCD_ShowString(30,230,200,16,16,"Press Key_UP to exit!"); pwm_measure(); } } else if(mode==0) { if(key_status==1) { duty+=5.0; if(duty>90)duty=90.0; PWMMODE_84psc(frequency,100-duty); _show_info(); LCD_ShowString(30,190,280,16,16,"Duty change mode"); delay_ms(1000); } else if(key_status==2) { duty-=5.0; if(duty<10)duty=10.0; PWMMODE_84psc(frequency,100-duty); _show_info(); LCD_ShowString(30,190,280,16,16,"Duty change mode"); delay_ms(1000); } } else if(mode==1) { if(key_status==1) { frequency+=100.0; if(frequency>1001)frequency=1000.0; PWMMODE_84psc(frequency,duty); _show_info(); LCD_ShowString(30,190,240,16,16,"Frequency change mode"); delay_ms(1000); } else if(key_status==2) { frequency-=100.0; if(frequency<99)frequency=100.0; PWMMODE_84psc(frequency,duty); _show_info(); LCD_ShowString(30,190,240,16,16,"Frequency change mode"); delay_ms(1000); } } } } } void _show_info(void) { LCD_Clear(WHITE); char prin1[100]; char prin2[100]; sprintf(prin1,"Frequency:%.2f KHz",frequency/1000); sprintf(prin2,"Duty:%.2f%%",duty); printf("Frequency:%.2f KHz Duty:%.2f%% \r\n",frequency/1000,duty); LCD_ShowString(30,70,200,16,16,prin1); LCD_ShowString(30,110,200,16,16,prin2); return; } void pwm_measure(void) { char prin1[30],prin2[30]; double frequency,duty; int key_status=0; while(1) { key_status=KEY_Scan(0); if(TIM5CH1_CAPTURE_STA&0X80) { frequency=1.0/(in2/1000000); duty=(double)in1/in2; printf("in1=%.0f,in2=%.0f ",in1,in2); printf("frequency= %.0fHz,duty=%.2f%%\r\n",frequency,duty*100); sprintf(prin1,"Input frequency is:%.0f Hz",frequency); sprintf(prin2,"Input duty is:%.2f %",duty*100); TIM5CH1_CAPTURE_STA=0; LCD_Clear(WHITE); LCD_ShowString(30,70,200,16,16,prin1); LCD_ShowString(30,110,200,16,16,prin2); LCD_ShowString(30,150,200,16,16,"Now is the input mode!"); LCD_ShowString(30,230,200,16,16,"Press Key_UP to exit!"); PWMMODE_84psc(frequency*5,50); delay_ms(500); TIM5CH1_CAPTURE_STA=0; } if(key_status==3) { LCD_Clear(WHITE); LCD_ShowString(30,70,200,16,16,"Exiting the input mode!"); delay_ms(2000); break; } } }
发表评论