智能小车实训之STM32 -- 点亮LED灯
原理图:
实现代码
light.h
#ifndef __LIGHT_H
#define __LIGHT_H
#include <sys.h>
#define LED1 PDout(0)
#define LED2 PDout(1)
#define LED3 PDout(3)
void LED_init(void);
#endif
light.c
#include "sys.h"
#include "light.h"
void LED_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);//´ò¿ªÊ±ÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//Êä³ö¹¦ÄÜ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊäÈë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯D¶Ë¿Ú
}
main.c
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "light.h"
int main(void)
{
delay_init(168);
LED_init();
LED1 = 0;
LED2 = 1;
LED3 = 0;
while(1)
{
LED1 = ~LED1;
LED2 = ~LED2;
LED3 = ~LED3;
delay_ms(1000);
}
return 0;
}
效果LED1和LED3同步每个1秒闪烁,同时LED2交替闪烁(LED在液晶屏右下方)