点亮开发板的LED
1、官方下载的AN_N32G430_GCC Development Environment Application Note_V1.zip文件里
有LedBlink例子,我们在此基础上修改即可。
2、将LedBlink工程 的bsp_led.c bsp_led.h移到我们的代码,这里包含了对板载三颗LED灯的初始化以及控制。
bsp_led.c主要代码:
void LED_Initialize(GPIO_Module* GPIOx, uint16_t pin, uint32_t AHB_periph)
{
/* Define a structure of type GPIO_InitType */
GPIO_InitType GPIO_InitStructure;
/* Enable LED related GPIO peripheral clock */
RCC_AHB_Peripheral_Clock_Enable(AHB_periph);
/* Assign default value to GPIO_InitStructure structure */
GPIO_Structure_Initialize(&GPIO_InitStructure);
/* Select the GPIO pin to control */
GPIO_InitStructure.Pin = pin;
/* Set pin mode to general push-pull output */
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
/* Set the pin drive current to 4MA*/
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
/* Initialize GPIO */
GPIO_Peripheral_Initialize(GPIOx, &GPIO_InitStructure);
}
void LED_Toggle(GPIO_Module* GPIOx, uint16_t pin)
{
GPIO_Pin_Toggle(GPIOx, pin);;
}
void LED_On(GPIO_Module* GPIOx,uint16_t pin)
{
GPIO_Pins_Set(GPIOx, pin);
}
void LED_Off(GPIO_Module* GPIOx,uint16_t pin)
{
GPIO_PBC_Pins_Reset(GPIOx, pin);
}
bsp_led.h
#include "n32g430.h"
/** Define the GPIO port to which the LED is connected **/
#define LED1_GPIO_PORT GPIOA /* GPIO port */
#define LED1_GPIO_CLK RCC_AHB_PERIPH_GPIOA /* GPIO port clock */
#define LED1_GPIO_PIN GPIO_PIN_1 /* GPIO connected to the SCL clock line */
#define LED2_GPIO_PORT GPIOA /* GPIO port */
#define LED2_GPIO_CLK RCC_AHB_PERIPH_GPIOA /* GPIO port clock */
#define LED2_GPIO_PIN GPIO_PIN_7 /* GPIO connected to the SCL clock line */
#define LED3_GPIO_PORT GPIOA /* GPIO port */
#define LED3_GPIO_CLK RCC_AHB_PERIPH_GPIOA /* GPIO port clock */
#define LED3_GPIO_PIN GPIO_PIN_9 /* GPIO connected to the SCL clock line */
void LED_Initialize(GPIO_Module* GPIOx, uint16_t pin, uint32_t AHB_periph);
void LED_Toggle(GPIO_Module* GPIOx, uint16_t pin);
void LED_On(GPIO_Module* GPIOx,uint16_t pin);
void LED_Off(GPIO_Module* GPIOx,uint16_t pin);
3、main.c 添加对bsp_led.c的调用即可,在main.c里添加如下代码:
/* Initialize Led1~Led3 as output push-pull mode */
LED_Initialize(LED1_GPIO_PORT, LED1_GPIO_PIN, LED1_GPIO_CLK);
LED_Initialize(LED2_GPIO_PORT, LED2_GPIO_PIN | LED3_GPIO_PIN, LED2_GPIO_CLK);
/* Turn off Led1~Led3 */
LED_Off(LED2_GPIO_PORT, LED1_GPIO_PIN | LED2_GPIO_PIN | LED3_GPIO_PIN);
/* Turn on Led2~Led3 */
LED_On(LED2_GPIO_PORT, LED2_GPIO_PIN | LED3_GPIO_PIN);