本文主要是介绍5050 RGB8路LED灯驱动(IO驱动,STM32),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、前期准备
单片机:STM32F103ZET6
开发环境:MDK5.14
库函数:标准库V3.5
RGB LED模块:淘宝有售
二、实验效果
8种颜色的LED流水灯,分配见下表,0代表Disable,1代表Enable
R G B
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
PWM驱动方式
三、驱动原理
模块上面的RGB引脚低电平有效,LED灯IO口也是低电平有效。
需要完整工程或者有问题的请加QQ:1002521871,验证:呵呵。
四、驱动代码
led.h
#ifndef __LED_H__
#define __LED_H__
#include "stm32f10x.h"
#include "gpio.h"#define LED_R PEout(0)
#define LED_G PEout(1)
#define LED_B PEout(2)#define RGB_LED1 PGout(8)
#define RGB_LED2 PGout(9)
#define RGB_LED3 PGout(10)
#define RGB_LED4 PGout(11)
#define RGB_LED5 PGout(12)
#define RGB_LED6 PGout(13)
#define RGB_LED7 PGout(14)
#define RGB_LED8 PGout(15)extern void LEDConfiguration(void);
#endif
led.C
#include "led.h"void LEDConfiguration(void)
{ GPIO_InitTypeDef GPIO;//Enable APB2 BusRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOG, ENABLE);//Register IO GPIO.GPIO_Pin = 0x07;GPIO.GPIO_Speed = GPIO_Speed_50MHz;GPIO.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOE, &GPIO);GPIO.GPIO_Pin = 0xff00;GPIO.GPIO_Mode = GPIO_Mode_Out_PP;GPIO.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOG, &GPIO);LED_R = OFF; LED_G = OFF; LED_B = OFF; RGB_LED1 = OFF; RGB_LED2 = OFF;RGB_LED3 = OFF; RGB_LED4 = OFF;RGB_LED5 = OFF; RGB_LED6 = OFF;RGB_LED7 = OFF; RGB_LED8 = OFF;
}
APP.c
#include "app.h"void ShowRGBLEDs(void)
{uint8_t i = 0;uint8_t times = 50;//RGB : 001LED_R = OFF;LED_G = OFF;LED_B = ON;for (i = 8; i < 16; i ++){GPIOG->ODR = 0xff00 & (~(1 << i));DelayMs(times);}DelayMs(times);//RGB : 010LED_R = OFF;LED_G = ON;LED_B = OFF;for (i = 8; i < 16; i ++){GPIOG->ODR = 0xff00 & (~(1 << i));DelayMs(times);}DelayMs(times);//RGB : 011LED_R = OFF;LED_G = ON;LED_B = ON;for (i = 8; i < 16; i ++){GPIOG->ODR = 0xff00 & (~(1 << i));DelayMs(times);}DelayMs(times);//RGB : 100LED_R = ON;LED_G = OFF;LED_B = OFF;for (i = 8; i < 16; i ++){GPIOG->ODR = 0xff00 & (~(1 << i));DelayMs(times);}DelayMs(times);//RGB : 101LED_R = ON;LED_G = OFF;LED_B = ON;for (i = 8; i < 16; i ++){GPIOG->ODR = 0xff00 & (~(1 << i));DelayMs(times);}DelayMs(times);//RGB : 110LED_R = ON;LED_G = ON;LED_B = OFF;for (i = 8; i < 16; i ++){GPIOG->ODR = 0xff00 & (~(1 << i));DelayMs(times);}DelayMs(times);//RGB : 111LED_R = ON;LED_G = ON;LED_B = ON;for (i = 8; i < 16; i ++){GPIOG->ODR = 0xff00 & (~(1 << i));DelayMs(times);}DelayMs(times);
}
由于作者能力有限,有不妥之处欢迎指正,邮箱alex_hua@foxmail.com
这篇关于5050 RGB8路LED灯驱动(IO驱动,STM32)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!