با سلام
کد راه اندازی ماژول تشخیص رنگ tcs3200 با بسکام . با قابلیت تشخیص 10 رنگ.




'color detector project
'auther :saeed darban
'date:25/11/1396
'version:1.0.0
'details: we detect 10 colors by using tsc3200 sensor
'it's give us 3rgb percentege color in frequency variation
'we shoulld calculate frequency to detect colours


$regfile = "m16def.dat"
$crystal = 8000000


Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 , Rs = Portc.5 , E = Portc.4


Config Timer1 = Timer , Prescale = 1 'for time measurment
Stop Timer1




Config Porta.0 = Output
Config Portb.1 = Output
Config Portb.2 = Output
Config Portb.3 = Output
Config Portb.4 = Output
Config Pina.5 = Input
Config Portb.6 = Output


Tcs_output Alias Pinb.5
Buzzer Alias Porta.0
S0 Alias Portb.3 'scaling selector(s0,s1)
S1 Alias Portb.4
S2 Alias Portb.1 'color filter selectors(s2,s1)
S3 Alias Portb.2
Tcs_led Alias Portb.6




Declare Sub Scaling(byval A As Byte , Byval B As Byte)
Declare Sub Rgb_select(byval A As Byte , Byval B As Byte)
Declare Sub Red_measure
Declare Sub Green_measure
Declare Sub Blue_measure
Declare Sub Tcs_measure
Declare Sub Clear_measure
'Declare Sub Tcs_led_on
'Declare Sub Tcs_led_off




Dim A As Byte
Dim Red As Dword
Dim Green As Dword
Dim Blue As Dword
Dim Color As Dword
Dim Object As Long
Dim Temp1 As Dword
Dim Temp2 As Dword
Dim Temp3 As Dword
Dim Smallest As Dword
Dim _red As Dword
Dim _green As Dword
Dim _blue As Dword
Dim C As Dword
Dim R As Dword
Dim B As Dword
Dim G As Dword
Dim Frequency As Dword


Cls
Cursor Off 'clear lcd
'Locate 1 , 5
'Lcd "welcome"
'Wait 2
'cls
'lcd "vahid raouf "
'wait 2
'Cls
'Cursor Off


Call Scaling(0 , 1) '2% scaling
Cls
Lcd "put the thing"
'Waitms 500
Do
B = 0
G = 0
R = 0
C = 0
Red = 0
Blue = 0
Green = 0
_red = 0
_blue = 0
_green = 0


Cls
Lcd "waiting..."
'Lowerline
'Lcd "befor sensor"


'Tcs_led_on


Clear_measure 'to find an object we measure clear color and compare those
Temp1 = C
Waitms 100
'Tcs_led_off
Clear_measure 'absoulut maximum
Temp2 = C
Object = Temp1 - Temp2
Object = Abs(object)
'Lcd "temp1=" ; Temp1
'Lowerline : Lcd "temp2=" ; Temp2
'Lcd "o=" ; Object
Wait 1




If Object > 25 Then 'this value was reached from example downloaded code


Cls
Lcd "object found"
Set Buzzer
Waitms 40
Reset Buzzer
Waitms 20


'Tcs_led_on
Red_measure
_red = R
Blue_measure
_blue = B
Green_measure
_green = G
'Tcs_led_off


If _red < _blue Then
If _red < _green Then
Smallest = _red
Else
Smallest = _green
End If
Else
If _blue < _green Then
Smallest = _blue
Else
Smallest = _green
End If
End If
'scalling


Smallest = Smallest / 10
Red = _red / Smallest
Green = _green / Smallest
Blue = _blue / Smallest


