標(biāo)題: 顏色傳感器單片機(jī)不能讀取數(shù)值,各位好,能不能指導(dǎo)一下小弟的東西? [打印本頁]

作者: 傅小諾    時(shí)間: 2017-7-27 12:13
標(biāo)題: 顏色傳感器單片機(jī)不能讀取數(shù)值,各位好,能不能指導(dǎo)一下小弟的東西?
如題。求各位大佬幫忙。如圖,一個(gè)顏色傳感器,一個(gè)單片機(jī)。我現(xiàn)有的程序只能讓LCD屏顯示,并不能讀取顏色傳感器的數(shù)值,求大佬幫忙。另外,接線的話。能不能也請(qǐng)各位指導(dǎo)一下。。拜謝。。。

程序如下:

  1. /*******************************************
  2. * 文件名: TCS3200顏色測(cè)試
  3. * 型號(hào):   GY-31
  4. * 功能:   讀取RGB值
  5. * 單片機(jī): STC89C52
  6. * 晶振:   11.0592m
  7. * 時(shí)間:   2011-3-20
  8. * LCD1602顯示R,G,B值
  9. /********************************************/
  10. //接線說明:
  11. //模塊S2-----單片機(jī)P1.1
  12. //模塊S3-----單片機(jī)P1.0
  13. //模塊OUT----單片機(jī)P3.5(計(jì)數(shù)器1輸入)
  14. //模塊VCC----單片機(jī)VCC
  15. //模塊GND----單片機(jī)GND
  16. //**********************************************
  17. #include<reg51.h>
  18. #include<lcd.h>         
  19. #include<math.h>       //Keil library  
  20. #include<stdio.h>      //Keil library        
  21. #include<INTRINS.H>
  22. #define uchar unsigned char
  23. #define uint  unsigned int        
  24. #define DataPort P0           //LCD1602 數(shù)據(jù)端口        
  25. sbit    LCM_RS=P2^6;   //LCD1602 控制端口               
  26. sbit    LCM_RW=P2^5;   //LCD1602 控制端口        
  27. sbit    LCM_EN=P2^7;   //LCD1602 控制端口

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

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

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


  51. //**********延時(shí)函數(shù)***************
  52. void delay(unsigned int k)        
  53. {                                                
  54.         unsigned int i,j;                                
  55.         for(i=0;i<k;i++)
  56.         {                        
  57.         for(j=0;j<121;j++)                        
  58.         {;}
  59.         }                                                
  60. }                                                            

  61. /*******************************************
  62. * 函數(shù)名稱: t0_init()
  63. * 函數(shù)功能: 定時(shí)器0初始化
  64. * 入口參數(shù): 無
  65. * 出口參數(shù): 無
  66. /********************************************/
  67. void t0_init()
  68. {
  69.      TMOD=0x51;        //T1計(jì)數(shù) T0定時(shí) 工作方式1

  70.      TH1=0x00;        //計(jì)數(shù)初值
  71.      TL1=0x00;

  72.      TH0=0xE0;
  73.      TL0=0x00;        //11。0592M 晶振10ms
  74.      EA=1;            //開中斷

  75.      ET0=1;        
  76.      TR0=1;           //啟動(dòng)
  77.      TR1=1;
  78. }

  79. //*********************************************
  80. //數(shù)值轉(zhuǎn)換出個(gè)十百千的ASCII碼
  81. //*********************************************
  82. void conversion(uint temp_data)  
  83. {  
  84.     bai=temp_data/100+0x30 ;
  85.     temp_data=temp_data%100;   //取余運(yùn)算
  86.     shi=temp_data/10+0x30 ;
  87.     ge=temp_data%10+0x30;      //取余運(yùn)算
  88. }

  89. /*******************************************
  90. * 函數(shù)名稱: c10ms_out()
  91. * 函數(shù)功能: 定時(shí)中斷0服務(wù)程序
  92.             修改顏色標(biāo)志disp_tc(0:紅 1:綠 2:藍(lán))
  93.             設(shè)置S0 S1 S2 選擇濾波器
  94.             計(jì)算脈沖,讀取色值
  95. * 入口參數(shù): 無
  96. * 出口參數(shù): 無
  97. /********************************************/
  98. void c10ms_out() interrupt 1
  99. {  uint temp;
  100.         test_pin=!test_pin; //測(cè)試定時(shí)器中斷頻率引腳,可以用示波器觀察
  101.         TR0=0;              //關(guān)閉定時(shí)
  102.         TR1=0;              //關(guān)閉計(jì)數(shù)
  103. //   count+1實(shí)現(xiàn)先檢測(cè)綠色,再檢測(cè)藍(lán)色,然后檢測(cè)紅色,循環(huán)檢測(cè)      
  104.         if(count==0)
  105.         {
  106.         count++;   
  107.         s2=1;s3=1;             //選擇濾波器為綠色     
  108.          
  109.                 temp=(8<<TH1)+TL1;    //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)        
  110.                 temp/=rp;                        
  111.         conversion(temp);
  112.                 disp_R[2]=ge;         //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
  113.                 disp_R[1]=shi;
  114.                 disp_R[0]=bai;
  115.         }

  116.         else if(count==1)
  117.         {            
  118.         count++;
  119.         s2=0;s3=1;            //選擇濾波器為藍(lán)色

  120.                 temp=(8<<TH1)+TL1;    //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)        
  121.                 temp/=gp;                        
  122.         conversion(temp);
  123.                 disp_G[2]=ge;         //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
  124.                 disp_G[1]=shi;
  125.                 disp_G[0]=bai;
  126.         }

  127.         else if(count==2)
  128.         {            
  129.         count=0;
  130.         s2=0;s3=0;            //選擇濾波器為紅色

  131.                 temp=(8<<TH1)+TL1;    //計(jì)算這段時(shí)間內(nèi) TCS230 的輸出脈沖數(shù)        
  132.                 temp/=bp;               
  133.         conversion(temp);
  134.               disp_B[2]=ge;         //因?yàn)檫@次的中斷,是上次選擇濾波器的數(shù)值
  135.                 disp_B[1]=shi;
  136.                 disp_B[0]=bai;

  137.         }

  138.      //定時(shí)器計(jì)數(shù)器重賦初值
  139.      TH0=0xE0;
  140.      TL0=0x00; //11。0592M 晶振,為10ms
  141.      TL1=0x00;//計(jì)數(shù)器清零
  142.      TH1=0x00;//計(jì)數(shù)器清零
  143.      TR0=1;   //打開定時(shí)器
  144.      TR1=1;   //打開計(jì)數(shù)器
  145. }


  146. /*******************************************
  147. * 函數(shù)名稱: main()
  148. /********************************************/
  149. void main()
  150. {
  151.       delay(10);
  152.       LcdInit();                  //LCD初始化子程序
  153.           s2=0;           //初始設(shè)定S2引腳
  154.           s3=0;           //初始設(shè)定S3引腳
  155.       t0_init();      //定時(shí)計(jì)數(shù)初使化

  156.      while(1)
  157.      {     
  158.         LcdWriteCom(0x80);
  159.                 LcdWriteData('G');
  160.                 LcdWriteData(':');
  161.                 LcdWriteData(disp_G[0]);
  162.                 LcdWriteData(disp_G[1]);
  163.                 LcdWriteData(disp_G[2]);   //綠

  164.                 LcdWriteCom(0x80+6);
  165.                 LcdWriteData('B');
  166.                 LcdWriteData(':');
  167.                 LcdWriteData(disp_B[0]);
  168.                 LcdWriteData(disp_B[1]);
  169.                 LcdWriteData(disp_B[2]);   //藍(lán)

  170.                 LcdWriteCom(0xc0);
  171.                 LcdWriteData('R');
  172.                 LcdWriteData(':');
  173.                 LcdWriteData(disp_R[0]);
  174.                 LcdWriteData(disp_R[1]);
  175.                 LcdWriteData(disp_R[2]);   //紅

  176.         delay(100) ;
  177.      }
  178. }
復(fù)制代碼




作者: cjjcjj1    時(shí)間: 2017-7-27 17:58
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
作者: yzwzfyz    時(shí)間: 2017-7-27 23:38
研讀傳感器手冊(cè)




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