初学STM32,了解了一下中断的库函数和中断机制--------- /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Version : V2.0
* Date : 05/23/2008
* DescriptWHY8W-QT9DJ-P0VNX-L15PE-W4DCR-VP3FMion : Main program body
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
* FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
* IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
*******************************************************************************/ /* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_it.h"
#include "misc.h" ITStatus EXTIStatus;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/ void GPIO_Config(void);
void EXTI_Config(void);
void NVIC_Config(void);
void TIME_Config(void);
/* Private functions ---------------------------------------------------------*/ void Delay (u32 nCount)//延时函数
{
for(; nCount != 0; nCount--);
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
SystemInit();
GPIO_Config();
EXTI_Config();
NVIC_Config();
TIME_Config();
/* Infinite loop */
while (1){
if(key_1_flag==0x02)
{
/*====LED1-ON=======*/
GPIO_ResetBits(GPIOB , GPIO_Pin_8);
GPIO_SetBits(GPIOB , GPIO_Pin_9);
GPIO_SetBits(GPIOE , GPIO_Pin_0);
GPIO_SetBits(GPIOE , GPIO_Pin_1);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
/*====LED12-ON=======*/ GPIO_ResetBits(GPIOB , GPIO_Pin_8); GPIO_ResetBits(GPIOB , GPIO_Pin_9);
GPIO_SetBits(GPIOE , GPIO_Pin_0);
GPIO_SetBits(GPIOE , GPIO_Pin_1);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
/*====LED123-ON=======*/ GPIO_ResetBits(GPIOB , GPIO_Pin_8); GPIO_ResetBits(GPIOB , GPIO_Pin_9);
GPIO_ResetBits(GPIOE , GPIO_Pin_0);
GPIO_SetBits(GPIOE , GPIO_Pin_1);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
/*====LED1234-ON=======*/ GPIO_ResetBits(GPIOB , GPIO_Pin_8); GPIO_ResetBits(GPIOB , GPIO_Pin_9);
GPIO_ResetBits(GPIOE , GPIO_Pin_0);
GPIO_ResetBits(GPIOE , GPIO_Pin_1);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff); /*====LED1234-OFF=======*/ GPIO_SetBits(GPIOB , GPIO_Pin_8); GPIO_SetBits(GPIOB , GPIO_Pin_9);
GPIO_SetBits(GPIOE , GPIO_Pin_0);
GPIO_SetBits(GPIOE , GPIO_Pin_1);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
}
}
}
void GPIO_Config(void)//IO口的配置
{
GPIO_InitTypeDef GPIO_InitStructure;//定义一个IO口初始化变量
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |RCC_APB2Periph_AFIO, ENABLE); //开启IO口时钟和外围时钟
/**
* LED1 -> PB8 , LED2 -> PB9 , LED3 -> PE0 , LED4 -> PE1
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/**
* PE2-- KEY1 PE3----KEY2
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 |GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOE, &GPIO_InitStructure); } /**
* @brief EXTI_Config Program.
* @param None
* @retval : None
*/
void EXTI_Config(void)// 外部中断配置
{
EXTI_InitTypeDef EXTI_InitStructure;
//EXTI_StructInit(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);
EXTI_ClearITPendingBit(EXTI_Line3);
EXTI_ClearITPendingBit(EXTI_Line2); EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//EXTI_Cmd(EXTI , ENABLE);
}
/**
* @brief Configures the nested vectored interrupt controller.
* @param None
* @retval : None
*/
void NVIC_Config(void)//嵌套中断配置
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //选择优先级组
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //选择外部中断2号线
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//主优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //选择外部中断3号线
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //使能中断
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //选择TIM2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
NVIC_Init(&NVIC_InitStructure); }
/*-----------------------------------------------------------------------
tim2 配置 --------------------------------------------------------------------*/
void TIME_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); TIM_DeInit(TIM2); //复位定时器 TIM_TimeBaseStructure.TIM_Period=2000; //定时器初始值 TIM_TimeBaseStructure.TIM_Prescaler=(36000-1); //时钟预分频 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // 时钟分割 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //初始化定时器的值 TIM_ClearFlag(TIM2,TIM_FLAG_Update); //清除定时器中断标志 TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //使能中断 TIM_Cmd(TIM2,ENABLE); //开启时钟
} #include "stm32f10x_it.h" void TIM2_IRQHandler(void)//定时器中断函数
{//判断是否定时中断
if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET ) {
TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);//必须清除中断标志位否则一直中断
}
}
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
|