重玩LPC1768——GPIO实现
LPC1768芯片同其它Cortex-M3芯片类似,引脚也提供较为灵活的多路复用功能,粗略的看了一下,基本上每个引脚都有4个复用功能(含本身GPIO),默认为GPIO功能,且带有上拉电阻。
我们拿来做GPIO实验的引脚是P0.7,根据原理图,我们需要将P0.7配置为推挽输出。
本实验仍然使用LPCopen库,应用库中的gpio_17xx_40xx.c/.h与iocon_17xx_40xx.c/.h两组文件。在库的包装下,配置流程也相对简单,直接上代码:
- void heartbeat_init(void)
- {
- Chip_GPIO_Init(LPC_GPIO);
- Chip_IOCON_PinMux(LPC_IOCON, 0, 7, IOCON_MODE_PULLUP, IOCON_FUNC0);
- Chip_GPIO_WriteDirBit(LPC_GPIO, 0, 7, true);
- Chip_GPIO_SetPortOutLow(LPC_GPIO, 0, 7);
- }
复制代码
在配合systick的实验,我们实现1Hz下的LED灯闪烁实验。
附bspgpio.c源代码
- /**
- ******************************************************************************
- * @file bspgpio.c
- * @author jobs
- * @version v0.00
- * @date 2020-08-29
- * @brief
- * @note
- *
- ******************************************************************************
- */
- #include "bspgpio.h"
- #include "chip.h"
- void heartbeat_init(void)
- {
- Chip_GPIO_Init(LPC_GPIO);
- Chip_IOCON_PinMux(LPC_IOCON, 0, 7, IOCON_MODE_PULLUP, IOCON_FUNC0);
- Chip_GPIO_WriteDirBit(LPC_GPIO, 0, 7, true);
- Chip_GPIO_SetPortOutLow(LPC_GPIO, 0, 7);
- }
- void heartbeat_blink(void)
- {
- Chip_GPIO_SetPinToggle(LPC_GPIO, 0, 7);
- }
- /******************************** END OF FILE *********************************/
复制代码附main.c源代码 - /**
- ******************************************************************************
- * @file main.c
- * @author jobs
- * @version v0.00
- * @date 2020-08-23
- * @brief
- * @note
- *
- ******************************************************************************
- */
- #include <stdio.h>
- #include <stdint.h>
- #include "main.h"
- #include "chip.h"
- #include "bspgpio.h"
- const uint32_t OscRateIn = 12000000;
- const uint32_t RTCOscRateIn = 32768;
- volatile uint32_t time_line_ms = 0;
- int32_t heartbeat_cycle = 0;
- void main(void)
- {
- Chip_SetupXtalClocking();
- SystemCoreClockUpdate();
- SysTick_Config(SystemCoreClock / 1000);
- heartbeat_init();
- heartbeat_cycle = 500;
- while(1)
- {
- if(heartbeat_cycle == 0)
- {
- heartbeat_cycle = 500;
- heartbeat_blink();
- }
- }
- }
- void SysTick_Handler(void)
- {
- time_line_ms++;
- if(heartbeat_cycle > 0)
- {
- heartbeat_cycle--;
- }
- }
- /******************************** END OF FILE *********************************/
复制代码
|