# LED lighting program

# 设计要求

  • 用按键控制 LED 发光类型
  • 按 KEY1,控制 LED 灯在 “红光 - 绿光 - 蓝光 - 白光” 四种方式之间切换
  • 按 KEY2,控制 LED 灯熄灭

# 设计思路

# \Hardware\beep

# beep.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "beep.h"


void BEEP_Init(void)
{
//实例化配置GPIO模式和速度结构体
GPIO_InitTypeDef GPIO_InitStruct;

//使能蜂鸣器时钟
RCC_APB2PeriphClockCmd(BEEP_CLK, ENABLE );

//配置端口输出并初始化
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = BEEP_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( BEEP_PORT, &GPIO_InitStruct);

//蜂鸣器初始化后不响
BEEP(0);
}

# beep.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef __BEEP_H
#define __BEEP_H

#include "stm32f10x.h"

//定义蜂鸣器端口、引脚和时钟,对应PA8
#define BEEP_PIN GPIO_Pin_8
#define BEEP_PORT GPIOA
#define BEEP_CLK RCC_APB2Periph_GPIOA

//BEEP(ON/OFF)控制蜂鸣器开关
#define ON 1
#define OFF 0
#define BEEP(x) if (x) GPIO_SetBits(BEEP_PORT,BEEP_PIN);\
else GPIO_ResetBits( BEEP_PORT,BEEP_PIN);

//声明蜂鸣器初始化函数
void BEEP_Init(void);
#endif

# \Project

# Delay.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stm32f10x.h"

/**
* @brief ?????
* @param xus ????,??:0~233015
* @retval ?
*/
void Delay_us(uint32_t xus)
{
SysTick->LOAD = 72 * xus; //????????
SysTick->VAL = 0x00; //???????
SysTick->CTRL = 0x00000005; //??????HCLK,?????
while(!(SysTick->CTRL & 0x00010000)); //?????0
SysTick->CTRL = 0x00000004; //?????
}

/**
* @brief ?????
* @param xms ????,??:0~4294967295
* @retval ?
*/
void Delay_ms(uint32_t xms)
{
while(xms--)
{
Delay_us(1000);
}
}

/**
* @brief ????
* @param xs ????,??:0~4294967295
* @retval ?
*/
void Delay_s(uint32_t xs)
{
while(xs--)
{
Delay_ms(1000);
}
}

# Dlay.h

1
2
3
4
5
6
7
8
#ifndef __DELAY_H
#define __DELAY_H

void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);

#endif

# newstart.c

1
2
3
4
5
6
7
8
9
10
11
#include "stm32f10x.h"

sbit FM = PA8;

void main()
{
while (1){
FM=1;//1.08us
FM=0;
}
}

# main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "bsp_led.h"

uint8_t Key_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
if (GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON)
{
while (GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON)//scan whether KEY_ON has been pushed
{
return KEY_ON;
}
}
else
{
return KEY_OFF;
}
}

void Delay(__IO u32 nCount);

void LED_Color(uint8_t color)
{
switch (color) {
case 0:
LED_RED;
break;
case 1:
LED_GREEN;
break;
case 2:
LED_BLUE;
break;
case 3:
LED_WHITE;
break;
}
}

int main(void)
{
LED_GPIO_Config();

uint8_t color = -1; // 0: Red, 1: Green, 2: Blue, 3: White
uint8_t prev_color = -1; // Store the previous color for KEY2 functionality

ff: while (1) {
if (Key_Scan(KEY1_GPIO_PORT, KEY1_GPIO_PIN) == KEY_ON) {

//Detect KEY_ Is ON turned on to prevent KEY_ When ON is turned on, the light changes. Wait until the KEY1 key is pressed and bounces before changing the color
while (Key_Scan(KEY1_GPIO_PORT, KEY1_GPIO_PIN) == KEY_ON) {
}

prev_color = color;

color = (color + 1) % 4;// use mod to simplize this project

LED_Color(color);
}

if (Key_Scan(KEY2_GPIO_PORT, KEY2_GPIO_PIN) == KEY_ON) {
while (Key_Scan(KEY2_GPIO_PORT, KEY2_GPIO_PIN) == KEY_ON) {
}
LED_RGBOFF;//Turn off all lights

goto ff;//Enter the ff function and rerun
}
}
}

# bsp_led.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include "bsp_led.h"

void LED_GPIO_Config(void)
{
/*¶¨ÒåÒ»¸öGPIO_InitTypeDefÀàÐ͵ĽṹÌå*/
GPIO_InitTypeDef GPIO_InitStructure;

/*¿ªÆôLED and KEYÏà¹ØµÄGPIOÍâÉèʱÖÓ*/
RCC_APB2PeriphClockCmd( LED1_GPIO_CLK | LED2_GPIO_CLK | LED3_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK | KEY2_GPIO_CLK | LED_GPIO_CLK, ENABLE);

/*Ñ¡ÔñÒª¿ØÖƵÄGPIOÒý½Å*/
GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN;

/*ÉèÖÃÒý½ÅģʽΪͨÓÃÍÆÍìÊä³ö*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

/*ÉèÖÃÒý½ÅËÙÂÊΪ50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

/*µ÷Óÿ⺯Êý£¬³õʼ»¯GPIO*/
GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);

