本帖最后由 lmywudi 于 2017-11-6 16:45 编辑
摸索了一天,终于解决了这个问题!很有成就感。
[mw_shl_code=c,true]GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
USART_InitTypeDef USART_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1|RCC_APB2Periph_USART1,ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_DeInit(TIM3);
GPIO_DeInit(GPIOA);
USART_DeInit(USART1);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_7);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1_RX PA.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
TIM_TimeBaseStructure.TIM_Period=x;
TIM_TimeBaseStructure.TIM_Prescaler=71;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE );
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
//TIM_OCInitStructure.TIM_Channel=TIM_Channel_1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; //ÇàÕ¼óÅÏ輶 3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óóÅÏ輶 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ í¨μàê1Äü
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM3 ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏèÕ¼óÅÏ輶 0 ¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶 3 ¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ í¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_SetCompare1(TIM1, x/2);
while(1)
{
}
}
void TIM1_UP_IRQHandler(void) //TIM3 ÖD¶Ï
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //¼ì2é TIM3 ¸üDÂÖD¶Ï·¢éúóë·ñ
{
// x=~x;
TIM_ClearITPendingBit(TIM1, TIM_IT_Update ); //Çå3y TIM3 ¸üDÂÖD¶Ï±êÖ¾
y++;
if(y>1600)//1600为指定脉冲数,1600个脉冲刚好使步进电机转一圈。
{
y=0;
TIM_Cmd(TIM1,DISABLE);
}
USART_SendData(USART1, 0x00);
// if(x)GPIO_SetBits(GPIOA,GPIO_Pin_8);
// else GPIO_ResetBits(GPIOA,GPIO_Pin_8);
}[/mw_shl_code]
用这个方法可以很精确的控制步进电机的位置
不过还有个问题不明白,NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
在官方库中,中断通道是这样写的:TIM1_UP_IRQChannel
但没法这样用,提示未定义,但在库中没有发现TIM1_UP_IRQn这种写法,现在已经搞不清楚了
你好,有相应的代码供参考吗?分享分享吧多谢
一周热门 更多>