-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathSTM32复位和系统时钟.html
143 lines (140 loc) · 7.76 KB
/
STM32复位和系统时钟.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<link rel="stylesheet" href="../stylesheets/Github.css" type="text/css" />
<title>STM32复位和系统时钟</title>
</head>
<body>
<div id="header"><center>
<p class="header_titleline">
<a href="../index.html" target="_self" title="主页">主页 </a><a href="../Search.html" target="_self" title="站内搜索">站内搜索 </a><a href="../Projects.html" target="_self" title="项目研究">项目研究 </a><a href="../Archives.html" target="_self" title="文章存档">文章存档 </a><a href="../README.html" target="_self" title="分类目录">分类目录 </a><a href="../AboutMe.html" target="_self" title="关于我">关于我 </a>
</p>
</center></div>
<h1>STM32复位和系统时钟</h1>
<h4>2011-10-17,整理于2014-09-25 / xiahouzuoxin</h4>
<h4>Tags: ARM</h4>
转载请注明出处: <a href="http://xiahouzuoxin.github.io/notes/">http://xiahouzuoxin.github.io/notes/</a>
<!---title:STM32复位和系统时钟-->
<!---keywords:ARM-->
<!---date:2011-10-17,整理于2014-09-25-->
<p>这是一篇旧文,早在上大二的时候,在地下室(当时实验室在防空地下室)顺手在word上写的,当时STM32正刚刚开始蓬勃发展,如今时隔3年,整理到这里!</p>
<p>系统复位后,首先使用内部RC时钟源,由于内部时钟精度不高且不能倍频到较高频率,所以实际应用中一般使用外接晶振(8M),下面是建立系统时钟的步骤:</p>
<ol style="list-style-type: decimal">
<li>将所有RCC外设寄存器重设为默认值</li>
<li>启用外部高速晶振</li>
<li>等待,直到外部晶振稳定</li>
<li>设置欲取址缓存使能和代码延时值</li>
<li>设置AHB时钟(HCLK)=系统时钟</li>
<li>设置高速APB2时钟(PCLK2)为系统时钟</li>
<li>设置低速APB1时钟(PCLK1)为系统时钟1/2,最高36MHz</li>
<li>设置PLL时钟源及倍频系数,使能PLL。PLL最高72MHz</li>
<li>等待PLL初始化成功</li>
<li>设置PLL为系统时钟源</li>
<li>等待PLL成功作为系统时钟源</li>
</ol>
<pre class="sourceCode c"><code class="sourceCode c"><span class="co">/*******************************************************************/</span>
<span class="co">/* function : Sysclk configuration</span>
<span class="co">/* author : 夏侯佐鑫</span>
<span class="co">/* data : 2011-10-17</span>
<span class="co">/*******************************************************************/</span>
<span class="dt">void</span> SysclkInit(<span class="dt">void</span>)
{
ErrorStatus HSEStartUpStatus;
<span class="co">//重设默认值</span>
RCC_DeInit();
<span class="co">//HSE</span>
RCC_HSEConfig(RCC_HES_ON);
<span class="co">//等待HSE稳定</span>
HSEStratUpStatus = RCC_WaitForHSEStartUp();
<span class="kw">if</span>(HSEStratUpStatus == SUCCESS)
{
<span class="co">//预取址缓存使能</span>
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
<span class="co">//设置代码延时值,2延时周期</span>
FLASH_SetLatency(FLSAH_Latency_2);
<span class="co">//AHB</span>
RCC_HCLKConfig(RCC_SYSCLK_Div1);
<span class="co">//APB2</span>
RCC_PCLK2Config(RCC_SYSCLK_Div1);
<span class="co">//APB1</span>
RCC_PCLK1Config(RCC_SYSCLK_Div2);
<span class="co">//PLLCLK = 8MHz * 9</span>
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
<span class="co">//Enable PLL</span>
RCC_PLLCmd(ENABLE);
<span class="co">//Wait PLL Init successed</span>
<span class="kw">while</span>(RESET == RCC_GetFlagStatus(RCC_FLAG_PLLRDY));
<span class="co">//SYSCLK = PLL clk</span>
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
<span class="co">//Wait PLL be SYSCLK</span>
<span class="kw">while</span>(RCC_GetSYSCLKSource() != <span class="bn">0x08</span>);
}
}</code></pre>
<div class="ds-thread" data-thread-key="STM32复位和系统时钟" data-title="STM32复位和系统时钟" data-url="xiahouzuoxin.github.io/notes/html/STM32复位和系统时钟.html"></div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"slide":{"type":"slide","bdImg":"5","bdPos":"right","bdTop":"300"},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"xiahouzuoxin"};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<!-- 多说公共JS代码 end -->
<div id="footer">
<p class="footer_subline">联系邮箱: [email protected]</p>
<p class="footer_subline">声明: 本站所有文章如非特别说明均为原创,转载请注明出处!
<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253219218'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1253219218%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script>
</p>
</div>
<!-- 回到顶部 -->
<script>
lastScrollY=0;
function heartBeat(){
var diffY;
if (document.documentElement && document.documentElement.scrollTop)
diffY = document.documentElement.scrollTop;
else if (document.body)
diffY = document.body.scrollTop
else
{/*Netscape stuff*/}
percent=.1*(diffY-lastScrollY);
if(percent>0)percent=Math.ceil(percent);
else percent=Math.floor(percent);
document.getElementById("full").style.top=parseInt(document.getElementById("full").style.top)+percent+"px";
lastScrollY=lastScrollY+percent;
}
suspendcode="<div id=\"full\" style='right:1px;POSITION:absolute;TOP:600px;z-index:100'><a onclick='window.scrollTo(0,0);'><img src='../images/top.png'></a><br></div>"
document.write(suspendcode);
window.setInterval("heartBeat()",1);
</script>
</body>
</html>