搜档网
当前位置:搜档网 › 颜色传感器程序

颜色传感器程序

//模块默认拉高S0 S1,单片机可以只接S2 S3 引脚。
//测试代码:
/*******************************************
* 文件名: TCS3200 颜色测试
* 型号: CJMCU-M125
S0 S1 S2 S3
GND VCC OUT LED
* 功能: 读取RGB 值
* 单片机: STC89C52
* 晶振: 11.0592m
* 时间: 2011-3-20
* LCD1602 显示R,G,B 值
/********************************************/
//接线说明:
//模块S2-----单片机P1.1
//模块S3-----单片机P1.0
//模块OUT----单片机P3.5(计数器1 输入)
//模块VCC----单片机VCC
//模块GND----单片机GND
//**********************************************
#include
#include //Keil library
#include //Keil library
#include
#define uchar unsigned char
#define uint unsigned int

//----------------------------------------
sfr T2MOD=0x0c9;
//----------------------------------------
/**引脚定义**/
sbit s2=P1^0; //TCS3200 S2
sbit s3=P1^1; //TCS3200 S3
/**比例因子**/
sbit s0=P1^2;
sbit s1=P1^3;
//TCS3200 S0 模块内部默认上拉
//TCS3200 S1 模块内部默认上拉
//TCS3200 OE 模块内部接地
//变量、常量定义
uchar ge,shi,bai,LJ=0 ;
//uchar rp=2,gp=1,bp=2; //定义比例因子,具体环境可以修改
uchar count; //颜色标志位(0:红1:绿2:蓝)
//显示数组
uchar disp_R[3]; //红
uchar disp_G[3]; //绿
uchar disp_B[3]; //蓝
//uchar FS[3]={0xfd,0x00,0xaa};
uchar AA[3]={0x01,0x02,0x03};
uchar CC[3]={0x04,0x05,0x06};
uchar DD[3]={0x07,0x08,0x09};

uchar FS[2]={0};
sbit LED1=P2^2;
//------------------------------------------
//********定义函数*****************************
void delay(unsigned int k);
//**********延时函数***************
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i{
for(j=0;j<121;j++)
{;}
}
}
/*******************************************
* 函数名称: t0_init()
* 函数功能: 定时器0 初始化
* 入口参数: 无
* 出口参数: 无
/********************************************/
void t0_init()
{
TMOD=0x51; //T1 计数T0 定时工作方式1
TH1=0x00; //计数初值
TL1=0x00;
TH0=0x0dc;
TL0=0x00; //11。0592M 晶振10ms
EA=1; //开中断
ET0=1;
TR0=1; //启动
TR1=1;
}
//---------------------------------------------
void UART2(void)
{
T2MOD = 0x01; //自动重载
T2CON = 0x30; //T2用做发送接收时钟
TH2 = 0xFF; //9600波特率,11.0592Mhz晶振
TL2 = 0xDC;
RCAP2H = 0xFF;
RCAP2L = 0xDC;
SCON = 0x50; //串口方式1,1个起始位,1个停止位,8位数据
TR2 = 1; //启动T2
}
//---------------------------------------------
void SendByte(unsigned char c,unsigned char *YS)
{
char i;
for(i=0;i{
SBUF = *YS++;
while(!TI);
TI = 0;
}
}
//*********************************************
//数值转换出个十百千的ASCII 码
//*********************************************
void conv

ersion(uint temp_data)
{
bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //取余运算
shi=temp_data/10+0x30 ;
ge=temp_data%10+0x30; //取余运算
}
/*******************************************
* 函数名称: main()
/********************************************/
void main()
{
delay(10);
t0_init(); //定时计数初使化
UART2();
while(1)
{
//disp_R[0]=;

// disp_G[0]=disp_G[0]*100+disp_G[1]*10+disp_G[2];
SendByte(1,disp_G);

// disp_B[0]=disp_B[0]*100+disp_B[1]*10+disp_B[2];
SendByte(1,disp_B);

// SendByte(3,FS);
SendByte(1,disp_R);


delay(800);
}
}
/*******************************************
* 函数名称: c10ms_out()
* 函数功能: 定时中断0 服务程序
修改颜色标志disp_tc(0:红1:绿2:蓝)
设置S0 S1 S2 选择滤波器
计算脉冲,读取色值
* 入口参数: 无
* 出口参数: 无
/********************************************/
void c10ms_out() interrupt 1
{

uint temp;
s0=1;
s1=1;
TR0=0; //关闭定时
TR1=0; //关闭计数
/*LJ++;
if(LJ>100)
{
LJ=0;
LED1=~LED1;
}*/
/*temp=123;
conversion(temp);
disp_R[2]=ge; //因为这次的中断,是上次选择滤波器的数值
disp_R[1]=shi;
disp_R[0]=bai;*/
if(count==0)
{
count++;
s2=0;s3=0; //选择滤波器为red色
//temp=(8<//temp/=rp;
//temp=123;
temp=TL1;
// conversion(temp);
//disp_R[2]=ge; //因为这次的中断,是上次选择滤波器的数值
//disp_R[1]=shi;
disp_R[0]=temp;

//SendByte(3,AA);
}
else if(count==1)
{
count++;
s2=1;s3=1; //选择滤波器为green色
//temp=(8<// temp/=gp;
//temp=456;
temp=TL1;
//conversion(temp);
//disp_G[2]=ge; //因为这次的中断,是上次选择滤波器的数值
//disp_G[1]=shi;
// disp_G[0]=bai;
disp_G[0]=temp;
//SendByte(3,CC);
}
else if(count==2)
{
count=0;
s2=0;s3=1; //选择滤波器为blue色

temp=TL1;
//temp=(8<//temp/=bp;
//temp=789;
/*FS[0]=TL1;
FS[1]=TH1;
SendByte(2,FS); */
//conversion(temp);
//disp_B[2]=ge; //因为这次的中断,是上次选择滤波器的数值
//disp_B[1]=shi;
// disp_B[0]=bai;
disp_B[0]=temp;
//SendByte(3,DD);
}
//定时器计数器重赋初值
TH0=0x0dc;
TL0=0x00; //11。0592M 晶振,为10ms
TL1=0x00;//计数器清零
TH1=0x00;//计数器清零
TR0=1; //打开定时器
TR1=1; //打开计数器
}
//------------------------------------------------------

相关主题