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

2024-05-28 12:48

本文主要是介绍低功耗蓝牙4.0BLE编程-nrf51822开发(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先看的示例是心率计一个示例程序:<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函数:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /***************************************************************************** 
  2. * Main Function 
  3. *****************************************************************************/  
  4.   
  5. /**@brief Function for the application main entry. 
  6.  */  
  7. int main(void)  
  8. {  
  9.     uint32_t err_code;  
  10.   
  11.     timers_init();  
  12.     gpiote_init();  
  13.     buttons_init();  
  14.     ble_stack_init();  
  15.     bond_manager_init();  
  16.   
  17.     // Initialize Bluetooth Stack parameters  
  18.     gap_params_init();  
  19.     advertising_init();  
  20.     services_init();  
  21.     conn_params_init();  
  22.     sec_params_init();  
  23.   
  24.     // Start advertising  
  25.     advertising_start();  
  26.   
  27.     // Enter main loop  
  28.     for (;;)  
  29.     {  
  30.         // Switch to a low power state until an event is available for the application  
  31.         err_code = sd_app_evt_wait();  
  32.         APP_ERROR_CHECK(err_code);  
  33.     }  
  34. }  
它很简洁:初始化->start->loop。

(1)Timer初始化

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Function for the Timer initialization. 
  2.  * 
  3. * @details Initializes the timer module. This creates and starts application timers. 
  4. */  
  5. static void timers_init(void)  
  6. {  
  7.     uint32_t err_code;  
  8.   
  9.     // Initialize timer module  
  10.     APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);  
  11.   
  12.     // Create timers  
  13.     err_code = app_timer_create(&m_battery_timer_id,  
  14.                                 APP_TIMER_MODE_REPEATED,  
  15.                                 battery_level_meas_timeout_handler);  
  16.     APP_ERROR_CHECK(err_code);  
  17.   
  18.     err_code = app_timer_create(&m_heart_rate_timer_id,  
  19.                                 APP_TIMER_MODE_REPEATED,  
  20.                                 heart_rate_meas_timeout_handler);  
  21.     APP_ERROR_CHECK(err_code);  
  22. }  
使用app_timer_create创建了两个时钟,处理函数分别是battery_level_meas_timeout_handler和heart_rate_meas_timeout_handler。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Function for handling the Battery measurement timer timeout. 
  2.  * 
  3.  * @details This function will be called each time the battery level measurement timer expires. 
  4.  *          This function will start the ADC. 
  5.  * 
  6.  * @param[in]   p_context   Pointer used for passing some arbitrary information (context) from the 
  7.  *                          app_start_timer() call to the timeout handler. 
  8.  */  
  9. static void battery_level_meas_timeout_handler(void * p_context)  
  10. {  
  11.     UNUSED_PARAMETER(p_context);  
  12.     battery_start();  
  13. }  
  14.   
  15.   
  16. /**@brief Function for handling the Heart rate measurement timer timeout. 
  17.  * 
  18.  * @details This function will be called each time the heart rate measurement timer expires. 
  19.  *          It will exclude RR Interval data from every third measurement. 
  20.  * 
  21.  * @param[in]   p_context   Pointer used for passing some arbitrary information (context) from the 
  22.  *                          app_start_timer() call to the timeout handler. 
  23.  */  
  24. static void heart_rate_meas_timeout_handler(void * p_context)  
  25. {  
  26.     uint32_t err_code;  
  27.   
  28.     UNUSED_PARAMETER(p_context);  
  29.   
  30.     err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, m_cur_heart_rate);  
  31.   
  32.     if (  
  33.         (err_code != NRF_SUCCESS)  
  34.         &&  
  35.         (err_code != NRF_ERROR_INVALID_STATE)  
  36.         &&  
  37.         (err_code != BLE_ERROR_NO_TX_BUFFERS)  
  38.         &&  
  39.         (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)  
  40.     )  
  41.     {  
  42.         APP_ERROR_HANDLER(err_code);  
  43.     }  
  44. }  
时钟创建后并不会自动运行,当调用application_timers_start后时钟开始运行:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Function for starting the application timers. 
  2.  */  
  3. static void application_timers_start(void)  
  4. {  
  5.     uint32_t err_code;  
  6.   
  7.     // Start application timers  
  8.     err_code = app_timer_start(m_battery_timer_id, BATTERY_LEVEL_MEAS_INTERVAL, NULL);  
  9.     APP_ERROR_CHECK(err_code);  
  10.   
  11.     err_code = app_timer_start(m_heart_rate_timer_id, HEART_RATE_MEAS_INTERVAL, NULL);  
  12.     APP_ERROR_CHECK(err_code);  
  13. }  
services_init()初始化程序中的三个服务:ble_dis.c, ble_bas.c, ble_hrs.c。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Function for initializing the services that will be used by the application. 
  2.  * 
  3.  * @details Initialize the Heart Rate, Battery and Device Information services. 
  4.  */  
  5. static void services_init(void)  
  6. {  
  7.     uint32_t       err_code;  
  8.     ble_hrs_init_t hrs_init;  
  9.     ble_bas_init_t bas_init;  
  10.     ble_dis_init_t dis_init;  
  11.     uint8_t        body_sensor_location;  
  12.   
  13.     // Initialize Heart Rate Service  
  14.     body_sensor_location = BLE_HRS_BODY_SENSOR_LOCATION_FINGER;  
  15.   
  16.     memset(&hrs_init, 0, sizeof(hrs_init));  
  17.       
  18.     hrs_init.is_sensor_contact_supported = false;  
  19.     hrs_init.p_body_sensor_location      = &body_sensor_location;  
  20.   
  21.     // Here the sec level for the Heart Rate Service can be changed/increased.  
  22.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_init.hrs_hrm_attr_md.cccd_write_perm);  
  23.     BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.read_perm);  
  24.     BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.write_perm);  
  25.   
  26.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_init.hrs_bsl_attr_md.read_perm);  
  27.     BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_bsl_attr_md.write_perm);  
  28.   
  29.     err_code = ble_hrs_init(&m_hrs, &hrs_init);  
  30.     APP_ERROR_CHECK(err_code);  
  31.   
  32.     // Initialize Battery Service  
  33.     memset(&bas_init, 0, sizeof(bas_init));  
  34.   
  35.     // Here the sec level for the Battery Service can be changed/increased.  
  36.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.cccd_write_perm);  
  37.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.read_perm);  
  38.     BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&bas_init.battery_level_char_attr_md.write_perm);  
  39.   
  40.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_report_read_perm);  
  41.   
  42.     bas_init.evt_handler          = NULL;  
  43.     bas_init.support_notification = true;  
  44.     bas_init.p_report_ref         = NULL;  
  45.     bas_init.initial_batt_level   = 100;  
  46.   
  47.     err_code = ble_bas_init(&bas, &bas_init);  
  48.     APP_ERROR_CHECK(err_code);  
  49.   
  50.     // Initialize Device Information Service  
  51.     memset(&dis_init, 0, sizeof(dis_init));  
  52.   
  53.     ble_srv_ascii_to_utf8(&dis_init.manufact_name_str, MANUFACTURER_NAME);  
  54.   
  55.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&dis_init.dis_attr_md.read_perm);  
  56.     BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&dis_init.dis_attr_md.write_perm);  
  57.   
  58.     err_code = ble_dis_init(&dis_init);  
  59.     APP_ERROR_CHECK(err_code);  
  60. }  
