RGB全彩LED——树莓派基于C语言教程


三色全彩LED模块
  1. RGB LED模块由一个贴片全彩LED制成,通过R、G、B三个引脚的PWM电压输入可以调节三种基色(红/蓝/绿)的强度从而实现全彩的混色效果。用Arduino对模块的控制可实现酷炫的灯光效果。
原理图
  1. 产品特性:
    1、使用 5050 全彩 LED
    2、RGB 三基色接限流电阻防止烧坏
    3、通过 PWM 调节三基色可混合得到不同的颜色
    4、可与各种单片机接口
    5、工作电压:5V
    6、LED 驱动模式:共阴驱动
  2. 调用头文件,wiringPi.h定义了对wiringPi相应引脚的API函数,softPwm.h定义了软件实现PWM函数
  1. 配置LED模块引脚
  1. 定义一个存储RGB数值的数组colors[],共有15个数值对应不同的LED模块发光颜色
int colors[] = {0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0x00ffff,
                 0xff00ff, 0xffffff, 0xffb6c1, 0x4b0082, 0x00008b,
                 0x00bfff, 0x008000, 0x8b4513, 0xd8bfd8, 0x4b0082};
  1. 定义LED模块初始化函数,配置相应引脚的模式void RGB_Color_Init()
使用软件实现pwm的方式,所以使用该API配置引脚功能:
int softPwmCreate (int pin, int initialValue, int pwmRange)
pin:对应wiringPi引脚编号
initialValue:引脚输出的初始值
pwmRange:PWM值的范围上限,推荐100
  1. 对于main函数,需先调用int wiringPiSetup (void)函数来初始化树莓派引脚,若该函数返回值为-1则说明初始化失败
  1. 对于给LED模块的RGB相应引脚赋值,使用位操作方式
Red_color = (color & 0xff0000) >> 16;  该位操作是给Red_Pin引脚赋值,相应的其它引脚也类似,赋值完之后,并通过void softPwmWrite (int pin, int value)函数将值写入引脚
Pin:通过softPwmCreate创建的引脚
Value:PWM引脚输出的值
  1. 对于如何实现让RGB全彩LED模块循环发出不同种颜色的光,可以在while(1)循环里面使用for语句不断遍历数组colors[],并使用延时函数delay(1000)使颜色变化速度减慢
 
  10、程序编写完成后,接下来就是编译问题,若使用c++编写的代码则使用第一条语句,若使用c编写则使用第二条语句
g++ -Wall -o test test.cpp  -lwiringPi -lpthread       
//使用C++编程 , -Wall 是为了使能所有警告,以便发现程序中的问题
gcc -Wall -o test test.c   -lwiringPi -lpthread         
//使用C语言编程, -lwiringPi是链接wiringPi库, -lpthread是链接softPwm库

编译完之后,并将模块相应引脚接到树莓派上,执行命令./test,则可以实现程序中相应功能

 

 1 #include <stdio.h>
 2 #include <wiringPi.h>  //添加头文件,wiringPi.h是调用相应的API库函数
 3 #include <softPwm.h>   //添加softPwm.h,调用软件PWM库函数
 4 
 5 const int Red_Pin = 9;  //RGB相关引脚
 6 const int Green_Pin = 10;
 7 const int Blue_Pin = 11;
 8 
 9 void RGB_Color_Init()   //初始化。创建模拟PWM引脚功能
10 {
11     softPwmCreate(Red_Pin, 0, 100);
12     softPwmCreate(Green_Pin, 0, 100);
13     softPwmCreate(Blue_Pin, 0, 100);
14 }
15 
16 int main()
17 {
18     if(wiringPiSetup() == -1)  //树莓派引脚初始化,返回-1则初始化失败
19     {
20         printf("Error!\n");
21         return 1;
22     }
23     RGB_Color_Init();
24     while(1){
25         for(int i = 255; i >= 0; i--)  //实现彩色渐变循环亮功能
26         {
27             softPwmWrite(Red_Pin, i);
28             softPwmWrite(Green_Pin, 128-i);
29             softPwmWrite(Blue_Pin, 255-i);
30             delay(10);
31         }
32         for(int i = 0; i <= 255; i++)  //同上
33         {
34             softPwmWrite(Red_Pin, i);
35             softPwmWrite(Green_Pin, 128-i);
36             softPwmWrite(Blue_Pin, 255-i);
37             delay(10);
38         }
39     }
40     return 0;
41 }

 

 

下面程序实现多种颜色循环变化,延时1s

 1 #include <stdio.h>
 2 #include <wiringPi.h>
 3 #include <softPwm.h>
 4 
 5 #define uchar unsigned char
 6 
 7 const int Red_Pin = 9;
 8 const int Green_Pin = 10;
 9 const int Blue_Pin = 11;
10 
11 int colors[] = {0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0x00ffff, 
12                 0xff00ff, 0xffffff, 0xffb6c1, 0x4b0082, 0x00008b, 
13                 0x00bfff, 0x008000, 0x8b4513, 0xd8bfd8, 0x4b0082};
14 
15 void RGB_Color_Init()
16 {
17     softPwmCreate(Red_Pin, 0, 100);
18     softPwmCreate(Green_Pin, 0, 100);
19     softPwmCreate(Blue_Pin, 0, 100);
20 }
21 
22 
23 void Set_Color(int color)
24 {
25     int Red_color, Green_color, Blue_color;
26     
27     Red_color = (color & 0xff0000) >> 16;
28     Green_color = (color & 0x00ff00) >> 8;
29     Blue_color = (color & 0x0000ff) >> 0;
30     
31     softPwmWrite(Red_Pin, Red_color);
32     softPwmWrite(Green_Pin, Green_color);
33     softPwmWrite(Blue_Pin, Blue_color);
34 }
35 
36 int main()
37 {
38     if(wiringPiSetup() == -1)
39     {
40         printf("Error!\n");
41         return 1;
42     }
43     RGB_Color_Init();
44     while(1){
45         for(int i=0; i<sizeof(colors)/sizeof(int); i++)
46         {
47             Set_Color(colors[i]);
48             delay(1000);
49         }
50     }
51     return 0;
52 }

 

posted @ 2020-11-21 21:50  .ivan  阅读(2120)  评论(4编辑  收藏  举报