''''''show'''
Cls
Lcd "r=" ; Red ; "-g=" ; Green ; "-b=" ; Blue


'''''result colours'''''''''''''''''


If Red > 8 And Red < 12 And Green > 12 And Green < 18 And Blue > 9 And Blue < 13 Then
Lowerline : Lcd "result:green"


Elseif Red > 36 And Red < 45 And Green > 8 And Green < 12 And Blue > 10 And Blue < 18 Then
Lowerline : Lcd "result:red"


Elseif Red > 8 And Red < 12 And Green > 20 And Green < 32 And Blue > 38 And Blue < 60 Then
Lowerline : Lcd "result:blue"


Elseif Red > 18 And Red < 25 And Green > 17 And Green < 22 And Blue > 8 And Blue < 14 Then
Lowerline : Lcd "result:yellow"


Elseif Red > 8 And Red < 12 And Green > 8 And Green < 14 And Blue > 8 And Blue < 12 Then
Lowerline : Lcd "result:black"


Elseif Red > 18 And Red < 30 And Green > 8 And Green < 14 And Blue > 12 And Blue < 20 Then
Lowerline : Lcd "result:pink"


Elseif Red > 26 And Red < 34 And Green > 9 And Green < 17 And Blue > 8 And Blue < 14 Then
Lowerline : Lcd "resultrange"


Elseif Red > 14 And Red < 22 And Green > 8 And Green < 14 And Blue > 8 And Blue < 14 Then
Lowerline : Lcd "result:brown"


Elseif Red > 14 And Red < 20 And Green > 8 And Green < 14 And Blue > 14 And Blue < 22 Then
Lowerline : Lcd "result:violet"


Elseif Red > 14 And Red < 20 And Green > 16 And Green < 24 And Blue > 8 And Blue < 14 Then
Lowerline : Lcd "result:light green"
Else


Lowerline
Lcd "unknown colour"
End If


Wait 5
End If


Loop
End




Sub Red_measure
'Cls : Lcd "red measure" : Wait 1
Call Rgb_select(0 , 0) 'red filter


Call Tcs_measure
R = Frequency ' / 1000 '/1000 is just for easing the calculation because it make kilo hz to hz
Waitms 10
Call Tcs_measure
R = R + Frequency ' / 1000
Waitms 10
Call Tcs_measure
R = R + Frequency '/ 1000
R = R / 3 'averaging
'Lowerline : Lcd "r=" ; R
'Wait 3
'in an example code that had downloded from the internet here was r=r/3.3 (i dont know why?!!!)
Return
End Sub




Sub Green_measure
'Cls : Lcd "green measure" : Wait 1
Call Rgb_select(1 , 1) 'green filter
G = 0
Call Tcs_measure
G = Frequency ' / 1000
Waitms 10
Call Tcs_measure
G = G + Frequency '/ 1000
Waitms 10
Call Tcs_measure
G = G + Frequency '/ 1000
G = G / 3
'Lowerline : Lcd "g=" ; G
'Wait 3
Return
End Sub






Sub Blue_measure
'Cls : Lcd "blue measure" : Wait 1
Call Rgb_select(0 , 1)
B = 0
Call Tcs_measure
B = Frequency
Waitms 10
Call Tcs_measure
B = B + Frequency
Waitms 10
Call Tcs_measure
B = B + Frequency
B = B / 3 'in an example code that had downloded from the internet here was r=r/4.2 (i dont know why?!!!)
'Lowerline : Lcd "b=" ; B
'Wait 3
Return


End Sub


Sub Clear_measure
'Cls : Lcd "clear measure" : Wait 1
Call Rgb_select(1 , 0) 'clear select
C = 0
Call Tcs_measure
C = Frequency
Waitms 5
Call Tcs_measure
C = C + Frequency
Waitms 5
Call Tcs_measure
C = C + Frequency
C = C / 3
'Lowerline : Lcd "c=" ; C
'Wait 3
Return
End Sub




Sub Tcs_measure
Frequency = 0
Bitwait Tcs_output , Set
Bitwait Tcs_output , Reset 'for debounce and sure that output is low
Tcnt1 = 0
Bitwait Tcs_output , Set
Start Timer1
Bitwait Tcs_output , Reset
Stop Timer1
Temp3 = Tcnt1 * 2 'for earn priod
Frequency = 8000000 / Temp3 'for earn real frequency we must multipy oscilator frequency in our measurment
Return
End Sub


Sub Scaling(a As Byte , B As Byte)
Select Case A
Case 0 : S0 = 0
Case 1 : S0 = 1
End Select


Select Case B
Case 0 : S1 = 0
Case 1 : S1 = 1
End Select
Return
End Sub






Sub Rgb_select(a As Byte , B As Byte)
Select Case A
Case 0 : S2 = 0
Case 1 : S2 = 1
End Select


Select Case B
Case 0 : S3 = 0
Case 1 : S3 = 1
End Select
Return
End Sub