一、超低功耗蓝牙 BLE 5.0 模组介绍
本次评测选用的是大联大 世平安森美 超低功耗蓝牙BLE 5.0 开发模组。
该模组包括蓝牙开发板和自发电无线遥控开关,蓝牙模组采用SiP封装集成天线,体积仅为:6x8x1.46mm 。具体参数参考安美森半导体官网。蓝牙开发模组和自发电无线遥控开关如下图所示:
图1蓝牙开发模组和自发电无线遥控开关实物图
二、系统架构
本次评测实现的项目是“远程控制LED灯及红外报警”。系统架构如下图所示,包括BLE开发板、电源、红绿黄三色LED灯、红外对射、手机。
图2系统架构图
三、功能展示
(1)系统运行后,通过手机上运行的蓝牙调试助手可以搜索到本项目的蓝牙名称“LED_ALARM”。当没有物体遮挡红外对射时,绿色LED灯亮起。如下图所示:
图3系统运行绿色灯亮
(2)当有物体遮挡红外对射时,红色LED灯亮起。此时为报警状态。如下图所示:
图4异物闯入红色灯亮
(3)通过手机向开发板发送“Y”,此时黄色LED灯亮起。如下图所示:
图5手机控制黄色灯亮
四、关键代码
本项目主要用到GPIO的输入和输出,输入采用中断的方式,核心代码如下:
- /* ----------------------------------------------------------------------------
- * Function : void DIO0_IRQHandler(void)
- * ----------------------------------------------------------------------------
- * Description : Toggle the toggle status global flag.
- * Inputs : None
- * Outputs : None
- * Assumptions : None
- * ------------------------------------------------------------------------- */
- void DIO0_IRQHandler(void)
- {
- if (DIO_DATA->ALIAS[BUTTON_DIO] == 0)
- {
- LED_Flag='R';
- PRINTF("ALARM START\r\n");
- }
- else if (DIO_DATA->ALIAS[BUTTON_DIO] == 1)
- {
- LED_Flag='G';
- PRINTF("ALARM STOP\n");
- }
- }
- int main(void)
- {
- App_Initialize();
- Sys_GPIO_Set_High(LED_DIO_R);
- Sys_GPIO_Set_High(LED_DIO_G);
- Sys_GPIO_Set_High(LED_DIO_Y);
- LED_Flag=0;
- /* Debug/trace initialization. In order to enable UART or RTT trace,
- * configure the 'OUTPUT_INTERFACE' macro in printf.h */
- printf_init();
- PRINTF("__peripheral_server has started!\n");
- for (unsigned int i = 0; i < 40; i++)
- {
- BLE_Receive[i] = 0;
- }
- /* Main application loop:
- * - Run the kernel scheduler
- * - Send notifications for the battery voltage and RSSI values
- * - Refresh the watchdog and wait for an interrupt before continuing */
- while (1)
- {
- Kernel_Schedule();
- for (unsigned int i = 0; i < NUM_MASTERS; i++)
- {
- if (ble_env[i].state == APPM_CONNECTED)
- {
- /* Send battery level if battery service is enabled */
- if (app_env.send_batt_ntf[i] && bass_support_env[i].enable)
- {
- PRINTF("__SEND BATTERY LEVEL %d\n",app_env.batt_lvl);
- app_env.send_batt_ntf[i] = 0;
- Batt_LevelUpdateSend(ble_env[i].conidx,
- app_env.batt_lvl, 0);
- }
- /* Update custom service characteristics, send notifications if
- * notification is enabled */
- if (cs_env[i].tx_value_changed && cs_env[i].sent_success)
- {
- cs_env[i].tx_value_changed = false;
- (cs_env[i].val_notif)++;
- if (cs_env[i].tx_cccd_value & ATT_CCC_START_NTF)
- {
- memset(cs_env[i].tx_value, cs_env[i].val_notif,
- CS_TX_VALUE_MAX_LENGTH);
- CustomService_SendNotification(ble_env[i].conidx,
- CS_IDX_TX_VALUE_VAL,
- &cs_env[i].tx_value[0],
- CS_TX_VALUE_MAX_LENGTH);
- }
- }
- /* Update TX long characteristic if new RX long characteristic was received.
- * Write the inverted version of RX characteristic into TX */
- if (cs_env[i].rx_long_value_changed == true)
- {
- for (unsigned int j = 0; j < CS_RX_LONG_VALUE_MAX_LENGTH; j++)
- {
- BLE_Receive[j] = cs_env[i].rx_long_value[j];
- cs_env[i].rx_long_value[j]=0;
- }
- if(BLE_Receive[0]=='Y')
- {
- LED_Flag='Y';
- for (unsigned int i = 0; i < 40; i++)
- {
- BLE_Receive[i] = 0;
- }
- }
- }
- }
- }
- if(LED_Flag=='R')
- {
- Sys_GPIO_Set_Low(LED_DIO_R);
- Sys_GPIO_Set_High(LED_DIO_G);
- Sys_GPIO_Set_High(LED_DIO_Y);
- PRINTF("LED RED is ON\r\n");
- LED_Flag=0;
- }
- else if(LED_Flag=='G')
- {
- Sys_GPIO_Set_High(LED_DIO_R);
- Sys_GPIO_Set_Low(LED_DIO_G);
- Sys_GPIO_Set_High(LED_DIO_Y);
- PRINTF("LED GREEN is ON\r\n");
- LED_Flag=0;
- }
- else if(LED_Flag=='Y')
- {
- Sys_GPIO_Set_High(LED_DIO_R);
- Sys_GPIO_Set_High(LED_DIO_G);
- Sys_GPIO_Set_Low(LED_DIO_Y);
- PRINTF("LED YELLOW is ON\r\n");
- LED_Flag=0;
- }
- else
- {
- ;
- //Sys_GPIO_Set_High(LED_DIO_R);
- //Sys_GPIO_Set_High(LED_DIO_G);
- //Sys_GPIO_Set_High(LED_DIO_Y);
- //PRINTF("ALL LED is OFF\r\n");
- }
- /* Refresh the watchdog timer */
- Sys_Watchdog_Refresh();
- /* Wait for an event before executing the scheduler again */
- SYS_WAIT_FOR_EVENT;
- }
- }
五、总结
本项目主要实现了红外对射判断是否有物体闯入并进行报警。当系统处于正常状态时为绿灯,当有物体闯入时为红灯,当需要进行维护时通过手机控制亮黄灯。可以将本项目进行拓展用于以下场景:
安防领域;
智慧交通;
生产线货物检测;
智能家居。