当前位置:网站首页>野火stm32霸道,通过固件库实现流水灯
野火stm32霸道,通过固件库实现流水灯
2022-07-24 14:47:00 【香樟树叶�】
本文运用了三个方法实现红绿蓝三个灯的依次点亮
方法1
通过控制每一个GPIO开关实现流水灯
main.c
int main(void)
{
LEC_GPIO_Config();//配置led端口的初始化
while(1)
{
//控制GPIO开关实现流水灯
LED_G(ON); //开绿灯
LED_R(OFF); //关红灯
LED_B(OFF); //关蓝灯
delay_ms(1000);//软件延时
LED_R(ON);
LED_G(OFF);
LED_B(OFF);
delay_ms(1000);
LED_B(ON);
LED_R(OFF);
LED_G(OFF);
delay_ms(1000);
}
}bsp_led.c
这里的配置有两个方法(1,逐个写入 2,通过|运算)
#if 0:这段程序不执行
#if 1:执行程序
void LEC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(LED_G_CLK|LED_B_CLK|LED_R_CLK, ENABLE);
//配置方法1 逐个写入方法
#if 0
GPIO_InitStruct.GPIO_Pin=(LED_G_GPIO_PIN);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//配置,但是还没有写入
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=LED_B_GPIO_PIN;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=LED_R_GPIO_PIN;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
#endif
//配置方法2 通过 | 运算
#if 1
GPIO_InitStruct.GPIO_Pin=(LED_G_GPIO_PIN|LED_B_GPIO_PIN|LED_R_GPIO_PIN);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//配置,但是还没有写入
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
#endif
//流水灯配置:如果要用多个GPIO并且在一个Port中(例如霸道的灯),需要挨个把pin初始化,也就是写进寄存器,如果只是最后再写GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);就会发现只亮红灯
//因为第一次绿灯的写进结构体后没有写进CRL寄存器中,如果紧接着蓝灯的pin脚,就会覆盖掉绿灯。实现方法有两个,一个是挨个写,绿灯写进结构体后用init写进寄存器。另一个是
//绿灯|蓝灯|红灯
}bsp_led.h
#define ON 1
#define OFF 0
#define LED_G(a) if(a) GPIO_ResetBits(LED_GPIO_PORT, LED_G_GPIO_PIN); else GPIO_SetBits(LED_GPIO_PORT, LED_G_GPIO_PIN);
#define LED_R(a) if(a) GPIO_ResetBits(LED_GPIO_PORT, LED_R_GPIO_PIN); else GPIO_SetBits(LED_GPIO_PORT, LED_R_GPIO_PIN);
#define LED_B(a) if(a) GPIO_ResetBits(LED_GPIO_PORT, LED_B_GPIO_PIN); else GPIO_SetBits(LED_GPIO_PORT, LED_B_GPIO_PIN);方法2
通过使用函数调用实现流水灯
main.c
int main(void)
{
LEC_GPIO_Config();//配置led端口的初始化
while(1)
{
//使用函数调用实现流水灯
LED_GREEN();
delay_ms(1000);
LED_RED();
delay_ms(1000);
LED_BLUE();
delay_ms(1000);
}
}
bsp_led.c
void LEC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(LED_G_CLK|LED_B_CLK|LED_R_CLK, ENABLE);
//配置方法1 逐个写入方法
#if 0
GPIO_InitStruct.GPIO_Pin=(LED_G_GPIO_PIN);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//以上都配置完成,但是还没有写入CRL寄存器
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=LED_B_GPIO_PIN;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=LED_R_GPIO_PIN;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
#endif
//配置方法2 通过 | 运算
#if 1
GPIO_InitStruct.GPIO_Pin=(LED_G_GPIO_PIN|LED_B_GPIO_PIN|LED_R_GPIO_PIN);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//配置,但是还没有写入
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
#endif
//流水灯配置:如果要用多个GPIO并且在一个Port中(例如霸道的灯),需要挨个把pin初始化,也就是写进寄存器,如果只是最后再写GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);就会发现只亮红灯
//因为第一次绿灯的写进结构体后没有写进CRL寄存器中,如果紧接着蓝灯的pin脚,就会覆盖掉绿灯。实现方法有两个,一个是挨个写,绿灯写进结构体后用init写进寄存器。另一个是
//绿灯|蓝灯|红灯
}
#if 1
//使用函数调用实现流水灯
void LED_GREEN()//打开绿灯
{
LED_G_ON;
LED_R_OFF;
LED_B_OFF;
}
void LED_RED()//打开红灯
{
LED_R_ON;
LED_G_OFF;
LED_B_OFF;
}
void LED_BLUE()//打开蓝灯
{
LED_B_ON;
LED_R_OFF;
LED_G_OFF;
}
#endifbsp_led.h
//使用函数调用实现流水灯
//LED GREEN
#define LED_G_ON GPIO_ResetBits(LED_GPIO_PORT, LED_G_GPIO_PIN)//打开绿灯
#define LED_G_OFF GPIO_SetBits(LED_GPIO_PORT, LED_G_GPIO_PIN)//关闭绿灯
//LED RED
#define LED_R_ON GPIO_ResetBits(LED_GPIO_PORT, LED_R_GPIO_PIN)
#define LED_R_OFF GPIO_SetBits(LED_GPIO_PORT, LED_R_GPIO_PIN)
//LED BLUE
#define LED_B_ON GPIO_ResetBits(LED_GPIO_PORT, LED_B_GPIO_PIN)
#define LED_B_OFF GPIO_SetBits(LED_GPIO_PORT, LED_B_GPIO_PIN)
void LEC_GPIO_Config(void);
void LED_GREEN(void);
void LED_RED(void);
void LED_BLUE(void);方法3
使用宏定义实现流水灯
main.c
int main(void)
{
LEC_GPIO_Config();//配置led端口的初始化
while(1)
{
//使用宏定义实现流水灯
LED_G;
delay_ms(1000);
LED_R;
delay_ms(1000);
LED_B;
delay_ms(1000);
}
}
bsp_led.c
void LEC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(LED_G_CLK|LED_B_CLK|LED_R_CLK, ENABLE);
//配置方法1 逐个写入方法
#if 0
GPIO_InitStruct.GPIO_Pin=(LED_G_GPIO_PIN);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//配置,但是还没有写入
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=LED_B_GPIO_PIN;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=LED_R_GPIO_PIN;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
#endif
//配置方法2 通过 | 运算
#if 1
GPIO_InitStruct.GPIO_Pin=(LED_G_GPIO_PIN|LED_B_GPIO_PIN|LED_R_GPIO_PIN);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//配置,但是还没有写入
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
#endif
//流水灯配置:如果要用多个GPIO并且在一个Port中(例如霸道的灯),需要挨个把pin初始化,也就是写进寄存器,如果只是最后再写GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);就会发现只亮红灯
//因为第一次绿灯的写进结构体后没有写进CRL寄存器中,如果紧接着蓝灯的pin脚,就会覆盖掉绿灯。实现方法有两个,一个是挨个写,绿灯写进结构体后用init写进寄存器。另一个是
//绿灯|蓝灯|红灯
}bsp_led.h
\:续行符,续行符后面不可有任何符号
//使用宏定义实现流水灯
//LED GREEN
#define LED_G_ON GPIO_ResetBits(LED_GPIO_PORT, LED_G_GPIO_PIN)//打开绿灯
#define LED_G_OFF GPIO_SetBits(LED_GPIO_PORT, LED_G_GPIO_PIN)//关闭绿灯
//LED RED
#define LED_R_ON GPIO_ResetBits(LED_GPIO_PORT, LED_R_GPIO_PIN)
#define LED_R_OFF GPIO_SetBits(LED_GPIO_PORT, LED_R_GPIO_PIN)
//LED BLUE
#define LED_B_ON GPIO_ResetBits(LED_GPIO_PORT, LED_B_GPIO_PIN)
#define LED_B_OFF GPIO_SetBits(LED_GPIO_PORT, LED_B_GPIO_PIN)
#define LED_G LED_G_ON;\
LED_R_OFF;\
LED_B_OFF;
#define LED_R LED_R_ON;\
LED_G_OFF;\
LED_B_OFF;
#define LED_B LED_B_ON;\
LED_R_OFF;\
LED_G_OFF;大家可以举一反三,因为霸道是RGB灯,所以可以实现多种颜色的灯的点亮
实现stm32霸道流水灯可以用以上的三个方法,大家更喜欢哪种方法呢。
如果有更直观便捷的方法,欢迎下方评论
本人初学stm32,以上代码有任何问题欢迎指正提点。
编写不易,如果帮到了你欢迎点赞支持!!
边栏推荐
- 2.4. properties of special profile
- onBlur和onChange冲突解决方法
- XOR procedure
- 交换
- Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
- Introduction to Xiaoxiong school
- Spark: specify the date and output the log of the corresponding date (entry level - simple implementation)
- Moving the mouse into select options will trigger the mouseleave event processing scheme
- JS judge whether it is an integer
- Spark Learning Notes (III) -- basic knowledge of spark core
猜你喜欢
![Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022](/img/fd/c7bff1ce199e8b600761d77828c674.png)
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022

Unity 委托 (Delegate) 的简单理解以及实现

Spark Learning Notes (III) -- basic knowledge of spark core

LeetCode·每日一题·1184.公交站间的距离·模拟

使用 Fiddler Hook 报错:502 Fiddler - Connection Failed

Grpc middleware implements grpc call retry

Similarities and differences between nor flash and NAND flash

Rest style

ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测

北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%
随机推荐
[oauth2] II. Known changes in oauth2.1
The difference and relation among list, set and map
Production environment tidb cluster capacity reduction tikv operation steps
Leetcode · daily question · 1184. distance between bus stops · simulation
Operation of MySQL Library
Tiger mouth waterfall: Tongliang version of xiaohukou waterfall
Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
Caffe framework and production data source for deep learning
看完这篇文章,才发现我的测试用例写的就是垃圾
LeetCode·每日一题·1184.公交站间的距离·模拟
C language -- program environment and preprocessing
The vs compiled application is missing DLL
Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
Must use destructuring props assignmenteslint
小熊派 课程导读
Activate the newly installed Anaconda in the server
Beijing all in one card listed and sold 68.45% of its equity at 352.888529 million yuan, with a premium rate of 84%
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布
Time series of machine learning