用蓝牙芯片CC2541/CC2540实现一个智能恒温箱

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用蓝牙芯片CC2541/CC2540实现一个智能恒温箱相关的知识,希望对你有一定的参考价值。

最近突然想自己做一个智能小冰箱玩一玩,于是决定动手试一试。

成品效果图

技术分享技术分享技术分享

 

原材料

半导体制冷片一只

散热风扇

12V电源一台

智能恒温箱电路板

 

控制板的PCB图

技术分享

原理图

技术分享

供电部分原理图

技术分享

制冷片控制部分原理图

技术分享

 

蜂鸣器原理图

技术分享

 

温度传感器原理图

 

技术分享

 

GITHUB地址

完整代码以及电路图我放在了GITHUB上,欢迎下载哦。

https://github.com/dingbig/bluttooth_ice_box

 

控制部分源代码

 

  1 /**************************************************************************************************
  2   Filename:       simpleBLEPeripheral.c
  3   Revised:        $Date: 2010-08-06 08:56:11 -0700 (Fri, 06 Aug 2010) $
  4   Revision:       $Revision: 23333 $
  5 
  6   Description:    This file contains the Simple BLE Peripheral sample application
  7                   for use with the CC2540 Bluetooth Low Energy Protocol Stack.
  8 
  9   Copyright 2010 - 2014 Texas Instruments Incorporated. All rights reserved.
 10 
 11   IMPORTANT: Your use of this Software is limited to those specific rights
 12   granted under the terms of a software license agreement between the user
 13   who downloaded the software, his/her employer (which must be your employer)
 14   and Texas Instruments Incorporated (the "License").  You may not use this
 15   Software unless you agree to abide by the terms of the License. The License
 16   limits your use, and you acknowledge, that the Software may not be modified,
 17   copied or distributed unless embedded on a Texas Instruments microcontroller
 18   or used solely and exclusively in conjunction with a Texas Instruments radio
 19   frequency transceiver, which is integrated into your product.  Other than for
 20   the foregoing purpose, you may not use, reproduce, copy, prepare derivative
 21   works of, modify, distribute, perform, display or sell this Software and/or
 22   its documentation for any purpose.
 23 
 24   YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
 25   PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 26   INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
 27   NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
 28   TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
 29   NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
 30   LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
 31   INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
 32   OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
 33   OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
 34   (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
 35 
 36   Should you have any questions regarding your right to use this Software,
 37   contact Texas Instruments Incorporated at www.TI.com.
 38 **************************************************************************************************/
 39 
 40 /*********************************************************************
 41  * INCLUDES
 42  */
 43 
 44 #include "bcomdef.h"
 45 #include "OSAL.h"
 46 #include "OSAL_PwrMgr.h"
 47 
 48 #include "OnBoard.h"
 49 #include "hal_adc.h"
 50 #include "hal_led.h"
 51 #include "hal_key.h"
 52 #include "hal_lcd.h"
 53 
 54 #include "gatt.h"
 55 
 56 #include "hci.h"
 57 
 58 #include "gapgattserver.h"
 59 #include "gattservapp.h"
 60 #include "devinfoservice.h"
 61 #include "simpleGATTprofile.h"
 62 #include "ds18b20.h"
 63 #include "stdio.h"
 64 #include "oled.h"
 65 
 66 #if defined( CC2540_MINIDK )
 67   #include "simplekeys.h"
 68 #endif
 69 
 70 #include "peripheral.h"
 71 
 72 #include "gapbondmgr.h"
 73 
 74 #include "simpleBLEPeripheral.h"
 75 
 76 #if defined FEATURE_OAD
 77   #include "oad.h"
 78   #include "oad_target.h"
 79 #endif
 80 
 81 /*********************************************************************
 82  * MACROS
 83  */
 84 
 85 /*********************************************************************
 86  * CONSTANTS
 87  */
 88 
 89 // How often to perform periodic event
 90 #define SBP_PERIODIC_EVT_PERIOD                   5000
 91 #define SBP_TEMP_EVT_PERIOD                   500
 92 
 93 
 94 // What is the advertising interval when device is discoverable (units of 625us, 160=100ms)
 95 #define DEFAULT_ADVERTISING_INTERVAL          160
 96 
 97 // Limited discoverable mode advertises for 30.72s, and then stops
 98 // General discoverable mode advertises indefinitely
 99 
100 #if defined ( CC2540_MINIDK )
101 #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_LIMITED
102 #else
103 #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_GENERAL
104 #endif  // defined ( CC2540_MINIDK )
105 
106 // Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled
107 #define DEFAULT_DESIRED_MIN_CONN_INTERVAL     80
108 
109 // Maximum connection interval (units of 1.25ms, 800=1000ms) if automatic parameter update request is enabled
110 #define DEFAULT_DESIRED_MAX_CONN_INTERVAL     800
111 
112 // Slave latency to use if automatic parameter update request is enabled
113 #define DEFAULT_DESIRED_SLAVE_LATENCY         0
114 
115 // Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter update request is enabled
116 #define DEFAULT_DESIRED_CONN_TIMEOUT          1000
117 
118 // Whether to enable automatic parameter update request when a connection is formed
119 #define DEFAULT_ENABLE_UPDATE_REQUEST         TRUE
120 
121 // Connection Pause Peripheral time value (in seconds)
122 #define DEFAULT_CONN_PAUSE_PERIPHERAL         6
123 
124 // Company Identifier: Texas Instruments Inc. (13)
125 #define TI_COMPANY_ID                         0x000D
126 
127 #define INVALID_CONNHANDLE                    0xFFFF
128 
129 // Length of bd addr as a string
130 #define B_ADDR_STR_LEN                        15
131 
132 /*********************************************************************
133  * TYPEDEFS
134  */
135 
136 /*********************************************************************
137  * GLOBAL VARIABLES
138  */
139 
140 /*********************************************************************
141  * EXTERNAL VARIABLES
142  */
143 
144 /*********************************************************************
145  * EXTERNAL FUNCTIONS
146  */
147 
148 /*********************************************************************
149  * LOCAL VARIABLES
150  */
151 static uint8 simpleBLEPeripheral_TaskID;   // Task ID for internal task/event processing
152 
153 static gaprole_States_t gapProfileState = GAPROLE_INIT;
154 
155 // GAP - SCAN RSP data (max size = 31 bytes)
156 static uint8 scanRspData[] =
157 {
158   // complete name
159   0x14,   // length of this data
160   GAP_ADTYPE_LOCAL_NAME_COMPLETE,
161   0x51,   // ‘Q‘
162   0x69,   // ‘i‘
163   0x6d,   // ‘m‘
164   0x70,   // ‘p‘
165   0x6c,   // ‘l‘
166   0x65,   // ‘e‘
167   0x42,   // ‘B‘
168   0x4c,   // ‘L‘
169   0x45,   // ‘E‘
170   0x50,   // ‘P‘
171   0x65,   // ‘e‘
172   0x72,   // ‘r‘
173   0x69,   // ‘i‘
174   0x70,   // ‘p‘
175   0x68,   // ‘h‘
176   0x65,   // ‘e‘
177   0x72,   // ‘r‘
178   0x61,   // ‘a‘
179   0x6c,   // ‘l‘
180 
181   // connection interval range
182   0x05,   // length of this data
183   GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
184   LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),   // 100ms
185   HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),
186   LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),   // 1s
187   HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),
188 
189   // Tx power level
190   0x02,   // length of this data
191   GAP_ADTYPE_POWER_LEVEL,
192   0       // 0dBm
193 };
194 
195 // GAP - Advertisement data (max size = 31 bytes, though this is
196 // best kept short to conserve power while advertisting)
197 static uint8 advertData[] =
198 {
199   // Flags; this sets the device to use limited discoverable
200   // mode (advertises for 30 seconds at a time) instead of general
201   // discoverable mode (advertises indefinitely)
202   0x02,   // length of this data
203   GAP_ADTYPE_FLAGS,
204   DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
205 
206   // service UUID, to notify central devices what services are included
207   // in this peripheral
208   0x03,   // length of this data
209   GAP_ADTYPE_16BIT_MORE,      // some of the UUID‘s, but not all
210   LO_UINT16( SIMPLEPROFILE_SERV_UUID ),
211   HI_UINT16( SIMPLEPROFILE_SERV_UUID ),
212 
213 };
214 
215 // GAP GATT Attributes
216 static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple BLE Peripheral";
217 
218 /*********************************************************************
219  * LOCAL FUNCTIONS
220  */
221 static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg );
222 static void simpleBLEPeripheral_ProcessGATTMsg( gattMsgEvent_t *pMsg );
223 static void peripheralStateNotificationCB( gaprole_States_t newState );
224 static void performPeriodicTask( void );
225 static void simpleProfileChangeCB( uint8 paramID );
226 
227 #if defined( CC2540_MINIDK )
228 static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys );
229 #endif
230 
231 #if (defined HAL_LCD) && (HAL_LCD == TRUE)
232 static char *bdAddr2Str ( uint8 *pAddr );
233 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
234 
235 
236 
237 /*********************************************************************
238  * PROFILE CALLBACKS
239  */
240 
241 // GAP Role Callbacks
242 static gapRolesCBs_t simpleBLEPeripheral_PeripheralCBs =
243 {
244   peripheralStateNotificationCB,  // Profile State Change Callbacks
245   NULL                            // When a valid RSSI is read from controller (not used by application)
246 };
247 
248 // GAP Bond Manager Callbacks
249 static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs =
250 {
251   NULL,                     // Passcode callback (not used by application)
252   NULL                      // Pairing / Bonding state Callback (not used by application)
253 };
254 
255 // Simple GATT Profile Callbacks
256 static simpleProfileCBs_t simpleBLEPeripheral_SimpleProfileCBs =
257 {
258   simpleProfileChangeCB    // Charactersitic value change callback
259 };
260 /*********************************************************************
261  * PUBLIC FUNCTIONS
262  */
263 
264 /*********************************************************************
265  * @fn      SimpleBLEPeripheral_Init
266  *
267  * @brief   Initialization function for the Simple BLE Peripheral App Task.
268  *          This is called during initialization and should contain
269  *          any application specific initialization (ie. hardware
270  *          initialization/setup, table initialization, power up
271  *          notificaiton ... ).
272  *
273  * @param   task_id - the ID assigned by OSAL.  This ID should be
274  *                    used to send messages and set timers.
275  *
276  * @return  none
277  */
278 void SimpleBLEPeripheral_Init( uint8 task_id )
279 {
280   simpleBLEPeripheral_TaskID = task_id;
281 
282   osal_set_event(simpleBLEPeripheral_TaskID, SBP_START_TEMP_TIMER_EVT);
283   // Setup the GAP
284   VOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL );
285   
286   // Setup the GAP Peripheral Role Profile
287   {
288     #if defined( CC2540_MINIDK )
289       // For the CC2540DK-MINI keyfob, device doesn‘t start advertising until button is pressed
290       uint8 initial_advertising_enable = FALSE;
291     #else
292       // For other hardware platforms, device starts advertising upon initialization
293       uint8 initial_advertising_enable = TRUE;
294     #endif
295 
296     // By setting this to zero, the device will go into the waiting state after
297     // being discoverable for 30.72 second, and will not being advertising again
298     // until the enabler is set back to TRUE
299     uint16 gapRole_AdvertOffTime = 0;
300 
301     uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST;
302     uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
303     uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
304     uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY;
305     uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT;
306 
307     // Set the GAP Role Parameters
308     GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
309     GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );
310 
311     GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
312     GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
313 
314     GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request );
315     GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval );
316     GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval );
317     GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency );
318     GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout );
319   }
320 
321   // Set the GAP Characteristics
322   GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );
323 
324   // Set advertising interval
325   {
326     uint16 advInt = DEFAULT_ADVERTISING_INTERVAL;
327 
328     GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );
329     GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );
330     GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );
331     GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );
332   }
333 
334   // Setup the GAP Bond Manager
335   {
336     uint32 passkey = 0; // passkey "000000"
337     uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
338     uint8 mitm = TRUE;
339     uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
340     uint8 bonding = TRUE;
341     GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey );
342     GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode );
343     GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm );
344     GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap );
345     GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding );
346   }
347 
348   // Initialize GATT attributes
349   GGS_AddService( GATT_ALL_SERVICES );            // GAP
350   GATTServApp_AddService( GATT_ALL_SERVICES );    // GATT attributes
351   DevInfo_AddService();                           // Device Information Service
352   SimpleProfile_AddService( GATT_ALL_SERVICES );  // Simple GATT Profile
353 #if defined FEATURE_OAD
354   VOID OADTarget_AddService();                    // OAD Profile
355 #endif
356 
357   // Setup the SimpleProfile Characteristic Values
358   {
359     uint8 charValue1 = 1;
360     uint8 charValue2 = 2;
361     uint8 charValue3 = 3;
362     uint8 charValue4 = 4;
363     uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };
364     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 );
365     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 );
366     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 );
367     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 );
368     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 );
369   }
370 
371 
372 #if defined( CC2540_MINIDK )
373 
374   SK_AddService( GATT_ALL_SERVICES ); // Simple Keys Profile
375 
376   // Register for all key events - This app will handle all key events
377   RegisterForKeys( simpleBLEPeripheral_TaskID );
378 
379   // makes sure LEDs are off
380   HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );
381 
382   // For keyfob board set GPIO pins into a power-optimized state
383   // Note that there is still some leakage current from the buzzer,
384   // accelerometer, LEDs, and buttons on the PCB.
385 
386   P0SEL = 0; // Configure Port 0 as GPIO
387   P1SEL = 0; // Configure Port 1 as GPIO
388   P2SEL = 0; // Configure Port 2 as GPIO
389 
390   P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons),
391                 // all others (P0.2-P0.7) as output
392   P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output
393   P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output
394 
395   P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons)
396   P1 = 0;   // All pins on port 1 to low
397   P2 = 0;   // All pins on port 2 to low
398 
399 #endif // #if defined( CC2540_MINIDK )
400 
401 #if (defined HAL_LCD) && (HAL_LCD == TRUE)
402 
403 #if defined FEATURE_OAD
404   #if defined (HAL_IMAGE_A)
405     HalLcdWriteStringValue( "BLE Peri-A", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 );
406   #else
407     HalLcdWriteStringValue( "BLE Peri-B", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 );
408   #endif // HAL_IMAGE_A
409 #else
410   HalLcdWriteString( "BLE Peripheral", HAL_LCD_LINE_1 );
411 #endif // FEATURE_OAD
412 
413 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
414 
415   // Register callback with SimpleGATTprofile
416   VOID SimpleProfile_RegisterAppCBs( &simpleBLEPeripheral_SimpleProfileCBs );
417 
418   // Enable clock divide on halt
419   // This reduces active current while radio is active and CC254x MCU
420   // is halted
421   HCI_EXT_ClkDivOnHaltCmd( HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT );
422 
423 #if defined ( DC_DC_P0_7 )
424 
425   // Enable stack to toggle bypass control on TPS62730 (DC/DC converter)
426   HCI_EXT_MapPmIoPortCmd( HCI_EXT_PM_IO_PORT_P0, HCI_EXT_PM_IO_PORT_PIN7 );
427 
428 #endif // defined ( DC_DC_P0_7 )
429 
430   // Setup a delayed profile startup
431   osal_set_event( simpleBLEPeripheral_TaskID, SBP_START_DEVICE_EVT );
432 
433 }
434 
435 
436 float last_temp;
437 /*********************************************************************
438  * @fn      SimpleBLEPeripheral_ProcessEvent
439  *
440  * @brief   Simple BLE Peripheral Application Task event processor.  This function
441  *          is called to process all events for the task.  Events
442  *          include timers, messages and any other user defined events.
443  *
444  * @param   task_id  - The OSAL assigned task ID.
445  * @param   events - events to process.  This is a bit map and can
446  *                   contain more than one event.
447  *
448  * @return  events not processed
449  */
450 uint16 SimpleBLEPeripheral_ProcessEvent( uint8 task_id, uint16 events )
451 {
452 
453   VOID task_id; // OSAL required parameter that isn‘t used in this function
454 
455   if ( events & SYS_EVENT_MSG )
456   {
457     uint8 *pMsg;
458 
459     if ( (pMsg = osal_msg_receive( simpleBLEPeripheral_TaskID )) != NULL )
460     {
461       simpleBLEPeripheral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
462 
463       // Release the OSAL message
464       VOID osal_msg_deallocate( pMsg );
465     }
466 
467     // return unprocessed events
468     return (events ^ SYS_EVENT_MSG);
469   }
470 
471   if ( events & SBP_START_DEVICE_EVT )
472   {
473     // Start the Device
474     VOID GAPRole_StartDevice( &simpleBLEPeripheral_PeripheralCBs );
475 
476     // Start Bond Manager
477     VOID GAPBondMgr_Register( &simpleBLEPeripheral_BondMgrCBs );
478 

以上是关于用蓝牙芯片CC2541/CC2540实现一个智能恒温箱的主要内容,如果未能解决你的问题,请参考以下文章

CC2540/CC2541/CC254x之OSAL操作系统抽象层

CC2540 芯片资料浅析

BLE蓝牙SOC CC2540之一:基本的了解

蓝牙模块选择哪个型号好

与 CC2540 蓝牙 LE 设备配对

CC2540蓝牙模块学习