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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM