找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3919|回复: 1
打印 上一主题 下一主题
收起左侧

智能手环核心芯片nRF51822连接蓝牙,连接震动马达 源代码下载

[复制链接]
跳转到指定楼层
楼主
蓝牙智能手环要求收到消息或者建立连接后震动提示,分享代码,共同学习~欢迎互相讨论,改进技术.

全部资料源代码下载:
nrf51_ble_motor.rar (5.66 MB, 下载次数: 49)




部分源码预览:
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */

  12. /**
  13. * @brief Blinky Sample Application main file.
  14. *
  15. * This file contains the source code for a sample server application using the LED Button service.
  16. */

  17. #include <stdint.h>
  18. #include <string.h>
  19. #include "nordic_common.h"
  20. #include "nrf.h"
  21. #include "app_error.h"
  22. #include "ble.h"
  23. #include "ble_hci.h"
  24. #include "ble_srv_common.h"
  25. #include "ble_advdata.h"
  26. #include "ble_conn_params.h"
  27. #include "softdevice_handler.h"
  28. #include "app_timer.h"
  29. #include "app_button.h"
  30. #include "ble_lbs.h"
  31. #include "bsp.h"
  32. #include "ble_gap.h"

  33. #define CENTRAL_LINK_COUNT              0                                           /**< Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
  34. #define PERIPHERAL_LINK_COUNT           1                                           /**< Number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/

  35. #define ADVERTISING_LED_PIN             BSP_LED_0_MASK                              /**< Is on when device is advertising. */
  36. #define CONNECTED_LED_PIN               BSP_LED_1_MASK                              /**< Is on when device has connected. */

  37. #define LEDBUTTON_LED_PIN               BSP_LED_2_MASK                              /**< LED to be toggled with the help of the LED Button Service. */
  38. #define LEDBUTTON_BUTTON_PIN            BSP_BUTTON_0                                /**< Button that will trigger the notification event with the LED Button Service */

  39. #define DEVICE_NAME                     "Nordic_Blinky"                             /**< Name of device. Will be included in the advertising data. */

  40. #define APP_ADV_INTERVAL                64                                          /**< The advertising interval (in units of 0.625 ms; this value corresponds to 40 ms). */
  41. #define APP_ADV_TIMEOUT_IN_SECONDS      BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED       /**< The advertising time-out (in units of seconds). When set to 0, we will never time out. */

  42. #define APP_TIMER_PRESCALER             0                                           /**< Value of the RTC1 PRESCALER register. */
  43. #define APP_TIMER_MAX_TIMERS            6                                           /**< Maximum number of simultaneously created timers. */
  44. #define APP_TIMER_OP_QUEUE_SIZE         4                                           /**< Size of timer operation queues. */

  45. #define MIN_CONN_INTERVAL               MSEC_TO_UNITS(100, UNIT_1_25_MS)            /**< Minimum acceptable connection interval (0.5 seconds). */
  46. #define MAX_CONN_INTERVAL               MSEC_TO_UNITS(200, UNIT_1_25_MS)            /**< Maximum acceptable connection interval (1 second). */
  47. #define SLAVE_LATENCY                   0                                           /**< Slave latency. */
  48. #define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)             /**< Connection supervisory time-out (4 seconds). */
  49. #define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(20000, APP_TIMER_PRESCALER) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (15 seconds). */
  50. #define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)  /**< Time between each call to sd_ble_gap_conn_param_update after the first call (5 seconds). */
  51. #define MAX_CONN_PARAMS_UPDATE_COUNT    3                                           /**< Number of attempts before giving up the connection parameter negotiation. */

  52. #define APP_GPIOTE_MAX_USERS            1                                           /**< Maximum number of users of the GPIOTE handler. */
  53. #define BUTTON_DETECTION_DELAY          APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)    /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */

  54. #define DEAD_BEEF                       0xDEADBEEF                                  /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */

  55. static uint16_t                         m_conn_handle = BLE_CONN_HANDLE_INVALID;    /**< Handle of the current connection. */
  56. static ble_lbs_t                        m_lbs;                                      /**< LED Button Service instance. */


  57. /**@brief Function for assert macro callback.
  58. *
  59. * @details This function will be called in case of an assert in the SoftDevice.
  60. *
  61. * @warning This handler is an example only and does not fit a final product. You need to analyze
  62. *          how your product is supposed to react in case of Assert.
  63. * @warning On assert from the SoftDevice, the system can only recover on reset.
  64. *
  65. * @param[in] line_num    Line number of the failing ASSERT call.
  66. * @param[in] p_file_name File name of the failing ASSERT call.
  67. */
  68. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  69. {
  70.     app_error_handler(DEAD_BEEF, line_num, p_file_name);
  71. }


  72. /**@brief Function for the LEDs initialization.
  73. *
  74. * @details Initializes all LEDs used by the application.
  75. */
  76. static void leds_init(void)
  77. {
  78.     LEDS_CONFIGURE(ADVERTISING_LED_PIN | CONNECTED_LED_PIN | LEDBUTTON_LED_PIN);
  79.           LEDS_ON(LEDBUTTON_LED_PIN);
  80.     LEDS_OFF(ADVERTISING_LED_PIN | CONNECTED_LED_PIN );
  81. }


  82. /**@brief Function for the Timer initialization.
  83. *
  84. * @details Initializes the timer module.
  85. */
  86. static void timers_init(void)
  87. {
  88.     // Initialize timer module, making it use the scheduler
  89.     APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
  90. }


  91. /**@brief Function for the GAP initialization.
  92. *
  93. * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
  94. *          device including the device name, appearance, and the preferred connection parameters.
  95. */
  96. static void gap_params_init(void)
  97. {
  98.     uint32_t                err_code;
  99.     ble_gap_conn_params_t   gap_conn_params;
  100.     ble_gap_conn_sec_mode_t sec_mode;

  101.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);

  102.     err_code = sd_ble_gap_device_name_set(&sec_mode,
  103.                                           (const uint8_t *)DEVICE_NAME,
  104.                                           strlen(DEVICE_NAME));
  105.     APP_ERROR_CHECK(err_code);

  106.     memset(&gap_conn_params, 0, sizeof(gap_conn_params));

  107.     gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  108.     gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  109.     gap_conn_params.slave_latency     = SLAVE_LATENCY;
  110.     gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;

  111.     err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  112.     APP_ERROR_CHECK(err_code);
  113. }


  114. /**@brief Function for initializing the Advertising functionality.
  115. *
  116. * @details Encodes the required advertising data and passes it to the stack.
  117. *          Also builds a structure to be passed to the stack when starting advertising.
  118. */
  119. static void advertising_init(void)
  120. {
  121.     uint32_t      err_code;
  122.     ble_advdata_t advdata;
  123.     ble_advdata_t scanrsp;

  124.     ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}};

  125.     // Build and set advertising data
  126.     memset(&advdata, 0, sizeof(advdata));

  127.     advdata.name_type          = BLE_ADVDATA_FULL_NAME;
  128.     advdata.include_appearance = true;
  129.     advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;


  130.     memset(&scanrsp, 0, sizeof(scanrsp));
  131.     scanrsp.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
  132.     scanrsp.uuids_complete.p_uuids  = adv_uuids;

  133.     err_code = ble_advdata_set(&advdata, &scanrsp);
  134.     APP_ERROR_CHECK(err_code);
  135. }


  136. /**@brief Function for handling write events to the LED characteristic.
  137. *
  138. * @param[in] p_lbs     Instance of LED Button Service to which the write applies.
  139. * @param[in] led_state Written/desired state of the LED.
  140. */
  141. static void led_write_handler(ble_lbs_t * p_lbs, uint8_t led_state)
  142. {
  143.     if (led_state)
  144.     {
  145.         LEDS_OFF(LEDBUTTON_LED_PIN);
  146.     }
  147.     else
  148.     {
  149.         LEDS_ON(LEDBUTTON_LED_PIN);
  150.     }
  151. }


  152. /**@brief Function for initializing services that will be used by the application.
  153. */
  154. static void services_init(void)
  155. {
  156.     uint32_t       err_code;
  157.     ble_lbs_init_t init;

  158.     init.led_write_handler = led_write_handler;

  159.     err_code = ble_lbs_init(&m_lbs, &init);
  160.     APP_ERROR_CHECK(err_code);
  161. }


  162. /**@brief Function for handling the Connection Parameters Module.
  163. *
  164. * @details This function will be called for all events in the Connection Parameters Module that
  165. *          are passed to the application.
  166. *
  167. * @note All this function does is to disconnect. This could have been done by simply
  168. *       setting the disconnect_on_fail config parameter, but instead we use the event
  169. *       handler mechanism to demonstrate its use.
  170. *
  171. * @param[in] p_evt  Event received from the Connection Parameters Module.
  172. */
  173. static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
  174. {
  175.     uint32_t err_code;

  176.     if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
  177.     {
  178.         err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
  179.         APP_ERROR_CHECK(err_code);
  180.     }
  181. }


  182. /**@brief Function for handling a Connection Parameters error.
  183. *
  184. * @param[in] nrf_error  Error code containing information about what went wrong.
  185. */
  186. static void conn_params_error_handler(uint32_t nrf_error)
  187. {
  188.     APP_ERROR_HANDLER(nrf_error);
  189. }


  190. /**@brief Function for initializing the Connection Parameters module.
  191. */
  192. static void conn_params_init(void)
  193. {
  194.     uint32_t               err_code;
  195.     ble_conn_params_init_t cp_init;

  196.     memset(&cp_init, 0, sizeof(cp_init));

  197.     cp_init.p_conn_params                  = NULL;
  198.     cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  199.     cp_init.next_conn_params_update_delay  = NEXT_CONN_PARAMS_UPDATE_DELAY;
  200.     cp_init.max_conn_params_update_count   = MAX_CONN_PARAMS_UPDATE_COUNT;
  201.     cp_init.start_on_notify_cccd_handle    = BLE_GATT_HANDLE_INVALID;
  202.     cp_init.disconnect_on_fail             = false;
  203.     cp_init.evt_handler                    = on_conn_params_evt;
  204.     cp_init.error_handler                  = conn_params_error_handler;

  205.     err_code = ble_conn_params_init(&cp_init);
  206.     APP_ERROR_CHECK(err_code);
  207. }


  208. /**@brief Function for starting advertising.
  209. */
  210. static void advertising_start(void)
  211. {
  212.     uint32_t             err_code;
  213.     ble_gap_adv_params_t adv_params;

  214.     // Start advertising
  215.     memset(&adv_params, 0, sizeof(adv_params));

  216.     adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
  217.     adv_params.p_peer_addr = NULL;
  218.     adv_params.fp          = BLE_GAP_ADV_FP_ANY;
  219.     adv_params.interval    = APP_ADV_INTERVAL;
  220.     adv_params.timeout     = APP_ADV_TIMEOUT_IN_SECONDS;

  221.     err_code = sd_ble_gap_adv_start(&adv_params);
  222.     APP_ERROR_CHECK(err_code);
  223.     LEDS_ON(ADVERTISING_LED_PIN);
  224. }


  225. /**@brief Function for handling the Application's BLE stack events.
  226. *
  227. * @param[in] p_ble_evt  Bluetooth stack event.
  228. */
  229. static void on_ble_evt(ble_evt_t * p_ble_evt)
  230. {
  231.     uint32_t err_code;

  232.     switch (p_ble_evt->header.evt_id)
  233.     {
  234.         case BLE_GAP_EVT_CONNECTED:
  235.             LEDS_ON(CONNECTED_LED_PIN);
  236.             LEDS_OFF(ADVERTISING_LED_PIN);
  237.             m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

  238.             err_code = app_button_enable();
  239.             APP_ERROR_CHECK(err_code);
  240.             break;

  241.         case BLE_GAP_EVT_DISCONNECTED:
  242.             LEDS_OFF(CONNECTED_LED_PIN);
  243.             m_conn_handle = BLE_CONN_HANDLE_INVALID;

  244.             err_code = app_button_disable();
  245.             APP_ERROR_CHECK(err_code);

  246.             advertising_start();
  247.             break;

  248.         case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  249.             // Pairing not supported
  250.             err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
  251.                                                    BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP,
  252.                                                    NULL,
  253.                                                    NULL);
  254.             APP_ERROR_CHECK(err_code);
  255.             break;

  256.         case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  257.             // No system attributes have been stored.
  258.             err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
  259.             APP_ERROR_CHECK(err_code);
  260.             break;

  261.         default:
  262.             // No implementation needed.
  263.             break;
  264.     }
  265. }


  266. /**@brief Function for dispatching a BLE stack event to all modules with a BLE stack event handler.
  267. *
  268. * @details This function is called from the scheduler in the main loop after a BLE stack
  269. *          event has been received.
  270. *
  271. * @param[in] p_ble_evt  Bluetooth stack event.
  272. */
  273. static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
  274. {
  275.     on_ble_evt(p_ble_evt);
  276.     ble_conn_params_on_ble_evt(p_ble_evt);
  277.     ble_lbs_on_ble_evt(&m_lbs, p_ble_evt);
  278. }


  279. /**@brief Function for initializing the BLE stack.
  280. *
  281. * @details Initializes the SoftDevice and the BLE event interrupt.
  282. */
  283. static void ble_stack_init(void)
  284. {
  285.     uint32_t err_code;
  286.    
  287.     nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
  288.    
  289.     // Initialize the SoftDevice handler module.
  290.     SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
  291.    
  292.     ble_enable_params_t ble_enable_params;
  293.     err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
  294.                                                     PERIPHERAL_LINK_COUNT,
  295.                                                     &ble_enable_params);
  296.     APP_ERROR_CHECK(err_code);
  297.    
  298.     //Check the ram settings against the used number of links
  299.     CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
  300.    
  301.     // Enable BLE stack.
  302.     err_code = softdevice_enable(&ble_enable_params);
  303.     APP_ERROR_CHECK(err_code);

  304.     ble_gap_addr_t addr;

  305.     err_code = sd_ble_gap_address_get(&addr);
  306.     APP_ERROR_CHECK(err_code);
  307.     err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr);
  308.     APP_ERROR_CHECK(err_code);

  309.     // Subscribe for BLE events.
  310.     err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
  311.     APP_ERROR_CHECK(err_code);
  312. }


  313. /**@brief Function for handling events from the button handler module.
  314. *
  315. * @param[in] pin_no        The pin that the event applies to.
  316. * @param[in] button_action The button action (press/release).
  317. */
  318. static void button_event_handler(uint8_t pin_no, uint8_t button_action)
  319. {
  320.     uint32_t err_code;

  321.     switch (pin_no)
  322.     {
  323.         case LEDBUTTON_BUTTON_PIN:
  324.             err_code = ble_lbs_on_button_change(&m_lbs, button_action);
  325.             if (err_code != NRF_SUCCESS &&
  326.                 err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
  327.                 err_code != NRF_ERROR_INVALID_STATE)
  328.             {
  329.                 APP_ERROR_CHECK(err_code);
  330.             }
  331.             break;

  332.         default:
  333.             APP_ERROR_HANDLER(pin_no);
  334.             break;
  335.     }
  336. }


  337. /**@brief Function for initializing the button handler module.
  338. */
  339. static void buttons_init(void)
  340. {
  341.     uint32_t err_code;

  342.     //The array must be static because a pointer to it will be saved in the button handler module.
  343.     static app_button_cfg_t buttons[] =
  344.     {
  345.         {LEDBUTTON_BUTTON_PIN, false, BUTTON_PULL, button_event_handler}
  346.     };

  347.     err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]),
  348.                                BUTTON_DETECTION_DELAY);
  349.     APP_ERROR_CHECK(err_code);
  350. }


  351. /**@brief Function for the Power Manager.
  352. */
  353. static void power_manage(void)
  354. {
  355.     uint32_t err_code = sd_app_evt_wait();

  356.     APP_ERROR_CHECK(err_code);
  357. }


  358. /**@brief Function for application main entry.
  359. */
  360. int main(void)
  361. {
  362.     // Initialize.
  363.     leds_init();
  364.     timers_init();
  365.     buttons_init();
  366.     ble_stack_init();
  367.     gap_params_init();
  368.     services_init();
  369.     advertising_init();
  370.     conn_params_init();

  371.     // Start execution.
  372.     advertising_start();

  373.     // Enter main loop.
  374.     for (;;)
  375.     {
  376.         power_manage();
  377.     }
  378. }


  379. /**
  380. * @}
  381. */
复制代码


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:188110 发表于 2017-4-11 09:57 | 只看该作者
智能手环 有没有一起学习哒
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表