#include<stm32f10x_lib.h>
#define GPIO_A 0
#define FTIR 1
#define RTIR 2
void LED_Config(){
RCC->APB2ENR|=1<<2;
GPIOA->CRL&=0xFFFFFF0F;
GPIOA->CRL|=0x00000030;
}
void KEY_Config(){
RCC->APB2ENR|=1<<4;
GPIOC->CRL&=0xFFFFFFF0;
GPIOC->CRL|=0x00000008;
GPIOC->ODR|=1<<0;
}
void NVIC_Config(u8 GPIOx,u8 BITx,u8 TRIM){
u8 EXTIADDR;
u8 EXTIOFFSET;
EXTIADDR=BITx/4;
EXTIOFFSET=(BITx%4)*4;
RCC->APB2ENR|=0x01;//使能复用IO功能
AFIO->EXTICR[EXTIADDR]&=~(0x000F<<EXTIOFFSET);
AFIO->EXTICR[EXTIADDR]|=GPIOx<<EXTIOFFSET;
EXTI->IMR|=1<<BITx;
if(TRIM&0x01)
EXTI->FTSR|=1<<BITx;//下降沿触发
if(TRIM&0x02)
EXTI->RTSR|=1<<BITx;
}
void MY_NVIC_PriorityGroupConfig(u8 NVIC_Group){
u32 temp,temp1;
temp1=(~NVIC_Group)&0x07;
temp1<<=8;
temp=SCB->AIRCR;
temp&=0x0000F8FF;
temp|=0x05FA0000;
temp|=temp1;
SCB->AIRCR=temp;
}
void MY_NVIC_Init(u8 NVIC_PreemptionPriority,u8 NVIC_SubPriority,u8 NVIC_Channel,u8 NVIC_Group){
u32 temp;
u8 IPRADDR=NVIC_Channel/4;
u8 IPROFFSET=NVIC_Channel%4;
IPROFFSET=IPROFFSET*8+4;
MY_NVIC_PriorityGroupConfig(NVIC_Group);
temp=NVIC_PreemptionPriority<<(4-NVIC_Group);
temp|=NVIC_SubPriority&(0x0F>>NVIC_Group);
temp&=0xF;
if(NVIC_Channel<32)
NVIC->ISER[0]|=1<<NVIC_Channel;
else
NVIC->ISER[1]|=1<<(NVIC_Channel-32);
NVIC->IPR[IPRADDR]|=temp<<IPROFFSET;
}
void EXTI_Config(){
NVIC_Config(GPIO_A,0,FTIR);
MY_NVIC_Init(2,2,EXTI0_IRQChannel,2);
}
void EXTI0_IRQHandler(){
GPIOA->ODR|=1<<1;
}
void MY_NVIC_SetVectorTable(u32 NVIC_VectTab, u32 Offset)
{
//检查参数合法性
// assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
// assert_param(IS_NVIC_OFFSET(Offset));
SCB->VTOR = NVIC_VectTab|(Offset & (u32)0x1FFFFF80);//设置NVIC的向量表偏移寄存器
//用于标识向量表是在CODE区还是在RAM区
}
void MYRCC_DeInit(void)
{
RCC->APB1RSTR = 0x00000000;//复位结束
RCC->APB2RSTR = 0x00000000;
RCC->AHBENR = 0x00000014; //睡眠模式闪存和SRAM时钟使能.其他关闭.
RCC->APB2ENR = 0x00000000; //外设时钟关闭.
RCC->APB1ENR = 0x00000000;
RCC->CR |= 0x00000001; //使能内部高速时钟HSION
RCC->CFGR &= 0xF8FF0000; //复位SW[1:0],HPRE[3:0],PPRE1[2:0],PPRE2[2:0],ADCPRE[1:0],MCO[2:0]
RCC->CR &= 0xFEF6FFFF; //复位HSEON,CSSON,PLLON
RCC->CR &= 0xFFFBFFFF; //复位HSEBYP
RCC->CFGR &= 0xFF80FFFF; //复位PLLSRC, PLLXTPRE, PLLMUL[3:0] and USBPRE
RCC->CIR = 0x00000000; //关闭所有中断
//配置向量表
#ifdef VECT_TAB_RAM
MY_NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
MY_NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
void Stm32_Clock_Init(u8 PLL)
{
unsigned char temp=0;
MYRCC_DeInit(); //复位并配置向量表
RCC->CR|=0x00010000; //外部高速时钟使能HSEON
while(!(RCC->CR>>17));//等待外部时钟就绪
RCC->CFGR=0X00000400; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
 LL-=2;//抵消2个单位
RCC->CFGR|=PLL<<18; //设置PLL值 2~16
RCC->CFGR|=1<<16; //PLLSRC ON
FLASH->ACR|=0x32; //FLASH 2个延时周期
RCC->CR|=0x01000000; //PLLON
while(!(RCC->CR>>25));//等待PLL锁定
RCC->CFGR|=0x00000002;//PLL作为系统时钟
while(temp!=0x02) //等待PLL作为系统时钟设置成功
{
temp=RCC->CFGR>>2;
temp&=0x03;
}
}
int main(){
Stm32_Clock_Init(9);
LED_Config();
KEY_Config();
EXTI_Config();
while(1){
// if((GPIOC->IDR&0x00000001)==0x00000000)
// GPIOA->ODR|=1<<1;
//GPIOA->ODR|=1<<1;
}
}
不知道哪里配置有问题
|