平时在做项目的时候都用的是外部晶振做为时钟源,想试试用内部RC振荡器做为时钟源,在网上搜了一下如何设置内部时钟,发现资料比较少的。决定将设置内部RC振荡器做为时钟源的方法记录下来。( ^% d6 y- O9 A6 z
用的单片机是STM32F103C8T6,项目工程是在正点原子的示例代码上修改来的。用一个LED工程测试,在主程序中闪烁LED灯。( n4 a8 H* a# {4 ?6 t( a% J' O
1 o6 r$ l. Y* C m* R0 T- #include "sys.h"
4 C" ]) S/ \' `, Y* ?' y - #include "delay.h"" @6 q: E0 p" d! d& T
- #include "usart.h"
$ A# S/ W1 U. g9 G" N( C - #include "led.h"0 p4 f0 m, w8 ^ H9 o! E
- int main ( void )1 t& b! C0 ~# D& _
- {
1 |! u; z% c* _( m: H S - delay_init(); //延时函数初始化
( S5 o% e w/ ?+ R( F. h7 } - LED_Init(); //初始化与LED连接的硬件接口
1 w2 }+ W8 O9 t - while ( 1 )
3 E. s6 e5 [6 k; e4 i) u - {
# ?1 h5 s X8 ` - LED = 0;2 m6 Y6 Q g1 ^9 f& c5 f' S" U5 d) {4 s
- delay_ms ( 100 ); //延时300ms
+ U2 ^% h& s" c - LED = 1;
* `7 R+ r6 N# w- L8 K- ~ - delay_ms ( 100 ); //延时300ms
. U' _" e2 s3 x& M1 u - }' ?) f5 \9 j1 Z4 D. p
- }
复制代码 o* M$ \4 C T. y, M7 @
首先使用默认时钟设置,也就是外部8M晶振做为时钟源。LED灯灭100ms,然后再亮100ms。
7 |* o0 Z2 \! b) P; T3 a- h! o/ S下来看默认时钟设置的代码:. i9 W3 n$ \+ d3 _
在 system_stm32f10x.c 文件里面, SystemInit()函数设置时钟。
6 o5 D& W& g+ X' e l
) n% l% B9 A1 N" d) {- void SystemInit (void)' o! P/ T5 l* V5 C" G, w- e
- {
. F4 e0 F: ]6 Q& t9 g) x - /* Reset the RCC clock configuration to the default reset state(for debug purpose) */. o* e5 V) T1 @" G+ N) W
- /* Set HSION bit */* K; a0 |+ S- u7 s1 i G
- RCC->CR |= (uint32_t)0x00000001;
. F, I3 o+ T) W, l, p - /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */3 J `) y4 U- s1 Z9 U' A
- #ifndef STM32F10X_CL' `5 z! ^# {: \! v$ r1 O' t. I
- RCC->CFGR &= (uint32_t)0xF8FF0000;7 |( H4 k* b+ g
- #else1 C3 ?0 T, j* z" B; X
- RCC->CFGR &= (uint32_t)0xF0FF0000;, }0 I; P2 i/ y2 W# l: ?) G$ a/ C- c
- #endif /* STM32F10X_CL */) J5 y0 h1 m1 s/ L
- /* Reset HSEON, CSSON and PLLON bits */3 {1 n3 h5 l, P0 W( W1 c
- RCC->CR &= (uint32_t)0xFEF6FFFF;3 |* M( r5 D! S6 P; H
- /* Reset HSEBYP bit */
( A6 R+ O }9 i+ P- d7 z( X, A( T - RCC->CR &= (uint32_t)0xFFFBFFFF;5 |# @$ k: |" |9 T: m
- /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */! }" K% \1 l( p' q7 s8 X6 m
- RCC->CFGR &= (uint32_t)0xFF80FFFF;0 a5 G$ P: s( F4 M. u
- #ifdef STM32F10X_CL E% T5 c5 M' N. O
- /* Reset PLL2ON and PLL3ON bits */3 F2 W7 o& t4 m: v Y/ y. b
- RCC->CR &= (uint32_t)0xEBFFFFFF;
- P$ W, G; c- }7 o4 ?, x, F' e - /* Disable all interrupts and clear pending bits */# P' y& x+ n7 g' D0 z
- RCC->CIR = 0x00FF0000;" Q8 M( Q! Z! \3 q, @. ^9 q
- /* Reset CFGR2 register */
5 K3 J8 A# p# J# S - RCC->CFGR2 = 0x00000000;) z6 S1 s3 Z$ e0 q- d9 `
- #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL), s+ s$ g& v* g" p
- /* Disable all interrupts and clear pending bits */
8 @* V8 R+ H P0 n4 t, R - RCC->CIR = 0x009F0000;
3 _' T2 N" V+ G7 _: x8 h! i - /* Reset CFGR2 register */, Z) }- s4 ?- S- O+ [* }+ Q; j
- RCC->CFGR2 = 0x00000000;
: {& u: D' A, n, N: E+ m: | - #else8 G+ X" N9 n w; K8 A9 D, p
- /* Disable all interrupts and clear pending bits */" k! M2 U. ^, i6 q9 c5 F
- RCC->CIR = 0x009F0000;
( w n0 f, ~1 f' M+ ~( l - #endif /* STM32F10X_CL */
! H* y3 @: M# \8 K2 ^1 W/ A' w - #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
! W# U/ ]2 N1 n% Q4 M& Z+ D - #ifdef DATA_IN_ExtSRAM& P4 \1 w+ d! v& K v( b& |/ [/ I- f$ k
- SystemInit_ExtMemCtl();
! B+ l6 B& N: t% m+ k2 B9 [ - #endif /* DATA_IN_ExtSRAM */
' g6 H8 a) Y/ C! R9 @7 ~ - #endif* i2 \% y) y" J: ~, b, D
- /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */- s2 p8 ]' H7 V% w+ b, U6 \6 Q
- /* Configure the Flash Latency cycles and enable prefetch buffer */
( }1 N% C# a2 L: R - SetSysClock();' I! w2 k( P4 z
4 l4 w0 ~0 `- J H" _2 `" r- #ifdef VECT_TAB_SRAM( Q. A, _: R' H; d2 K/ r
- SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */% s$ W5 Y) q0 d
- #else/ R/ D7 v' _8 L
- SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */2 \3 d( E& A* V, g0 D$ m- X
- #endif
" }* U& S. {" [! v; T8 r/ B2 j - }
复制代码 3 z7 L- m- K+ [) c' t( F
如何要使用内部RC振荡器做时钟源的话,需要重写SystemInit()函数。设置代码如下:- /* 开启HSI 即内部晶振时钟 */. u. ~, u8 s# {% ~
- RCC->CR |= ( uint32_t ) 0x00000001;
1 F( k; B4 o* O3 s! J) Y - /*选择HSI为PLL的时钟源HSI必须2分频给PLL*/
- r; D# P4 A6 n8 ]/ V - RCC->CFGR |= ( uint32_t ) RCC_CFGR_PLLSRC_HSI_Div2;: Q* P* N: b& `3 y9 G4 R7 U* s
- /*PLLCLK=8/2*9=36MHz 设置倍频得到时钟源PLL的频率*/- O& ?- ~; A) T6 Q
- RCC->CFGR |= ( uint32_t ) RCC_CFGR_PLLMULL9; //设置倍频后的频率
* U% W" {' C U1 C* `$ R" f - /* PLL不分频输出 ?*/; }7 |. }- _* a6 j6 w; U
- RCC->CFGR |= ( uint32_t ) RCC_CFGR_HPRE_DIV1;5 R4 s3 K' t) \( q4 L) _4 g, M& s
- /* 使能 PLL时钟 */
5 m2 X% q z, u; V# f: e3 w - RCC->CR |= RCC_CR_PLLON;6 c; K( d) b) x7 k% X+ d3 i
- /* 等待PLL时钟就绪*/- g" r( s) }0 ~- f. U$ n
- while ( ( RCC->CR & RCC_CR_PLLRDY ) == 0 )
+ l; c) E* G; k - {4 N6 ^$ D8 l. X' T, ~( o
- }
5 B" b3 M9 d0 o% ~ - /* 选择PLL为系统时钟的时钟源 */) r: W1 {) y) I- c" B/ }) L
- RCC->CFGR &= ( uint32_t ) ( ( uint32_t ) ~ ( RCC_CFGR_SW ) );+ c- }; f. e& N) y' f' d
- RCC->CFGR |= ( uint32_t ) RCC_CFGR_SW_PLL;
; V8 C* W& _; y9 y) w; p; w - /* 等到PLL成为系统时钟的时钟源*/( s; h/ X4 y, ~, H. O% `1 T
- while ( ( RCC->CFGR & ( uint32_t ) RCC_CFGR_SWS ) != ( uint32_t ) 0x08 ); n& I5 m& x( O
- { }
复制代码
6 m; T1 ` c# J2 Z为了方便和以前的代码兼容,这里使用条件编译来选择使用外部晶振或者内部RC震荡。代码流程如下:
, C; b, h- e$ S( V# ^#if USE_HSI
; E4 f+ F& b/ w# \' C7 f/ O{undefined
. g$ A% L* b$ m* n2 ` t$ z//使用内部RC
) {9 a0 C& O( b1 x}
' F, D- q) I2 z3 @) e Kelse3 O: J2 y! x2 k
{undefined
+ c5 g3 J8 X6 ?% D$ o+ @//使用外部晶振% F' l% @+ e- [8 S; p
}$ Y$ ]4 ]6 X1 n, N* k: x; _$ g9 o
#endif$ X) c k/ G6 i- j, _+ Z* n
通过宏定义 USE_HSI 来选择时钟源,USE_HSI 为1时,使用内部RC做为时钟源,USE_HSI 为0时,使用外部晶振做为时钟源。" \4 C2 F- C" ~! G, ^7 [. E6 ~
修改后的代码如下:4 k8 `3 }+ `3 g1 \9 P
- ( s0 |) Z7 O& o9 Y
- #define USE_HSI 0 // 是否使用内部晶振 0 不使用 1使用, I G, {2 x8 ^; J t; ~
- void SystemInit ( void )$ k ?- J& D" J/ F4 M! H/ _
- {
' Y4 R; r% }; r6 ]9 v - ! L9 ]( c2 U7 v4 k# F+ {
- #if USE_HSI
+ G3 ~& p/ ]. c2 m/ W/ Y D - { s* ]: c p3 ~5 Q1 S9 |% y
- //设置使用内部晶振: E' z3 o. I8 E8 q" \
- /* 开启HSI 即内部晶振时钟 */1 f) V+ X; F- d% a7 @) r4 S% `
- RCC->CR |= ( uint32_t ) 0x00000001;8 a0 T( f* U4 m4 P
- /*选择HSI为PLL的时钟源HSI必须2分频给PLL*/) Q8 D, ^, [- a. ~
- RCC->CFGR |= ( uint32_t ) RCC_CFGR_PLLSRC_HSI_Div2;9 j2 U/ J# ^1 R' k5 ^. k
- /*PLLCLK=8/2*9=36MHz 设置倍频得到时钟源PLL的频率*/
6 a7 k8 ~/ X- j. D7 x - RCC->CFGR |= ( uint32_t ) RCC_CFGR_PLLMULL9; //设置倍频后的频率
3 x& d/ k3 @- l9 F/ S* J1 ? - /* PLL不分频输出 ?*// l G: n2 X4 _8 x
- RCC->CFGR |= ( uint32_t ) RCC_CFGR_HPRE_DIV1;3 M- @# \: t, X
- /* 使能 PLL时钟 */
& e! z Y/ \* ?& m$ t# g; B$ t - RCC->CR |= RCC_CR_PLLON;8 ^2 }* F; f0 v1 Y
- /* 等待PLL时钟就绪*/; B$ @, Y! T$ Z% J. _
- while ( ( RCC->CR & RCC_CR_PLLRDY ) == 0 )
) \9 {; Z0 v, E3 b" n" B. p5 B& n3 _ - {$ }0 w, U4 O) C
- }) f& A Y o) _/ `3 v F
- /* 选择PLL为系统时钟的时钟源 */
( p. y2 o) f3 q2 E - RCC->CFGR &= ( uint32_t ) ( ( uint32_t ) ~ ( RCC_CFGR_SW ) );7 r, s( X u$ d% F1 a2 n e4 Y
- RCC->CFGR |= ( uint32_t ) RCC_CFGR_SW_PLL;
" ~6 a: l8 o R: e7 y; L - /* 等到PLL成为系统时钟的时钟源*/# }% e a/ @- G d$ V6 ?0 R
- while ( ( RCC->CFGR & ( uint32_t ) RCC_CFGR_SWS ) != ( uint32_t ) 0x08 )
; r+ g3 ~4 C9 t1 k: J% g5 e - { }
) e! G; t2 i9 I0 l+ C* e8 c - }7 Z: c! c# I) b$ @0 E* V6 h* M# N
- #else: X% w$ ]+ ] d
- {
* {. o0 w9 a) f. X: @/ i% w - //设置使用外部8M晶振1 d, c9 H! O# ~0 D' G2 D
- /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
* c; r7 q4 m; u% X! f. N4 T6 I - /* Set HSION bit */2 N8 h, i4 h9 w, b# w5 |
- RCC->CR |= ( uint32_t ) 0x00000001;
/ q; K' K( u3 t" } ]5 R - + A# H1 L& X! }! v c% V
- /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */; Y) R# v% F! }+ `
- #ifndef STM32F10X_CL
: t4 M4 ?( n- N* F7 ^ - RCC->CFGR &= ( uint32_t ) 0xF8FF0000;& d' s- s% ^! P: S
- #else# w1 l/ [, _) X* m
- RCC->CFGR &= ( uint32_t ) 0xF0FF0000;) A! M' E. r+ p
- #endif /* STM32F10X_CL */) V# \+ F/ ^& C, s2 P
9 g V: C" z$ L F/ q7 ?& v- /* Reset HSEON, CSSON and PLLON bits */# b9 i/ J" b5 H1 I' @
- RCC->CR &= ( uint32_t ) 0xFEF6FFFF;5 x) c- `* O0 V
- / N, W# ^3 u/ p/ \( E2 ~2 ~5 R
- /* Reset HSEBYP bit */9 h2 w7 Y. @3 W$ u; K. I" j. n
- RCC->CR &= ( uint32_t ) 0xFFFBFFFF;) M( P; V( V; u+ o" j7 B1 A% o1 \
- ; \8 A/ b& Y3 R, v) F% J' B W6 ?
- /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
1 k; h) o2 \5 h. A- s. k - RCC->CFGR &= ( uint32_t ) 0xFF80FFFF;7 u$ o/ k ^7 C
2 Z1 \+ m3 q) K$ `- #ifdef STM32F10X_CL/ B& g, [" S; q. ]* T
- /* Reset PLL2ON and PLL3ON bits */
# @( i1 f+ d( n/ J' l - RCC->CR &= ( uint32_t ) 0xEBFFFFFF;
. l7 ~4 i _1 V* j3 a+ r E0 H
$ M# \5 Q8 f! v4 L- /* Disable all interrupts and clear pending bits */4 Y0 y7 S6 Z4 D& \) l' [% g: F
- RCC->CIR = 0x00FF0000;
4 [$ N/ q1 L% p/ Q3 L - - S( O; e+ b) M/ {7 Z b; k
- /* Reset CFGR2 register */
" N( e; e7 N' V; u: U) P) o. a - RCC->CFGR2 = 0x00000000;
1 S5 c4 M, u$ b& ]5 | - #elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)" h1 z' L! L- K
- /* Disable all interrupts and clear pending bits */6 G6 v( B5 ~8 I# T% {& u
- RCC->CIR = 0x009F0000;
?/ w- P) d8 r# `
+ G5 L2 E2 G/ [9 X* t7 U* r7 c- y- /* Reset CFGR2 register */
) F. l" ?: c# j' M6 I - RCC->CFGR2 = 0x00000000;# P% O! ^0 M! ] l
- #else
, M7 O) s( l: p2 v/ u2 _ - /* Disable all interrupts and clear pending bits */
$ k- E* w# H; d* x4 u/ ^ - RCC->CIR = 0x009F0000;0 Z! o4 n( J2 y; R+ a" M5 v, W
- #endif /* STM32F10X_CL */( S N \% h) D
, n0 t, p( T: C4 X0 V6 y1 H- #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)& N& V z/ z% [0 }5 \1 M0 d
- #ifdef DATA_IN_ExtSRAM
' s. b- K; R" x' E4 R' [# V - SystemInit_ExtMemCtl();
1 t8 \0 _. E! X, V$ s" O - #endif /* DATA_IN_ExtSRAM */
; Z' Y( q% w! {- \ @! B0 k - #endif
0 U* U% O6 U' ?& s! `7 B9 f - # C8 ?; @$ M2 t( @% F
- /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */1 N1 G- [, s1 V5 M3 }2 k' F
- /* Configure the Flash Latency cycles and enable prefetch buffer */
( ?6 }) ?) Y# N; [7 q+ y. v - SetSysClock();
# p3 @- I4 z8 k; V: h) _ - ?; O+ W, P% S m
- #ifdef VECT_TAB_SRAM
% n/ T+ E8 _6 b- ?0 o4 F& B - SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */, Z% ?8 U [4 i* J* m
- #else- @1 U2 Z9 i/ I& R- O( Q0 @
- SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */4 L+ r/ h' [" N* z- N7 r8 P
- #endif8 G; Q: W3 K9 G# U d
- }
" O6 i" F0 Y5 n( I6 y
( E, C6 c) E2 X! ?. X. F e$ j! C, T- #endif
3 P9 x% f) w% ]% [3 u* M7 k- y - }
复制代码 1 T& K# b0 ~4 l! y
内部RC默认为8M,将倍频数设置为9,这样使用内部RC振荡器之后,时钟频率就是36MHz。为使用外部晶振频率72MHz的一半。( F/ q! Z( u7 A( |" G" J
^! s( e$ K1 D ]" J- /*PLLCLK=8/2*9=36MHz 设置倍频得到时钟源PLL的频率*/) e* |: V$ y6 j, {/ B5 a$ Q
- RCC->CFGR |= ( uint32_t ) RCC_CFGR_PLLMULL9; //设置倍频后的频率
复制代码 , [+ r+ M7 G$ `5 z5 d1 u+ p7 ^5 o# u0 V
这时候在运行代码,用示波器测试LED的电平,发现是高电平200ms,低电平200ms。比使用外部时钟慢了一半,说明使用内部RC振荡器的设置代码是正确的。0 ^$ K3 i+ O' U/ ]. r. { Q/ v
$ x! f8 F }6 n6 ^* C p+ `' B, ]0 t, o- o8 y4 L1 D
|