库函数与寄存器IO初始化及点灯
时间:10-02
整理:3721RD
点击:
我是纯玩STM32的,第一次接触LPC的板子,经过这些天的学习发现,LPC的初始化方式跟STM32大同小异,毕竟都是m4内核么,但是细节实现又有很多不同,下面把LPC的初始化方式简单介绍下:控制LED应将IO口配置为输出模式。
第一步,时钟初始化:
CLOCK_EnableClock(kCLOCK_Gpio0);//这里LED对应引脚为0.15
首先是寄存器方式:
port是端口号,pin是引脚号
- void myled_reg_init(uint32_t port, uint32_t pin)
- {
- GPIO->DIR[port] |= 1U << pin;
- GPIO->B[port][pin] = 1;
- IOCON->PIO[port][pin] = IOCON_MODE_PULLUP;
- }
接下来是库函数:
- void myled_init(uint32_t port, uint32_t pin)
- {
- gpio_pin_config_t config=
- {
- kGPIO_DigitalOutput,
- 1
- };
- GPIO_PinInit(GPIO, port, pin, &config);
- IOCON_PinMuxSet(IOCON, port, pin, IOCON_MODE_PULLUP);
- }
寄存器方式
引脚0.15输出低电平:GPIO->B[0][15] = 0;
引脚0.15输出高电平:
GPIO->B[0][15] = 1;
库函数方式:
output变量决定电平高低:
- GPIO_WritePinOutput(GPIO_Type *base, uint32_t port, uint32_t pin, uint8_t output)
引脚设置高电平:
- GPIO_SetPinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)
引脚设置低电平:
- GPIO_ClearPinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)
反转IO电平
- GPIO_TogglePinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)
完整的代码如下:
- /*
- * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
- *
- * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "fsl_device_registers.h"
- #include "fsl_debug_console.h"
- #include "board.h"
- #include "pin_mux.h"
- #include "fsl_common.h"
- #include "fsl_iocon.h"
- #include <stdbool.h>
- #include "app_interrupt.h"
- #include "app_led.h"
- #include "app_adc.h"
- #include "app_key.h"
- #include "app_dmic.h"
- #include "app_spiflash.h"
- #include "app_pct2075.h"
- #include "app_wm8904.h"
- #include "app_usbdmsc.h"
- #include "ff.h"
- #include "diskio.h"
- #include "app_spisd.h"
- #include "fsl_dmic.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- volatile float fPCTValue;
- static FATFS g_fileSystem; /* File system object */
- const TCHAR driverNumberBuffer[3U] = {SDSPIDISK + '0', ':', '/'};
- unsigned int gvalue = 1000;
- uint16_t wADCValue = 0;
- /*******************************************************************************
- * Code
- ******************************************************************************/
- void myled_init(uint32_t port, uint32_t pin)
- {
- gpio_pin_config_t config=
- {
- kGPIO_DigitalOutput,
- 1
- };
- GPIO_PinInit(GPIO, port, pin, &config);
- IOCON_PinMuxSet(IOCON, port, pin, IOCON_MODE_PULLUP);
- }
- void myled_reg_init(uint32_t port, uint32_t pin)
- {
- GPIO->DIR[port] |= 1U << pin;
- GPIO->B[port][pin] = 1;
- IOCON->PIO[port][pin] = IOCON_MODE_PULLUP;
- }
- /*!
- * @brief Main function
- */
- int main(void)
- {
- char ch;
- uint8_t ret;
-
- /* Init board hardware. */
- /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
- CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
- BOARD_InitPins();
- BOARD_BootClockRUN();
- BOARD_InitDebugConsole();
-
- SystemCoreClockUpdate();
-
- SysTick_Config(SystemCoreClock/1000);
-
- PRINTF("\r\n-------------------------------\r\n\r\n");
- PRINTF("hello world.\r\n");
- PRINTF("LPC54110 Sys Clock is %dMhz.\r\n", SystemCoreClock/1000000);
- PRINTF("\r\n-------------------------------\r\n");
-
- CLOCK_EnableClock(kCLOCK_InputMux);
- CLOCK_EnableClock(kCLOCK_Iocon);
- CLOCK_EnableClock(kCLOCK_Gpio0);
- CLOCK_EnableClock(kCLOCK_Gpio1);
-
- // led_init();
- myled_init(0, 15);
- key_init();
- adc_init();
- while (1)
- {
- // ADC test schedule
- wADCValue = adc_read(ADC_CHANNEL_NUM);
- gvalue = wADCValue;
- for(gvalue = wADCValue;gvalue > 0;gvalue--)
- {
- uint32_t i = 0;
- for(i=1000;i>0;i--);
- }
- GPIO->B[0][15] = 0;
-
- for(gvalue = wADCValue;gvalue > 0;gvalue--)
- {
- uint32_t i = 0;
- for(i=1000;i>0;i--);
- }
- GPIO->B[0][15] = 1;
- // GPIO->NOT[0] |= 1<<15;
-
- // usbdmsc_proc();
- // __WFI();
- }
- }