本帖最后由 ycgxy1650601 于 2018-4-2 20:29 编辑
单片机板:STM32F1精英版;
外部中断1:IO口为PC1;初始化程序为EXIT_Init();
定时器3:IO口为PB1;初始化程序为TIM3_PWM_Init();
问题描述:EXIT_Init() 和 TIM3_PWM_Init() 两个函数在程序中互为前后,结果不一样;
1.EXIT_Init() 在 TIM3_PWM_Init() 前面,外部中断和定时器中断都能实现;
2.EXIT_Init() 在 TIM3_PWM_Init() 后面,外部中断不能实现,定时器中断能实现;(更改抢占和响应优先级没有效果)
为什么会出现这种情况,有遇到的给点提示呗。
注意:代码见四楼
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
//PC1初始化(忘了贴出来了)
void GPIO_Init1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_1);
}
void TIM3_IRQHandler()
{
long temp;
if(TIM_GetITStatus(TIM3,TIM_FLAG_Update)!=RESET)
{
...省略
TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
}
}
void TIM3_PWM_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
TIM_TimeBaseStructure.TIM_Period = 11;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_UpdateRequestConfig(TIM3,TIM_UpdateSource_Regular);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 6;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_Update ,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
// TIM_Cmd(TIM3, ENABLE); //在其他函数中打开定时器
}
void EXIT_Init()
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line=EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI1_IRQHandler(void)
{
delay_ms(10);
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)!=0)
{
...此处省略
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
int main(void)
{
SystemInit();
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
GPIO_Init1();
EXIT_Init();
TIM3_PWM_Init();
// EXIT_Init();
Variable_Init();
pwm_fun(200,600);
while(1)
{
move(3200,200);
dendai();
delay_ms(300);
}
}
代码见四楼
都没初始化PC1IO口能工作?
一周热门 更多>