找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4984|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

TCS230模塊資料 原理圖 測試程序 用于尋跡小車

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:113347 發(fā)表于 2016-4-10 20:06 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
模塊用于尋跡小車

  1. /*
  2. * TCS3200模塊
  3. *
  4. * 用途:TCS3200顏色測試,讀取RGB值,LCD1602顯示R,G,B值
  5. *
  6. * 作者                                        日期                                備注
  7. * Huafeng Lin                        2010/12/10                        新增
  8. * Huafeng Lin                        2010/12/11                        修改
  9. *
  10. */

  11. //接線說明:
  12. //模塊S2-----單片機P1.1
  13. //模塊S3-----單片機P1.0
  14. //模塊OUT----單片機P3.5(計數(shù)器1輸入)
  15. //模塊VCC----單片機VCC
  16. //模塊GND----單片機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=P2^4;   //LCD1602 控制端口               
  26. sbit    LCM_RW=P2^5;   //LCD1602 控制端口       
  27. sbit    LCM_EN=P2^6;   //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;  //用示波器看這個引腳,可知道定時器中斷頻率
  35. //變量、常量定義
  36. uchar ge,shi,bai ;
  37. uchar rp=3,gp=3,bp=6; //定義比例因子,具體環(huán)境可以修改
  38. uchar count;          //顏色標(biāo)志位(0:紅 1:綠 2:藍)

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

  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. //**********檢測忙信號************************
  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. //**********寫命令至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. //**********寫數(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. //*********寫一個字符數(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ù)***************
  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ù)名稱: t0_init()
  108. * 函數(shù)功能: 定時器0初始化
  109. * 入口參數(shù): 無
  110. * 出口參數(shù): 無
  111. /********************************************/
  112. void t0_init()
  113. {
  114.         TMOD=0x51;        //T1計數(shù) T0定時 工作方式1
  115.        
  116.         TH1=0x00;        //計數(shù)初值
  117.         TL1=0x00;
  118.        
  119.         TH0=0xE0;
  120.         TL0=0x00;        //11。0592M 晶振10ms
  121.         EA=1;            //開中斷
  122.        
  123.         ET0=1;        
  124.         TR0=1;           //啟動
  125.         TR1=1;
  126. }

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

  137. /*******************************************
  138. * 函數(shù)名稱: 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ù)初使?

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


TCS230模塊.zip

425.99 KB, 下載次數(shù): 32, 下載積分: 黑幣 -5

必須模塊

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表