Simple test for TCS230, color light-to-frequency converter.

Dependencies:   mbed

Committer:
lnadal
Date:
Sun Aug 28 07:53:11 2011 +0000
Revision:
0:211fa784ae3e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lnadal 0:211fa784ae3e 1 /*
lnadal 0:211fa784ae3e 2 ****************************************************
lnadal 0:211fa784ae3e 3 TCS230 test. COLOR LIGHT TO FREQUENCY CONVERTER.
lnadal 0:211fa784ae3e 4 Frequency is measured by interrupts.
lnadal 0:211fa784ae3e 5
lnadal 0:211fa784ae3e 6 Wiring: TCS230 mBed
lnadal 0:211fa784ae3e 7 p1(s0) p5
lnadal 0:211fa784ae3e 8 p2(s1) p6
lnadal 0:211fa784ae3e 9 p7(s2) p7
lnadal 0:211fa784ae3e 10 p8(s3) p8
lnadal 0:211fa784ae3e 11 p6(out) p9
lnadal 0:211fa784ae3e 12 p3(OE), p4(GND) tied to ground. p5(Vcc) tied to 3V3.
lnadal 0:211fa784ae3e 13
lnadal 0:211fa784ae3e 14 Author: Lluis Nadal. August 2011.
lnadal 0:211fa784ae3e 15 ****************************************************
lnadal 0:211fa784ae3e 16 */
lnadal 0:211fa784ae3e 17
lnadal 0:211fa784ae3e 18
lnadal 0:211fa784ae3e 19 #include "mbed.h"
lnadal 0:211fa784ae3e 20
lnadal 0:211fa784ae3e 21
lnadal 0:211fa784ae3e 22 Serial pc(USBTX, USBRX);
lnadal 0:211fa784ae3e 23 InterruptIn in(p9);
lnadal 0:211fa784ae3e 24 DigitalOut s0(p5), s1(p6); // s2(p7), s3(p8)
lnadal 0:211fa784ae3e 25 BusOut setColor(p8, p7); //(LSB pin,..., MSB pin): (s3, s2). Red: 0, Blue: 1, Clear: 2, Green: 3.
lnadal 0:211fa784ae3e 26 Timer t;
lnadal 0:211fa784ae3e 27
lnadal 0:211fa784ae3e 28 float period = 0; // This is the period between interrupts in microseconds
lnadal 0:211fa784ae3e 29 float freq = 0;
lnadal 0:211fa784ae3e 30 int n;
lnadal 0:211fa784ae3e 31 int color; // Color
lnadal 0:211fa784ae3e 32
lnadal 0:211fa784ae3e 33
lnadal 0:211fa784ae3e 34 void print() { // Print to PC
lnadal 0:211fa784ae3e 35 switch (color) {
lnadal 0:211fa784ae3e 36 case 0:
lnadal 0:211fa784ae3e 37 pc.printf(" Red: \t\t%.2f Hz, \t%.2f us\r\n", freq, period);
lnadal 0:211fa784ae3e 38 break;
lnadal 0:211fa784ae3e 39 case 1:
lnadal 0:211fa784ae3e 40 pc.printf(" Blue: \t\t%.2f Hz, \t%.2f us\r\n", freq, period);
lnadal 0:211fa784ae3e 41 break;
lnadal 0:211fa784ae3e 42 case 2:
lnadal 0:211fa784ae3e 43 pc.printf(" Clear: \t%.2f Hz, \t%.2f us\r\n", freq, period);
lnadal 0:211fa784ae3e 44 break;
lnadal 0:211fa784ae3e 45 case 3:
lnadal 0:211fa784ae3e 46 pc.printf(" Green: \t%.2f Hz, \t%.2f us\r\n", freq, period);
lnadal 0:211fa784ae3e 47 pc.printf("\r\n");
lnadal 0:211fa784ae3e 48 break;
lnadal 0:211fa784ae3e 49 }
lnadal 0:211fa784ae3e 50 }
lnadal 0:211fa784ae3e 51
lnadal 0:211fa784ae3e 52
lnadal 0:211fa784ae3e 53 void time() {
lnadal 0:211fa784ae3e 54 if (n>99) { // Wait 100 interrupts
lnadal 0:211fa784ae3e 55 period = t.read_us()/(float)n; // Get time
lnadal 0:211fa784ae3e 56 freq = (1/period)*1000000; // Convert period (in us) to frequency (Hz). Works up to 100kHz.
lnadal 0:211fa784ae3e 57 n = 0;
lnadal 0:211fa784ae3e 58
lnadal 0:211fa784ae3e 59 print(); // Print values to PC
lnadal 0:211fa784ae3e 60
lnadal 0:211fa784ae3e 61 color++;
lnadal 0:211fa784ae3e 62 if (color > 3) color = 0;
lnadal 0:211fa784ae3e 63 setColor = color;
lnadal 0:211fa784ae3e 64 wait(0.5);
lnadal 0:211fa784ae3e 65 t.reset(); // Reset timer and wait for next interrupt
lnadal 0:211fa784ae3e 66 }
lnadal 0:211fa784ae3e 67 n++;
lnadal 0:211fa784ae3e 68 }
lnadal 0:211fa784ae3e 69
lnadal 0:211fa784ae3e 70
lnadal 0:211fa784ae3e 71 int main() {
lnadal 0:211fa784ae3e 72
lnadal 0:211fa784ae3e 73 in.mode(PullDown); // Set the pin to Pull Down mode.
lnadal 0:211fa784ae3e 74 wait(1);
lnadal 0:211fa784ae3e 75 n = 0;
lnadal 0:211fa784ae3e 76 color = 0;
lnadal 0:211fa784ae3e 77 setColor = color;
lnadal 0:211fa784ae3e 78
lnadal 0:211fa784ae3e 79 s0 = 0;
lnadal 0:211fa784ae3e 80 s1 = 1; // Frequency 2% = 12 kHz full-scale.
lnadal 0:211fa784ae3e 81
lnadal 0:211fa784ae3e 82 in.rise(&time); // Set up the interrupt for rising edge
lnadal 0:211fa784ae3e 83 t.start(); // Start the timer
lnadal 0:211fa784ae3e 84
lnadal 0:211fa784ae3e 85 while (1) {
lnadal 0:211fa784ae3e 86
lnadal 0:211fa784ae3e 87 }
lnadal 0:211fa784ae3e 88 }
lnadal 0:211fa784ae3e 89
lnadal 0:211fa784ae3e 90
lnadal 0:211fa784ae3e 91