完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include "encoding_disk.h"
#include "delay.h" /************************************************************************************** PA.6-> RUN-A2 tiM3-CH1// PA.7-> RUN-B2 TIM3-CH2// PB.0-> RUN-A1 TIM3-CH3// PB.1-> RUN-B1 TIM3-CH4// ****************************************************************************************/ #define ENCODER_TIMER TIM3 // Encoder unit connected to TIM3 #define ENCODER_PPR (u16)(100) // number of pulses per revolution #define SPEED_BUFFER_SIZE 8 #define COUNTER_RESET (u16)0 #define ICx_FILTER (u8) 6 // 6<-> 670nsec #define TIMx_PRE_EMPTION_PRIORITY 1 #define TIMx_SUB_PRIORITY 0 #define ENCODER_TIM_PERIOD 0xffff// #define MAX_COUNT 10000// void EncodingDisk_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);// /* Enable GPIOA, clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//??GPIOA?? GPIO_StructInit(&GPIO_InitStructure); /* Configure PA.06,07 as encoder input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PA8 PA9???? GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_DeInit(ENCODER_TIMER); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling //??????????0,??? //TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1; //???????? TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD-1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//?????? T_dts = T_ck_int TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM???? TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);/ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter =ICx_FILTER; TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure); TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);//??TIM1?????? TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE); TIM3->CNT = 0; TIM_Cmd(ENCODER_TIMER, ENABLE); } s16 Enc_GetCount(void) { static u16 lastCount = 0; u16 curCount = ENCODER_TIMER->CNT; s32 dAngle = curCount - lastCount; if(dAngle >= MAX_COUNT) { dAngle -= ENCODER_TIM_PERIOD; } else if(dAngle < -MAX_COUNT) { dAngle += ENCODER_TIM_PERIOD; } lastCount = curCount; return (s16)dAngle; } 主函数通过 int main(void) { s16 temp,NUM; delay_init(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); EncodingDisk_Init(); Usart_Init(9600); GPIO_SetBits (GPIOA,GPIO_Pin_11); while(1) { NUM = TIM_GetCounter(TIM3)/2; if(NUM!=temp) { temp=NUM; printf("%drn",NUM); } delay_ms(300); } |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2105 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1941 浏览 3 评论
4537 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2084 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2599 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 21:54 , Processed in 0.465876 second(s), Total 69, Slave 52 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号