大家帮我看看为什么PB.2那个中断没有反应?非常谢谢!
#include "
STM32f10x.h"
#include <stdio.h>
#include "delay.h"
//RCCÅäÖÃ
void RCC_Configura
tion(void)
{
//-----------ʹÓÃÍⲿRC¾§Õñ-----------------
RCC_DeInit(); //³õʼ»¯ÎªÈ±Ê¡Öµ×´Ì¬
RCC_HSEConfig(RCC_HSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);
}
//GPIOÉèÖÃ
void GPIO_Config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜPA¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11); // ¶Ë¿ÚÅäÖÃGPIOA3£¬4£¬5£¬6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜPB¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_2|GPIO_Pin_4);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀÊäÈë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯ GPIOB
}
//PWMÊä³ö
void TIM3_PWM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //¢ÙʹÄܶ¨Ê±Æ÷ 2 ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //¢ÙʹÄÜ GPIOA£¬B¶Ë¿Ú
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³ö TIM2 µÄ PWM Âö³å²¨ÐÎ
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_6|GPIO_Pin_7); //TIM_CH1,2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //¢Ù³õʼ»¯ GPIOA
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0|GPIO_Pin_1); //TIM_CH3,4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //¢Ù³õʼ»¯ GPIOB
//³õʼ»¯ TIM3
TIM_TimeBaseStructure.TIM_Period = 1000; //ÉèÖÃÔÚ×Ô¶¯ÖØ×°ÔØÖÜÆÚÖµ
TIM_TimeBaseStructure.TIM_Prescaler =0; //ÉèÖÃÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM ÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¢Û³õʼ»¯ TIM3
//³õʼ»¯ TIM3 Channel2 PWM ģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ PWM ģʽ 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ¸ß
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //¢Ü³õʼ»¯ÍâÉè TIM3 OC1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜԤװÔؼĴæÆ÷
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //¢Ü³õʼ»¯ÍâÉè TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜԤװÔؼĴæÆ÷
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //¢Ü³õʼ»¯ÍâÉè TIM3 OC3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜԤװÔؼĴæÆ÷
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //¢Ü³õʼ»¯ÍâÉè TIM3 OC4
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜԤװÔؼĴæÆ÷
TIM_ARRPreloadConfig(TIM3, ENABLE); //ʹÄÜTIM3ÖØÔؼĴæÆ÷AR
TIM_Cmd(TIM3, ENABLE); //¢ÝʹÄÜ TIM3
}
//ÍⲿÖÐ¶Ï 0 ·þÎñ³ÌÐò
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// EXTI_ClearITPendingBit(EXTI_Line2);
// EXTI_ClearITPendingBit(EXTI_Line4);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//GPIOB.2ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖÃ
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource2);
EXTI_DeInit();
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Line=EXTI_Line2;
EXTI_InitStructure.EXTI_Trigger =EXTI_Trigger_Falling; //ϽµÑØ´¥·¢
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //³õʼ»¯ÖжÏÏß²ÎÊý
//GPIOB.4ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖÃ
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource4);
EXTI_DeInit();
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //ϽµÑØ´¥·¢
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //³õʼ»¯ÖжÏÏß²ÎÊý
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÇÀÕ¼ÓÅÏȼ¶ 0£¬
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×ÓÓÅÏȼ¶ 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÇÀÕ¼ÓÅÏȼ¶ 0£¬
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×ÓÓÅÏȼ¶ 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure);
}
//ÍⲿÖжÏ0·þÎñ³ÌÐò
void EXTI3_IRQHandler(void)
{
// EXTI_ClearITPendingBit(EXTI_Line4);
{
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3)==0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_5| GPIO_Pin_8| GPIO_Pin_11);
GPIO_ResetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4| GPIO_Pin_9| GPIO_Pin_10);
}
}
EXTI_ClearITPendingBit(EXTI_Line3); //Çå³ýLINE0ÉϵÄÖжϱê־λ
}
void EXTI4_IRQHandler(void)
{
// EXTI_ClearITPendingBit(EXTI_Line3);
{
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4)==0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_3 | GPIO_Pin_9| GPIO_Pin_10);
GPIO_ResetBits(GPIOA,GPIO_Pin_2| GPIO_Pin_5| GPIO_Pin_4| GPIO_Pin_8| GPIO_Pin_11);
}
}
EXTI_ClearITPendingBit(EXTI_Line4); //Çå³ýLINE1ÉϵÄÖжϱê־λ
}
int main(void)
{
delay_init();
RCC_Configuration();
GPIO_Config();
EXTIX_Init();
TIM3_PWM_Init();
//GPIO_SetBits(GPIOA, GPIO_Pin_2 | GPIO_Pin_4| GPIO_Pin_8| GPIO_Pin_10);
//GPIO_ResetBits(GPIOA,GPIO_Pin_3| GPIO_Pin_5| GPIO_Pin_9| GPIO_Pin_11);
while(1)
{delay_ms(1);
GPIO_SetBits(GPIOA, GPIO_Pin_2 | GPIO_Pin_4| GPIO_Pin_8| GPIO_Pin_10);
GPIO_ResetBits(GPIOA,GPIO_Pin_3| GPIO_Pin_5| GPIO_Pin_9| GPIO_Pin_11);
}
}
一周热门 更多>