找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3142|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

LPC1788 SPI主從機(jī)代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:440851 發(fā)表于 2018-12-6 13:19 | 只看該作者 |只看大圖 回帖獎勵 |正序瀏覽 |閱讀模式
在做LPC1788開發(fā)板的主從機(jī)開發(fā),分享下代碼

單片機(jī)源程序如下:
  1. /**********************************************************************
  2. * $Id[        DISCUZ_CODE_1        ]nbsp;               mcu_main_new.c                        2012-05-04
  3. *//**
  4. * @file                mcu_main_new.c
  5. * @brief        User program.
  6. * @version        1.0
  7. * @date                04. May. 2012
  8. * @author        NXP MCU SW Application Team
  9. *
  10. * Copyright(C) 2011, NXP Semiconductor
  11. * All rights reserved.
  12. *
  13. ***********************************************************************
  14. * Software that is described herein is for illustrative purposes only
  15. * which provides customers with programming information regarding the
  16. * products. This software is supplied "AS IS" without any warranties.
  17. * NXP Semiconductors assumes no responsibility or liability for the
  18. * use of the software, conveys no license or title under any patent,
  19. * copyright, or mask work right to the product. NXP Semiconductors
  20. * reserves the right to make changes in the software without
  21. * notification. NXP Semiconductors also make no representation or
  22. * warranty that such application will be suitable for the specified
  23. * use without further testing or modification.
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors'
  26. * relevant copyright in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers.  This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. **********************************************************************/

  31. #include "includes.h"

  32. #define VTOR_OFFSET         (0x00001000)

  33. SBL_FirmVerion_Type firm_vers __attribute__((section("firmware_id_section")))= {0,1,'b'};

  34. /************************** PRIVATE DEFINITIONS *************************/
  35. #ifdef __MCU_LPC17xx
  36. #define MCB_1700
  37. //#define IAR_LPC_1768
  38. #elif defined (__MCU_LPC11xx)
  39. #define MCB_1100
  40. #endif

  41. #if defined (MCB_1700)
  42. /* Number of user LEDs */
  43. #define LED_NUM     4
  44. const unsigned long led_mask[] = { 1<<3, 1<<4, 1<<5, 1<<6 };
  45. #elif defined(MCB_1100)
  46. #define LED_NUM     4
  47. const unsigned long led_pin[] = { 3, 4, 5, 6 };
  48. #endif

  49. /************************** PRIVATE VARIABLES *************************/
  50. /* SysTick Counter */
  51. volatile unsigned long SysTickCnt;

  52. /************************** PRIVATE FUNCTIONS *************************/
  53. void SysTick_Handler (void);
  54. void ReAllocateNVIC(void);

  55. /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
  56. /*********************************************************************//**
  57. * @brief            SysTick handler sub-routine (1ms)
  58. * @param[in]        None
  59. * @return           None
  60. **********************************************************************/
  61. void SysTick_Handler (void) {
  62.   SysTickCnt++;
  63. }

  64. /*-------------------------PRIVATE FUNCTIONS------------------------------*/
  65. /*********************************************************************//**
  66. * @brief            Re-allocate vector interrupt table
  67. * @param[in]        None
  68. * @return           int
  69. **********************************************************************/
  70. void ReAllocateNVIC(void)
  71. {
  72. #ifdef __MCU_LPC17xx
  73.     __disable_irq();
  74.     NVIC_SetVTOR(VTOR_OFFSET);
  75.     __enable_irq();
  76. #elif defined (__MCU_LPC11xx)
  77.     uint32_t* src,*dst;
  78.     int32_t size;

  79.     __disable_irq();
  80.     // copy vector table
  81.     src = (uint32_t*)VTOR_OFFSET;
  82.     dst = (uint32_t*)0x10000000;
  83.     size = 192;

  84.     while(size > 0)
  85.     {
  86.         *dst++ = *src++;
  87.         size -= 4;
  88.     }
  89.      LPC_SYSCON->SYSMEMREMAP = 0x1;    /* remap to internal RAM */
  90.     __enable_irq();
  91. #endif
  92. }
  93. /*-------------------------MAIN FUNCTION------------------------------*/
  94. /*********************************************************************//**
  95. * @brief          c_entry: Main program body
  96. * @param[in]      None
  97. * @return         None
  98. **********************************************************************/
  99. void c_entry(void)
  100. {   
  101.     int num = 0;
  102.     uint32_t systickcnt;
  103.     SBL_SlaveInit();

  104.     /* Relocate NVIC */
  105.     ReAllocateNVIC();

  106.     SystemCoreClockUpdate();
  107.     SysTick_Config(SystemCoreClock/1000 - 1); /* Generate interrupt each 1 ms   */

  108. #if defined (MCB_1700)
  109.     GPIO_SetDir(2, 0x0000007C, 1);           /* LEDs on PORT2 defined as Output    */
  110.     GPIO_ClearValue(2, 0x0000007C);
  111. #elif defined(MCB_1100)
  112.     for(num = LED_NUM-1;num>=0;num--)
  113.     {
  114.         GPIO_SetDir(2, led_pin[num],1);
  115.         GPIO_SetValue(2, led_pin[num]);
  116.     }
  117. #elif defined(IAR_LPC_1768)
  118.     GPIO_SetDir(1, (1<<25), 1);
  119.     GPIO_ClearValue(1, (1<<25));
  120. #endif

  121.     while(1)
  122.     {
  123.         if(SBL_SlaveCmdRecv())
  124.         {
  125.             SBL_SlaveCmdHandler(SBL_SlaveGetRecvCmd());
  126.         }
  127.         else
  128.         {
  129.             if(((SysTickCnt - systickcnt) < 500))
  130.             {
  131. #if defined (MCB_1700)
  132.                 for(num = LED_NUM-1;num>=0;num--)
  133.                     GPIO_SetValue(2, led_mask[num]);
  134. #elif defined(MCB_1100)   
  135.                 for(num = LED_NUM-1;num>=0;num--)
  136.                     GPIO_SetValue(2, led_pin[num]);
  137. #else
  138.                   GPIO_SetValue(2,(1<<25));
  139. #endif
  140.             }
  141.             else if(((SysTickCnt - systickcnt) < 1000))
  142.             {
  143. #ifdef MCB_1700              
  144.                 for(num = LED_NUM-1;num>=0;num--)
  145.                     GPIO_ClearValue(2, led_mask[num]);
  146. #elif defined(MCB_1100)   
  147.                 for(num = LED_NUM-1;num>=0;num--)
  148.                     GPIO_ClearValue(2, led_pin[num]);
  149. #else
  150.                 GPIO_ClearValue(1,(1<<25));
  151. #endif   
  152.             }
  153.             else
  154.             {
  155.                  systickcnt = SysTickCnt;
  156.             }
  157.         }
  158.         
  159.     }
  160.     //SBL_DeInit();
  161. }

  162. /* With ARM and GHS toolsets, the entry point is main() - this will
  163.    allow the linker to generate wrapper code to setup stacks, allocate
  164.    heap area, and initialize and copy code and data segments. For GNU
  165.    toolsets, the entry point is through __start() in the crt0_gnu.asm
  166.    file, and that startup code will setup stacks and data */
  167. int main(void)
  168. {
  169.     c_entry();
  170.     return 0;
  171. }
復(fù)制代碼

所有資料51hei提供下載:
AN11257.zip (637.92 KB, 下載次數(shù): 11)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:6656 發(fā)表于 2018-12-10 14:14 | 只看該作者
多謝樓主分享資料
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表