一、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)
-
{
-
}
-
}
阅读(633) | 评论(0) | 转发(0) |