微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 库函数与寄存器IO初始化及点灯

库函数与寄存器IO初始化及点灯

时间:10-02 整理:3721RD 点击:

我是纯玩STM32的,第一次接触LPC的板子,经过这些天的学习发现,LPC的初始化方式跟STM32大同小异,毕竟都是m4内核么,但是细节实现又有很多不同,下面把LPC的初始化方式简单介绍下:控制LED应将IO口配置为输出模式。
第一步,时钟初始化:
CLOCK_EnableClock(kCLOCK_Gpio0);//这里LED对应引脚为0.15
首先是寄存器方式:
port是端口号,pin是引脚号

  1. void myled_reg_init(uint32_t port, uint32_t pin)
  2. {
  3. GPIO->DIR[port] |= 1U << pin;
  4. GPIO->B[port][pin] = 1;
  5. IOCON->PIO[port][pin] = IOCON_MODE_PULLUP;
  6. }

复制代码



接下来是库函数:

  1. void myled_init(uint32_t port, uint32_t pin)
  2. {
  3.     gpio_pin_config_t config=
  4.     {
  5.             kGPIO_DigitalOutput,
  6.             1
  7.     };
  8.     GPIO_PinInit(GPIO, port, pin, &config);
  9.     IOCON_PinMuxSet(IOCON, port, pin, IOCON_MODE_PULLUP);
  10. }

复制代码

控制引脚电平:
寄存器方式
引脚0.15输出低电平:GPIO->B[0][15] = 0;
引脚0.15输出高电平:
GPIO->B[0][15] = 1;

库函数方式:
output变量决定电平高低:

  1. GPIO_WritePinOutput(GPIO_Type *base, uint32_t port, uint32_t pin, uint8_t output)

复制代码


引脚设置高电平:

  1. GPIO_SetPinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)

复制代码


引脚设置低电平:

  1. GPIO_ClearPinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)

复制代码


反转IO电平

  1. GPIO_TogglePinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)

复制代码


完整的代码如下:

  1. /*
  2. * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. *   of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. *   list of conditions and the following disclaimer in the documentation and/or
  13. *   other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. *   contributors may be used to endorse or promote products derived from this
  17. *   software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */

  30. #include "fsl_device_registers.h"
  31. #include "fsl_debug_console.h"
  32. #include "board.h"

  33. #include "pin_mux.h"

  34. #include "fsl_common.h"
  35. #include "fsl_iocon.h"

  36. #include <stdbool.h>


  37. #include "app_interrupt.h"
  38. #include "app_led.h"
  39. #include "app_adc.h"
  40. #include "app_key.h"
  41. #include "app_dmic.h"
  42. #include "app_spiflash.h"
  43. #include "app_pct2075.h"
  44. #include "app_wm8904.h"
  45. #include "app_usbdmsc.h"
  46. #include "ff.h"
  47. #include "diskio.h"
  48. #include "app_spisd.h"

  49. #include "fsl_dmic.h"

  50. /*******************************************************************************
  51. * Definitions
  52. ******************************************************************************/


  53. /*******************************************************************************
  54. * Prototypes
  55. ******************************************************************************/
  56. volatile float fPCTValue;
  57. static FATFS g_fileSystem; /* File system object */
  58. const TCHAR driverNumberBuffer[3U] = {SDSPIDISK + '0', ':', '/'};
  59. unsigned int gvalue = 1000;
  60. uint16_t wADCValue = 0;

  61. /*******************************************************************************
  62. * Code
  63. ******************************************************************************/
  64. void myled_init(uint32_t port, uint32_t pin)
  65. {
  66.     gpio_pin_config_t config=
  67.     {
  68.             kGPIO_DigitalOutput,
  69.             1
  70.     };
  71.     GPIO_PinInit(GPIO, port, pin, &config);
  72.     IOCON_PinMuxSet(IOCON, port, pin, IOCON_MODE_PULLUP);
  73. }

  74. void myled_reg_init(uint32_t port, uint32_t pin)
  75. {
  76.         GPIO->DIR[port] |= 1U << pin;
  77.         GPIO->B[port][pin] = 1;
  78.     IOCON->PIO[port][pin] = IOCON_MODE_PULLUP;
  79. }


  80. /*!
  81. * @brief Main function
  82. */
  83. int main(void)
  84. {
  85.         char ch;

  86.         uint8_t ret;
  87.         
  88.         /* Init board hardware.  */
  89.         /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
  90.         CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

  91.         BOARD_InitPins();
  92.         BOARD_BootClockRUN();
  93.         BOARD_InitDebugConsole();
  94.         
  95.         SystemCoreClockUpdate();
  96.         
  97.         SysTick_Config(SystemCoreClock/1000);
  98.         
  99.         PRINTF("\r\n-------------------------------\r\n\r\n");
  100.         PRINTF("hello world.\r\n");
  101.         PRINTF("LPC54110 Sys Clock is %dMhz.\r\n", SystemCoreClock/1000000);
  102.         PRINTF("\r\n-------------------------------\r\n");
  103.         
  104.         CLOCK_EnableClock(kCLOCK_InputMux);
  105.         CLOCK_EnableClock(kCLOCK_Iocon);
  106.         CLOCK_EnableClock(kCLOCK_Gpio0);
  107.         CLOCK_EnableClock(kCLOCK_Gpio1);
  108.         
  109.         // led_init();
  110.         myled_init(0, 15);
  111.         key_init();
  112.         adc_init();

  113.         while (1)
  114.         {
  115. // ADC test schedule
  116.                 wADCValue = adc_read(ADC_CHANNEL_NUM);
  117.                 gvalue = wADCValue;

  118.        for(gvalue = wADCValue;gvalue > 0;gvalue--)
  119.        {
  120.            uint32_t i = 0;
  121.            for(i=1000;i>0;i--);
  122.        }

  123.                    GPIO->B[0][15] = 0;
  124.         
  125.         for(gvalue = wADCValue;gvalue > 0;gvalue--)
  126.         {
  127.             uint32_t i = 0;
  128.             for(i=1000;i>0;i--);
  129.         }

  130.                    GPIO->B[0][15] = 1;
  131.             // GPIO->NOT[0] |= 1<<15;
  132.                
  133. //                usbdmsc_proc();
  134. //                __WFI();
  135.         }
  136. }

复制代码





Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top