By Spencer Kiser

This code reads color values from the Taos TCS230 color sensor and sends them out serially in ASCII-formatted values.

In PicBasic Pro for a PIC 18F252/452.

Comments by tigoe



define OSC 20

' set up portB and portC as all outputs except for portB.0 and portc.0:
TRISB = %00000001
TRISC = %00000001

' TCS230 connections:
OE VAR PORTB.0      ' Output Enable
S0 VAR PORTB.1      ' sensor channel 0 trigger
S1 VAR PORTB.2      ' sensor channel 1 trigger
S2 VAR PORTB.3      ' sensor channel 2 trigger
S3 VAR PORTB.4      ' sensor channel 3 trigger
nLED var PORTB.6    ' illuminating LED
OUT var PORTB.7     ' TCS230 output

tx VAR portc.6      ' serial transmit
rx VAR portc.7      ' serial receive
inv9600 con 16468
CR CON 13
LF CON 10

' counter divisors for the respective colors
pRED con 12
pGreen con 6
pBlue con 5

' variables to store color values
RED var word
GREEN var word
BLUE var word

main:
   LOW OE
   HIGH S0
   HIGH S1
   LOW nLED

   gosub Color

   serout2 tx, inv9600, ["R", DEC RED," G",dec GREEN," B",dec BLUE," ",CR,LF]
GoTo main

color:
   low S2
   low S3
   count OUT, pRed, RED
   high S3
   count OUT, pBLUE, BLUE
   high S2
   count OUT, pGreen, GREEN
return