static ble_hrs_t的结构定义:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Heart Rate Service structure. This contains various status information for the service. */  
  2. typedef struct ble_hrs_s  
  3. {  
  4.     ble_hrs_evt_handler_t        evt_handler;                                          /**< Event handler to be called for handling events in the Heart Rate Service. */  
  5.     bool                         is_expended_energy_supported;                         /**< TRUE if Expended Energy measurement is supported. */  
  6.     bool                         is_sensor_contact_supported;                          /**< TRUE if sensor contact detection is supported. */  
  7.     uint16_t                     service_handle;                                       /**< Handle of Heart Rate Service (as provided by the BLE stack). */  
  8.     ble_gatts_char_handles_t     hrm_handles;                                          /**< Handles related to the Heart Rate Measurement characteristic. */  
  9.     ble_gatts_char_handles_t     bsl_handles;                                          /**< Handles related to the Body Sensor Location characteristic. */  
  10.     ble_gatts_char_handles_t     hrcp_handles;                                         /**< Handles related to the Heart Rate Control Point characteristic. */  
  11.     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). */  
  12.     bool                         is_sensor_contact_detected;                           /**< TRUE if sensor contact has been detected. */  
  13.     uint16_t                     rr_interval[BLE_HRS_MAX_BUFFERED_RR_INTERVALS];       /**< Set of RR Interval measurements since the last Heart Rate Measurement transmission. */  
  14.     uint16_t                     rr_interval_count;                                    /**< Number of RR Interval measurements since the last Heart Rate Measurement transmission. */  
  15. } ble_h
ble_hrs.h/ble_hrs.c是心率计程序服务的代码。


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

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Function for initializing the button module. 
  2.  */  
  3. static void buttons_init(void)  
  4. {  
  5.     // Configure HR_INC_BUTTON_PIN_NO and HR_DEC_BUTTON_PIN_NO as wake up buttons and also configure  
  6.     // for 'pull up' because the eval board does not have external pull up resistors connected to  
  7.     // the buttons.  
  8.     static app_button_cfg_t buttons[] =  
  9.     {  
  10.         {HR_INC_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler},  
  11.         {HR_DEC_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler}  // Note: This pin is also BONDMNGR_DELETE_BUTTON_PIN_NO  
  12.     };  
  13.       
  14.     APP_BUTTON_INIT(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY, false);  
  15. }  
当按下按钮时,处理程序是button_event_handler(),它处理心率计的加减模拟:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**@brief Function for handling button events. 
  2.  * 
  3.  * @param[in]   pin_no   The pin number of the button pressed. 
  4.  */  
  5. static void button_event_handler(uint8_t pin_no, uint8_t button_action)  
  6. {  
  7.     if (button_action == APP_BUTTON_PUSH)  
  8.     {  
  9.         switch (pin_no)  
  10.         {  
  11.             case HR_INC_BUTTON_PIN_NO:  
  12.                 // Increase Heart Rate measurement  
  13.                 m_cur_heart_rate += HEART_RATE_CHANGE;  
  14.                 if (m_cur_heart_rate > MAX_HEART_RATE)  
  15.                 {  
  16.                     m_cur_heart_rate = MIN_HEART_RATE; // Loop back  
  17.                 }  
  18.                 break;  
  19.                   
  20.             case HR_DEC_BUTTON_PIN_NO:  
  21.                 // Decrease Heart Rate measurement  
  22.                 m_cur_heart_rate -= HEART_RATE_CHANGE;  
  23.                 if (m_cur_heart_rate < MIN_HEART_RATE)  
  24.                 {  
  25.                     m_cur_heart_rate = MAX_HEART_RATE; // Loop back  
  26.                 }  
  27.                 break;  
  28.                   
  29.             default:  
  30.                 APP_ERROR_HANDLER(pin_no);  
  31.                 break;  
  32.         }  
  33.     }      
  34. }

