四极管:颜色识别 之 AVR TCS230

 颜色识别

四极管:颜色识别传感器TCS230驱动(TCS230.C)

 /*==========TCS230颜色识别传感器管脚说明===========================================	
	引 脚 号	符 号		功 能 说 明
		1	     S0		输出频率分频系数选择输入端
		2		 S1		输出频率分频系数选择输入端
		3		 OE		输入频率使能端。低电平有效
		4		 GND	 	电源地
		5		 VDD	 	电源电压
		6		 OUT		输出频率(fo)
		7		 S2		光电二极管类型选择输入端
		8		 S3		光电二极管类型选择输入端	

===============================程序接口========================================*/
     /*----S0,S1输出频率分频比例选择----*/
	 /*		S0 	S1 	输出频率分频比例    */
	 /*		L 	L 	掉电        0       */
	 /*		L 	H 	2%          1       */
	 /*		H 	L 	20%         2       */
	 /*		H 	H 	100%        3       */   //硬件接口已经下拉


     //使能E低电平有效

     /*------S2,S3光电二极管类型选择-----*/
	 /*		S2	S3	光电二极管类型       */
	 /*		L	L	红色			  0  */
	 /*		L	H	蓝色			  1  */
	 /*		H	L	清除(无滤波器)  2  */
	 /*		H	H	绿色			  3  */
	 /*     PB6  PB7                     */
/*==============================================================================*/

#define uchar unsigned char 
#define uint  unsigned int 
#include<util/delay.h>
#define S2_S3_R  {DDRB|=(1<<6);DDRB|=(1<<7);  PORTB &=~(1<<6);PORTB &=~(1<<7);}    //红色  red
#define S2_S3_B  {DDRB|=(1<<6);DDRB|=(1<<7);  PORTB &=~(1<<6);PORTB |= (1<<7);}    //蓝色  blue
#define S2_S3_0  {DDRB|=(1<<6);DDRB|=(1<<7);  PORTB |= (1<<6);PORTB &=~(1<<7);}    //清除  
#define S2_S3_G  {DDRB|=(1<<6);DDRB|=(1<<7);  PORTB |= (1<<6);PORTB |= (1<<7);}    //绿色  green

/*-----1602显示用到的字符串--------*/
unsigned char data_[]="0123456789";	
uchar black[] ="黑色 ";
uchar white[] ="白色 ";
uchar red[]   ="红色 ";
uchar green[] ="绿色 ";
uchar blue[]  ="蓝色 ";


uchar TCS230_count=0;  //外部中断计数
volatile uchar TCS230_flag=0; //RGB中断计数
uchar count_rgb;  //通道选择计数
 
 
 
/*---GB数值存储--*/ 
uchar R_count=0;
uchar G_count=0;
uchar B_count=0;

/*****************************************************
  * 函数名:Init_TCS230(uchar way)
  * 功  能:TCS230通道选择
  * 说  明:0--R,3--G,1--B
  * 子函数:无
  * 变  量:无
******************************************************/
void Init_TCS230(uchar way)
{
	/*switch(fre)  //硬件接口已经下拉
	{
		case 0: {S0_S1_0;   break;}
        case 1: {S0_S1_2;   break;}
		case 2: {S0_S1_20;  break;}
		case 3: {S0_S1_100; break;}
	}*/
		
	switch(way)
	{
		case 0: {S2_S3_R;  break;}  //红色通道
        case 1: {S2_S3_B;  break;}  //蓝色通道
		case 2: {S2_S3_0;  break;}  //清除通道
		case 3: {S2_S3_G;  break;}  //绿色通道
	}
	
	TCCR2  = 0x0F;//启动定时器
	GIFR  |= (1<<INTF2);//清中断标志
	GICR  |= 0x20; //开颜色中断
}



/*****************************************************
  * 函数名:display(uchar i1,uchar i2,uchar i3,uchar a)
  * 功  能:颜色RGB读取,1602显示
  * 说  明:
  * 子函数:Displaychar_1602
  * 变  量:无
******************************************************/
void display(uchar i1,uchar i2,uchar i3,uchar a)
{
    distwochar_12864(i1,0,data_[a/100],data_[a/10%10]);
	distwochar_12864(1,0,data_[a%10],' ');
}

//1602显示
/*
{
	Displaychar(i1,0,data_[a/100]);   //显示百位
	Displaychar(i2,0,data_[a/10%10]); //显示十位
	Displaychar(i3,0,data_[a%10]);    //显示个位
}*/



