标题:求解析这段C51代码写的关于TCS230颜色传感器识别颜色并LCD显示的代码、?
只看楼主
androider185
Rank: 2
等 级:论坛游民
帖 子:10
专家分:17
注 册:2013-8-16
结帖率:0
 问题点数:0 回复次数:0 
求解析这段C51代码写的关于TCS230颜色传感器识别颜色并LCD显示的代码、?
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned int
uchar code table[]="RGB IS:";
uchar code tablenum[]="0123456789";
ulong frequency;
int tempr[3],a;
int tempg[3];
int tempb[3];
sbit lcden=P3^4;
sbit rs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
sbit ckey=P3^6;
sbit wkey=P3^7;
ulong fr,fg,fb,flr,flb,flg,r,g,b;//fr,fg,fb 存放光源频率和转换后的RGB
int temp[5],m,h;
uchar flag=0,i,sflag=0,n;//flag为是否进入中断判断标志。

void initport()
{
 SM0=0;
 SM1=1;
 TR1=1;
 TMOD=0x20;
 TH1=0xFD;
 TL1=0xFD;     
}

void inittimer()  //T1定时,T0计数初始化
{
  EA=1,ET0=1;ET1=1;//开总中断,开定时器0中断
  TMOD=0x51;//to方式1计数,T1方式1定时
  TH0=5536/256;
  TL0=5536%256;// 定时60ms,测量频率17HZ-50KHZ
  TR0=0;
  TR1=0;
  P3=0xff;                     
}

ulong cblyz(ulong dataa)//求比例因子
{
 ulong blyz;
 blyz=25500/dataa;
 return blyz;     
}

void delay(int k)
{
 int j;
 for(j=110;j>0;j--)
    for(k;k>0;k--);     
}
void whitetest()
{
 while(flag!=3)
 {
   switch(flag)
   {
      case 0:
      {
           TH0=5536/256;
           TL0=5536%256;
           TH1=0;
           TL1=0;
           P1=0x30;
           delay(50);
           TR0=1;TR1=1;
           while(!=sflag);
           sflag=0;
           if(frequency==0)
           {  r=0;}
           else
           {  r=cblyz(frequency);}
           frequency=0;   
      } break;
      case 1:
      {
           TH0=5536/256;
           TL0=5536%256;
           TH1=0;
           TL1=0;
           P1=0x33;
           delay(50);
           TR0=1;TR1=1;
           while(!=sflag);
           sflag=0;
           if(frequency==0)
           {  g=0;}
           else
           {  g=cblyz(frequency);}
           frequency=0;   
      } break;
      case 2:
      {
           TH0=5536/256;
           TL0=5536%256;
           TH1=0;
           TL1=0;
           P1=0x32;
           delay(50);
           TR0=1;TR1=1;
           while(!=sflag);
           sflag=0;
           if(frequency==0)
           {  b=0;}
           else
           {  b=cblyz(frequency);}
           frequency=0;   
      } break;
                 
   }              
 }
 flag=0;   
}
void colortest()
{
 while(flag!=3)
   {
     switch(flag)
     {
        case 0:
             {
              P1=0x30;
              TH0=5536/256;
              TL0=5536%256;
              TH1=0;
              TL1=0;
              delay(50);
              TR0=1;
              TR1=1;
              while(!sflag);
              sflag=0;
              frequency=frequency*r;
              fr=frequency/100;
              if(fr>255)
              fr=255;
              frequency=0;     
             } break;
        case 1:
             {
              P1=0x33;
              TH0=5536/256;
              TL0=5536%256;
              TH1=0;
              TL1=0;
              delay(50);
              TR0=1;
              TR1=1;
              while(!sflag);
              sflag=0;
              frequency=frequency*g;
              fg=frequency/100;
              if(fg>255)
              fg=255;
              frequency=0;     
             } break;
        case 2:
             {
              P1=0x32;
              TH0=5536/256;
              TL0=5536%256;
              TH1=0;
              TL1=0;
              delay(50);
              TR0=1;
              TR1=1;
              while(!sflag);
              sflag=0;
              frequency=frequency*b;
              fb=frequency/100;
              if(fb>255)
              fb=255;
              frequency=0;     
             } break;                 
     }            
   }
   flag=0;     
}
void write_com(uchar com)
{
  rs=0;
  P0=com;
  delay(1);
  lcden=1;
  delay(5);
  lcden=0;
  delay(1);
  rs=1;     
}
void write_date(uchar date)
{
  rs=1;
  P0=date;
  delay(1);
  lcden=1;
  delay(5);
  lcden=0;
  delay(1);
  rs=0;      
}
void lcdinit()
{
  TR0=0;TR1=0;
  lcden=0;
  write_com(0x38);
  write_com(0x0e);
  write_com(0x06);
  write_com(0x01);
  write_com(0x80);
}
void drg_r(ulong value)
{
 tempr[0]=value/100;
 tempr[1]=(value-tempr[0]*100)/10;
 tempr[2]=value%10;     
}
void drg_g(ulong value)
{
 tempg[0]=value/100;
 tempg[1]=(value-tempg[0]*100)/10;
 tempg[2]=value%10;     
}
void drg_b(ulong value)
{
 tempb[0]=value/100;
 tempb[1]=(value-tempb[0]*100)/10;
 tempb[2]=value%10;     
}
void lcdxianshi()
{
  lcdinit();
  drg_r(fr);
  drg_g(fg);
  drg_b(fb);
  for(i=0;i<8;i++)
  {
    write_date(table[i]);
    delay(5);               
  }
  write_date('R');
  write_date(':');
  for(m=0;m<3;m++)
  {
     write_date(tablenum[tempr[m]]);
     delay(5);               
  }
  write_com(0xc0);
  write_date('G');
  write_date(':');
  for(m=0;m<3;m++)
  {
     write_date(tablenum[tempg[m]]);
     delay(5);                 
  }  
  write_com(0xC8);
  write_date('B');
  write_date(':');
  for(m=0;m<3;m++)
  {
     write_date(tablenum[tempb[m]]);
     delay(5);               
  }
  write_com(0x0c);  
}

void serialport()
{
  SBUF=fr;
  while(!TI);
  TI=0;
  SBUF=fg;
  while(!TI);
  TI=0;
  SBUF=fb;
  while(!TI);
  TI=0;
}  

void main()
{
      dula=0;
      wela=0;
WHITE:inittimer();
      whitetest();
      while(1)
      {
        inittimer();
        colortest();
        EA=0;
        P1=0x00;
        lcdxianshi();
        initport();
        serialport();
        P3=0xff;
        while(P3==0xff);
        if(wkey==0)
        {
           goto WHITE;           
        }        
      }
}
void timer0() interrupt 1
{
  TR0=0;
  TR1=0;
  frequency=TH1*256+TL1+n*65536;
  TH0=5536/256;
  TL0=5536%256;
  TH1=0;
  TL1=0;
  flag++;
  sflag=1;
  n=0;     
}
void counter1()interrupt 3
{
  TR1=0;
  TH1=0;
  TL1=0;
  n++;
  TR1=1;     
}








搜索更多相关主题的帖子: frequency include 传感器 
2013-08-16 10:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-419190-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017627 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved