专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
请高人讲一下STM32 定时器的编码器接口部分,计数方向与编码器信号关系图怎么看,搞不懂啊!
2019-08-17 00:20
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
16694
24
1044
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
24条回答
HC0821
2019-08-19 04:07
回复【15楼】kkingkeen:
---------------------------------
如果还需要的话,代码如下:
void TIM_Configuration() //PWM???? ??????·???????±à???ù?? ??????·???
{
TIM_ICInitTypeDef TIM_ICInitStructure; //hc
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
/*Configure TIM1_CH1 ~ CH3 and TIM8_CH3 as 4 
WM output channels*/
/*TIM base Configuration*/
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //?ò??????
TIM_TimeBaseInitStructure.TIM_Period = 3599; //TIMx_ARR??????20k
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //??×????¨?÷??·???
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitStructure); //±??????????±??·????ò×?tDTS = tCK_INT
/* TIM1_CH1/2/3 and TIM8_CH3 Configuration in 
WM1 mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //TIM_OutputState_Enable
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //CHxN????????????????????????·??òCHx???á???¨??????
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
/* TIM1, TIM8 enable counter */
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM8, ENABLE); //TIMx_CR1_0/CEN=1,
/* TIM1, TIM8 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE); //TIMx_BDTR_15/MOE=1: ?÷????????
/***************************************TIM2-TIM5??????????****************************************************/
// /*Configure TIM2 TIM3 TIM4 TIM5 as Encoder Interfaces input*/
// /*TIM base Configuration*/
// TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
// TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
// TIM_TimeBaseInitStructure.TIM_Period = 65535;
// TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
//
// /*?¤·???=1???ò??????????·?????×?????×°??=65535???í???ú?ú?????ü???????¨UG??*/
// TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
//
// /*±à???÷????3???¨??1??2??????????????TI1??2???? IC1?????????????????? IC2????????????????*/
// TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
//
// /* TIM2 3 4 enable counter */
// TIM_Cmd(TIM2, ENABLE);
// TIM_Cmd(TIM3, ENABLE);
// TIM_Cmd(TIM4, ENABLE);
// TIM_Cmd(TIM5, ENABLE);
/***********??????????TIM5????????????????????USART2??ADC12??????????????????????TIM5 CH1??2??·¨??????PA0??1***************/
//////////////////////TIM2-TIM5????????????TIM_ICInit(TIMx, &TIM_ICInitStructure);////////////////////
TIM_DeInit(TIM2); TIM_DeInit(TIM3); TIM_DeInit(TIM4); TIM_DeInit(TIM5);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
TIM_TimeBaseInitStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 0xffff;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
TIM2->CR2 &= 0xFF7F; TIM3->CR2 &= 0xFF7F; TIM4->CR2 &= 0xFF7F; TIM5->CR2 &= 0xFF7F; //TI1S=0,TIM5_CH1??????????TI1??????
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0x06;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInit(TIM5, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInit(TIM5, &TIM_ICInitStructure);
//////////////????·??????????????????????????è??????????????///////////////
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM2->CNT = 0x0000;TIM3->CNT = 0x0000;TIM4->CNT = 0x0000;TIM5->CNT = 0x0000;
//////////////////????????????????????????????×???????????/////////////////
TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM4, ENABLE);TIM_Cmd(TIM5, ENABLE);
//////////////////////????????TIM5????PA0??PA1??CH1??CH2????????????????·???????////////////////////////////////
/*config TIM6 as sample timer interrupt*/
TIM_TimeBaseInitStructure.TIM_Prescaler = 0x47; //71
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 999; //1000 us ??????????????????1000us??
//??????????????????????????????????????????·???SampleTime?????ú????????????
/*?¤·?????72MHZ/(71+1)=1MHZ,×?????×°????????500us???*/
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStructure); //?¨?±?÷TIM6??TIM2345???ù???ú????????,?¨?????íPID,?ü??PWM
// TIM_Cmd(TIM6, ENABLE);//TIM6????·¨?¨?±?÷??·????÷???ò??????
TIM_ARRPreloadConfig(TIM6, ENABLE); //TIM6_CR1_7/ARPE=1,TIMx_ARR?????÷????????
TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //TIM6_SR_0/UIF=0???í???????ü??????±ê????
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //enable tim6 count overflow interrupt TIM6_DIER_0/UIE=1???????ü??????
}
GPIO的
/*configure TIM2_CH1/CH2,remap to 
A15/PB3*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PA15??TIM2_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // 
B3 ??TIM2_CH2
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);//AFIO_MAPR_TIM2_REMAP[1:0]/[9:8]=01;TIM2????????????·?????1 CH1/PA15??CH2/PB3
/*Confiture TIM3_CH1 & CH2 as Encoder Interfaces, Remaped on 
C6 & 
C7*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //TIM3???ê??????????CH1=PC6??CH2=PC7
/*Configure TIM4_CH1 (PD12) & TIM4_CH2 (PD13) as Encoder Interfaces*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_Init(GPIOD,&GPIO_InitStructure); //TIM4_REMAP=0??????????????CH1/CH2????????????????PB6/PB7????
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
/*Configure TIM5_CH1 (PA0) & TIM5_CH2 (PA1) as Encoder Interfaces*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStructure); //TIM5_CH1/CH2,??????????????????????PA0/PA1
定时采集函数的语句
void TIM6_IRQHandler() //?¨?±1000us
{
if(TIM_GetITStatus(TIM6, TIM_IT_Update) == SET) //TIM6?????òif????
{
TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //TIM6_SR_0/UIF=0,????????±ê????
speed_fb[0] = TIM_GetCounter(TIM2); //???ù???????ú????·??????????¨????????????????
TIM_SetCounter(TIM2, 0); //???????ú?????÷TIMx_CNT????????????????
speed_fb[1] = TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3, 0);
speed_fb[2] = TIM_GetCounter(TIM4);
TIM_SetCounter(TIM4, 0);
speed_fb[3] = TIM_GetCounter(TIM5);
TIM_SetCounter(TIM5, 0);
加载中...
查看其它24个回答
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
---------------------------------
如果还需要的话,代码如下:
void TIM_Configuration() //PWM???? ??????·???????±à???ù?? ??????·???
{
TIM_ICInitTypeDef TIM_ICInitStructure; //hc
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
/*Configure TIM1_CH1 ~ CH3 and TIM8_CH3 as 4 WM output channels*/
/*TIM base Configuration*/
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //?ò??????
TIM_TimeBaseInitStructure.TIM_Period = 3599; //TIMx_ARR??????20k
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //??×????¨?÷??·???
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitStructure); //±??????????±??·????ò×?tDTS = tCK_INT
/* TIM1_CH1/2/3 and TIM8_CH3 Configuration in WM1 mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //TIM_OutputState_Enable
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //CHxN????????????????????????·??òCHx???á???¨??????
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
/* TIM1, TIM8 enable counter */
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM8, ENABLE); //TIMx_CR1_0/CEN=1,
/* TIM1, TIM8 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE); //TIMx_BDTR_15/MOE=1: ?÷????????
/***************************************TIM2-TIM5??????????****************************************************/
// /*Configure TIM2 TIM3 TIM4 TIM5 as Encoder Interfaces input*/
// /*TIM base Configuration*/
// TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
// TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
// TIM_TimeBaseInitStructure.TIM_Period = 65535;
// TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
//
// /*?¤·???=1???ò??????????·?????×?????×°??=65535???í???ú?ú?????ü???????¨UG??*/
// TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
// TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
//
// /*±à???÷????3???¨??1??2??????????????TI1??2???? IC1?????????????????? IC2????????????????*/
// TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
// TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
//
// /* TIM2 3 4 enable counter */
// TIM_Cmd(TIM2, ENABLE);
// TIM_Cmd(TIM3, ENABLE);
// TIM_Cmd(TIM4, ENABLE);
// TIM_Cmd(TIM5, ENABLE);
/***********??????????TIM5????????????????????USART2??ADC12??????????????????????TIM5 CH1??2??·¨??????PA0??1***************/
//////////////////////TIM2-TIM5????????????TIM_ICInit(TIMx, &TIM_ICInitStructure);////////////////////
TIM_DeInit(TIM2); TIM_DeInit(TIM3); TIM_DeInit(TIM4); TIM_DeInit(TIM5);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
TIM_TimeBaseInitStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 0xffff;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
TIM2->CR2 &= 0xFF7F; TIM3->CR2 &= 0xFF7F; TIM4->CR2 &= 0xFF7F; TIM5->CR2 &= 0xFF7F; //TI1S=0,TIM5_CH1??????????TI1??????
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_EncoderInterfaceConfig(TIM5, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0x06;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInit(TIM5, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInit(TIM5, &TIM_ICInitStructure);
//////////////????·??????????????????????????è??????????????///////////////
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM2->CNT = 0x0000;TIM3->CNT = 0x0000;TIM4->CNT = 0x0000;TIM5->CNT = 0x0000;
//////////////////????????????????????????????×???????????/////////////////
TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM4, ENABLE);TIM_Cmd(TIM5, ENABLE);
//////////////////////????????TIM5????PA0??PA1??CH1??CH2????????????????·???????////////////////////////////////
/*config TIM6 as sample timer interrupt*/
TIM_TimeBaseInitStructure.TIM_Prescaler = 0x47; //71
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 999; //1000 us ??????????????????1000us??
//??????????????????????????????????????????·???SampleTime?????ú????????????
/*?¤·?????72MHZ/(71+1)=1MHZ,×?????×°????????500us???*/
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStructure); //?¨?±?÷TIM6??TIM2345???ù???ú????????,?¨?????íPID,?ü??PWM
// TIM_Cmd(TIM6, ENABLE);//TIM6????·¨?¨?±?÷??·????÷???ò??????
TIM_ARRPreloadConfig(TIM6, ENABLE); //TIM6_CR1_7/ARPE=1,TIMx_ARR?????÷????????
TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //TIM6_SR_0/UIF=0???í???????ü??????±ê????
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //enable tim6 count overflow interrupt TIM6_DIER_0/UIE=1???????ü??????
}
GPIO的
/*configure TIM2_CH1/CH2,remap to A15/PB3*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PA15??TIM2_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // B3 ??TIM2_CH2
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);//AFIO_MAPR_TIM2_REMAP[1:0]/[9:8]=01;TIM2????????????·?????1 CH1/PA15??CH2/PB3
/*Confiture TIM3_CH1 & CH2 as Encoder Interfaces, Remaped on C6 & C7*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //TIM3???ê??????????CH1=PC6??CH2=PC7
/*Configure TIM4_CH1 (PD12) & TIM4_CH2 (PD13) as Encoder Interfaces*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_Init(GPIOD,&GPIO_InitStructure); //TIM4_REMAP=0??????????????CH1/CH2????????????????PB6/PB7????
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
/*Configure TIM5_CH1 (PA0) & TIM5_CH2 (PA1) as Encoder Interfaces*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStructure); //TIM5_CH1/CH2,??????????????????????PA0/PA1
定时采集函数的语句
void TIM6_IRQHandler() //?¨?±1000us
{
if(TIM_GetITStatus(TIM6, TIM_IT_Update) == SET) //TIM6?????òif????
{
TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //TIM6_SR_0/UIF=0,????????±ê????
speed_fb[0] = TIM_GetCounter(TIM2); //???ù???????ú????·??????????¨????????????????
TIM_SetCounter(TIM2, 0); //???????ú?????÷TIMx_CNT????????????????
speed_fb[1] = TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3, 0);
speed_fb[2] = TIM_GetCounter(TIM4);
TIM_SetCounter(TIM4, 0);
speed_fb[3] = TIM_GetCounter(TIM5);
TIM_SetCounter(TIM5, 0);
一周热门 更多>