模块简介:

        TCS230是TAOS公司推出的可编程彩色光到频率的转换器。它把可配置的硅光电二极管与电流频率转换器集成在一个单一的CMOS电路上,同时在单一芯片上集成了红绿蓝(RGB)三种滤光器,是业界第一个有数字兼容接口的RGB彩色传感器。

        当入射光投射到TCS230上时,通过光电二极管控制引脚S2、S3的不同组合,可以选择不同的滤波器;经过电流到频率转换器后输出不同频率的方波(占空比是50%),不同的颜色和光强对应不同频率的方波;还可以通过输出定标控制引脚S0、S1,选择不同的输出比例因子,对输出频率范围进行调整,以适应不同的需求。

        S0、S1用于选择输出比例因子或电源关断模式;S2、S3用于选择滤波器的类型;OE是频率输出使能引脚,可以控制输出的状态,当有多个芯片引脚共用微处理器的输入引脚时,也可以作为片选信号;OUT是频率输出引脚,GND是芯片的接地引脚,VCC为芯片提供工作电压。表1是S0、S1及S2、S3的可用组合。

工作原理:

白平衡校准:

        理想状态下白色由:红色(R)=255,绿色(G)=255,蓝色(B)=255混合而成,但是在实际环境中,由于环境光不同,并且对于TCS230的光传感器来说,它对这三种基本色的敏感性是不相同的,导致TCS230的RGB输出并不相等,白色所对应三原色的参数也不同,此时就需要进行白平衡校准告诉系统什么是白色。使得TCS230对所检测的“白色”中的三原色是相等的。进行白平衡调整是为后续的颜色识别作准备。

        白平衡调整的具体步骤和方法如下:将白色纸板放在传感器前方,使入射光能够照射到TCS230上;根据前面所介绍的方法,依次选通红色、绿色和蓝色滤波器,分别测得红色、绿色和蓝色所产生到第255个脉冲时的时间,这些时间对应于实际测试时TCS230每种滤波器所采用的时间基准,在这段时间内所测得的脉冲数就是所对应的R、G和B的值。

颜色识别:

        当用TCS230识别颜色时,就用上述3个时间基准分别测得在时间内TCS-230所输出的脉冲个数,该脉冲个数即为测得三个颜色通道的颜色参数。

核心代码实现:

整体RTL视图:

(1)顶层模块:

//基于Verilog的TCS230颜色传感器驱动程序
/*
引脚定义:
GND(地线):电源地线。
OE(输出使能):启用输出频率(低电平有效)。
OUT:输出频率。
S0、S1(缩放选择输入):用于缩放输出频率(Arduino 通常使用 20% 缩放)。
S2、S3(光电二极管类型选择输入):选择要读取的颜色。
VDD(电压供应):提供电压。
滤光片选择:确定光电二极管的颜色(例如红色、蓝色、绿色或无滤光片)
*/
module  TCS230_Demo (
    input clk_50Mhz,
    input rst,
    input TCS_Out,
    input EN,
    input test,
    output wire OE,S0,S1,S2,S3,
    output wire [23:0] Color_Out_D,
    output wire DONE,
    output uart_tx
);
wire delay,timer_out,pulse_en,timer_en,pulse_clr,pulse_done;//定时器使能和脉冲使能
wire [7:0] Color_Cnt_RGB,Color_Cnt_D;//颜色计数器
wire [13:0] SR_Cnt;//定时器计数器
wire [13:0] CR_Cnt;
wire [41:0]SR_RGB;
//例化DataPath模块
DataPath DP(
    .rst(rst),
    .clk(clk_50Mhz),
    .TCS_Out(TCS_Out),
    .test_EN(test_en),
    .CR_RGB_en(CR_RGB_en),
    .delay_en(delay_en),
    .test_down(test_down),
    .delay(delay),
   // .SR_RGB(SR_RGB),
    .SR_Cnt(SR_Cnt),
    .Color_Cnt_D(Color_Cnt_D),
    .CR_down(CR_down),
    .CR_Cnt(CR_Cnt)
    
);
wire [4:0]present_state;
 
