本帖最后由 萧芸凤 于 2012-12-12 20:45 编辑
- #include <TimerOne.h>
-
- #define COLOR_S0 6
- #define COLOR_S1 5
- #define COLOR_S2 4
- #define COLOR_S3 3
- #define COLOR_OUT 2
- int frequencecount = 0; // 计数频率
- int RGB_arr[4];
- float RGB_scale_factor[4];
- ////////////////////// 初始化///////////////////////////////////////
- void color_Init()
- {
- pinMode(COLOR_S0, OUTPUT);
- pinMode(COLOR_S1, OUTPUT);
- pinMode(COLOR_S2, OUTPUT);
- pinMode(COLOR_S3, OUTPUT);
- pinMode(COLOR_OUT, INPUT);
-
- digitalWrite(COLOR_S0, LOW); // 输出频率设定, 2%
- digitalWrite(COLOR_S1, HIGH);
-
- //Timer1.attachInterrupt(colorRead);
- //attachInterrupt(0, frequence, RISING);
- }
-
- /////////////////// RGB色彩滤镜选择 //////////////////////////////////////////////
- void color_Filter(char color)
- {
- byte s2,s3;
- if ( color == 'R' || color == 'r')
- {
- s2 = 0;
- s3 = 0;
- }
- if ( color == 'G' || color == 'g')
- {
- s2 = 1;
- s3 = 1;
- }
- if ( color == 'B' || color == 'b')
- {
- s2 = 0;
- s3 = 1;
- }
- if ( color == 'C' || color == 'c')
- {
- s2 = 1;
- s3 = 0;
- }
-
- digitalWrite(COLOR_S2, s2);
- digitalWrite(COLOR_S3, s3);
- }
- ////////////////////////////频率函数,中断函数 //////////////////////////////////////////
- void frequence()
- {
- frequencecount ++ ;
- }
-
- void colorRead( )
- {
- //int RGB_arr[4]
- byte RGB_flag;
- RGB_flag = 0;
- while(RGB_flag >= 0 && RGB_flag <= 3 )
- {
- switch(RGB_flag)
- {
- case 0:
- color_Filter('R');
- frequencecount = 0;
- RGB_flag++;
- Timer1.setPeriod(1000000);
- RGB_arr[0] = frequencecount;
- break;
- case 1:
- color_Filter('G');
- frequencecount = 0;
- RGB_flag++;
- Timer1.setPeriod(1000000);
- RGB_arr[1] = frequencecount; //绿色
- break;
- case 2:
- color_Filter('G');
- frequencecount = 0;
- RGB_flag++;
- Timer1.setPeriod(1000000);
- RGB_arr[2] = frequencecount;
- break;
- case 3:
- color_Filter('C');
- frequencecount = 0;
- RGB_flag++;
- Timer1.setPeriod(1000000);
- RGB_arr[3] = frequencecount; //清除
- break;
- default:
- frequencecount = 0;
- break;
- }
- }
- }
- /////////////////白平衡设置/////////////////////////////////////
- void color_WB_set( )
- {
- //int RGB_arr[4];
- //float *RGB_scale_factor
- colorRead();
- for ( byte i = 0 ; i < 4 ; i ++)
- {
- RGB_scale_factor[i] = 255 / RGB_arr[i];
- }
- }
-
- void setup()
- {
- color_Init();
- Serial.begin(9600);
- Timer1.initialize(); // 定时器初始化,缺省计时周期1s
- //Timer1.attachInterrupt(colorRead);
- attachInterrupt(0, frequence, RISING);
-
- }
-
- void loop()
- {
- float sf[4];
- int RGB_arr[4];
- int RGB_out[4];
- color_WB_set();
- while(1)
- {
- colorRead();
- for ( byte i = 0 ; i < 4 ; i ++)
- {
- RGB_out[i] = RGB_arr[4] * RGB_scale_factor[i];
- }
- for ( byte i = 0 ; i < 4 ; i ++)
- {
- Serial.print(RGB_out[i]);
- Serial.print(" | ");
- }
- Serial.println();
- }
-
- }
复制代码 |