萧芸凤 发表于 2012-12-12 20:43:19

TSC3200 颜色传感器

本帖最后由 萧芸凤 于 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;
float RGB_scale_factor;
////////////////////// 初始化///////////////////////////////////////
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
        byteRGB_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 = frequencecount;   
                                        break;
                        case 1:
                                        color_Filter('G');
                                        frequencecount = 0;
                                        RGB_flag++;
                                        Timer1.setPeriod(1000000);
                                        RGB_arr = frequencecount;            //绿色
                                        break;
                        case 2:
                                        color_Filter('G');
                                        frequencecount = 0;
                                        RGB_flag++;
                                        Timer1.setPeriod(1000000);
                                        RGB_arr = frequencecount;   
                                        break;
                        case 3:
                                        color_Filter('C');
                                        frequencecount = 0;
                                        RGB_flag++;
                                        Timer1.setPeriod(1000000);
                                        RGB_arr = frequencecount;            //清除   
                                        break;
                        default:
                                        frequencecount = 0;
                                break;
                }

        }
}
/////////////////白平衡设置/////////////////////////////////////
void color_WB_set( )   
{
        //int RGB_arr;
        //float *RGB_scale_factor
        colorRead();
        for ( byte i = 0 ; i < 4 ; i ++)
        {
                RGB_scale_factor = 255 / RGB_arr;
        }
}

void setup()
{
        color_Init();
        Serial.begin(9600);
        Timer1.initialize();                                // 定时器初始化,缺省计时周期1s
        //Timer1.attachInterrupt(colorRead);
        attachInterrupt(0, frequence, RISING);
       
}

void loop()
{
        float sf;
        int RGB_arr;
        int RGB_out;
        color_WB_set();
        while(1)
        {
                colorRead();
                for ( byte i = 0 ; i < 4 ; i ++)
                {
                        RGB_out = RGB_arr * RGB_scale_factor;
                }

                for ( byte i = 0 ; i < 4 ; i ++)
                {
                        Serial.print(RGB_out);
                        Serial.print("|");
                }
                Serial.println();
        }

}

keyosa 发表于 2013-7-25 12:24:56

能问下内置的那4个白色LED怎么点亮吗

hdxabout 发表于 2013-7-26 09:29:42

我都想知道楼上的问题如何解决

Mr.Dante 发表于 2013-8-6 13:45:20

这个编程看起来比R大的好。对于我的项目,由于是外部光源透射采光,所以也不用点亮自带的灯了。

Mr.Dante 发表于 2013-8-6 15:19:49

经实践 该程序实现不了测量。串口无输出。在认真修改中。应该是在中断的地方出错了。

hongshimizi 发表于 2015-9-22 16:01:22

怎么用啊,不能用啊,串口输出的都是啥啊
页: [1]
查看完整版本: TSC3200 颜色传感器