/*********************************************************************
  * 函数名:find_colour(uchar r,uchar g,uchar b)
  * 功  能:颜色识别显示
  * 说  明:通过读取R\G\B判断是什么颜色
  * 子函数:Displaystr_1602
  * 变  量:无
*********************************************************************/
void find_colour(uchar r,uchar g,uchar b)
{
	if( ((r>3)&&(r<10)) && ((g>3)&&(g<20)) && ((b>5)&&(b<13)) )
	{
		dispstr_12864(0,1,black);
	}

	if( ((r>9)&&(r<20)) && ((g>22)&&(g<42)) && ((b>34)&&(b<65)) )
	{
		dispstr_12864(0,1,blue);
	}
	
	if( ((r>11)&&(r<23)) && ((g>26)&&(g<49)) && ((b>24)&&(b<46)) )
	{
		dispstr_12864(0,1,green);
	}
	
	if( ((r>20)&&(r<40)) && ((g>5)&&(g<13)) && ((b>5)&&(b<13)) )
	{
		dispstr_12864(0,1,red);
	}
	
	if( ((r>33)&&(r<63)) && ((g>42)&&(g<90)) && ((b>56)&&(b<103)) )
	{
		dispstr_12864(0,1,white);
	}
	
	if( ((r>8)&&(r<15)) && ((g>13)&&(g<24)) && ((b>11)&&(b<20)) )
	{
		dispstr_12864(0,1,"深绿色");
	}
	
	if( ((r>8)&&(r<14)) && ((g>11)&&(g<22)) && ((b>26)&&(b<49)) )
	{
		dispstr_12864(0,1,"深蓝色");
	}
	
	if( ((r>18)&&(r<34)) && ((g>18)&&(g<33)) && ((b>12)&&(b<23)) )
	{
		dispstr_12864(0,1,"橙色 ");
	}
}


/**************************************************************
  * 函数名:TCS230_task(void)V
  * 功  能:TCS230颜色处理
  * 说  明:分别读取不同颜色的RGB值,判断显示颜色及其RGB数值
  * 子函数:find_colour,Init_TCS230
  * 变  量:无
**************************************************************/
void TCS230_task(void)
{
	
	for(count_rgb=0;count_rgb<3;)    //读三个通道的值  但因为不是连续的读
	{
		Init_TCS230(0);//选择R通道
		while(TCS230_flag==0)       //等待读数
		{
			//1602显示函数
			//display(0,1,2,R_count);
			//display(5,6,7,G_count);
			//display(10,11,12,B_count);
			distwochar_12864(0,0,data_[R_count/100],data_[R_count/10%10]);
			distwochar_12864(1,0,data_[R_count%10],' ');

			distwochar_12864(2,0,data_[G_count/100],data_[G_count/10%10]);
			distwochar_12864(3,0,data_[G_count%10],' ');
			
			distwochar_12864(4,0,data_[B_count/100],data_[B_count/10%10]);
			distwochar_12864(5,0,data_[B_count%10],' ');
			
			
			find_colour(R_count,G_count,B_count); //对颜色进行判断并显示

		}
		
		if(TCS230_flag==1) //读取R通道数据
		{
			R_count=TCS230_count; //读取R值
			distwochar_12864(0,0,data_[R_count/100],data_[R_count/10%10]);//更新显示R值
			distwochar_12864(1,0,data_[R_count%10],' '); 
			find_colour(R_count,G_count,B_count);//颜色判断
 			TCS230_flag=1;   
			TCS230_count=0;     //清零
			TCNT2  = 0x00;//重新给T2初始值
			Init_TCS230(3);//G通道选择
			count_rgb++;    //通道计数++
		}
		
		while(TCS230_flag==1) //等待读取G值
		{
			//display(0,1,2,R_count);      
			//display(5,6,7,G_count); 
			//display(10,11,12,B_count);
			distwochar_12864(0,0,data_[R_count/100],data_[R_count/10%10]);
			distwochar_12864(1,0,data_[R_count%10],' ');

			distwochar_12864(2,0,data_[G_count/100],data_[G_count/10%10]);
			distwochar_12864(3,0,data_[G_count%10],' ');
			
			distwochar_12864(4,0,data_[B_count/100],data_[B_count/10%10]);
			distwochar_12864(5,0,data_[B_count%10],' ');
			
			find_colour(R_count,G_count,B_count);//颜色识别显示
		}
		
		
		if(TCS230_flag==2)
		{
			G_count=TCS230_count;  //读取G值
			
			distwochar_12864(2,0,data_[G_count/100],data_[G_count/10%10]);
			distwochar_12864(3,0,data_[G_count%10],' ');
			
			find_colour(R_count,G_count,B_count);
			TCS230_flag=2;
			TCS230_count=0;   //清零
			TCNT2  = 0x00;  //T2初始值
			Init_TCS230(1); //B通道选择
			count_rgb++;
		}
		
		while(TCS230_flag==2)
		{
			
			distwochar_12864(0,0,data_[R_count/100],data_[R_count/10%10]);
			distwochar_12864(1,0,data_[R_count%10],' ');

			distwochar_12864(2,0,data_[G_count/100],data_[G_count/10%10]);
			distwochar_12864(3,0,data_[G_count%10],' ');
			
			distwochar_12864(4,0,data_[B_count/100],data_[B_count/10%10]);
			distwochar_12864(5,0,data_[B_count%10],' ');
			
			find_colour(R_count,G_count,B_count);//颜色识别显示
			_delay_ms(1);
		}
		
		if(TCS230_flag==3)
		{
			B_count=TCS230_count;  //读取B值
			
			distwochar_12864(4,0,data_[B_count/100],data_[B_count/10%10]);
			distwochar_12864(5,0,data_[B_count%10],' ');
			
			find_colour(R_count,G_count,B_count);
			TCS230_flag=0;
			TCS230_count=0;
			count_rgb++;
		}
	}
	
	find_colour(R_count,G_count,B_count); //颜色识别显示  
}

转载请注明出处。作者:四极管。广西师范大学 电子工程学院大学生科技创新基地 邮箱: [email protected]

你可能感兴趣的:(存储,delay)