软件设计
led.h
#ifndef __LED_H
#define __LED_H
#include "led.h"
#include "sys.h"
#define LED1 PBout(5)// PB5
#define LED2 PBout(0)// PB0
#define LED3 PBout(1)// PB1
#define ON 1
#define OFF 0
///* 基本混色,后面高级用法使用PWM可混出全彩颜色,且效果更好 */
//红
#define LED_RED \
{LED1=0; \
LED2=1; \
LED3=1;} \
//绿
#define LED_GREEN \
{LED1=1; \
LED2=0; \
LED3=1;}
//蓝
#define LED_BLUE \
LED1=1; \
LED2=1; \
LED3=0
//黄(红+绿)
#define LED_YELLOW \
LED1=0;\
LED2=0; \
LED3=1
//紫(红+蓝)
#define LED_PURPLE \
LED1=0;\
LED2=1; \
LED3=0
//青(绿+蓝)
#define LED_CYAN \
LED1=1;\
LED2=0 ;\
LED3=0
//白(红+绿+蓝)
#define LED_WHITE \
LED1=0;\
LED2=0; \
LED3=0
//黑(全部关闭)
#define LED_RGBOFF \
LED1=1;\
LED2=1 ;\
LED3=1
void LED_Init(void);
#endif /* __LED_H */
?led.c
#include "led.h"
#include "stm32f10x.h"
//初始化PB5和PE5为输出口.并使能这两个口的时钟
//LED IO初始化
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义一个GPIO_InitTypeDef类型的结构体
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); //开启LED相关的GPIO外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_0|GPIO_Pin_1; //选择要控制的GPIO引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置引脚模式为通用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚速率为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //调用库函数,初始化GPIO
/* 关闭所有led灯 */
GPIO_SetBits(GPIOB, GPIO_Pin_5);
GPIO_SetBits(GPIOB, GPIO_Pin_0);
GPIO_SetBits(GPIOB, GPIO_Pin_1);
}
main.c
#include "stm32f10x.h"
#include "led.h"
#define SOFT_DELAY Delay(0x0FFFFF);
void Delay(__IO u32 nCount);
int main(void)
{
/* LED 端口初始化 */
LED_Init();
while (1)
{
LED1=0;
LED1=0; // 亮
SOFT_DELAY;
LED1=1; // 灭
LED2=1; // 亮
SOFT_DELAY;
LED2=1; // 灭
LED3=0; // 亮
SOFT_DELAY;
LED3=1; // 灭
/*轮流显示 红绿蓝黄紫青白 颜色*/
LED_RED;
SOFT_DELAY;
LED_GREEN;
SOFT_DELAY;
LED_BLUE;
SOFT_DELAY;
LED_YELLOW;
SOFT_DELAY;
LED_GREEN;
SOFT_DELAY;
LED_BLUE;
SOFT_DELAY;
LED_YELLOW;
SOFT_DELAY;
LED_PURPLE;
SOFT_DELAY;
LED_CYAN;
SOFT_DELAY;
LED_WHITE;
SOFT_DELAY;
LED_RGBOFF;
SOFT_DELAY;
}
}
void Delay(__IO uint32_t nCount) //简单的延时函数
{
for(; nCount != 0; nCount--);
}