生活随笔
收集整理的這篇文章主要介紹了
按键中断总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、RCC初始化
/****************************************************************************
* Function Name : RCC_Configuration
* Description : Sets System clock frequency to 72MHz and configure HCLK, PCLK2
* and PCLK1 prescalers.
* Input : None
* Output : None
* Return : None
****************************************************************************/
void RCC_Configuration(void)
{
????/* Deinitialize the RCC registers */
????RCC_DeInit();
????
????/* Enable the HSE */????
????RCC_HSEConfig(RCC_HSE_ON);
????
????/* Wait till HSE is ready and if Time out is reached exit */
????HSEStartUpStatus = RCC_WaitForHSEStartUp();
????if(HSEStartUpStatus == SUCCESS)
????{
????????/* Add here PLL ans system clock config */
????????
????????/* Enable The Prefetch Buffer */
????????FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
????????
????????/* Configure Tthe Latency cycle: Set 0 Latency cycles */
????????FLASH_SetLatency(FLASH_Latency_2);
????????
????????/* Configure HCLK such as HCLK = SYSCLK */
????????RCC_HCLKConfig(RCC_SYSCLK_Div1);
????????
????????/* PCLK2 = HCLK */
????????RCC_PCLK2Config(RCC_HCLK_Div1);
????????
????????/* PCLK1 = HCLK/2 */
????????RCC_PCLK1Config(RCC_HCLK_Div2);
????????
????????/* Select HSE as system clock source*/
????????RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
????????
????????/* PLLCLK = 8MHz * 9 = 72MHz */
????????RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
????????
????????/* ADCCLK = PCLK/4 */
????????RCC_ADCCLKConfig(RCC_PCLK2_Div4);
????????
????????/* Enable PLL */
????????RCC_PLLCmd(ENABLE);
????????
????????/* Wait till PLL is ready */
????????while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
????????
????????/* Select PLL as system clock source */
????????RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till HSE is used as system clock source */
????????while(RCC_GetSYSCLKSource() != 0x08)
????????{
????????????
????????}
????}
????else
????{
????????/* If HSE fails to start-up. */
????????while(1)
????????{
????????}
????}
} 相比之前的時鐘初始化,多初始化了ADCCLK。
二、GPIO初始化
/****************************************************************************
* Function Name : GPIO_Configuration
* Description :
* Input : None
* Output : None
* Return : None
****************************************************************************/
void GPIO_Configuration(void)
{
????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
????GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
????GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
????GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
????GPIO_Init(GPIOB, &GPIO_InitStructure);
????
????GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
????GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
????GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
????GPIO_Init(GPIOB, &GPIO_InitStructure);????
} 使用PB8作為推挽輸出,PB6為浮空輸入。
三、NVIC初始化
/****************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
****************************************************************************/
void NVIC_Configuration(void)
{
????NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VET_TAB_RAM
????/* Set the Vector Table base location at 0x2000 0000 */
????NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
????/* Set the Vector Table base location at 0x8000 0000 */
????NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
????
????NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
????
????NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
????NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
????NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
????NVIC_Init(&NVIC_InitStructure);????
} 因位PB6位輸入,也就是外部中斷源。所以初始化EXTI9_5_IRQn作為外部中斷源
四、EXTI初始化
/****************************************************************************
* Function Name : EXTI_PE6_Config
* Description :
* Input : None
* Output : None
* Return : None
****************************************************************************/
void EXTI_PE6_Config(void)
{
????GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6);
????
????EXTI_InitStructure.EXTI_Line = EXTI_Line6;
????EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
????EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
????EXTI_InitStructure.EXTI_LineCmd = ENABLE;
????EXTI_Init(&EXTI_InitStructure);
????
????EXTI_GenerateSWInterrupt(EXTI_Line6);
} 設置了外部中斷源,把GPIOB6作為中斷源。外部中斷線是6,結合原理圖使用下降沿觸發。
EXTI_GenerateSWInterrupt是觸發一次軟件中斷。
同時需要編寫中斷函數在stm32f10x_it.c中:
/*******************************************************************************
* Function Name : EXTI9_5_IRQHandler
* Description : This function handles External lines 9 to 5 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI9_5_IRQHandler(void)
{
????if(EXTI_GetITStatus(EXTI_Line6) != RESET)
????{
????????GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8)));
????????
????????EXTI_ClearITPendingBit(EXTI_Line6);
????}
} 五、main函數
/****************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
****************************************************************************/
int main(void)
{
????RCC_Configuration();
????
????NVIC_Configuration();
????
????GPIO_Configuration();
????
????EXTI_PE6_Config();
????
????while(1)
????{
????}????
}
轉載于:https://www.cnblogs.com/ch122633/p/7363271.html
總結
以上是生活随笔為你收集整理的按键中断总结的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。