#include "stm32f10x.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void EXTI_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
EXTI_Configuration();
NVIC_Configuration();
while(1);
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;//定义外部高速晶振启动状态枚举变量
RCC_DeInit(); //复位RCC外部设备寄存器
RCC_HSEConfig(RCC_HSE_ON);//打开外部晶振
HSEStartUpStatus=RCC_WaitForHSEStartUp();//等待外部高速时钟准备好
if(HSEStartUpStatus==SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//开启flash操作
FLASH_SetLatency(FLASH_Latency_2);//flash操作
RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB(HCLK)时钟
RCC_PCLK2Config(RCC_HCLK_Div1);//设置APB2时钟(PCLK2)
RCC_PCLK1Config(RCC_HCLK_Div2);// 设置APB1时钟(PCLK1)
//锁相环的设置
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//配置PLL时钟源和频率系数
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET){};//等待PLL时钟就绪
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//配置系统时钟PLL
while(RCC_GetSYSCLKSource()!=0x08){};//检查pll始终是否启用
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO , ENABLE); //使能GPIOB GPIOA时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure1;
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(GPIOB, &GPIO_InitStructure);
GPIO_Write(GPIOB,0XFFFF); //初始化PB端口让 LED处于熄灭状态
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_0 ;//| GPIO_Pin_1; //上拉输入
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure1);
GPIO_Write(GPIOA,0XFFFF);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //设置中断优先级 抢占优先级1位,
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //强占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//次优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);//初始化中断
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_ClearITPendingBit(EXTI_Line0);
//配置按钮中断线触发方式
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
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_Line0); //EXTI_Line0中断允许
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); ///*将EXTI线0连接到PA0,配置其为中断脚*/
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_0); //置0点亮
EXTI_ClearFlag(EXTI_Line0);//清除中断标志
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
此帖出自
小平头技术问答
一周热门 更多>