//使用寄存器写的按键加led灯的stm32程序
#include "stm32f10x_lib.h"
#define LED 0x00000010
#define LED_SET (1<<4)
#define LED_RESET (1<<(4+16))
void rccinitsation(void);
void delay(u32 count);
int main(void)
{
//u32 i=0;
rccinitsation();//设置系统时钟
//PA.0输入
GPIOA->CRL &= 0xfffffff0;
GPIOA->CRL |= (0xfffffff8);
//PA.4输出
GPIOA->CRL &= (0xfff0ffff);
GPIOA->CRL |= (3<<16);
GPIOA->ODR &= (~(1<<4));
while(1)
{
if((GPIOA->IDR&0x00000001) == 0)
{
delay(5000);
if((GPIOA->IDR&0x00000001) == 0)
{
if((GPIOA->ODR&LED) == LED)
{
GPIOA->BSRR |= (LED_RESET);
}
else
{
GPIOA->BSRR |=(LED_SET );
}
}
}
}
}
void delay(u32 count)
{
while(count--);
}
void rccinitsation(void)
{
ErrorStatus startup; //定义枚举类型
RCC_DeInit(); //复位系统时钟设置
RCC_HSEConfig(RCC_HSE_ON);//启动hse
startup = RCC_WaitForHSEStartUp(); //等待hse起振并稳定
//判断hse是否起振
if(startup == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);//(hclk)ahb 时钟源为sysclk 1分频
RCC_PCLK2Config(RCC_HCLK_Div1);//(pclk2) 时钟源为hclk(ahb) 1分频
RCC_PCLK1Config(RCC_HCLK_Div2);//(pclk1) 时钟源为hclk(ahb) 2分频
FLASH_SetLatency(FLASH_Latency_2);//设置flash延时周期为2s
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能flash预取缓存
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//分频,倍频数为9 则pll=8*9=72M
RCC_PLLCmd(ENABLE);// 使能pll
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);//等待pll输出稳定
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);// 选择sysclk时钟源为pll
while(RCC_GetSYSCLKSource() != 0x08); //等待pll成为sysclk时钟源
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
}