|
如題。求各位大佬幫忙。如圖,一個(gè)顏色傳感器,一個(gè)單片機(jī)。我現(xiàn)有的程序只能讓LCD屏顯示,并不能讀取顏色傳感器的數(shù)值,求大佬幫忙。另外,接線的話。能不能也請(qǐng)各位指導(dǎo)一下。。拜謝。。。
QQ圖片20170727121229.jpg (861.72 KB, 下載次數(shù): 76)
下載附件
2017-7-27 12:13 上傳
QQ圖片20170727121235.jpg (1.35 MB, 下載次數(shù): 53)
下載附件
2017-7-27 12:13 上傳
程序如下:
- /*******************************************
- * 文件名: TCS3200顏色測(cè)試
- * 型號(hào): GY-31
- * 功能: 讀取RGB值
- * 單片機(jī): STC89C52
- * 晶振: 11.0592m
- * 時(shí)間: 2011-3-20
- * LCD1602顯示R,G,B值
- /********************************************/
- //接線說明:
- //模塊S2-----單片機(jī)P1.1
- //模塊S3-----單片機(jī)P1.0
- //模塊OUT----單片機(jī)P3.5(計(jì)數(shù)器1輸入)
- //模塊VCC----單片機(jī)VCC
- //模塊GND----單片機(jī)GND
- //**********************************************
- #include<reg51.h>
- #include<lcd.h>
- #include<math.h> //Keil library
- #include<stdio.h> //Keil library
- #include<INTRINS.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define DataPort P0 //LCD1602 數(shù)據(jù)端口
- sbit LCM_RS=P2^6; //LCD1602 控制端口
- sbit LCM_RW=P2^5; //LCD1602 控制端口
- sbit LCM_EN=P2^7; //LCD1602 控制端口
- /**引腳定義**/
- sbit s2=P1^1; //TCS3200 S2
- sbit s3=P1^0; //TCS3200 S3
- sbit s1=P1^2; //TCS3200 S1
- sbit s0=P1^3; //TCS3200 S0
- //TCS3200 S0 模塊內(nèi)部默認(rèn)上拉
- //TCS3200 S1 模塊內(nèi)部默認(rèn)上拉
- //TCS3200 OE 模塊內(nèi)部接地
- sbit test_pin=P1^2; //用示波器看這個(gè)引腳,可知道定時(shí)器中斷頻率
- //變量、常量定義
- uchar ge,shi,bai ;
- uchar rp=2,gp=2,bp=2; //定義比例因子,具體環(huán)境可以修改
- uchar count; //顏色標(biāo)志位(0:紅 1:綠 2:藍(lán))
- //顯示數(shù)組
- uchar disp_R[3]; //紅
- uchar disp_G[3]; //綠
- uchar disp_B[3]; //藍(lán)
- //********定義函數(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);
- //**********延時(shí)函數(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ù)功能: 定時(shí)器0初始化
- * 入口參數(shù): 無
- * 出口參數(shù): 無
- /********************************************/
- void t0_init()
- {
- TMOD=0x51; //T1計(jì)數(shù) T0定時(shí) 工作方式1
- TH1=0x00; //計(jì)數(shù)初值
- TL1=0x00;
- TH0=0xE0;
- TL0=0x00; //11。0592M 晶振10ms
- EA=1; //開中斷
- ET0=1;
- TR0=1; //啟動(dòng)
- TR1=1;
- }
- //*********************************************
- //數(shù)值轉(zhuǎn)換出個(gè)十百千的ASCII碼
- //*********************************************
- void conversion(uint temp_data)
- {
- bai=temp_data/100+0x30 ;
- temp_data=temp_data%100; //取余運(yùn)算
- shi=temp_data/10+0x30 ;
- ge=temp_data%10+0x30; //取余運(yùn)算
- }
- /*******************************************
- * 函數(shù)名稱: c10ms_out()
- * 函數(shù)功能: 定時(shí)中斷0服務(wù)程序
- 修改顏色標(biāo)志disp_tc(0:紅 1:綠 2:藍(lán))
- 設(shè)置S0 S1 S2 選擇濾波器
- 計(jì)算脈沖,讀取色值
- * 入口參數(shù): 無
- * 出口參數(shù): 無
- /********************************************/
- void c10ms_out() interrupt 1
- { uint temp;
- test_pin=!test_pin; //測(cè)試定時(shí)器中斷頻率引腳,可以用示波器觀察
- TR0=0; //關(guān)閉定時(shí)
- TR1=0; //關(guān)閉計(jì)數(shù)
- // count+1實(shí)現(xiàn)先檢測(cè)綠色,再檢測(cè)藍(lán)色,然后檢測(cè)紅色,循環(huán)檢測(cè)
- if(count==0)
- {
- count++;
- s2=1;s3=1; //選擇濾波器為綠色
-
- temp=(8<<TH1)+TL1; //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)
- temp/=rp;
- conversion(temp);
- disp_R[2]=ge; //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
- disp_R[1]=shi;
- disp_R[0]=bai;
- }
- else if(count==1)
- {
- count++;
- s2=0;s3=1; //選擇濾波器為藍(lán)色
- temp=(8<<TH1)+TL1; //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)
- temp/=gp;
- conversion(temp);
- disp_G[2]=ge; //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
- disp_G[1]=shi;
- disp_G[0]=bai;
- }
- else if(count==2)
- {
- count=0;
- s2=0;s3=0; //選擇濾波器為紅色
- temp=(8<<TH1)+TL1; //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)
- temp/=bp;
- conversion(temp);
- disp_B[2]=ge; //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
- disp_B[1]=shi;
- disp_B[0]=bai;
- }
- //定時(shí)器計(jì)數(shù)器重賦初值
- TH0=0xE0;
- TL0=0x00; //11。0592M 晶振,為10ms
- TL1=0x00;//計(jì)數(shù)器清零
- TH1=0x00;//計(jì)數(shù)器清零
- TR0=1; //打開定時(shí)器
- TR1=1; //打開計(jì)數(shù)器
- }
- /*******************************************
- * 函數(shù)名稱: main()
- /********************************************/
- void main()
- {
- delay(10);
- LcdInit(); //LCD初始化子程序
- s2=0; //初始設(shè)定S2引腳
- s3=0; //初始設(shè)定S3引腳
- t0_init(); //定時(shí)計(jì)數(shù)初使化
- while(1)
- {
- LcdWriteCom(0x80);
- LcdWriteData('G');
- LcdWriteData(':');
- LcdWriteData(disp_G[0]);
- LcdWriteData(disp_G[1]);
- LcdWriteData(disp_G[2]); //綠
- LcdWriteCom(0x80+6);
- LcdWriteData('B');
- LcdWriteData(':');
- LcdWriteData(disp_B[0]);
- LcdWriteData(disp_B[1]);
- LcdWriteData(disp_B[2]); //藍(lán)
- LcdWriteCom(0xc0);
- LcdWriteData('R');
- LcdWriteData(':');
- LcdWriteData(disp_R[0]);
- LcdWriteData(disp_R[1]);
- LcdWriteData(disp_R[2]); //紅
- delay(100) ;
- }
- }
復(fù)制代碼
|
|