5050 RGB8路LED灯驱动(IO驱动,STM32)

一、前期准备
单片机:STM32F103ZET6
开发环境:MDK5.14
库函数:标准库V3.5
RGB LED模块:淘宝有售
5050 RGB8路LED灯驱动(IO驱动,STM32)_第1张图片

二、实验效果
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 Bus
    RCC_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 : 001
	LED_R = OFF;
	LED_G = OFF;
	LED_B = ON;
	for (i = 8; i < 16; i ++)
	{
		GPIOG->ODR = 0xff00 & (~(1 << i));
		DelayMs(times);
	}
	DelayMs(times);
	
	//RGB : 010
	LED_R = OFF;
	LED_G = ON;
	LED_B = OFF;
	for (i = 8; i < 16; i ++)
	{
		GPIOG->ODR = 0xff00 & (~(1 << i));
		DelayMs(times);
	}
	DelayMs(times);
	
	//RGB : 011
	LED_R = OFF;
	LED_G = ON;
	LED_B = ON;
	for (i = 8; i < 16; i ++)
	{
		GPIOG->ODR = 0xff00 & (~(1 << i));
		DelayMs(times);
	}
	DelayMs(times);
	
	//RGB : 100
	LED_R = ON;
	LED_G = OFF;
	LED_B = OFF;
	for (i = 8; i < 16; i ++)
	{
		GPIOG->ODR = 0xff00 & (~(1 << i));
		DelayMs(times);
	}
	DelayMs(times);
	
	//RGB : 101
	LED_R = ON;
	LED_G = OFF;
	LED_B = ON;
	for (i = 8; i < 16; i ++)
	{
		GPIOG->ODR = 0xff00 & (~(1 << i));
		DelayMs(times);
	}
	DelayMs(times);
	
	//RGB : 110
	LED_R = ON;
	LED_G = ON;
	LED_B = OFF;
	for (i = 8; i < 16; i ++)
	{
		GPIOG->ODR = 0xff00 & (~(1 << i));
		DelayMs(times);
	}
	DelayMs(times);
	
	//RGB : 111
	LED_R = ON;
	LED_G = ON;
	LED_B = ON;
	for (i = 8; i < 16; i ++)
	{
		GPIOG->ODR = 0xff00 & (~(1 << i));
		DelayMs(times);
	}
	DelayMs(times);
}

由于作者能力有限,有不妥之处欢迎指正,邮箱[email protected]

你可能感兴趣的:(嵌入式常用模块驱动)