STM32F103 72MHz时钟设置

将系统时钟初始化到72MHz的函数

根据数据手册和库函数,设置STM32时钟为72MHz
  1. 这是.c文件
#include "clock.h"

void Set_SysClockTo72M(void)
{
  	unsigned int faultTime = 0;
	unsigned int rccCrHserdy = 0;
	unsigned int rccCrPLLrdy = 0;
	
	rRCC_CR = RCC_CR_RESET;
	rRCC_CR &= ~(1<<16);
	rRCC_CR |= (1<<16);	
	
	do
	{
		rccCrHserdy = rRCC_CR & (1<<17);
		faultTime++;
	}
	while ((faultTime<0x0FFFFFFF) && (rccCrHserdy==0));
	
	if ((rRCC_CR & (1<<17)) != 0)
	{
		rFLASH_ACR |= 0x10;
		rFLASH_ACR &= (~0x03);
		rFLASH_ACR |= 0x02;
	
		rRCC_CFGR &= (~((0x0F<<4) | (0x07<<8) | (0x07<<11)));
		rRCC_CFGR |= ((0x0<<4) | (0x04<<8) | (0x0<<11));
		rRCC_CFGR &= (~((0x01<<16) | (0x1<<17)));
		rRCC_CFGR |= ((0x01<<16) | (0x0<<17));
		rRCC_CFGR &= (~(0x0f<<18));
		rRCC_CFGR |= (0x07<<18);
		rRCC_CR 	|= (0x01<<24);
		
		faultTime = 0;
		do
		{
			rccCrPLLrdy = rRCC_CR & (1<<25);
			faultTime++;
		}
		while ((faultTime<0x0FFFFFFF) && (rccCrPLLrdy==0));
		
		if ((rRCC_CR & (1<<25)) == (1<<25))
		{
			rRCC_CFGR &= (~(0x03<<0));
			rRCC_CFGR |= (0x02<<0);
			
			faultTime = 0;
			do
			{
				rccCrPLLrdy = rRCC_CFGR & (0x03<<2);
				faultTime++;
			}
			while ((faultTime<0x0FFFFFFF) && (rccCrPLLrdy!=(0x02<<2)));
			
			if(rRCC_CFGR & (0x03<<2))
			{
			}
			else
			{
				while(1);
			}
		}
		else
		{
			while(1);
		}
	}
	else
	{
		while(1);
	}
}
  1. 这是.h文件
#ifndef __CLOCK_H__
#define __CLOCK_H__

#define FLASH_ACR	0x40022000
#define RCC_BASE	0x40021000				
#define RCC_CR		(RCC_BASE + 0x00)	
#define RCC_CFGR	(RCC_BASE + 0x04)

#define rRCC_CR		(*((volatile unsigned int *)RCC_CR))
#define rRCC_CFGR	(*((volatile unsigned int *)RCC_CFGR))
#define rFLASH_ACR	(*((volatile unsigned int *)FLASH_ACR))

#define RCC_CR_RESET  (0x00000083)

void Set_SysClockTo72M(void);

#endif

(为什么无法正常显示keil文件的中文注释…)

你可能感兴趣的:(C语言,STM32,stm32,单片机,嵌入式)