初级会员

- 积分
- 121
- 金钱
- 121
- 注册时间
- 2015-11-16
- 在线时间
- 8 小时
|
1金钱
代码如下
#include "sys.h"
#include "delay.h"
u8 start_Frequency=15;
u16 pulse=0;
void motor_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM3_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = (int)( ( 72000 / ( ( ((double) start_Frequency) / 10 ) * 1 ) ) -1 );
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (int)( (int)( ( 72000 / ( ( ((double) start_Frequency) / 10 ) * 1 ) ) -1 ) * 50 / 100 );
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
//êäèë2¶»ñ3õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
TIM3_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM3_ICInitStructure.TIM_ICFilter=TIM_ICPolarity_Rising;
TIM3_ICInitStructure.TIM_ICPolarity=TIM_ICSelection_DirectTI;
TIM3_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM3_ICInitStructure.TIM_ICSelection= 0x00;
TIM_ICInit(TIM3, &TIM3_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel =TIM3_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM3,ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void B_motor(u32 pwm_amount,u8 direction,u8 frequency)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
if(direction)
{
GPIO_SetBits(GPIOA, GPIO_Pin_3);
}
else GPIO_ResetBits(GPIOA, GPIO_Pin_3);
TIM_TimeBaseStructure.TIM_Period = (int)( ( 72000 / ( ( ((double) frequency) / 10 ) * 1 ) ) -1 );
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (int)( (int)( ( 72000 / ( ( ((double) frequency) / 10 ) * 1 ) ) -1 ) * 50 / 100 );
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
while(pulse<=pwm_amount);
TIM_Cmd(TIM2, DISABLE);
pulse=0;
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET )
{
pulse++;
}
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
}
void GPIO_init(void);
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
motor_init();
delay_init();
GPIO_init();
while(1)
{
B_motor(10000,1,15);
GPIO_ResetBits(GPIOE, GPIO_Pin_5);
// B_motor(1000,1,30);
// B_motor(1000,1,100);
// B_motor(1000,1,500);
// B_motor(1000,1,100);
// B_motor(1000,1,30);
}
}
void GPIO_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
}
请问为什么一上电之后他就自动进入了TIM3_IRQHandler函数?本来是想用TIM3_CH1捕获TIM2_CH2输出的PWM的个数的,可是现在不将两个引脚接在一起单片机都会自动进入到TIM3_IRQHandler里面,求各路大神解答。
|
最佳答案
查看完整内容[请看2#楼]
把void B_motor(u32 pwm_amount,u8 direction,u8 frequency)里的while(pulse
|