相关下载:http://download.csdn.net/detail/xgbing/9565708

首先看的示例是心率计一个示例程序:<KEIL path> \ARM\Device\Nordic\nrf51822\Board\pca10001\s110\ble_app_hrs\arm。

打开工程前需要下载蓝牙协议栈S110 nRF51822 SoftDevice(s110_nrf51822_6.0.0_softdevice.hex)到板子中,这个手册上有说明。

首先看的是main.c中的main函数:

/*****************************************************************************
* Main Function
*****************************************************************************/  

/**@brief Function for the application main entry.
 */
int main(void)
{
    uint32_t err_code;  

    timers_init();
    gpiote_init();
    buttons_init();
    ble_stack_init();
    bond_manager_init();  

    // Initialize Bluetooth Stack parameters
    gap_params_init();
    advertising_init();
    services_init();
    conn_params_init();
    sec_params_init();  

    // Start advertising
    advertising_start();  

    // Enter main loop
    for (;;)
    {
        // Switch to a low power state until an event is available for the application
        err_code = sd_app_evt_wait();
        APP_ERROR_CHECK(err_code);
    }
}  

它很简洁:初始化->start->loop。

(1)Timer初始化

/**@brief Function for the Timer initialization.
 *
* @details Initializes the timer module. This creates and starts application timers.
*/
static void timers_init(void)
{
    uint32_t err_code;  

    // Initialize timer module
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);  

    // Create timers
    err_code = app_timer_create(&m_battery_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                battery_level_meas_timeout_handler);
    APP_ERROR_CHECK(err_code);  

    err_code = app_timer_create(&m_heart_rate_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                heart_rate_meas_timeout_handler);
    APP_ERROR_CHECK(err_code);
}

使用app_timer_create创建了两个时钟,处理函数分别是battery_level_meas_timeout_handler和heart_rate_meas_timeout_handler。

/**@brief Function for handling the Battery measurement timer timeout.
 *
 * @details This function will be called each time the battery level measurement timer expires.
 *          This function will start the ADC.
 *
 * @param[in]   p_context   Pointer used for passing some arbitrary information (context) from the
 *                          app_start_timer() call to the timeout handler.
 */
static void battery_level_meas_timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);
    battery_start();
}  

/**@brief Function for handling the Heart rate measurement timer timeout.
 *
 * @details This function will be called each time the heart rate measurement timer expires.
 *          It will exclude RR Interval data from every third measurement.
 *
 * @param[in]   p_context   Pointer used for passing some arbitrary information (context) from the
 *                          app_start_timer() call to the timeout handler.
 */
static void heart_rate_meas_timeout_handler(void * p_context)
{
    uint32_t err_code;  

    UNUSED_PARAMETER(p_context);  

    err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, m_cur_heart_rate);  

    if (
        (err_code != NRF_SUCCESS)
        &&
        (err_code != NRF_ERROR_INVALID_STATE)
        &&
        (err_code != BLE_ERROR_NO_TX_BUFFERS)
        &&
        (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    )
    {
        APP_ERROR_HANDLER(err_code);
    }
}  

时钟创建后并不会自动运行,当调用application_timers_start后时钟开始运行:

/**@brief Function for starting the application timers.
 */
static void application_timers_start(void)
{
    uint32_t err_code;  

    // Start application timers
    err_code = app_timer_start(m_battery_timer_id, BATTERY_LEVEL_MEAS_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);  

    err_code = app_timer_start(m_heart_rate_timer_id, HEART_RATE_MEAS_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);
}

services_init()初始化程序中的三个服务:ble_dis.c, ble_bas.c, ble_hrs.c。

/**@brief Function for initializing the services that will be used by the application.
 *
 * @details Initialize the Heart Rate, Battery and Device Information services.
 */
static void services_init(void)
{
    uint32_t       err_code;
    ble_hrs_init_t hrs_init;
    ble_bas_init_t bas_init;
    ble_dis_init_t dis_init;
    uint8_t        body_sensor_location;  

    // Initialize Heart Rate Service
    body_sensor_location = BLE_HRS_BODY_SENSOR_LOCATION_FINGER;  

    memset(&hrs_init, , sizeof(hrs_init));  

    hrs_init.is_sensor_contact_supported = false;
    hrs_init.p_body_sensor_location      = &body_sensor_location;  

    // Here the sec level for the Heart Rate Service can be changed/increased.
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_init.hrs_hrm_attr_md.cccd_write_perm);
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.write_perm);  

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_init.hrs_bsl_attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_bsl_attr_md.write_perm);  

    err_code = ble_hrs_init(&m_hrs, &hrs_init);
    APP_ERROR_CHECK(err_code);  

    // Initialize Battery Service
    memset(&bas_init, , sizeof(bas_init));  

    // Here the sec level for the Battery Service can be changed/increased.
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.cccd_write_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&bas_init.battery_level_char_attr_md.write_perm);  

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_report_read_perm);  

    bas_init.evt_handler          = NULL;
    bas_init.support_notification = true;
    bas_init.p_report_ref         = NULL;
    bas_init.initial_batt_level   = ;  

    err_code = ble_bas_init(&bas, &bas_init);
    APP_ERROR_CHECK(err_code);  

    // Initialize Device Information Service
    memset(&dis_init, , sizeof(dis_init));  

    ble_srv_ascii_to_utf8(&dis_init.manufact_name_str, MANUFACTURER_NAME);  

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&dis_init.dis_attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&dis_init.dis_attr_md.write_perm);  

    err_code = ble_dis_init(&dis_init);
    APP_ERROR_CHECK(err_code);
} 

