[mw_shl_code=c,true]
//Email:ykee@outlook.com
#include "timer.h"
#include "led.h"
u32 count=0; //脉冲计数
u32 count_val=0; //计数值
//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
if(TIM3->SR&0X0001)//溢出中断
{
SYS_LED=!SYS_LED;
}
TIM3->SR&=~(1<<0);//清除中断标志位
}
void TIM4_Init(u16 arr, u16 psc ,u32 count)
{
RCC->APB1ENR |= 1<<2; //TIM4时钟使能
RCC->APB2ENR |= 1<<3; //使能PORTB时钟
GPIOB->CRL &= 0XF0FFFFFF;//PB6输出
GPIOB->CRL |= 0X0B000000;//复用功能输出
GPIOB->ODR |= 1<<6;//PB6上拉
TIM4->ARR = arr;//设定计数器自动重装值
TIM4->
SC = psc;//预分频器不分频
TIM4->CCR1 = arr/2; //占空比为50%
count_val=count-1; //脉冲输出个数
TIM4->CCMR1 |= 7<<4; //CH1 PWM2模式
TIM4->CCMR1 |= 1<<3; //CH1预装载使能
TIM4->CCER |= 1<<0; //TIM4 OC1 PB6 输出使能
TIM4->DIER|=1<<0; //允许更新中断
TIM4->DIER|=1<<6; //允许触发中断
TIM4->EGR |=1<<1; //允许产生比较捕获事件
MY_NVIC_Init(1,3,TIM4_IRQChannel,2);//抢占1,子优先级3,组2
TIM4->CR1 |= 0x0080; //ARPE使能
TIM4->CR1 |= 0x0001; //使能定时器4
TIM4->CCMR1 |= 7<<4; //CH1 PWM2模式
TIM4->CCMR1 |= 1<<3; //CH1预装载使能
TIM4->CR1 |= 0x0080; //ARPE使能
TIM4->CR1 |= 0x0001; //使能定时器3
}
void TIM4_IRQHandler(void)
{
if(TIM4->SR&0X0001)//产生更新中断
{
count++;
if(count==count_val)
{
TIM4->CR1&=~(0x01); //CEN=0,禁止定时器3
count=0;
}
}
TIM4->SR&=~(1<<0);//清除中断标志位
}
void Encoder_Init(void)
{
/* TIM3 clock source enable */
RCC->APB1ENR|=1<<1; //TIM3时钟使能
/* Enable GPIOA, clock */
RCC->APB2ENR|=1<<2; //使能PORTA时钟
/* Configure PA.06,07 as encoder input */
GPIOA->CRL&=0XF0FFFFFF;//PA6
GPIOA->CRL|=0X04000000;//浮空输入
GPIOA->CRL&=0X0FFFFFFF;//PA7
GPIOA->CRL|=0X40000000;//浮空输入
/* Enable the TIM3 Update Interrupt */
//这两个东东要同时设置才可以使用中断
TIM3->DIER|=1<<0; //允许更新中断
TIM3->DIER|=1<<6; //允许触发中断
MY_NVIC_Init(1,3,TIM3_IRQChannel,2);
/* Timer configuration in Encoder mode */
TIM3->
SC = 0x0;//预分频器
TIM3->ARR = ENCODER_TIM_PERIOD-1;//设定计数器自动重装值
TIM3->CR1 &=~(3<<8);// 选择时钟分频:不分频
TIM3->CR1 &=~(3<<5);// 选择计数模式:边沿对齐模式
TIM3->CCMR1 |= 1<<0; //CC1S='01' IC1FP1映射到TI1
TIM3->CCMR1 |= 1<<8; //CC2S='01' IC2FP2映射到TI2
TIM3->CCER &= ~(1<<1); //CC1P='0' IC1FP1不反相,IC1FP1=TI1
TIM3->CCER &= ~(1<<5); //CC2P='0' IC2FP2不反相,IC2FP2=TI2
TIM3->CCMR1 |= 3<<4; // IC1F='1000' 输入捕获1滤波器
TIM3->SMCR |= 3<<0; //SMS='011' 所有的输入均在上升沿和下降沿有效
TIM3->CNT = COUNTER_RESET;
TIM3->CR1 |= 0x01; //CEN=1,使能定时器
}
[/mw_shl_code]
这个程序中TIM3为何要中断啊,中断只是为了让灯亮灭吗?按理不是TIM3的计数值就是正交编码器的脉冲计数吗?为何要需要TIM4? TIM4这里又是干什么的 啊?求解啊!看得我一头雾水!
---------------------------------
参见8 9 两楼,完整的工程已经是有很多代码的,不能随便给的!
---------------------------------
楼主,,能不能发一个stm32官方的正反编码器的例程啊,,官网上找不到
---------------------------------
#include "encode.h"
#include "stm32f10x.h"
#include "misc.h"
#include "NVIC.h"
extern int num;
void TIM4_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PA6 ch1 A,PA7 ch2 B
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//使能TIM3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOB时钟
GPIO_StructInit(&GPIO_InitStructure);//将GPIO_InitStruct中的参数按缺省值输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PA6 A7浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_Configuration(3);
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 360*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_ClearFlag(TIM4, TIM_FLAG_Update);//清除TIM3的更新标志位
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//开启TIM3更新标志使能
//Reset counter
TIM4->CNT = 0;//
TIM_Cmd(TIM4, ENABLE); //启动TIM3定时器
}
/*
void TIM3_Mode_Config(void)
{
///TIM3 clock source enable
RCC->APB1ENR|=1<<1; //TIM3时钟使能
// Enable 1GPIOA, clock
RCC->APB2ENR|=1<<2; //使能PORTA时钟
// Configure A.06,07 as encoder input
GPIOA->CRL&=0XF0FFFFFF;//PA6
GPIOA->CRL|=0X04000000;//浮空输入
GPIOA->CRL&=0X0FFFFFFF;//PA7
GPIOA->CRL|=0X40000000;//浮空输入
// Enable the TIM3 Update Interrupt
//这两个东东要同时设置才可以使用中断
TIM3->DIER|=1<<0; //允许更新中断
TIM3->DIER|=1<<6; //允许触发中断
TIM3_NVIC_Config();
//Timer configuration in Encoder mode
TIM3->SC = 0x0;//预分频器
TIM3->ARR = 15-1;//设定计数器自动重装值
TIM3->CR1 &=~(3<<8);// 选择时钟分频:不分频
TIM3->CR1 &=~(3<<5);// 选择计数模式:边沿对齐模式
TIM3->CCMR1 |= 1<<0; //CC1S='01' IC1FP1映射到TI1
TIM3->CCMR1 |= 1<<8; //CC2S='01' IC2FP2映射到TI2
TIM3->CCER &= ~(1<<1); //CC1P='0' IC1FP1不反相,IC1FP1=TI1
TIM3->CCER &= ~(1<<5); //CC2P='0' IC2FP2不反相,IC2FP2=TI2
TIM3->CCMR1 |= 3<<4; // IC1F='1000' 输入捕获1滤波器
TIM3->SMCR |= 3<<0; //SMS='011' 所有的输入均在上升沿和下降沿有效
TIM3->CNT = 0;
TIM3->CR1 |= 0x01; //CEN=1,使能定时器
}*/
void TIM4_Init(void)
{
TIM4_Mode_Config();
}
一周热门 更多>