如題,由于項(xiàng)
目需求不得已使用高級(jí)定時(shí)器tim10但發(fā)現(xiàn)使用tim10無法獲取編碼器的值
這是初始化函數(shù)
void?ENCODER_Init_TIM10(void)?{ ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseStructure={0}; ????TIM_ICInitTypeDef?TIM_ICInitStructure={0}; ????GPIO_InitTypeDef?GPIO_InitStructure={0}; //????NVIC_InitTypeDef?NVIC_InitStructure={0}; ????RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM10?|?RCC_APB2Periph_GPIOB,?ENABLE);?//使能定時(shí)器10,PB端口的時(shí)鐘 ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,?ENABLE);???//使能AFIO復(fù)用功能模塊時(shí)鐘 ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_8?|?GPIO_Pin_9;??//端口配置 ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING;?//浮空輸入 ????GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; ????GPIO_Init(GPIOB,?&GPIO_InitStructure);??????????????????????//根據(jù)設(shè)定參數(shù)初始化GPIOB ????TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); ????TIM_TimeBaseStructure.TIM_Prescaler?=?0x1;?//?預(yù)分頻器 //????TIM_TimeBaseStructure.TIM_Period?=?2500-1;?//設(shè)定計(jì)數(shù)器自動(dòng)重裝值 ????TIM_TimeBaseStructure.TIM_ClockDivision?=?TIM_CKD_DIV1;?//選擇時(shí)鐘分頻:不分頻 ????TIM_TimeBaseStructure.TIM_CounterMode?=?TIM_CounterMode_Up;?//TIM向上計(jì)數(shù) ????TIM_TimeBaseInit(TIM10,?&TIM_TimeBaseStructure);?//根據(jù)TIM_TimeBaseInitStruct的參數(shù)初始化定時(shí)?器TIM10 ????TIM_EncoderInterfaceConfig(TIM10,?TIM_EncoderMode_TI1, ????????????????TIM_ICPolarity_Rising,?TIM_ICPolarity_Rising);??//使用編碼器模式3 ????TIM_ICStructInit(&TIM_ICInitStructure);??//把TIM_ICInitStruct?中的每一個(gè)參數(shù)按缺省值填入 ????TIM_ICInitStructure.TIM_Channel?=?TIM_Channel_1;//|TIM_Channel_2 ????TIM_ICInitStructure.TIM_ICFilter?=?10;??//設(shè)置濾波器長(zhǎng)度 ????TIM_ICInit(TIM10,?&TIM_ICInitStructure); //????NVIC_InitStructure.NVIC_IRQChannel?=?TIM10_UP_IRQn; //????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; //????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?0x01; //????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0x02; //????NVIC_Init(&NVIC_InitStructure); ????//????TIM_ITConfig(TIM10,?TIM_IT_CC1,?ENABLE); ????TIM_ITConfig(TIM10,?TIM_IT_Update,?ENABLE); ????TIM_ClearFlag(TIM10,?TIM_FLAG_Update);??//清除TIM的更新標(biāo)志位 ????TIM_SetCounter(TIM10,?0); ????TIM_Cmd(TIM10,?ENABLE); }
這是中斷函數(shù):
int?circle_count=0;//全局變量-圈數(shù) void?TIM10_UP_IRQHandler(void) { ????circle_count=1; ????if(TIM_GetITStatus(TIM10,TIM_IT_Update)==SET) ????{ ????????TIM_ClearITPendingBit(TIM10,TIM_IT_Update); //????????if((TIM10->CTLR1>>4?&?0x01)==0)?//DIR==0 //????????????circle_count++; //????????else?if((TIM10->CTLR1>>4?&?0x01)==1)//DIR==1 //????????????circle_count--; ????} }
這是頭文件:
#ifndef?DRIVE_ENCODER_ENCODER_H_ #define?DRIVE_ENCODER_ENCODER_H_ #include?"ch32v30x.h" extern?int?circle_count; void?ENCODER_Init(void);//初始化 void?ENCODER_Init_TIM8(void);//初始化 void?ENCODER_Init_TIM10(void);//初始化 void?TIM10_UP_IRQHandler(void)?__attribute__((interrupt("WCH-Interrupt-fast"))); #endif?/*?DRIVE_ENCODER_ENCODER_C_?*/
這是主函數(shù):
int?volatile?Encoder; int?main(void) { ????Delay_Init(); ???NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); ???Led_Init(); ???ENCODER_Init_TIM10(); ???Encoder=1; ???while(1){ ???????GPIO_ResetBits(GPIOE,?GPIO_Pin_4); ???????GPIO_SetBits(GPIOE,?GPIO_Pin_5); ???????GPIO_ResetBits(GPIOE,?GPIO_Pin_6); ???????Encoder=TIM_GetCounter(TIM10);//當(dāng)前角度 ???????TIM_SetCounter(TIM10,?0); ???????Delay_Ms(500); ???????GPIO_SetBits(GPIOE,?GPIO_Pin_4); ???????GPIO_ResetBits(GPIOE,?GPIO_Pin_5); ???????GPIO_SetBits(GPIOE,?GPIO_Pin_6); ???????Encoder=TIM_GetCounter(TIM10);//當(dāng)前角度 ???????TIM_SetCounter(TIM10,?0); ???????Delay_Ms(500); ???} }
監(jiān)聽的是“Encoder”的值發(fā)現(xiàn)為0,檢查后發(fā)現(xiàn)寄存器為0;
熱門產(chǎn)品 :
CH585: 集成高速USB和NFC的藍(lán)牙MCU