static ble_hrs_t的结构定义:

/**@brief Heart Rate Service structure. This contains various status information for the service. */
typedef struct ble_hrs_s
{
    ble_hrs_evt_handler_t        evt_handler;                                          /**< Event handler to be called for handling events in the Heart Rate Service. */
    bool                         is_expended_energy_supported;                         /**< TRUE if Expended Energy measurement is supported. */
    bool                         is_sensor_contact_supported;                          /**< TRUE if sensor contact detection is supported. */
    uint16_t                     service_handle;                                       /**< Handle of Heart Rate Service (as provided by the BLE stack). */
    ble_gatts_char_handles_t     hrm_handles;                                          /**< Handles related to the Heart Rate Measurement characteristic. */
    ble_gatts_char_handles_t     bsl_handles;                                          /**< Handles related to the Body Sensor Location characteristic. */
    ble_gatts_char_handles_t     hrcp_handles;                                         /**< Handles related to the Heart Rate Control Point characteristic. */
    uint16_t                     conn_handle;                                          /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
    bool                         is_sensor_contact_detected;                           /**< TRUE if sensor contact has been detected. */
    uint16_t                     rr_interval[BLE_HRS_MAX_BUFFERED_RR_INTERVALS];       /**< Set of RR Interval measurements since the last Heart Rate Measurement transmission. */
    uint16_t                     rr_interval_count;                                    /**< Number of RR Interval measurements since the last Heart Rate Measurement transmission. */
} ble_hrs_t; 

ble_hrs.h/ble_hrs.c是心率计程序服务的代码。

buttons_init(void)初始化两个按钮:HR_INC_BUTTON_PIN_NO和HR_DEC_BUTTON_PIN_NO,分别模拟心率计的加减。

/**@brief Function for initializing the button module.
 */
static void buttons_init(void)
{
    // Configure HR_INC_BUTTON_PIN_NO and HR_DEC_BUTTON_PIN_NO as wake up buttons and also configure
    // for 'pull up' because the eval board does not have external pull up resistors connected to
    // the buttons.
    static app_button_cfg_t buttons[] =
    {
        {HR_INC_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler},
        {HR_DEC_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler}  // Note: This pin is also BONDMNGR_DELETE_BUTTON_PIN_NO
    };  

    APP_BUTTON_INIT(buttons, ]), BUTTON_DETECTION_DELAY, false);
}  

当按下按钮时,处理程序是button_event_handler(),它处理心率计的加减模拟:

/**@brief Function for handling button events.
 *
 * @param[in]   pin_no   The pin number of the button pressed.
 */
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    if (button_action == APP_BUTTON_PUSH)
    {
        switch (pin_no)
        {
            case HR_INC_BUTTON_PIN_NO:
                // Increase Heart Rate measurement
                m_cur_heart_rate += HEART_RATE_CHANGE;
                if (m_cur_heart_rate > MAX_HEART_RATE)
                {
                    m_cur_heart_rate = MIN_HEART_RATE; // Loop back
                }
                break;  

            case HR_DEC_BUTTON_PIN_NO:
                // Decrease Heart Rate measurement
                m_cur_heart_rate -= HEART_RATE_CHANGE;
                if (m_cur_heart_rate < MIN_HEART_RATE)
                {
                    m_cur_heart_rate = MAX_HEART_RATE; // Loop back
                }
                break;  

            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    }
}  

