毕设的东西,目前在测量电机转速,用的驱动模块是L298N,电机的测速传感器用的光电码盘,开发板为战舰,想实现的功能是:输出PWM波控制电机转速,光电编码器会输出方波脉冲,用中断对脉冲计数,遇到高电平就进入外部中断,中断函数计数加一,主函数中计算转速,通过串口输出,显示到电脑上。代码如下。现在的问题是进入中断出现问题,只有在PWM波输出线插拔的一瞬间才能进入中断,而且转速输出一直是0,求助[mw_shl_code=cpp,true]#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#include "led.h"
#include "timer.h"
#include "usart.h"
#include "exti.h"
float rot;
int count=0;
void GPIO_Configuration(void);
void TIM3_Configuration(void);
void EXTI_Configuration(void);
void USART_Configuration(void);
GPIO_Configuration();
EXTI_Configuration();
TIM3_Configuration();
USART_Configuration();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
delay_init();
while(1)
{
EXTI_ClearITPendingBit(EXTI_Line6);
delay_ms(500);
printf("
转速为%d
",count);
// rot=(float)count*60/0.5/448;
count=0;
// printf("
转速为(r/min):%f
",rot);
}
while(1)
{
EXTI_ClearITPendingBit(EXTI_Line6);
delay_ms(500);
printf("
计数为%d
",count);
// rot=(float)count*60/0.5/448;
count=0;
// printf("
转速为(r/min):%f
",rot);
}
void EXTI4_IRQnHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4)!=RESET)
{
count++;
printf("
01010101010101
");
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//½óêÕ
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//·¢Ëí
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//ê1ÄüAFIOê±Öó
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);//ÖD¶ÏÏßÅäÖÃ
EXTI_InitStructure.EXTI_Line=EXTI_Line6;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0X02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0X02;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
u16 CCR1_Val=700;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=999;
TIM_TimeBaseInitStructure.TIM_Prescaler=3599;
TIM_TimeBaseInitStructure.TIM_ClockDivision=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStruction;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_InitStruction.USART_BaudRate=115200;
USART_InitStruction.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStruction.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruction.USART_Parity=USART_Parity_No;
USART_InitStruction.USART_StopBits=USART_StopBits_1;
USART_InitStruction.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStruction);
USART_Cmd(USART1,ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>