我是一名大二学生,请各位前辈帮帮忙。void SystemInit (void)
{
/* Set HSION bit */
RCC->CR |= (uint32_t)0x00000001;
// select HSI as PLL source
RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC_HSI_Div2;
RCC->CFGR |= (uint32_t)RCC_CFGR_PLLMULL9;
/* HCLK = SYSCLK/1 */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* Enable PLL */
RCC->CR |= RCC_CR_PLLON;
/* Wait till PLL is ready */
while((RCC->CR & RCC_CR_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
{
}
这是使用内部晶振HSI的代码
void TIM3_Configuration(void) //±àÂëÆ÷½Ó¿ÚÉèÖã¨TIM3£©/PA6-AÏà PA7-BÏà
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //ʹÄÜGPIOAÍâÉèʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖÃΪÉÏÀÊäÈëģʽ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚµÄËÙÂÊΪ50M
GPIO_Init(GPIOA, &GPIO_InitStructure); //IO¿ÚÅäÖú¯Êý
GPIO_WriteBit(GPIOA, GPIO_Pin_6,Bit_SET);
GPIO_WriteBit(GPIOA, GPIO_Pin_7,Bit_SET);
TIM_TimeBaseStructure.TIM_Period = ECDPeriod; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler = 0; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ ²»·ÖƵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//ÉèÖö¨Ê±Æ÷3Ϊ±àÂëÆ÷ģʽ IT1 IT2ΪÉÏÉýÑؼÆÊý
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,TIM_ICPolarity_BothEdge,TIM_ICPolarity_BothEdge);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6; //ÊäÈëÂ˲¨Æ÷
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update); //Çå³ýËùÓбê־λ
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //ÔÊÐíÖжϸüÐÂ
TIM3->CNT = 0;
TIM_Cmd(TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStructure.TIM_Period = TIMPeriod-1;
TIM_TimeBaseStructure.TIM_Prescaler =48-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_UpdateRequestConfig(TIM4,TIM_UpdateSource_Regular);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM4,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
EncoderType GetEncoder;
double Get_Encoder_TWO(void)
{
s32 CNT3_temp,CNT3_last;////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
GetEncoder.cnt3 = TIM3 -> CNT;
CNT3_last = GetEncoder.CNT3;
CNT3_temp = GetEncoder.rcnt3 * prd + GetEncoder.cnt3;
GetEncoder.V3 = CNT3_temp - CNT3_last;
while ((s32)(GetEncoder.V3)>Vbreak)
{
GetEncoder.rcnt3--;
CNT3_temp = GetEncoder.rcnt3 * prd + GetEncoder.cnt3;
GetEncoder.V3 = CNT3_temp - CNT3_last;
}
while ((s32)(GetEncoder.V3)<-Vbreak)
{
GetEncoder.rcnt3++;
CNT3_temp = GetEncoder.rcnt3 * prd + GetEncoder.cnt3;
GetEncoder.V3 = CNT3_temp - CNT3_last;
}
GetEncoder.CNT3 = CNT3_temp;
return CNT3_temp;
}
/*********************************************END OF FILE**********************/
这是编码器的配置,为什么电机能转,定时也准确,就是采集不到编码器的值。
|