欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標(biāo)題: TCS230顏色識(shí)別傳感器單片機(jī)編程源碼 LCD1602顯示R,G,B值 [打印本頁(yè)]

作者: 貔貅求    時(shí)間: 2018-7-19 15:18
標(biāo)題: TCS230顏色識(shí)別傳感器單片機(jī)編程源碼 LCD1602顯示R,G,B值
好好學(xué)習(xí),天天向上

電路原理圖如下:


單片機(jī)源程序如下:
  1. /*
  2. * TCS3200模塊
  3. *
  4. * 用途:TCS3200顏色測(cè)試,讀取RGB值,LCD1602顯示R,G,B值
  5. *
  6. * 作者                                        日期                                備注
  7. * Huafeng Lin                        2010/12/10                        新增
  8. * Huafeng Lin                        2010/12/11                        修改
  9. *
  10. */

  11. //接線說(shuō)明:
  12. //模塊S2-----單片機(jī)P1.1
  13. //模塊S3-----單片機(jī)P1.0
  14. //模塊OUT----單片機(jī)P3.5(計(jì)數(shù)器1輸入)
  15. //模塊VCC----單片機(jī)VCC
  16. //模塊GND----單片機(jī)GND

  17. #include<REG52.H>        
  18. #include<math.h>       //Keil library  
  19. #include<stdio.h>      //Keil library        
  20. #include<INTRINS.H>

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

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

  39. //顯示數(shù)組
  40. uchar disp_R[3];  //紅
  41. uchar disp_G[3];  //綠
  42. uchar disp_B[3];  //藍(lán)

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

  49. //*********LCD1602初始化**********************
  50. void InitLcd()                                
  51. {                        
  52.         WriteCommandLCM(0x38,1);        
  53.         WriteCommandLCM(0x08,1);        
  54.         WriteCommandLCM(0x01,1);
  55.         WriteCommandLCM(0x06,1);        
  56.         WriteCommandLCM(0x0c,1);
  57. }

  58. //**********檢測(cè)忙信號(hào)************************
  59. void WaitForEnable(void)        
  60. {                                       
  61.         DataPort=0xff;               
  62.         LCM_RS=0;LCM_RW=1;_nop_();
  63.         LCM_EN=1;_nop_();_nop_();
  64.         while(DataPort&0x80);        
  65.         LCM_EN=0;                                
  66. }
  67.                                        
  68. //**********寫(xiě)命令至LCD***********************
  69. void WriteCommandLCM(uchar CMD,uchar Attribc)
  70. {                                       
  71.         if(Attribc)WaitForEnable();        
  72.         LCM_RS=0;LCM_RW=0;_nop_();
  73.         DataPort=CMD;_nop_();        
  74.         LCM_EN=1;_nop_();_nop_();LCM_EN=0;
  75. }        
  76.                                 
  77. //**********寫(xiě)數(shù)據(jù)至LCD************************
  78. void WriteDataLCM(uchar dataW)
  79. {                                       
  80.         WaitForEnable();               
  81.         LCM_RS=1;LCM_RW=0;_nop_();
  82.         DataPort=dataW;_nop_();        
  83.         LCM_EN=1;_nop_();_nop_();LCM_EN=0;
  84. }
  85.                                        
  86. //*********寫(xiě)一個(gè)字符數(shù)據(jù)到指定的目標(biāo)***********
  87. void DisplayOneChar(uchar X,uchar Y,uchar DData)
  88. {                                                
  89.         Y&=1;                                                
  90.         X&=15;                                                
  91.         if(Y)X|=0x40;                                       
  92.         X|=0x80;                        
  93.         WriteCommandLCM(X,0);               
  94.         WriteDataLCM(DData);               
  95. }

  96. //**********延時(shí)函數(shù)***************
  97. void delay(unsigned int k)        
  98. {                                                
  99.         unsigned int i,j;                                
  100.         for(i=0;i<k;i++)
  101.         {                        
  102.                 for(j=0;j<121;j++)                        
  103.                 {;}
  104.         }                                                
  105. }                                                            

  106. /*******************************************
  107. * 函數(shù)名稱(chēng): t0_init()
  108. * 函數(shù)功能: 定時(shí)器0初始化
  109. * 入口參數(shù): 無(wú)
  110. * 出口參數(shù): 無(wú)
  111. /********************************************/
  112. void t0_init()
  113. {
  114.         TMOD=0x51;        //T1計(jì)數(shù) T0定時(shí) 工作方式1
  115.         
  116.         TH1=0x00;        //計(jì)數(shù)初值
  117.         TL1=0x00;
  118.         
  119.         TH0=0xE0;
  120.         TL0=0x00;        //11。0592M 晶振10ms
  121.         EA=1;            //開(kāi)中斷
  122.         
  123.         ET0=1;        
  124.         TR0=1;           //啟動(dòng)
  125.         TR1=1;
  126. }

  127. //*********************************************
  128. //數(shù)值轉(zhuǎn)換出個(gè)十百千的ASCII碼
  129. //*********************************************
  130. void conversion(uint temp_data)  
  131. {  
  132.     bai=temp_data/100+0x30 ;
  133.     temp_data=temp_data%100;   //取余運(yùn)算
  134.     shi=temp_data/10+0x30 ;
  135.     ge=temp_data%10+0x30;      //取余運(yùn)算
  136. }

  137. /*******************************************
  138. * 函數(shù)名稱(chēng): main()
  139. /********************************************/
  140. void main()
  141. {
  142.         delay(10);
  143.         InitLcd();      //lcd初始化
  144.         s2=0;           //初始設(shè)定S2引腳
  145.         s3=0;           //初始設(shè)定S3引腳
  146.         t0_init();      //定時(shí)計(jì)數(shù)初使?

  147.         while(1)
  148.         {
  149.                 DisplayOneChar(0, 0, 'T');
  150.                 DisplayOneChar(1, 0, 'C');
  151.                 DisplayOneChar(2, 0, 'S');
  152.                 DisplayOneChar(3, 0, '2');
  153.                 DisplayOneChar(4, 0, '3');
  154.                 DisplayOneChar(5, 0, '0');

  155.                 DisplayOneChar(10, 0, 'R');
  156.                 DisplayOneChar(11, 0, '[');
  157.                 DisplayOneChar(12, 0, disp_R[0]);
  158.                 DisplayOneChar(13, 0, disp_R[1]);
  159.                 DisplayOneChar(14, 0, disp_R[2]);
  160.                 DisplayOneChar(15, 0, ']');        
  161.         
  162.                 DisplayOneChar(0, 1, 'G');
  163.                 DisplayOneChar(1, 1, '[');
  164.                 DisplayOneChar(2, 1, disp_G[0]);
  165.                 DisplayOneChar(3, 1, disp_G[1]);
  166.                 DisplayOneChar(4, 1, disp_G[2]);
  167.                 DisplayOneChar(5, 1, ']');
  168.                
  169.                 DisplayOneChar(10, 1, 'B');
  170.                 DisplayOneChar(11, 1, '[');
  171.                 DisplayOneChar(12, 1, disp_B[0]);
  172.                 DisplayOneChar(13, 1, disp_B[1]);
  173.                 DisplayOneChar(14, 1, disp_B[2]);
  174.                 DisplayOneChar(15, 1, ']');                                
  175.                
  176.                 delay(100) ;        
  177.         }
  178. }

  179. /*******************************************
  180. * 函數(shù)名稱(chēng): c10ms_out()
  181. * 函數(shù)功能: 定時(shí)中斷0服務(wù)程序
  182.             修改顏色標(biāo)志disp_tc(0:紅 1:綠 2:藍(lán))
  183.             設(shè)置S0 S1 S2 選擇濾波器
  184.             計(jì)算脈沖,讀取色值
  185. * 入口參數(shù): 無(wú)
  186. * 出口參數(shù): 無(wú)
  187. /********************************************/
  188. void c10ms_out() interrupt 1
  189. {
  190.         uint temp;
  191.         test_pin=!test_pin; //測(cè)試定時(shí)器中斷頻率引腳,可以用示波器觀察
  192.         TR0=0;              //關(guān)閉定時(shí)
  193.         TR1=0;              //關(guān)閉計(jì)數(shù)
  194.         //   count+1實(shí)現(xiàn)先檢測(cè)綠色,再檢測(cè)藍(lán)色,然后檢測(cè)紅色,循環(huán)檢測(cè)      
  195.         if(count==0)
  196.         {
  197.                 count++;   
  198.                 s2=1;s3=1;             //選擇濾波器為綠色     
  199.                
  200.                 temp=(8<<TH1)+TL1;    //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)        
  201.                 temp/=rp;                        
  202.                 conversion(temp);
  203.                 disp_R[2]=ge;         //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
  204.                 disp_R[1]=shi;
  205.                 disp_R[0]=bai;
  206.         }        
  207.         else if(count==1)
  208.         {            
  209.                 count++;
  210.                 s2=1;s3=0;            //選擇濾波器為藍(lán)色
  211.                 temp=(8<<TH1)+TL1;    //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)        
  212.                 temp/=gp;                        
  213.                 conversion(temp);
  214.                 disp_G[2]=ge;         //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
  215.                 disp_G[1]=shi;
  216.                 disp_G[0]=bai;
  217.         }        
  218.         else if(count==2)
  219.         {            
  220.                 count=0;
  221.                 s2=0;s3=0;            //選擇濾波器為紅色
  222.                
  223.                 temp=(8<<TH1)+TL1;    //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)        
  224.                 temp/=bp;               
  225.                 conversion(temp);
  226.                 disp_B[2]=ge;         //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
  227.                 disp_B[1]=shi;
  228.                 disp_B[0]=bai;        
  229.         }
  230.         
  231.         //定時(shí)器計(jì)數(shù)器重賦初值
  232.         TH0=0xE0;
  233.         TL0=0x00; //11。0592M 晶振,為10ms
  234.         TL1=0x00;//計(jì)數(shù)器清零
  235.         TH1=0x00;//計(jì)數(shù)器清零
  236.         TR0=1;   //打開(kāi)定時(shí)器
  237.         TR1=1;   //打開(kāi)計(jì)數(shù)器
  238. }
復(fù)制代碼

所有資料51hei提供下載:
【Realplay】模塊+顏色傳感器TCS230顏色識(shí)別傳感器.rar (420.51 KB, 下載次數(shù): 55)



作者: hbetterwithy    時(shí)間: 2018-10-24 14:17
你好,代碼里沒(méi)有白平衡的程序嗎,為什么比例因子就固定了?




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