PIC16位單片機(jī)CAN(2)徹底弄懂時(shí)鐘
首先看一下時(shí)鐘框圖:
我們使用的是外部晶振,所以配置為HS,F(xiàn)CY是CPU時(shí)鐘,F(xiàn)P是外設(shè)時(shí)鐘,F(xiàn)CY=FP(除打盹模式外)。FCY=Fosc/2。也就是cpu時(shí)鐘是晶振頻率的1/2。下面還有分頻。DOZE<2:0>:011 = FCY 被 8 分頻 (默認(rèn))處理器時(shí)鐘分頻比選擇位。
因此實(shí)際的FCY=0.5MHz(使用8M的外部晶振,并八分頻8/2/8=0.5MHz)。
看一下振蕩器選擇配置位的說明:
這里我們需要看的是FOSCSEL配置位 第二列是寄存器地址,需要設(shè)置的是IESO<7>和FNOSC<2:0>
看一下頭文件p33EP32GP502.h里面的說明:
/*Register FOSCSEL (0x57f8) */
extern __attribute__((space(prog))) int _FOSCSEL;
#define _FOSCSEL(x) __attribute__((section("__FOSCSEL.sec"),space(prog))) int _FOSCSEL = (x);
/*
** Only one invocation of FOSCSEL should appear in a project,
** at the top of a C source file (outside of any function).
**
** The following constants can be used to set FOSCSEL.
** Multiple options may be combined, as shown:
**
** _FOSCSEL( OPT1_ON & OPT2_OFF & OPT3_PLL )
**
** Oscillator Source Selection:
** FNOSC_FRC Internal Fast RC (FRC)
** FNOSC_FRCPLL Fast RC Oscillator with divide-by-N with PLL module (FRCPLL)
** FNOSC_PRI Primary Oscillator (XT, HS, EC)
** FNOSC_PRIPLL Primary Oscillator with PLL module (XT + PLL, HS + PLL, EC + PLL)
** FNOSC_LPRC Low-Power RC Oscillator (LPRC)
** FNOSC_FRCDIVN Internal Fast RC (FRC) Oscillator with postscaler
**
** Two-speed Oscillator Start-up Enable bit:
** IESO_OFF Start up with user-selected oscillator source
** IESO_ON Start up device with FRC, then switch to user-selected oscillator source
**
*/
#define FNOSC_FRC 0xFFF8
#define FNOSC_FRCPLL 0xFFF9
#define FNOSC_PRI 0xFFFA
#define FNOSC_PRIPLL 0xFFFB
#define FNOSC_LPRC 0xFFFD
#define FNOSC_FRCDIVN 0xFFFF
#define IESO_OFF 0xFF7F
#define IESO_ON 0xFFFF
由頭文件得知
1:Register FOSCSEL (0x57f8),也就是寄存器地址是0x57f8,和表格一致。
2:_FOSCSEL(FNOSC_PRI&IESO_OFF); 我們在程序開頭由此句來聲明我們的配置位是0XFFFA&0XFF7F=0XFF7A
也就是FOSCSEL=0XFF7A 根據(jù)寄存器說明得知我們的配置是外部HS振蕩器并使用用戶選擇的振蕩器源啟動器件
3:配置位只是說明使用的振蕩器,并不設(shè)置分頻比DOZE。因此DOZE還是默認(rèn)的8分頻。
總結(jié):配置時(shí)鐘有以下幾個(gè)方面:
1:配置相應(yīng)的配置位選擇時(shí)鐘源是內(nèi)部時(shí)鐘還是外部晶振等。
2:配置相應(yīng)的寄存器選擇分頻比。
評論