这篇关于低功耗蓝牙4.0BLE编程-nrf51822开发(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010617

相关文章

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

16.Spring前世今生与Spring编程思想

1.1.课程目标 1、通过对本章内容的学习,可以掌握Spring的基本架构及各子模块之间的依赖关系。 2、 了解Spring的发展历史,启发思维。 3、 对 Spring形成一个整体的认识,为之后的深入学习做铺垫。 4、 通过对本章内容的学习,可以了解Spring版本升级的规律,从而应用到自己的系统升级版本命名。 5、Spring编程思想总结。 1.2.内容定位 Spring使用经验

Eclipse+ADT与Android Studio开发的区别

下文的EA指Eclipse+ADT,AS就是指Android Studio。 就编写界面布局来说AS可以边开发边预览(所见即所得,以及多个屏幕预览),这个优势比较大。AS运行时占的内存比EA的要小。AS创建项目时要创建gradle项目框架,so,创建项目时AS比较慢。android studio基于gradle构建项目,你无法同时集中管理和维护多个项目的源码,而eclipse ADT可以同时打开

探索蓝牙协议的奥秘:用ESP32实现高质量蓝牙音频传输

蓝牙(Bluetooth)是一种短距离无线通信技术,广泛应用于各种电子设备之间的数据传输。自1994年由爱立信公司首次提出以来,蓝牙技术已经经历了多个版本的更新和改进。本文将详细介绍蓝牙协议,并通过一个具体的项目——使用ESP32实现蓝牙音频传输,来展示蓝牙协议的实际应用及其优点。 蓝牙协议概述 蓝牙协议栈 蓝牙协议栈是蓝牙技术的核心,定义了蓝牙设备之间如何进行通信。蓝牙协议

Python应用开发——30天学习Streamlit Python包进行APP的构建(9)

st.area_chart 显示区域图。 这是围绕 st.altair_chart 的语法糖。主要区别在于该命令使用数据自身的列和指数来计算图表的 Altair 规格。因此,在许多 "只需绘制此图 "的情况下,该命令更易于使用,但可定制性较差。 如果 st.area_chart 无法正确猜测数据规格,请尝试使用 st.altair_chart 指定所需的图表。 Function signa

蓝牙ble数传芯片推荐,TD5327A芯片蓝牙5.1—拓达半导体

蓝牙数传芯片TD5327A芯片是一款支持蓝牙BLE的纯数传芯片,蓝牙5.1版本。芯片的亮点在于性能强,除了支持APP端直接对芯片做设置与查询操作,包括直接操作蓝牙芯片自身的IO与PWM口以外,还支持RTC日历功能,可以做各类定时类操作,极大丰富了蓝牙在IOT产品中的应用。此外,在数传应用方面,此芯片支持串口流控功能,提大提高了数据传输的稳定与可靠性。 拓达蓝牙芯片特点: 支持RTC日历功能,超

WDF驱动开发-WDF总线枚举(一)

支持在总线驱动程序中进行 PnP 和电源管理 某些设备永久插入系统,而其他设备可以在系统运行时插入和拔出电源。 总线驱动 必须识别并报告连接到其总线的设备,并且他们必须发现并报告系统中设备的到达和离开情况。 总线驱动程序标识和报告的设备称为总线的 子设备。 标识和报告子设备的过程称为 总线枚举。 在总线枚举期间,总线驱动程序会为其子 设备创建设备对象 。  总线驱动程序本质上是同时处理总线枚

JavaWeb系列六: 动态WEB开发核心(Servlet) 上

韩老师学生 官网文档为什么会出现Servlet什么是ServletServlet在JavaWeb项目位置Servlet基本使用Servlet开发方式说明快速入门- 手动开发 servlet浏览器请求Servlet UML分析Servlet生命周期GET和POST请求分发处理通过继承HttpServlet开发ServletIDEA配置ServletServlet注意事项和细节 Servlet注

uniapp 低功耗蓝牙BLE分包

ble.js // 分包写入蓝牙async sendWriteBLECharacteristicValue(deviceId,serviceId,writeCharacteristicId,readCharacteristicId,buffer,success, // 成功回调failure, // 失败回调) {const offset = 500; // 偏移量let pos = 0;