一、前期准备
单片机: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
三、驱动原理
模块上面的RGB引脚低电平有效,LED灯IO口也是低电平有效。
需要完整工程或者有问题的请加QQ:1002521871,验证:呵呵。
四、驱动代码
GPIO.h
#ifndef __GPIO_H__
#define __GPIO_H__
#include "stm32f10x.h"
#define BITBAND(addr, bitnum) \
((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
/*---------------------LED(Output) Definition------------------*/
#define LED0 PEout(6)
#define LED0_Pin GPIO_Pin_6
#define LED1 PEout(5)
#define LED1_Pin GPIO_Pin_5
#define LEDPORT GPIOE
#define LEDCLKLINE RCC_APB2Periph_GPIOE
/*---------------------KEY(Input) Definition------------------*/
#define KEY PAin(0)
#define KEY_Pin GPIO_Pin_0
#define KEYPORT GPIOA
#define KEYCLKLINE RCC_APB2Periph_GPIOA
/*---------------------Initalize Status-----------------------*/
#define ON 0
#define OFF 1
#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)
typedef enum
{
IO_RESET
} SYS_RESET;
extern void GPIOConfiguration(void);
#endif
GPIO.C
#include "gpio.h"
void SystemReset(SYS_RESET type)
{
switch(type)
{
case IO_RESET:
LED0 = OFF;
LED1 = OFF;
break;
}
}
void GPIOConfiguration(void)
{
GPIO_InitTypeDef GPIO;
//Enable APB2 Bus
RCC_APB2PeriphClockCmd(LEDCLKLINE | KEYCLKLINE | RCC_APB2Periph_GPIOG, ENABLE);
//Register IO
GPIO.GPIO_Pin = LED0_Pin | LED1_Pin | 0x07;
GPIO.GPIO_Speed = GPIO_Speed_50MHz;
GPIO.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LEDPORT, &GPIO);
GPIO.GPIO_Pin = KEY_Pin;
GPIO.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEYPORT, &GPIO);
GPIO.GPIO_Pin = 0xff00;
GPIO.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO);
//LED Initialize and all of them is turned off.
SystemReset(IO_RESET);
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]