#include
"system_clock.h"
#include
"stm32f10x.h"
#define sys_default_count_freq 285522
//系統上電後預設時鐘計數頻率hz,測試得出
#define vect_tab_offset 0x0
/*!< vector table base offset field. this value must be a multiple of 0x200. */
/** *@brief 設定內部高速時鐘為系統時鐘
*@param[pll_mul] pll倍頻係數;參考 pll_multiplication_factor
*@note stm32f1中非互聯型使用內部時鐘,最大獲得64mhz;互聯型最大36mhz
*/void
set_sysclock_to_hsi
(uint32_t pll_mul)
void
config_rcc_interrupt
(void
)/**
* @brief sets system clock frequency to 72mhz and configure hclk, pclk2
* and pclk1 prescalers.
* @note this function should be used only after reset.
* @param none
* @retval -1:hse fails; -2:pll2 fails; -3:pll fails; -4:pll fails switch to system clock
*/static
intset_sys_clock_to_72
(void
)while
((hsestatus ==0)
&&(startupcounter != hse_startup_timeout));
if((rcc->cr & rcc_cr_hserdy)
!= reset)
else
if(hsestatus ==
(uint32_t)
0x01
)while((
(rcc->cr & rcc_cr_pll2rdy)==0
)&&(sys_default_count_freq*
5!= startupcounter))if
((rcc->cr & rcc_cr_pll2rdy)==0
)/* pll configuration: pllclk = prediv1 * 9 = 72 mhz */
rcc->cfgr &
=(uint32_t)
~(rcc_cfgr_pllxtpre | rcc_cfgr_pllsrc | rcc_cfgr_pllmull)
; rcc->cfgr |
=(uint32_t)
(rcc_cfgr_pllxtpre_prediv1 | rcc_cfgr_pllsrc_prediv1 |
rcc_cfgr_pllmull9)
;#else
/* pll configuration: pllclk = hse * 9 = 72 mhz */
rcc->cfgr &
=(uint32_t)
((uint32_t)
~(rcc_cfgr_pllsrc | rcc_cfgr_pllxtpre |
rcc_cfgr_pllmull));
rcc->cfgr |
=(uint32_t)
(rcc_cfgr_pllsrc_hse | rcc_cfgr_pllmull9)
;#endif
/* stm32f10x_cl */
/* enable pll */
rcc->cr |
= rcc_cr_pllon;
/* wait till pll is ready */
startupcounter =0;
dowhile((
(rcc->cr & rcc_cr_pllrdy)==0
)&&(sys_default_count_freq*
5!= startupcounter));
if((rcc->cr & rcc_cr_pllrdy)==0
)/* select pll as system clock source */
rcc->cfgr &
=(uint32_t)
((uint32_t)
~(rcc_cfgr_sw));
rcc->cfgr |
=(uint32_t)rcc_cfgr_sw_pll;
/* wait till pll is used as system clock source */
uint32_t pll_sta =0;
startupcounter =0;
dowhile
((pll_sta !=
(uint32_t)
0x8)
&&(sys_default_count_freq*
5!= startupcounter));
if(pll_sta !=
(uint32_t)
0x8)
}else
return0;
}
/**
* @brief setup the microcontroller system
* initialize the embedded flash inte***ce, the pll and update the
* systemcoreclock variable.
* @note this function should be used only after reset.
* @param none
* @retval none
*/void system_clock_init (
void
)#ifdef vect_tab_sram
scb->vtor = sram_base | vect_tab_offset;
/* vector table relocation in internal sram. */
#else
scb->vtor = flash_base | vect_tab_offset;
/* vector table relocation in internal flash. */
#endif
}
/**
* @brief this function handles nmi exception.
* @param none
* @retval none
*/void
nmi_handler
(void
)}
小窺STM32內部時鐘
在電子設計競賽中,我們有時候希望能夠對機械人雲台發射機構的摩擦輪進行測速,一是為了摩擦輪斜坡啟動不至於啟動電流過大,二是為了防止摩擦輪在發射子彈過程中轉速受到撥彈的影響以致轉速波動。在初期,我們的方案有兩個,一是使用as5048編碼器測量轉速,二是使用霍爾感測器測量轉速。在對兩個方案進行 驗證時,由...
STM32系統時鐘
1 參考資料 stm32f1開發指南 庫函式版本 4.3小節 時鐘系統 stm32中文參考手冊v10 第六章 復位和時鐘控制 rcc 2 時鐘系統知識總結 1 stm32有5個時鐘源 hsi hse lsi lse pll hsi是高速內部時鐘,rc振盪器,頻率為8mhz,精度不高 hse是高速外部...
stm32系統時鐘
hsi 約等於8mhz rc振盪器產生的時鐘 hse 外部時鐘 css 如果檢測到外部時鐘的損壞,自動切換到hsi 系統時鐘和rtc時鐘 均有三個 systick定時器 簡單的定時器,常用來做延時和心跳時鐘 24位的倒計時定時器 ctrl暫存器 其實滴答定時器很簡單,就是設定初值,然後延遲,最後就是...