//例化TCS_230_SYS模块
TCS_230_SYS TCS230_SYS(
    .clk(clk_50Mhz),
    .RSTN(rst),
    .EN(EN),
    .test(test),
    .delay(delay),
    .test_down(test_down),
    .CR_down(CR_down),
    .SR_Cnt(SR_Cnt),
    .delay_en(delay_en),
    .test_en(test_en),
    .CR_RGB_en(CR_RGB_en),
    .SR_RGB(SR_RGB),
    .oe(OE),.S0(S0),.S1(S1),.S2(S2),.S3(S3),
    .present_state(present_state),
    .CR_Cnt(CR_Cnt),
    .Color_Out_D(Color_Out_D),
    .R_TCS_done(DONE),
    .Color_Cnt_D(Color_Cnt_D)
);
 
//例化uart_tx模块
Uart_tx UT(
    .uart_tx(uart_tx),
    .clk_100m(clk_50Mhz),
    .rst(rst),
    .data1({6'd0,Color_Out_D[23:16]}),
    .data2({6'd0,Color_Out_D[15:8]}),
    .data3({6'd0,Color_Out_D[7:0]}),
    .data4(SR_RGB[41:28]),
    .data5(SR_RGB[27:14]),
    .data6(SR_RGB[13:0]),
    .data7({9'd0,present_state})
);
 
 
endmodule

(2)数据通道模块:

//数据通道模块
module DataPath(
        input rst,clk,//复位和时钟
        input TCS_Out,//TCS230输出
        input test_EN,//测试使能
        input delay_en,//延时使能
        input CR_RGB_en,//颜色读取使能
        input [13:0] CR_Cnt,//颜色计数器
        //input [41:0]SR_RGB,//定时器计数器
        output test_down,//测试完成
        output CR_down,//颜色识别完成
        output delay,//延时
        output [13:0]SR_Cnt,//定时器计数器
        output [7:0] Color_Cnt_D//颜色计数器
);
 
wire [7:0] Color_Cnt_RGB;//颜色计数器
 
//例化timer模块
timer TIMER0(
    .timeout(timer_out),
    .clk(clk),
    .rst(!rst),
    .en(timer_en)
);
wire timer_en1,timer_en2,pulse_en1,pulse_en2,pulse_clr1,pulse_clr2;
assign timer_en = timer_en1 || timer_en2;
//例化timer模块
timer delay1(
    .timeout(delay),
    .clk(clk),
    .rst(!rst),
    .en(delay_en)
);
 
assign pulse_en = pulse_en1 || pulse_en2;
assign pulse_clr = pulse_en?1'b0:1'b1;
//例化脉冲计数模块
pulse_count PCNT(
    .count(Color_Cnt_RGB),
    .pulse_in(TCS_Out),
    .clk(clk),
    .rst(!rst),
    .pulse_en(pulse_en),
    .pulse_clr(pulse_clr)
    );
//例化color_test模块
Color_test CT(
    .clk(clk),
    .RSTN(rst),
    .test_EN(test_EN),
    .timeout(timer_out),
    .Color_Cnt_RGB(Color_Cnt_RGB),
    .pulse_en(pulse_en1),
    .pulse_clr(pulse_clr1),
    .timer_en(timer_en1),
    .SR_Cnt(SR_Cnt),
    .test_down(test_down)
);
 
//例化color_CR模块
Color_CR CR(
    .clk(clk),
    .RSTN(rst),
    .CR_RGB_en(CR_RGB_en),
    .CR_Cnt(CR_Cnt),
    .timeout(timer_out),
    .Color_Cnt_RGB(Color_Cnt_RGB),
    .Color_Cnt_D(Color_Cnt_D),
    .pulse_en(pulse_en2),
    .pulse_clr(pulse_clr2),
    .timer_en(timer_en2),
    .CR_down(CR_down)
);
 
endmodule

(3)状态机模块:

//状态机模块
module TCS_230_SYS(
        input clk,//时钟
        input RSTN,//复位
		input EN,test,
        input delay,//延时
        input test_down,//测试完成
        input CR_down,//颜色识别完成
        input [13:0]SR_Cnt,//定时器计数器
        input [7:0] Color_Cnt_D,//颜色计数器
        output reg delay_en,//延时使能
        output reg test_en,//测试使能
        output reg CR_RGB_en,//颜色读取使能
        output reg [41:0]SR_RGB,//定时器计数器
        output reg oe,S0,S1,S2,S3,//TCS230输入信号
        output reg [4:0]present_state,
        output reg [13:0] CR_Cnt,//颜色计数器
        output reg [23:0] Color_Out_D,//颜色输出
        output reg R_TCS_done//TCS230完成
        );
//reg [4:0]present_state;
 
reg [4:0]next_state;//当前状态和下一状态
parameter   Init    =   5'b00000,//初始状态
            Wait    =   5'b00001,//等待状态
            C_R_W   =   5'b00010,//R_W
            C_R     =   5'b00011,//G
            C_G_W   =   5'b00111,//B
            C_G     =   5'b00101,//R
            C_B_W   =   5'b00100,//G
            C_B     =   5'b01100,//B
            done_out=   5'b01101,//输出
            t_R_W   =   5'b01111,//T_R_W
            t_R     =   5'b01110,//T_R
            t_G_W   =   5'b11110,//T_G_W
            t_G     =   5'b11111,//T_G
            t_B_W   =   5'b11101,//T_B_W
            t_B     =   5'b11001;//T_B
 
always @ (posedge clk or negedge RSTN)
    if(!RSTN)begin//复位
        present_state<=Init;  
    end
    else
        present_state<=next_state;
//状态机
/*
    TCS230输入信号
    {oe,S0,S1,S2,S3}=5'b01110;//初始化
    {oe,S0,S1,S2,S3}=5'b11100;//100%缩放,选择红色
    {oe,S0,S1,S2,S3}=5'b11111;//100%缩放,选择绿色
    {oe,S0,S1,S2,S3}=5'b11101;//100%缩放,选择蓝色
    脉冲计数器输入信号
    {pulse_en,pulse_clr} = 3'b01;//初始化
    {pulse_en,pulse_clr} = 3'b10;//启用计数器
    {pulse_en,pulse_clr} = 3'b01;//清零计数器
    计时器输入信号 
    timer_en = 1'b0;//初始化
    timer_en = 1'b1;//启用计时器
*/
always @ (*) begin
    case (present_state)
        Init:begin
            SR_RGB = {14'd1500,14'd1500,14'd1500};//初始化
            {oe,S0,S1,S2,S3}=5'b01110;//初始化
            {delay_en,test_en,CR_RGB_en}=3'b000;//初始化
            R_TCS_done = 0;
            next_state=Wait;
        end
        Wait:begin
            {oe,S0,S1,S2,S3}=5'b01110;//初始化
            {delay_en,test_en,CR_RGB_en}=3'b000;//初始化
            R_TCS_done = 0;
            next_state=Wait;
            if(!test)begin
                next_state=t_R_W;
                //SR_RGB = SR_RGB;
            end
            else begin
                if(!EN)begin
                    next_state=C_G_W;
                    //SR_RGB = SR_RGB;
                end
                else begin
                    next_state=Wait;
                    //SR_RGB = SR_RGB;
                end
            end
        end
        t_R_W:begin
            {oe,S0,S1,S2,S3}=5'b11100;//100%缩放,选择红色
            {delay_en,test_en,CR_RGB_en}=3'b100;//启用延迟
            if(delay)begin
                next_state=t_R;
            end
            else begin
                next_state=t_R_W;
            end
        end
        t_R:begin
            {oe,S0,S1,S2,S3}=5'b11100;//100%缩放,选择红色
            {delay_en,test_en,CR_RGB_en}=3'b010;//启用延迟
            if(test_down)begin
                next_state=t_G_W;
                SR_RGB[41:28] = SR_Cnt;
            end
            else begin
                SR_RGB[41:28] = SR_RGB[41:28];
                next_state=t_R;
            end
        end
        t_G_W:begin
            {oe,S0,S1,S2,S3}=5'b11111;//100%缩放,选择绿色
            {delay_en,test_en,CR_RGB_en}=3'b100;//启用延迟
            if(delay)begin
                next_state=t_G;
                //SR_RGB = SR_RGB;
            end
            else begin
                next_state=t_G_W;
                //SR_RGB = SR_RGB;
            end
        end
        t_G:begin
            {oe,S0,S1,S2,S3}=5'b11111;//100%缩放,选择绿色
            {delay_en,test_en,CR_RGB_en}=3'b010;//启用延迟
            if(test_down)begin
                next_state=t_B_W;
                SR_RGB[27:14] = SR_Cnt;
            end
            else begin
                SR_RGB[27:14] = SR_RGB[27:14];
                next_state=t_G;
            end
        end
        t_B_W:begin
            {oe,S0,S1,S2,S3}=5'b11101;//100%缩放,选择蓝色
            {delay_en,test_en,CR_RGB_en}=3'b100;//启用延迟
            if(delay)begin
                next_state=t_B;
                //SR_RGB = SR_RGB;
            end
            else begin
                next_state=t_B_W;
                //SR_RGB = SR_RGB;
            end
        end
        t_B:begin
            {oe,S0,S1,S2,S3}=5'b11101;//100%缩放,选择蓝色
            {delay_en,test_en,CR_RGB_en}=3'b010;//启用延迟
            if(test_down)begin
                next_state=Wait;
                SR_RGB[13:0] = SR_Cnt;
            end
            else begin
                SR_RGB[13:0] = SR_RGB[13:0];
                next_state=t_B;
            end
        end
 
        C_R_W:begin
            {oe,S0,S1,S2,S3}=5'b11100;//100%缩放,选择红色
            {delay_en,test_en,CR_RGB_en}=3'b100;//启用延迟
            if(delay)begin
                next_state=C_R;
                SR_RGB = SR_RGB;
               
            end
            else begin
                next_state=C_R_W;
                SR_RGB = SR_RGB;
               
            end
        end
        C_R:begin
            {oe,S0,S1,S2,S3}=5'b11100;//100%缩放,选择红色
            {delay_en,test_en,CR_RGB_en}=3'b001;//启用读取颜色CR_Cnt
            CR_Cnt = SR_RGB[41:28];
            if(CR_down)begin
                next_state=done_out;
                Color_Out_D[23:16] = Color_Cnt_D;
            end
            else begin
               // //SR_RGB = SR_RGB;
                
                next_state=C_R;
            end
        end
 
        C_G_W:begin
            {oe,S0,S1,S2,S3}=5'b11111;//100%缩放,选择绿色
            {delay_en,test_en,CR_RGB_en}=3'b100;//启用延迟
            if(delay)begin
                next_state=C_G;
                //SR_RGB = SR_RGB;
                
            end
            else begin
                next_state=C_G_W;
                //SR_RGB = SR_RGB;
               
            end
        end
 
        C_G:begin
            {oe,S0,S1,S2,S3}=5'b11111;//100%缩放,选择绿色
            {delay_en,test_en,CR_RGB_en}=3'b001;//启用读取颜色CR_Cnt
            CR_Cnt = SR_RGB[27:14];
            if(CR_down)begin
                next_state=C_B_W;
                Color_Out_D[15:8] = Color_Cnt_D;
            end
            else begin
                //SR_RGB = SR_RGB;
                
                next_state=C_G;
            end
        end
 
        C_B_W:begin
            {oe,S0,S1,S2,S3}=5'b11101;//100%缩放,选择蓝色
            {delay_en,test_en,CR_RGB_en}=3'b100;//启用延迟
            if(delay)begin
                next_state=C_B;
                //SR_RGB = SR_RGB;
               
            end
            else begin
                next_state=C_B_W;
                //SR_RGB = SR_RGB;
              
            end
        end
 
        C_B:begin
            {oe,S0,S1,S2,S3}=5'b11101;//100%缩放,选择蓝色
            {delay_en,test_en,CR_RGB_en}=3'b001;//启用读取颜色CR_Cnt
            CR_Cnt = SR_RGB[13:0];
            if(CR_down)begin
                next_state=C_R_W;
                Color_Out_D[7:0] = Color_Cnt_D;
            end
            else begin
                //SR_RGB = SR_RGB;
                
                next_state=C_B;
            end
        end
 
        done_out:begin
            {oe,S0,S1,S2,S3}=5'b01110;//初始化
            R_TCS_done = 1;
            {delay_en,test_en,CR_RGB_en}=3'b000;//初始化
            next_state=Wait;
        end
		  
        default: 
            next_state=Init;
		  
    endcase
end
endmodule

(4)白平衡校准模块:

//白平衡校准模块
module Color_test(
    input clk,//时钟
    input RSTN,//复位
    input test_EN,//使能
    input timeout,//定时器
    input [7:0] Color_Cnt_RGB,//颜色计数器
    output reg pulse_en,pulse_clr,//脉冲使能和定时器使能
    output reg timer_en,//延时使能和定时器使能
    output reg [13:0]SR_Cnt,//定时器计数器
    output reg test_down//白平衡完成
);
 
always @ (posedge clk or negedge RSTN)
    if(!RSTN)begin//复位
        {pulse_en,pulse_clr}<=2'b01;
        timer_en<=0;
        SR_Cnt<=0;
        test_down <= 0;
    end
    else begin
        if(test_EN)begin
            {pulse_en,pulse_clr}<=2'b10;
            timer_en<=1;
            if(timeout)begin
                SR_Cnt<=SR_Cnt+1;
                if(Color_Cnt_RGB>=14'd255)begin
                    test_down <= 1;
                end
                else begin
                    test_down <= 0;
                end
            end
            else begin
                test_down <= 0;
                SR_Cnt<=SR_Cnt;
            end
            
        end
        else begin
            {pulse_en,pulse_clr}<=2'b01;
            timer_en<=0;
            test_down <= 0;
            SR_Cnt<=0;
        end
    end
endmodule

(5)颜色识别模块

//颜色识别模块
module Color_CR(
    input clk,//时钟
    input RSTN,//复位
    input CR_RGB_en,//颜色识别使能
    //input [41:0]SR_RGB,//定时器计数器
    input wire [13:0]CR_Cnt,//定时器计数器
    input timeout,//定时器
    input [7:0] Color_Cnt_RGB,//颜色计数器
    output reg [7:0] Color_Cnt_D,//颜色计数器
    output reg pulse_en,pulse_clr,//脉冲使能和定时器使能
    output reg timer_en,//延时使能和定时器使能
    output reg CR_down//颜色识别完成
);
reg [13:0] tmp_cnt;
always @ (posedge clk or negedge RSTN)
    if(!RSTN)begin//复位
        {pulse_en,pulse_clr}<=2'b01;
        timer_en<=0;
        tmp_cnt<=0;
        CR_down <= 0;
    end
    else begin
        if(CR_RGB_en)begin
            {pulse_en,pulse_clr}<=2'b10;
            timer_en<=1;
            if(timeout)begin
                
                if(tmp_cnt>=CR_Cnt)begin
                    CR_down <= 1;
                    Color_Cnt_D<=Color_Cnt_RGB;
                    tmp_cnt <= 0;
                end
                else begin
                    tmp_cnt <=tmp_cnt+1;
                    CR_down <= 0;
                end
            end
            else begin
                CR_down <= 0;
                tmp_cnt<=tmp_cnt;
 
            end      
        end
        else begin
            {pulse_en,pulse_clr}<=2'b01;
            timer_en<=0;
            CR_down <= 0;
            tmp_cnt <= 0;
        end
    end
endmodule

(6)计时器模块

//计时器模块
module timer (
    output reg timeout,
    input clk,
    input rst,
    input en
);
    parameter div_value = 50;
    reg[5:0] cnt_value;
    
    always@(posedge clk or posedge rst)
    begin
        if(rst)
            timeout <= 0;
        else if(en)
                if(cnt_value == div_value - 1)
                    timeout <= 1;
                else
                    timeout <= 0;
            else
                timeout <= 0;
    end
    
    always@(posedge clk or posedge rst)
    begin
        if(rst )
            cnt_value <= 0;
        else
			if(en)
                if((cnt_value == div_value - 1))
				    cnt_value <= 0;
				else
                    cnt_value <= cnt_value + 1;
            else
                cnt_value <= 0;
    end
    
endmodule

(7)脉冲计数器模块

module pulse_count(
    output reg[7:0] count,
    input pulse_in,
    input clk,
    input rst,
    input pulse_en,
    input pulse_clr
    );
    
    wire pulse_filt;
    parameter filter_level = 2;
 
    pulse_conver PC0(
        .pulse_out(pulse_filt),
        .pulse_in(pulse_in),
        .clk(clk),
        .rst(rst)
        );
    
    integer j;
    always@(posedge clk or posedge rst)
    begin
        if(rst)
            count <= 0;
        else begin
            if(pulse_clr)begin
                count <= 0;
            end else begin
                if(pulse_en)begin
                    for(j = 0;j < 2;j = j + 1) begin
                        count <= count + pulse_filt;
                    end
                end
                else
                    count <= count;
            end
        end
    end
    
endmodule
此作者没有提供个人介绍
最后更新于 2024-06-04