//Green
GPIO_InitStructure.GPIO_Pin = LED2_GPIO_PIN;
GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);

//Blue
GPIO_InitStructure.GPIO_Pin = LED3_GPIO_PIN;
GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStructure);

/* ¹Ø±ÕËùÓÐledµÆ */
GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN);
GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PIN);
GPIO_SetBits(LED3_GPIO_PORT, LED3_GPIO_PIN);
}

# bsp_led.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef __LED_H
#define __LED_H

#include "stm32f10x.h"

// define key1
#define KEY1_GPIO_CLK RCC_APB2Periph_GPIOA
#define KEY1_GPIO_PORT GPIOA
#define KEY1_GPIO_PIN GPIO_Pin_0

// define key2
#define KEY2_GPIO_CLK RCC_APB2Periph_GPIOC
#define KEY2_GPIO_PORT GPIOC
#define KEY2_GPIO_PIN GPIO_Pin_13

#define LED_GPIO_CLK RCC_APB2Periph_GPIOB
#define LED_GPIO_PORT GPIOB
// R-ºìÉ«
#define LED1_GPIO_PORT GPIOB /* GPIO¶Ë¿Ú */
#define LED1_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO¶Ë¿ÚʱÖÓ */
#define LED1_GPIO_PIN GPIO_Pin_5 /* Á¬½Óµ½SCLʱÖÓÏßµÄGPIO */

// G-ÂÌÉ«
#define LED2_GPIO_PORT GPIOB /* GPIO¶Ë¿Ú */
#define LED2_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO¶Ë¿ÚʱÖÓ */
#define LED2_GPIO_PIN GPIO_Pin_0 /* Á¬½Óµ½SCLʱÖÓÏßµÄGPIO */

// B-À¶É«
#define LED3_GPIO_PORT GPIOB /* GPIO¶Ë¿Ú */
#define LED3_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO¶Ë¿ÚʱÖÓ */
#define LED3_GPIO_PIN GPIO_Pin_1 /* Á¬½Óµ½SCLʱÖÓÏßµÄGPIO */


/** the macro definition to trigger the led on or off
* 1 - off
*0 - on
*/
#define ON 0
#define OFF 1
#define KEY_ON 1
#define KEY_OFF 0

/* ʹÓñê×¼µÄ¹Ì¼þ¿â¿ØÖÆIO*/
#define LED1(a) if (a) \
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);\
else \
GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN)

#define LED2(a) if (a) \
GPIO_SetBits(LED2_GPIO_PORT,LED2_GPIO_PIN);\
else \
GPIO_ResetBits(LED2_GPIO_PORT,LED2_GPIO_PIN)

#define LED3(a) if (a) \
GPIO_SetBits(LED3_GPIO_PORT,LED3_GPIO_PIN);\
else \
GPIO_ResetBits(LED3_GPIO_PORT,LED3_GPIO_PIN)


/* Ö±½Ó²Ù×÷¼Ä´æÆ÷µÄ·½·¨¿ØÖÆIO */
#define digitalHi(p,i) {p->BSRR=i;} //Êä³öΪ¸ßµçƽ
#define digitalLo(p,i) {p->BRR=i;} //Êä³öµÍµçƽ
#define digitalToggle(p,i) {p->ODR ^=i;} //Êä³ö·´×ª×´Ì¬


/* ¶¨Òå¿ØÖÆIOµÄºê */
#define LED1_OFF digitalHi(LED1_GPIO_PORT,LED1_GPIO_PIN)
#define LED1_ON digitalLo(LED1_GPIO_PORT,LED1_GPIO_PIN)

#define LED2_OFF digitalHi(LED2_GPIO_PORT,LED2_GPIO_PIN)
#define LED2_ON digitalLo(LED2_GPIO_PORT,LED2_GPIO_PIN)

#define LED3_OFF digitalHi(LED3_GPIO_PORT,LED3_GPIO_PIN)
#define LED3_ON digitalLo(LED3_GPIO_PORT,LED3_GPIO_PIN)

//ºì
#define LED_RED \
LED1_ON;\
LED2_OFF\
LED3_OFF

//ÂÌ
#define LED_GREEN \
LED1_OFF;\
LED2_ON\
LED3_OFF

//˦
#define LED_BLUE \
LED1_OFF;\
LED2_OFF\
LED3_ON

//°×(ºì+ÂÌ+À¶)
#define LED_WHITE \
LED1_ON;\
LED2_ON\
LED3_ON

//ºÚ(È«²¿¹Ø±Õ)
#define LED_RGBOFF \
LED1_OFF;\
LED2_OFF\
LED3_OFF

void LED_GPIO_Config(void);

#endif