找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3245|回复: 2
收起左侧

CC2530远程控制灯亮灭程序

[复制链接]
ID:617015 发表于 2019-9-27 09:25 | 显示全部楼层 |阅读模式
一,按 A模块的按键---控制 B模块的 LED 亮灭
1、实验需求:
硬件需求:cc2530 模块两个、调试器一个。

单片机源程序如下:
  1. /***********************************************************************************
  2. 网蜂ZigBee-无线点灯实验代码 详细解释请参考《ZigBee实战演练》中无线电灯相关内容

  3. 实验操作:
  4.        第一步: 找到下面内容,把appLight();  注释掉,下载到发射模块。
  5.           appSwitch();        //节点为按键S1       P1_2
  6.        // appLight();        //节点为指示灯LED1    P1_0

  7.        第二步:找到相同位置,这次把appSwitch();注释掉,下载到接收模块。
  8.        //appSwitch();        //节点为按键S1       P0_4
  9.         appLight();         //节点为指示灯LED1   P1_0

  10. 完成烧写后上电,按下发射模块的S1按键,可以看到接收模块的LED1被点亮。
  11. *********************************************************************************

  12.   Filename: light_switch.c

  13.   Description:  This application function either as a light or a
  14.   switch toggling the ligh. The role of the
  15.   application is chosen in the menu with the joystick at initialisation.

  16.   Push S1 to enter the menu. Choose either switch or
  17.   light and confirm choice with S1.
  18.   Joystick Up: Sends data from switch to light

  19. ***********************************************************************************/

  20. /***********************************************************************************
  21. * INCLUDES
  22. */
  23. #include <hal_lcd.h>
  24. #include <hal_led.h>
  25. #include <hal_joystick.h>
  26. #include <hal_assert.h>
  27. #include <hal_board.h>
  28. #include <hal_int.h>
  29. #include "hal_mcu.h"
  30. #include "hal_button.h"
  31. #include "hal_rf.h"
  32. #include "util_lcd.h"
  33. #include "basic_rf.h"


  34. /***********************************************************************************
  35. * CONSTANTS
  36. */
  37. // Application parameters
  38. #define RF_CHANNEL                25      // 2.4 GHz RF channel

  39. // BasicRF address definitions
  40. #define PAN_ID                0x2007
  41. #define SWITCH_ADDR           0x2520
  42. #define LIGHT_ADDR            0xBEEF
  43. #define APP_PAYLOAD_LENGTH        1
  44. #define LIGHT_TOGGLE_CMD          0

  45. // Application states
  46. #define IDLE                      0
  47. #define SEND_CMD                  1

  48. // Application role
  49. #define NONE                      0
  50. #define SWITCH                    1
  51. #define LIGHT                     2
  52. #define APP_MODES                 2

  53. /***********************************************************************************
  54. * LOCAL VARIABLES
  55. */
  56. static uint8 pTxData[APP_PAYLOAD_LENGTH];
  57. static uint8 pRxData[APP_PAYLOAD_LENGTH];
  58. static basicRfCfg_t basicRfConfig;

  59. // Mode menu
  60. static menuItem_t pMenuItems[] =
  61. {
  62. #ifdef ASSY_EXP4618_CC2420
  63.   // Using Softbaugh 7-seg display
  64.   " L S    ", SWITCH,
  65.   " LIGHT  ", LIGHT
  66. #else
  67.   // SRF04EB and SRF05EB
  68.   "Switch",   SWITCH,
  69.   "Light",    LIGHT
  70. #endif
  71. };

  72. static menu_t pMenu =
  73. {
  74.   pMenuItems,
  75.   N_ITEMS(pMenuItems)
  76. };


  77. #ifdef SECURITY_CCM
  78. // Security key
  79. static uint8 key[]= {
  80.     0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  81.     0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  82. };
  83. #endif

  84. /***********************************************************************************
  85. * LOCAL FUNCTIONS
  86. */
  87. static void appLight();
  88. static void appSwitch();
  89. static uint8 appSelectMode(void);


  90. /***********************************************************************************
  91. * @fn          appLight
  92. *
  93. * @brief       Application code for light application. Puts MCU in endless
  94. *              loop waiting for user input from joystick.
  95. *
  96. * @param       basicRfConfig - file scope variable. Basic RF configuration data
  97. *              pRxData - file scope variable. Pointer to buffer for RX data
  98. *
  99. * @return      none
  100. */
  101. static void appLight()
  102. {

  103.     halLcdWriteLine(HAL_LCD_LINE_1, "    W e B e e      ");
  104.     halLcdWriteLine(HAL_LCD_LINE_2, "  ZigBee CC2530  ");
  105.     halLcdWriteLine(HAL_LCD_LINE_4, "     LIGHT     ");

  106. #ifdef ASSY_EXP4618_CC2420
  107.     halLcdClearLine(1);
  108.     halLcdWriteSymbol(HAL_LCD_SYMBOL_RX, 1);
  109. #endif

  110.     // Initialize BasicRF
  111.     basicRfConfig.myAddr = LIGHT_ADDR;
  112.     if(basicRfInit(&basicRfConfig)==FAILED) {
  113.       HAL_ASSERT(FALSE);
  114.     }
  115.     basicRfReceiveOn();

  116.     // Main loop
  117.     while (TRUE) {
  118.         while(!basicRfPacketIsReady());

  119.         if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) {
  120.             if(pRxData[0] == LIGHT_TOGGLE_CMD) {
  121.                 halLedToggle(1);
  122.             }
  123.         }
  124.     }
  125. }


  126. /***********************************************************************************
  127. * @fn          appSwitch
  128. *
  129. * @brief       Application code for switch application. Puts MCU in
  130. *              endless loop to wait for commands from from switch
  131. *
  132. * @param       basicRfConfig - file scope variable. Basic RF configuration data
  133. *              pTxData - file scope variable. Pointer to buffer for TX
  134. *              payload
  135. *              appState - file scope variable. Holds application state
  136. *
  137. * @return      none
  138. */
  139. static void appSwitch()
  140. {
  141.     halLcdWriteLine(HAL_LCD_LINE_1, "      GEC-EDU      ");
  142.     halLcdWriteLine(HAL_LCD_LINE_2, "  ZigBee CC2530  ");
  143.     halLcdWriteLine(HAL_LCD_LINE_4, "     SWITCH    ");
  144.    
  145. #ifdef ASSY_EXP4618_CC2420
  146.     halLcdClearLine(1);
  147.     halLcdWriteSymbol(HAL_LCD_SYMBOL_TX, 1);
  148. #endif


  149.     // Initialize BasicRF
  150.     basicRfConfig.myAddr = SWITCH_ADDR;
  151.     if(basicRfInit(&basicRfConfig)==FAILED) {
  152.       HAL_ASSERT(FALSE);
  153.     }
  154.    
  155.     pTxData[0] = LIGHT_TOGGLE_CMD;

  156.     // Keep Receiver off when not needed to save power
  157.     basicRfReceiveOff();

  158.     // Main loop
  159.     while (TRUE) {
  160.         //if( halJoystickPushed() )**********************by boo
  161.       if(halButtonPushed()==HAL_BUTTON_1)//**************by boo
  162.         {

  163.             basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);

  164.             // Put MCU to sleep. It will wake up on joystick interrupt
  165.             halIntOff();
  166.             halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
  167.             // interrupt enable
  168.             halIntOn();

  169.         }
  170.     }
  171. }


  172. /***********************************************************************************
  173. * @fn          main
  174. *
  175. * @brief       This is the main entry of the "Light Switch" application.
  176. *              After the application modes are chosen the switch can
  177. *              send toggle commands to a light device.
  178. *
  179. * @param       basicRfConfig - file scope variable. Basic RF configuration
  180. *              data
  181. *              appState - file scope variable. Holds application state
  182. *
  183. * @return      none
  184. */
  185. void main(void)
  186. {
  187.     uint8 appMode = NONE;

  188.     // Config basicRF
  189.     basicRfConfig.panId = PAN_ID;
  190.     basicRfConfig.channel = RF_CHANNEL;
  191.     basicRfConfig.ackRequest = TRUE;
  192. #ifdef SECURITY_CCM
  193.     basicRfConfig.securityKey = key;
  194. #endif

  195.     // Initalise board peripherals
  196.     halBoardInit();
  197.     halJoystickInit();

  198.     // Initalise hal_rf
  199.     if(halRfInit()==FAILED) {
  200.       HAL_ASSERT(FALSE);
  201.     }

  202.     // Indicate that device is powered
  203.     halLedSet(2);      //关闭LED2
  204.     halLedSet(1);      //关闭LED1


  205.     /***********************************************
  206.     // Print Logo and splash screen on LCD
  207.      utilPrintLogo("Light Switch");

  208.     // Wait for user to press S1 to enter menu
  209.     while (halButtonPushed()!=HAL_BUTTON_1);
  210.     halMcuWaitMs(350);
  211.     halLcdClear();

  212.     // Set application role
  213.     appMode = appSelectMode();
  214.     halLcdClear();

  215.     // Transmitter application
  216.     if(appMode == SWITCH) {
  217.         // No return from here
  218.         appSwitch();
  219.     }
  220.     // Receiver application
  221.     else if(appMode == LIGHT) {
  222.         // No return from here
  223.         appLight();
  224.     }
  225.     **************************************/
  226.    
  227.     /************Select one and shield to another***********by boo*/
  228.      //appSwitch();        //节点为按键S1      P1_2
  229.     appLight();         //节点为指示灯LED1   P1_0
  230.    
  231.    // Role is undefined. This code should not be reached
  232.     HAL_ASSERT(FALSE);
  233. }


  234. /****************************************************************************************
  235. * @fn          appSelectMode
  236. *
  237. * @brief       Select application mode
  238. *
  239. * @param       none
  240. *
  241. * @return      uint8 - Application mode chosen
  242. */
  243. static uint8 appSelectMode(void)
  244. {
  245.     halLcdWriteLine(1, "Device Mode: ");

  246.     return utilMenuSelect(&pMenu);
  247. }
复制代码

所有资料51hei提供下载:
CC2530 BasicRF无线点灯部署.7z (525.14 KB, 下载次数: 7)


回复

使用道具 举报

ID:1 发表于 2019-9-27 17:42 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

ID:157086 发表于 2019-9-28 13:25 | 显示全部楼层
学习了》》》厉害,英语牛逼
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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