找回密码
 立即注册

QQ登录

只需一步,快速开始

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

LPC1788 SPI主从机代码

[复制链接]
跳转到指定楼层
楼主
在做LPC1788开发板的主从机开发,分享下代码

单片机源程序如下:
  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. }
复制代码

所有资料51hei提供下载:
AN11257.zip (637.92 KB, 下载次数: 11)


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

使用道具 举报

沙发
ID:6656 发表于 2018-12-10 14:14 | 只看该作者
多谢楼主分享资料
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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