低功耗蓝牙4.0BLE编程-nrf51822开发(2)的更多相关文章

  1. 低功耗蓝牙4.0BLE编程-nrf51822开发(3)

    蓝牙协议栈 nrf51822开发中,蓝牙协议栈和应用开发是分开的. (1)兼容蓝牙4.0低功耗协议栈基带层,L2CAP\AAT\SM\GAP\GATT协议,设备和广播,GATT客户端和服务器,SMP支 ...

  2. 低功耗蓝牙4.0BLE编程-nrf51822开发(9)

    Android 4.3以后的系统自动支持蓝牙4.0规范的低功耗蓝牙(BLE).在android4.3之前,蓝牙4.0支持是由手机厂家加入支持的,接口各异,导致开发一个支持蓝牙4.0程序支持市面上的手机 ...

  3. 低功耗蓝牙4.0BLE编程-nrf51822开发(1)

    为了省钱,也为了提高手动能力,只买了块核心板,仿真器用的是旧的jinkv7,自己搭扩展板,DIY就这样开始了. 买这块之前做了些调查,最终选定了nrf51822,功耗低,性能强,开发难度小,虽然比TI ...

  4. 低功耗蓝牙4.0BLE编程-nrf51822开发(4)

    蓝牙是一种短距离的通讯方式,它设计的意图是取代电子便携设备之间的有线电缆连接.蓝牙的主要特性是健壮性.低功耗.成本低,它工作于免费的2.4无线传输频段. 蓝牙有两种技术系统:基本速率Basic Rat ...

  5. 低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析

    代码实例:点击打开链接 实现的功能是从uart口发送数据至另一个蓝牙串口,或是从蓝牙读取数据通过uart打印出数据. int main(void) { // Initialize leds_init( ...

  6. 低功耗蓝牙4.0BLE编程-nrf51822开发(7)-SDP服务发现协议

    SDP的全称是Service Discovery Protocol,中文是服务发现协议.SDP(服务发现协议)是蓝牙协议体系中的核心协议,是蓝牙系统重要组成部分,是所有用户模式的基础.在蓝牙系统中.客 ...

  7. 低功耗蓝牙4.0BLE编程-nrf51822开发(6)-Battery Service

    Battery Service是有关电池特性方面的服务,如果需要它,在初始化时将它加入到蓝牙协议栈. 如果通过ble_bas_battery_level_update(),电池电量将会通知,Batte ...

  8. 低功耗蓝牙4.0BLE编程-nrf51822开发(10)-描述符

    特性中的属性有两种:属性值或描述符. 支持通知或指示的特性中默认有一个描述符:客户端特性配置描述符(Client Characteristic Configuration Descriptor,CCC ...

  9. 低功耗蓝牙4.0BLE编程-nrf51822开发(8)-GATT

    The Generic Attribute Profile (GATT)使用属性定义一个服务框架,定义了服务和特性的过程和数据格式,包含发现.读取.写入.通知指示特性和配置特性广播. GATT配置文件 ...

随机推荐

  1. IntelliJ下使用Code/Live Template加快编码速度:程序员的工作不是写程序,而是写程序解决问题

    程序员的工作不是写程序,而是写程序解决问题. --- 某不知名程序员 我们每天都在写代码,有些代码有结构性的相似,但不是所有的代码都可以被抽成方法.在这种情况下,我们应该考虑使用template的方式 ...

  2. 转 BHO API HOOK Wininet基于IE编程的一些资料

      BHO原理:推荐vc base中的文章: 如何使用BHO定制你的Internet Explorer浏览器 API HOOK的基本原理:推荐C++ builder 研究中的文章: API Hook基 ...

  3. LightOJ1013 Love Calculator(DP)

    容易猜测到包含s1.s2序列的串的最短长度是LCS(s1,s2) + ( len(s1) - LCS(s1,s2) ) + ( len(s2) - LCS(s1,s2) ) ,即: len(s1)+l ...

  4. webkit webApp 开发技术要点总结【转】

    如果你是一名前端er,又想在移动设备上开发出自己的应用,那怎么实现呢?幸好,webkit内核的浏览器能帮助我们完成这一切.接触 webkit webApp的开发已经有一段时间了,现把一些技巧分享给大家 ...

  5. C#多线程与异步的区别

    随着拥有多个硬线程CPU(超线程.双核)的普及,多线程和异步操作等并发程序设计方法也受到了更多的关注和讨论.本文主要是想与园中各位高手一同探讨一下如何使用并发来最大化程序的性能. 多线程和异步操作的异 ...

  6. WP7.1 应用程序发布到Marketplace

    从8月22起Windows Phone marketplace可以提交7.1 sdk开发的应用了,尽管提交页面和方式与7.0是同一个,但是还是会出现一些问题.并且在提交之前也注意一些问题. 7.0 应 ...

  7. 【wikioi】1018 单词接龙

    题目链接 算法:DFS+考你阅题 题目描述: 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中 ...

  8. Http中涉及到的知识点总结

    1.URL地址 协议-> HTTP:超文本传输协议,除了用来传输文本,还可以传输HTML页面.CSS文件.JS文件.图片.音视频... HTTPS:SSL,它比HTTP更加安全一些 FTP:文件 ...

  9. [FollowUp] Combinations 组合项

    这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...

  10. Linux邮件服务器架构

    // 上面的过程只是实现了简单的本地用户的文件发送功能,只需要安装mailutil,不需要安装配置sendmail,看鸟哥的Linux私房菜中写的应该是本地用户发送邮件不需要sendmail.只有当非 ...