標題: 要一份基于51單片機的顏色識別系統(tǒng)設計 求幫助 [打印本頁]

作者: wfl123456    時間: 2018-6-9 21:16
標題: 要一份基于51單片機的顏色識別系統(tǒng)設計 求幫助


作者: 張顆牙    時間: 2021-12-10 19:33
/*
* TCS3200模塊
*
* 用途:TCS3200顏色測試,讀取RGB值,LCD1602顯示R,G,B值

*
*/

//接線說明:
//模塊S2-----單片機P1.1
//模塊S3-----單片機P1.0
//模塊OUT----單片機P3.5(計數(shù)器1輸入)
//模塊VCC----單片機VCC
//模塊GND----單片機GND

#include<REG52.H>        
#include<math.h>       //Keil library  
#include<stdio.h>      //Keil library        
#include<INTRINS.H>

#define uchar unsigned char
#define uint  unsigned int        
#define DataPort P2           //LCD1602 數(shù)據(jù)端口
        
sbit    LCM_RS=P0^2;   //LCD1602 控制端口               
sbit    LCM_RW=P0^1;   //LCD1602 控制端口        
sbit    LCM_EN=P0^0;   //LCD1602 控制端口

/**引腳定義**/  
sbit s2=P1^1;        //TCS3200 S2
sbit s3=P1^0;        //TCS3200 S3
                     //TCS3200 S0 模塊內(nèi)部默認上拉
                     //TCS3200 S1 模塊內(nèi)部默認上拉
                     //TCS3200 OE 模塊內(nèi)部接地
sbit test_pin=P1^2;  //用示波器看這個引腳,可知道定時器中斷頻率
//變量、常量定義
uchar ge,shi,bai ;
uchar rp=3,gp=3,bp=6; //定義比例因子,具體環(huán)境可以修改
uchar count;          //顏色標志位(0:紅 1:綠 2:藍)

//顯示數(shù)組
uchar disp_R[3];  //紅
uchar disp_G[3];  //綠
uchar disp_B[3];  //藍

//********定義函數(shù)*****************************
void    delay(unsigned int k);
void    InitLcd();
void    WriteDataLCM(uchar dataW);
void    WriteCommandLCM(uchar CMD,uchar Attribc);
void    DisplayOneChar(uchar X,uchar Y,uchar DData);

//*********LCD1602初始化**********************
void InitLcd()                                
{                        
        WriteCommandLCM(0x38,1);        
        WriteCommandLCM(0x08,1);        
        WriteCommandLCM(0x01,1);
        WriteCommandLCM(0x06,1);        
        WriteCommandLCM(0x0c,1);
}

//**********檢測忙信號************************
void WaitForEnable(void)        
{                                       
        DataPort=0xff;               
        LCM_RS=0;LCM_RW=1;_nop_();
        LCM_EN=1;_nop_();_nop_();
        while(DataPort&0x80);        
        LCM_EN=0;                                
}
                                       
//**********寫命令至LCD***********************
void WriteCommandLCM(uchar CMD,uchar Attribc)
{                                       
        if(Attribc)WaitForEnable();        
        LCM_RS=0;LCM_RW=0;_nop_();
        DataPort=CMD;_nop_();        
        LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}        
                                
