找回密码
 立即注册

QQ登录

只需一步,快速开始

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

zstack TI2530DB物联网国赛题

[复制链接]
跳转到指定楼层
楼主
ID:293462 发表于 2018-3-18 11:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
物联网国赛题单片机源程序如下:
  1. /*******************************************************************************
  2.   Filename:       ZMain.c
  3.   Revised:        $Date: 2009-09-17 20:35:33 -0700 (Thu, 17 Sep 2009) $
  4.   Revision:       $Revision: 20782 $

  5.   Description:    Startup and shutdown code for ZStack
  6.   Notes:          This version targets the Chipcon CC2530


  7.   Copyright 2005-2009 Texas Instruments Incorporated. All rights reserved.

  8.   IMPORTANT: Your use of this Software is limited to those specific rights
  9.   granted under the terms of a software license agreement between the user
  10.   who downloaded the software, his/her employer (which must be your employer)
  11.   and Texas Instruments Incorporated (the "License").  You may not use this
  12.   Software unless you agree to abide by the terms of the License. The License
  13.   limits your use, and you acknowledge, that the Software may not be modified,
  14.   copied or distributed unless embedded on a Texas Instruments microcontroller
  15.   or used solely and exclusively in conjunction with a Texas Instruments radio
  16.   frequency transceiver, which is integrated into your product.  Other than for
  17.   the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18.   works of, modify, distribute, perform, display or sell this Software and/or
  19.   its documentation for any purpose.

  20.   YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21.   PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22.   INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23.   NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24.   TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25.   NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26.   LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27.   INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28.   OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29.   OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30.   (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  31.   Should you have any questions regarding your right to use this Software,
  32.   contact Texas Instruments Incorporated at www.TI.com.
  33. *******************************************************************************/

  34. /*********************************************************************
  35. * INCLUDES
  36. */

  37. #include "ZComDef.h"
  38. #include "OSAL.h"
  39. #include "OSAL_Nv.h"
  40. #include "OnBoard.h"
  41. #include "ZMAC.h"

  42. #ifndef NONWK
  43.   #include "AF.h"
  44. #endif

  45. /* Hal */
  46. #include "hal_lcd.h"
  47. #include "hal_led.h"
  48. #include "hal_adc.h"
  49. #include "hal_drivers.h"
  50. #include "hal_assert.h"
  51. #include "hal_flash.h"

  52. /*********************************************************************
  53. * MACROS
  54. */

  55. /*********************************************************************
  56. * CONSTANTS
  57. */

  58. // Maximun number of Vdd samples checked before go on
  59. #define MAX_VDD_SAMPLES  3
  60. #define ZMAIN_VDD_LIMIT  HAL_ADC_VDD_LIMIT_4

  61. /*********************************************************************
  62. * TYPEDEFS
  63. */

  64. /*********************************************************************
  65. * GLOBAL VARIABLES
  66. */

  67. /*********************************************************************
  68. * EXTERNAL VARIABLES
  69. */

  70. /*********************************************************************
  71. * EXTERNAL FUNCTIONS
  72. */

  73. extern bool HalAdcCheckVdd (uint8 limit);

  74. /*********************************************************************
  75. * LOCAL VARIABLES
  76. */

  77. /*********************************************************************
  78. * LOCAL FUNCTIONS
  79. */

  80. static void zmain_dev_info( void );
  81. static void zmain_ext_addr( void );
  82. static void zmain_vdd_check( void );

  83. #ifdef LCD_SUPPORTED
  84. static void zmain_lcd_init( void );
  85. #endif
  86. void    test_sh10(void);
  87. /*********************************************************************
  88. * @fn      main
  89. * @brief   First function called after startup.
  90. * @return  don't care
  91. */
  92. int main( void )     
  93. {
  94.   // Turn off interrupts
  95.   osal_int_disable( INTS_ALL );

  96.   // Initialization for board related stuff such as LEDs
  97.   HAL_BOARD_INIT();

  98.   // Make sure supply voltage is high enough to run
  99.   zmain_vdd_check();

  100.   // Initialize board I/O
  101.   InitBoard( OB_COLD );

  102.   // Initialze HAL drivers
  103.   HalDriverInit();

  104.   // Initialize NV System
  105.   osal_nv_init( NULL );

  106.   // Initialize the MAC
  107.   ZMacInit();

  108.   // Determine the extended address
  109.   zmain_ext_addr();

  110.   // Initialize basic NV items
  111.   zgInit();

  112. #ifndef NONWK
  113.   // Since the AF isn't a task, call it's initialization routine
  114.   afInit();
  115. #endif

  116.   // Initialize the operating system
  117.   osal_init_system();

  118.   // Allow interrupts
  119.   osal_int_enable( INTS_ALL );

  120.   // Final board initialization
  121.   InitBoard( OB_READY );

  122.   // Display information about this device
  123.   zmain_dev_info();

  124.   /* Display the device info on the LCD */
  125. #ifdef LCD_SUPPORTED
  126.   zmain_lcd_init();
  127. #endif

  128. #ifdef WDT_IN_PM1
  129.   /* If WDT is used, this is a good place to enable it. */
  130.   WatchDogEnable( WDTIMX );
  131. #endif

  132.   osal_start_system(); // No Return from here

  133.   return 0;  // Shouldn't get here.
  134. } // main()

  135. /*********************************************************************
  136. * @fn      zmain_vdd_check
  137. * @brief   Check if the Vdd is OK to run the processor.
  138. * @return  Return if Vdd is ok; otherwise, flash LED, then reset
  139. *********************************************************************/
  140. static void zmain_vdd_check( void )
  141. {
  142.   uint8 vdd_passed_count = 0;
  143.   bool toggle = 0;

  144.   // Repeat getting the sample until number of failures or successes hits MAX
  145.   // then based on the count value, determine if the device is ready or not
  146.   while ( vdd_passed_count < MAX_VDD_SAMPLES )
  147.   {
  148.     if ( HalAdcCheckVdd (ZMAIN_VDD_LIMIT) )
  149.     {
  150.       vdd_passed_count++;    // Keep track # times Vdd passes in a row
  151.       MicroWait (10000);     // Wait 10ms to try again
  152.     }
  153.     else
  154.     {
  155.       vdd_passed_count = 0;  // Reset passed counter
  156.       MicroWait (50000);     // Wait 50ms
  157.       MicroWait (50000);     // Wait another 50ms to try again
  158.     }

  159.     /* toggle LED1 and LED2 */
  160.     if (vdd_passed_count == 0)
  161.     {
  162.       if ((toggle = !(toggle)))
  163.         HAL_TOGGLE_LED1();
  164.       else
  165.         HAL_TOGGLE_LED2();
  166.     }
  167.   }

  168.   /* turn off LED1 */
  169.   HAL_TURN_OFF_LED1();
  170.   HAL_TURN_OFF_LED2();
  171. }

  172. /**************************************************************************************************
  173. * @fn          zmain_ext_addr
  174. *
  175. * @brief       Execute a prioritized search for a valid extended address and write the results
  176. *              into the OSAL NV system for use by the system. Temporary address not saved to NV.
  177. *
  178. * input parameters
  179. *
  180. * None.
  181. *
  182. * output parameters
  183. *
  184. * None.
  185. *
  186. * @return      None.
  187. **************************************************************************************************
  188. */
  189. static void zmain_ext_addr(void)
  190. {
  191.   uint8 nullAddr[Z_EXTADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  192.   uint8 writeNV = TRUE;

  193.   // First check whether a non-erased extended address exists in the OSAL NV.
  194.   if ((SUCCESS != osal_nv_item_init(ZCD_NV_EXTADDR, Z_EXTADDR_LEN, NULL))  ||
  195.       (SUCCESS != osal_nv_read(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress)) ||
  196.       (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN)))
  197.   {
  198.     // Attempt to read the extended address from the location on the lock bits page
  199.     // where the programming tools know to reserve it.
  200.     HalFlashRead(HAL_FLASH_IEEE_PAGE, HAL_FLASH_IEEE_OSET, aExtendedAddress, Z_EXTADDR_LEN);

  201.     if (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN))
  202.     {
  203.       // Attempt to read the extended address from the designated location in the Info Page.
  204.       if (!osal_memcmp((uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), nullAddr, Z_EXTADDR_LEN))
  205.       {
  206.         osal_memcpy(aExtendedAddress, (uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), Z_EXTADDR_LEN);
  207.       }
  208.       else  // No valid extended address was found.
  209.       {
  210.         uint8 idx;
  211.         
  212. #if !defined ( NV_RESTORE )
  213.         writeNV = FALSE;  // Make this a temporary IEEE address
  214. #endif

  215.         /* Attempt to create a sufficiently random extended address for expediency.
  216.          * Note: this is only valid/legal in a test environment and
  217.          *       must never be used for a commercial product.
  218.          */
  219.         for (idx = 0; idx < (Z_EXTADDR_LEN - 2);)
  220.         {
  221.           uint16 randy = osal_rand();
  222.           aExtendedAddress[idx++] = LO_UINT16(randy);
  223.           aExtendedAddress[idx++] = HI_UINT16(randy);
  224.         }
  225.         // Next-to-MSB identifies ZigBee devicetype.
  226. #if ZG_BUILD_COORDINATOR_TYPE && !ZG_BUILD_JOINING_TYPE
  227.         aExtendedAddress[idx++] = 0x10;
  228. #elif ZG_BUILD_RTRONLY_TYPE
  229.         aExtendedAddress[idx++] = 0x20;
  230. #else
  231.         aExtendedAddress[idx++] = 0x30;
  232. #endif
  233.         // MSB has historical signficance.
  234.         aExtendedAddress[idx] = 0xF8;
  235.       }
  236.     }

  237.     if (writeNV)
  238.     {
  239.       (void)osal_nv_write(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress);
  240.     }
  241.   }

  242.   // Set the MAC PIB extended address according to results from above.
  243.   (void)ZMacSetReq(MAC_EXTENDED_ADDRESS, aExtendedAddress);
  244. }

  245. /**************************************************************************************************
  246. * @fn          zmain_dev_info
  247. *
  248. * @brief       This displays the IEEE (MSB to LSB) on the LCD.
  249. *
  250. * input parameters
  251. *
  252. * None.
  253. *
  254. * output parameters
  255. *
  256. * None.
  257. *
  258. * @return      None.
  259. **************************************************************************************************
  260. */
  261. static void zmain_dev_info(void)
  262. {
  263. #ifdef LCD_SUPPORTED
  264.   uint8 i;
  265.   uint8 *xad;
  266.   uint8 lcd_buf[Z_EXTADDR_LEN*2+1];

  267.   // Display the extended address.
  268.   xad = aExtendedAddress + Z_EXTADDR_LEN - 1;

  269.   for (i = 0; i < Z_EXTADDR_LEN*2; xad--)
  270.   {
  271.     uint8 ch;
  272.     ch = (*xad >> 4) & 0x0F;
  273.     lcd_buf[i++] = ch + (( ch < 10 ) ? '0' : '7');
  274.     ch = *xad & 0x0F;
  275.     lcd_buf[i++] = ch + (( ch < 10 ) ? '0' : '7');
  276.   }
  277.   lcd_buf[Z_EXTADDR_LEN*2] = '\0';
  278.   HalLcdWriteString( "IEEE: ", HAL_LCD_LINE_1 );
  279.   HalLcdWriteString( (char*)lcd_buf, HAL_LCD_LINE_2 );
  280. #endif
  281. }

  282. #ifdef LCD_SUPPORTED
  283. /*********************************************************************
  284. * @fn      zmain_lcd_init
  285. * @brief   Initialize LCD at start up.
  286. * @return  none
  287. *********************************************************************/
  288. static void zmain_lcd_init ( void )
  289. {
  290. #ifdef SERIAL_DEBUG_SUPPORTED
  291.   {
  292.     HalLcdWriteString( "TexasInstruments", HAL_LCD_LINE_1 );

  293. #if defined( MT_MAC_FUNC )
  294. #if defined( ZDO_COORDINATOR )
  295.       HalLcdWriteString( "MAC-MT Coord", HAL_LCD_LINE_2 );
  296. #else
  297.       HalLcdWriteString( "MAC-MT Device", HAL_LCD_LINE_2 );
  298. #endif // ZDO
  299. #elif defined( MT_NWK_FUNC )
  300. #if defined( ZDO_COORDINATOR )
  301.       HalLcdWriteString( "NWK Coordinator", HAL_LCD_LINE_2 );
  302. #else
  303.       HalLcdWriteString( "NWK Device", HAL_LCD_LINE_2 );
  304. #endif // ZDO
  305. #endif // MT_FUNC
  306.   }
  307. #endif // SERIAL_DEBUG_SUPPORTED
  308. }
  309. #endif

  310. /*********************************************************************
  311. *********************************************************************/

复制代码

所有资料51hei提供下载:
题3.rar (6.24 MB, 下载次数: 6)



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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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