高级会员

- 积分
- 598
- 金钱
- 598
- 注册时间
- 2016-8-31
- 在线时间
- 80 小时
|
发表于 2022-10-1 15:08:41
|
显示全部楼层
你好
:
#include "stm32f10x.h"
#include "encode.h"
#include "misc.h"
#include "nvic.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
void TIM4_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PB6 ch1 A,PB7 ch2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能TIM4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOA时钟
GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//PA6 PA7浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_Config(2);
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 3000*4; //设定计数器重装值 TIMx_ARR = 359*4
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM3时钟预分频值
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//设置时钟分割 T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数
TIM_ICStructInit(&TIM_ICInitStructure);//将结构体中的内容缺省输入
TIM_ICInitStructure.TIM_ICFilter = 6; //选择输入比较滤波器
TIM_ICInit(TIM4, &TIM_ICInitStructure);//将TIM_ICInitStructure中的指定参数初始化TIM3
// TIM_ARRPreloadConfig(TIM4, ENABLE);//使能预装载
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//清除TIM3的更新标志位
// TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//运行更新中断
//Reset counter
TIM4->CNT = 4000;//
TIM_Cmd(TIM4, ENABLE); //启动TIM4定时器
}
void TIM4_Init(void)
{
TIM4_Mode_Config();
}
void TIM4_IRQHandler(void) //定时器5中断函数
{
if(TIM4->SR&0X0001)//溢出中断
{
//设置pwm
/////////////////////////////限制编码器范围
if (TIM4->CNT<=1200)
{
TIM4->CNT=1200;
}
if (TIM4->CNT>=10000)
{
TIM4->CNT=10000;
/////////////////////////////
}
printf("tim4中断 %d\r\n",TIM4->CNT);
}
TIM4->SR&=~(1<<0);
}
//void TIM4_IRQHandler(void)
//{
//
// TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
// // LED1=!LED1;
//
//}
|
|