//**********寫數(shù)據(jù)至LCD************************
void WriteDataLCM(uchar dataW)
{                                       
        WaitForEnable();               
        LCM_RS=1;LCM_RW=0;_nop_();
        DataPort=dataW;_nop_();        
        LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
                                       
//*********寫一個字符數(shù)據(jù)到指定的目標***********
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{                                                
        Y&=1;                                                
        X&=15;                                                
        if(Y)X|=0x40;                                       
        X|=0x80;                        
        WriteCommandLCM(X,0);               
        WriteDataLCM(DData);               
}

//**********延時函數(shù)***************
void delay(unsigned int k)        
{                                                
        unsigned int i,j;                                
        for(i=0;i<k;i++)
        {                        
                for(j=0;j<121;j++)                        
                {;}
        }                                                
}                                                            

/*******************************************
* 函數(shù)名稱: t0_init()
* 函數(shù)功能: 定時器0初始化
* 入口參數(shù): 無
* 出口參數(shù): 無
/********************************************/
void t0_init()
{
        TMOD=0x51;        //T1計數(shù) T0定時 工作方式1
        
        TH1=0x00;        //計數(shù)初值
        TL1=0x00;
        
        TH0=0xE0;
        TL0=0x00;        //11。0592M 晶振10ms
        EA=1;            //開中斷
        
        ET0=1;        
        TR0=1;           //啟動
        TR1=1;
}

//*********************************************
//數(shù)值轉(zhuǎn)換出個十百千的ASCII碼
//*********************************************
void conversion(uint temp_data)  
{  
    bai=temp_data/100+0x30 ;
    temp_data=temp_data%100;   //取余運算
    shi=temp_data/10+0x30 ;
    ge=temp_data%10+0x30;      //取余運算
}

/*******************************************
* 函數(shù)名稱: main()
/********************************************/
void main()
{
        delay(10);
        InitLcd();      //lcd初始化
        s2=0;           //初始設定S2引腳
        s3=0;           //初始設定S3引腳
        t0_init();      //定時計數(shù)初使?

        while(1)
        {
                DisplayOneChar(0, 0, 'T');
                DisplayOneChar(1, 0, 'C');
                DisplayOneChar(2, 0, 'S');
                DisplayOneChar(3, 0, '2');
                DisplayOneChar(4, 0, '3');
                DisplayOneChar(5, 0, '0');

                DisplayOneChar(10, 0, 'R');
                DisplayOneChar(11, 0, '[');
                DisplayOneChar(12, 0, disp_R[0]);
                DisplayOneChar(13, 0, disp_R[1]);
                DisplayOneChar(14, 0, disp_R[2]);
                DisplayOneChar(15, 0, ']');        
        
                DisplayOneChar(0, 1, 'G');
                DisplayOneChar(1, 1, '[');
                DisplayOneChar(2, 1, disp_G[0]);
                DisplayOneChar(3, 1, disp_G[1]);
                DisplayOneChar(4, 1, disp_G[2]);
                DisplayOneChar(5, 1, ']');
               
                DisplayOneChar(10, 1, 'B');
                DisplayOneChar(11, 1, '[');
                DisplayOneChar(12, 1, disp_B[0]);
                DisplayOneChar(13, 1, disp_B[1]);
                DisplayOneChar(14, 1, disp_B[2]);
                DisplayOneChar(15, 1, ']');                                
               
                delay(100) ;        
        }
}

/*******************************************
* 函數(shù)名稱: c10ms_out()
* 函數(shù)功能: 定時中斷0服務程序
            修改顏色標志disp_tc(0:紅 1:綠 2:藍)
            設置S0 S1 S2 選擇濾波器
            計算脈沖,讀取色值
* 入口參數(shù): 無
* 出口參數(shù): 無
/********************************************/
void c10ms_out() interrupt 1
{
        uint temp;
        test_pin=!test_pin; //測試定時器中斷頻率引腳,可以用示波器觀察
        TR0=0;              //關(guān)閉定時
        TR1=0;              //關(guān)閉計數(shù)
        //   count+1實現(xiàn)先檢測綠色,再檢測藍色,然后檢測紅色,循環(huán)檢測      
        if(count==0)
        {
                count++;   
                s2=1;s3=1;             //選擇濾波器為綠色     
               
                temp=(8<<TH1)+TL1;    //計算這段時間內(nèi) TCS230 的輸出脈沖數(shù)        
                temp/=rp;                        
                conversion(temp);
                disp_R[2]=ge;         //因為這次的中斷,是上次選擇濾波器的數(shù)值
                disp_R[1]=shi;
                disp_R[0]=bai;
        }        
        else if(count==1)
        {            
                count++;
                s2=1;s3=0;            //選擇濾波器為藍色
                temp=(8<<TH1)+TL1;    //計算這段時間內(nèi) TCS230 的輸出脈沖數(shù)        
                temp/=gp;                        
                conversion(temp);
                disp_G[2]=ge;         //因為這次的中斷,是上次選擇濾波器的數(shù)值
                disp_G[1]=shi;
                disp_G[0]=bai;
        }        
        else if(count==2)
        {            
                count=0;
                s2=0;s3=0;            //選擇濾波器為紅色
               
                temp=(8<<TH1)+TL1;    //計算這段時間內(nèi) TCS230 的輸出脈沖數(shù)        
                temp/=bp;               
                conversion(temp);
                disp_B[2]=ge;         //因為這次的中斷,是上次選擇濾波器的數(shù)值
                disp_B[1]=shi;
                disp_B[0]=bai;        
        }
        
        //定時器計數(shù)器重賦初值
        TH0=0xE0;
        TL0=0x00; //11。0592M 晶振,為10ms
        TL1=0x00;//計數(shù)器清零
        TH1=0x00;//計數(shù)器清零
        TR0=1;   //打開定時器
        TR1=1;   //打開計數(shù)器
}





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1