1、一个LED灯闪烁
//GPIOF口相关寄存器的定义
//端口x输入输出模式配置寄存器,控制位=00输入,=01通用输出,=10复用,=11模拟
#define GPIOF_MODER (*(volatile unsigned *)0x40021400)
//端口输出类型配置寄存器。=0推挽输出,=1开漏输出
#define GPIOF_OTYPER (*(volatile unsigned *)0x40021404)
//端口x输出速度配置寄存器。=00,2MHz;=01,25M;=10,50M;=11,30pF时为100M.
#define GPIOF_OSPEEDR (*(volatile unsigned *)0x40021408)
//端口上拉下拉配置寄存器。=00无上下拉,=01上拉,=10下拉,=11保留
#define GPIOF_PUPDR (*(volatile unsigned *)0x4002140C)
//端口输出数据寄存器,某位=0对应端口输出低电平,=1输出高电平。
#define GPIOF_ODR (*(volatile unsigned *)0x40021414)
//时钟系统相关寄存器的定义,外设时钟使能寄存器:=1对应外设时钟使能
#define RCC_AHB1ENR (*(volatile unsigned *)0x40023830)
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int
void Led_Init(void);
void delay(void);
int main(void)
{
Led_Init(); //初始化LED接口,LED0接PF9
while(1)
{
GPIOF_ODR &= ~(1<<9); //LED0亮
delay(); //延时
GPIOF_ODR |= (1<<9); //LED0灭
delay(); //延时
}
}
//-----------------led0初始化函数定义-------------------
void Led_Init(void)
{
RCC_AHB1ENR |= 1<<5; //使能PORTF时钟
GPIOF_MODER &= ~(3<<(9*2)); //将配置PF9引脚相关位bit18,bit19清0
GPIOF_MODER |= (1<<(9*2)); //配置PF9为输出
GPIOF_OTYPER &= ~(1<<9); //电路工作方式为推挽
GPIOF_OSPEEDR &= ~(3<<(9*2));//对应位清0 ???
GPIOF_OSPEEDR |= (2<<(9*2)); //响应速度50M,其他值亦可
GPIOF_PUPDR &= ~(3<<(9*2)); //清0
GPIOF_PUPDR |= (1<<(9*2)); //上拉有效
}
//---------------------延时函数定义---------------------
void delay(void)
{
u32 i, j;
for(i=0; i<200; i++) //200*5000=1000000us=1000ms=1s
for(j=0; j<5000; j++);
}
2、外加系统时钟初始化函数
//----------------------------------------------------------
u8 Sys_Clock_Set(u32 plln,u32 pllm,u32 pllp,u32 pllq)
{
u16 retry=0;
u8 status=0;
RCC_CR |= 1<<16; //HSE 开启
while(((RCC_CR&(1<<17))==0)&&(retry<0X1FFF)) retry++;//等待HSE RDY
if(retry==0X1FFF) status=1; //HSE无法就绪
else
{
RCC_APB1ENR |= 1<<28; //电源接口时钟使能
PWR_CR |= 3<<14; //高性能模式,时钟可到168Mhz
RCC_CFGR |= (0<<4)|(5<<10)|(4<<13);//HCLK 不分频;APB1 4分频;APB2 2分频.
RCC_CR &= ~(1<<24); //关闭主PLL
RCC_PLLCFGR = pllm|(plln<<6)|(((pllp>>1)-1)<<16)|(pllq<<24)|(1<<22);//配置主PLL,PLL时钟源来自HSE
RCC_CR |= 1<<24; //打开主PLL
while((RCC_CR & (1<<25))==0);//等待PLL准备好
FLASH_ACR |= 1<<8; //指令预取使能.
FLASH_ACR |= 1<<9; //指令cache使能.
FLASH_ACR |= 1<<10; //数据cache使能.
FLASH_ACR |= 5<<0; //5个CPU等待周期.
RCC_CFGR &= ~(3<<0); //清零
RCC_CFGR |= 2<<0; //选择主PLL作为系统时钟
while((RCC_CFGR&(3<<2))!=(2<<2));//等待主PLL作为系统时钟成功.
}
return status;
}
//----------------------------------------------------------
void Stm32_Clock_Init(u32 plln,u32 pllm,u32 pllp,u32 pllq)
{
RCC_CR|=0x00000001; //设置HSISON,开启内部高速RC振荡
RCC_CFGR=0x00000000; //CFGR清零
RCC_CR&=0xFEF6FFFF; //HSEON,CSSON,PLLON清零
RCC_PLLCFGR=0x24003010; //PLLCFGR恢复复位值
RCC_CR&=~(1<<18); //HSEBYP清零,外部晶振不旁路
RCC_CIR=0x00000000; //禁止RCC时钟中断
Sys_Clock_Set(plln,pllm,pllp,pllq);//设置时钟
}