Lluis Nadal / Mbed 2 deprecated TCS230_Test

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 ****************************************************
00003 TCS230 test. COLOR LIGHT TO FREQUENCY CONVERTER.
00004 Frequency is measured by interrupts.
00005 
00006 Wiring: TCS230       mBed
00007          p1(s0)      p5
00008          p2(s1)      p6
00009          p7(s2)      p7
00010          p8(s3)      p8
00011          p6(out)     p9
00012 p3(OE), p4(GND) tied to ground. p5(Vcc) tied to 3V3.
00013 
00014 Author: Lluis Nadal. August 2011.
00015 ****************************************************
00016 */
00017 
00018 
00019 #include "mbed.h"
00020 
00021 
00022 Serial pc(USBTX, USBRX);
00023 InterruptIn in(p9);
00024 DigitalOut s0(p5), s1(p6); // s2(p7), s3(p8)
00025 BusOut setColor(p8, p7); //(LSB pin,..., MSB pin): (s3, s2). Red: 0, Blue: 1, Clear: 2, Green: 3.
00026 Timer t;
00027 
00028 float period = 0; // This is the period between interrupts in microseconds
00029 float freq = 0;
00030 int n;
00031 int color; // Color
00032 
00033 
00034 void print() {  // Print to PC
00035     switch (color) {
00036         case 0:
00037             pc.printf(" Red: \t\t%.2f Hz, \t%.2f us\r\n", freq, period);
00038             break;
00039         case 1:
00040             pc.printf(" Blue: \t\t%.2f Hz, \t%.2f us\r\n", freq, period);
00041             break;
00042         case 2:
00043             pc.printf(" Clear: \t%.2f Hz, \t%.2f us\r\n", freq, period);
00044             break;
00045         case 3:
00046             pc.printf(" Green: \t%.2f Hz, \t%.2f us\r\n", freq, period);
00047             pc.printf("\r\n");
00048             break;
00049     }
00050 }
00051 
00052 
00053 void time() {
00054     if (n>99) { // Wait 100 interrupts
00055         period = t.read_us()/(float)n; // Get time
00056         freq = (1/period)*1000000;   // Convert period (in us) to frequency (Hz). Works up to 100kHz.
00057         n = 0;
00058 
00059         print(); // Print values to PC
00060 
00061         color++;
00062         if (color > 3) color = 0;
00063         setColor = color;
00064         wait(0.5);
00065         t.reset(); // Reset timer and wait for next interrupt
00066     }
00067     n++;
00068 }
00069 
00070 
00071 int main() {
00072 
00073     in.mode(PullDown); // Set the pin to Pull Down mode.
00074     wait(1);
00075     n = 0;
00076     color = 0;
00077     setColor = color;
00078 
00079     s0 = 0;
00080     s1 = 1; // Frequency 2% = 12 kHz full-scale.
00081 
00082     in.rise(&time);  // Set up the interrupt for rising edge
00083     t.start();       // Start the timer
00084 
00085     while (1) {
00086 
00087     }
00088 }
00089 
00090 
00091