nRF52-QA-晶振配置

一:设置内部RC晶振

默认修改sdk_config.h文件中的以下宏定义 内部RC校准图示

// <0=> NRF_CLOCK_LF_SRC_RC 
// <1=> NRF_CLOCK_LF_SRC_XTAL 
// <2=> NRF_CLOCK_LF_SRC_SYNTH
// 设置为内部RC晶振
#define NRF_SDH_CLOCK_LF_SRC 0  

// <o> NRF_SDH_CLOCK_LF_RC_CTIV - SoftDevice calibration timer interval. 
//设置250ms*6 = 8s校准一次RC时钟
#define NRF_SDH_CLOCK_LF_RC_CTIV 16  

// <o> NRF_SDH_CLOCK_LF_RC_TEMP_CTIV - SoftDevice calibration timer interval under constant temperature. 
// <i> How often (in number of calibration intervals) the RC oscillator shall be calibrated
// <i>  if the temperature has not changed.
// 温度变化0.5摄氏度每4秒校准一次
#define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 2  

// <o> NRF_SDH_CLOCK_LF_ACCURACY  - External clock accuracy used in the LL to compute timing.
// <0=> NRF_CLOCK_LF_ACCURACY_250_PPM 
// <1=> NRF_CLOCK_LF_ACCURACY_500_PPM 
// <2=> NRF_CLOCK_LF_ACCURACY_150_PPM 
// <3=> NRF_CLOCK_LF_ACCURACY_100_PPM 
// <4=> NRF_CLOCK_LF_ACCURACY_75_PPM 
// <5=> NRF_CLOCK_LF_ACCURACY_50_PPM 
// <6=> NRF_CLOCK_LF_ACCURACY_30_PPM 
// <7=> NRF_CLOCK_LF_ACCURACY_20_PPM 
// <8=> NRF_CLOCK_LF_ACCURACY_10_PPM 
// <9=> NRF_CLOCK_LF_ACCURACY_5_PPM 
// <10=> NRF_CLOCK_LF_ACCURACY_2_PPM 
// <11=> NRF_CLOCK_LF_ACCURACY_1_PPM 

// 设置内部RC的精度为500PPM,即500万分之500  ,如果是外部晶体,则为20PPM
#define NRF_SDH_CLOCK_LF_ACCURACY 1  

二:不启用softdevice情况下校准内部RC,常用于bootloader或app使能softdevice前使用

//启用softdevice前设置内部RC时钟校准
void lfclk_calibrate(void)
{
  
    NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
  
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
  
    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
  
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
  
    NRF_CLOCK->EVENTS_DONE = 0;
    NRF_CLOCK->TASKS_CAL = 1;
  
    while(NRF_CLOCK->EVENTS_DONE == 0);
  
    NRF_CLOCK->TASKS_HFCLKSTOP = 1;
}

//使用方法:放在main入口处调用:
void main(void)
{
    lfclk_calibrate();
    //...其他代码
}

注意事项

蓝牙主从机连接时间戳对应示意图 使用内部RC注意事项:蓝牙主从机连接时,时间戳需要对准,才能正常收发。
如果内部RC误差较大,有没有即使的校准,就会导致蓝牙连接时,主从机时间戳对不上,进而导致连接断开

三:检测晶振是否有运行

使能低频时钟(softdevice启用后自动管理此函数)

void nrf_drv_clock_lfclk_request(nrf_drv_clock_handler_item_t * p_handler_item);

检测低频时钟是否运行起来(常用于检测外部晶振是否工作正常)

/**
 * @brief Function for checking the LFCLK state.
 *
 * @retval true  If the LFCLK is running.
 * @retval false If the LFCLK is not running.
 */
bool nrf_drv_clock_lfclk_is_running(void);

使能高频时钟(softdevice启用后自动管理此函数)

void nrf_drv_clock_hfclk_request(nrf_drv_clock_handler_item_t * p_handler_item);

检测高频时钟是否运行起来(常用于检测外部晶振是否工作正常)

/**
 * @brief Function for checking the HFCLK state.
 *
 * @retval true If the HFCLK is running (for \nRFXX XTAL source).
 * @retval false If the HFCLK is not running.
 */
bool nrf_drv_clock_hfclk_is_running(void);

新手必知

海量第三方学习资源.

超全常用工具与文档.

本站常用资源下载.

常见问题搜索.

QQ群: 542294007.

文章引用自:元仓库 OLIB.cn.