?? main.c
字號:
/*----------------------------------------------------------------------------
本例子實現外部中斷功能
例子使用PE2,PE2與KEY1相連,按下KEY1,就可以點亮LED燈了
*---------------------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#define SYS_GLOBALS
#include "include.h"
#define LED2_ON GPIO_ResetBits(GPIOC, GPIO_Pin_2)
#define LED2_OFF GPIO_SetBits(GPIOC, GPIO_Pin_2)
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Exit_Configuration(void);
/* Private functions ---------------------------------------------------------*/
//中斷函數
void EXTI15_10_IRQHandler(void)
{
//判斷中斷管腳
if(EXTI_GetITStatus(EXTI_Line12) != RESET)
{
/* Toggle PC8 pin */
GPIO_WriteBit(GPIOC, GPIO_Pin_8, (BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_8))));
/* Clear the EXTI line 12 pending bit */
//清除中斷標準
EXTI_ClearITPendingBit(EXTI_Line12);
}
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();//配置系統時鐘
GPIO_Configuration();//配置GPIO
/* NVIC configuration */
NVIC_Configuration();//配置中斷
Exit_Configuration();
//SysTick_Init();//系統定時器初始化
//Timer2_Configuration();//定時器2初始化
while(1);
}
void Exit_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//外部中斷使用的RCC時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO, ENABLE);//使能GPIO時鐘
//外部中斷使用的GPIO配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Connect EXTI Line2 to PE.2 */
EXTI_ClearITPendingBit(EXTI_Line2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);
/* Configure EXTI Line2 to generate an interrupt on falling edge */
//配置外部中斷
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Generate software interrupt: simulate a falling edge applied on EXTI line 12 */
//這里是軟件模擬產生中斷
// EXTI_GenerateSWInterrupt(EXTI_Line2);
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
/* Clear the EXTI line 2 pending bit */
EXTI_ClearITPendingBit(EXTI_Line2);
}
//外部中斷使用的NVIC配置
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the EXTI15_10 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);//使能外部時鐘
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部時鐘就緒
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer 啟用預取緩沖器 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state FLASH設置2個等待周期*/
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK 設置系統設置*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK PCLK2時鐘=主時鐘*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 PCLK1時鐘為主時鐘1/2*/
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz 設置時鐘為72M*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL 使能PLL*/
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready 等待PLL工作穩定*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source 選擇PLL做為系統時鐘源*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source 準備就緒,開始干活*/
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO_LED clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE );
/* Configure IO connected to LED0, LED1, LED2 *********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
//#ifdef VECT_TAB_RAM
#if defined (VECT_TAB_RAM)
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //設置中斷向量在RAM中
//#elif defined(VECT_TAB_FLASH_IAP)
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //設置中斷向量在FLASH中
#endif
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -