to test synchronize signals by a 22k signal.

Files at this revision

API Documentation at this revision

Comitter:
test
Date:
Tue Apr 17 07:40:02 2012 +0000
Commit message:
1.0

Changed in this revision

7SegSRDriver.cpp Show annotated file Show diff for this revision Revisions of this file
7SegSRDriver.h Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 3d76f0b19b14 7SegSRDriver.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/7SegSRDriver.cpp	Tue Apr 17 07:40:02 2012 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+#include "7SegSRDriver.h"
+
+/* mbed 7-Segment Display Driver Library (via an 8bit Shift Register)
+ * Copyright (c) 2011 Paul Law
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// Display should be hooked up to shift register as follows:
+// Q0 : Decimal Point, Q1-Q7 : Segments a-g
+
+SSegSRDriver::SSegSRDriver(PinName srData, PinName srClock, PinName srLatch, bool disp_type): _srData(srData), _srClock(srClock), _srLatch(srLatch) {
+    _disp_type = disp_type;
+    write_raw(0);
+}
+
+void SSegSRDriver::set_type(bool disp_type) {
+    _disp_type = disp_type;
+}
+
+void SSegSRDriver::clear() {
+    write_raw(0);
+}
+
+void SSegSRDriver::write(unsigned char number) {
+    write(number,0);
+}
+
+void SSegSRDriver::write(unsigned char number, bool dp) {
+    if (number<16) {
+        // Find the definition of the number in chardefs, shift left, set dp (LSB)
+        if(!_disp_type) //if Common Anode
+            write_raw((SSegSRDriver_chardefs[number])<<1 | dp); //small change to fit taobao buy LEDs.
+        else //if common Cathode
+            write_raw((SSegSRDriver_chardefs[number]) | (dp<<0x7)); //small change to fit taobao buy LEDs.
+    }
+}
+
+void SSegSRDriver::write_raw(unsigned char bValue) {
+
+    if (!_disp_type) bValue = ~bValue;  // Reverse value for Common Anode
+
+    if (bValue<=0xFF) {
+        // Push value to shift register and latch
+        _srLatch = 0;
+        for (int i=7;i>=0;i--) {    // Output MSB to LSB
+            _srClock = 0;
+            _srData = (bValue & (1<<i));
+            _srClock = 1;
+        }
+        _srLatch = 1;
+        _srData = 0;
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 3d76f0b19b14 7SegSRDriver.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/7SegSRDriver.h	Tue Apr 17 07:40:02 2012 +0000
@@ -0,0 +1,109 @@
+/* mbed 7-Segment Display Driver Library (via an 8bit Shift Register)
+ * Copyright (c) 2011 Paul Law
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef LIB_SSEGSRDRIVER_H
+#define LIB_SSEGSRDRIVER_H
+
+#include "mbed.h"
+
+#define SSegSRDriver_COMN_ANODE 0
+#define SSegSRDriver_COMN_CATHODE 1
+
+//Char defs: 0123456789AbCdEF
+const unsigned char SSegSRDriver_chardefs[16] = {0x3F, 0x6, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x7, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 };
+
+/** 7-Segment Display Driver class, via a 8-bit shift register (such as the 74HC595)
+ *
+ * Display should be hooked up to shift register as follows:
+ * Q0 : Decimal Point, Q1-Q7 : Segments a-g
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "7SegSRDriver.h"
+ *
+ * SSegSRDriver display(p27,p25,p26, SSegSRDriver_COMN_ANODE);
+ *
+ * int main() {
+ *     while (1) {
+ *         // Show the chars 0-9 then A-F, flashing the decimal point on and off
+ *         for (int i=0; i<16; i++) {
+ *             display.write(i,i%2 == 0);
+ *             wait(1);
+ *         }
+ *     }
+ * }
+ * @endcode
+ */
+class SSegSRDriver {
+
+public:
+
+    /** Create a 7-Segment Display Driver object connected to a 8-bit shift register on the given DigitalOut pins
+     *
+     * @param srData Shift Register Data pin (DigitalOut)
+     * @param srClock Shift Register Clock pin (DigitalOut)
+     * @param srLatch Shift Register Latch pin (DigitalOut)
+     * @param disp_type Display Type: Common Anode/Cathode (SSegSRDriver_COMN_ANODE (0) or SSegSRDriver_COMN_CATHODE (1))
+     */
+    SSegSRDriver(PinName srData, PinName srClock, PinName srLatch, bool disp_type);
+
+    /** Change the type of 7-Segment Display
+     *
+     * @param disp_type Display Type: Common Anode/Cathode (SSegSRDriver_COMN_ANODE (0) or SSegSRDriver_COMN_CATHODE (1))
+     */
+    void set_type(bool disp_type);
+    
+    /** Sets the currently shown digit on the display
+     *
+     * @param number The digit to display (0-15) to show numbers 0-9 and letters A-F
+     */
+    void write(unsigned char number); 
+    
+    /** Sets the currently shown digit on the display along with the decimal place
+     *
+     * @param number The digit to display (0-15) to show numbers 0-9 and letters A-F
+     * @param dp If the decimal place should be lit (0:Off, 1:On)
+     */
+    void write(unsigned char number, bool dp);   
+    
+    /** Sets the segments of the display directly
+     * Segments are lit by binary flags where LSB is the decimal point, then segments a-g up to the MSB
+     * e.g. to light segments e, f and the decimal place = 01100001
+     *
+     * @param bValue The segments to light
+     */
+    void write_raw(unsigned char number);   // Write directly to the display - Q0/Q1-Q7
+    
+    /** Turn all segments of the display off
+     */
+    void clear();   // Clears the display
+    
+private:
+
+    DigitalOut _srData;
+    DigitalOut _srClock;
+    DigitalOut _srLatch;
+    bool _disp_type;
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 3d76f0b19b14 TextLCD.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Tue Apr 17 07:40:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 000000000000 -r 3d76f0b19b14 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 17 07:40:02 2012 +0000
@@ -0,0 +1,157 @@
+#include "mbed.h"
+#include "7SegSRDriver.h"
+#include "TextLCD.h" //driver 1602 lcd screen
+
+#define pin_debug 0
+#if pin_debug
+DigitalOut pin8(P2_7),pin6(P1_24);
+Ticker pinFlip;
+#endif
+
+Timeout CalcRate;
+DigitalOut led1(P2_2);
+DigitalIn signal(P2_9),XFC(P2_8);;
+//InterruptIn flip(P1_21),flip1(P1_23);    in mbed P1_21 is defined to be LED1, connect LCD to 3V3, can't generate interrupt
+//InterruptIn flip(P2_8),flip1(P2_9); //flip XFC flip1 signal
+TextLCD lcd(P0_24, P0_25, P0_26, P0_3, P0_2,P1_0,TextLCD::LCD16x2); // rs, e, d4-d7
+int signalTotal,XFCTotal,sync_Pass,sync_Fail;
+int i,max; //used for dataToArry function
+int printArray[4];
+float dataToarray(float data);
+int sync_delay = 16;// sync delay micro seconds.
+void pinflip();
+//7 segment class definition,Data,Clock,latch
+SSegSRDriver display(P2_1,P0_7,P0_9, SSegSRDriver_COMN_CATHODE);
+
+
+void calcRate() {
+    float rate = 0;
+    int tempsignalTotal,temp_pass,temp_fail;
+    int j =0;
+    int LEDArray[4];
+#if pin_debug
+    pinFlip.detach();
+#endif
+    //put to temp value to ensure these values not change after this.
+    tempsignalTotal =signalTotal;
+    temp_pass = sync_Pass;
+    temp_fail = sync_Fail;
+    if(sync_Pass>0)
+        rate = (float)temp_pass/(float)(temp_pass+temp_fail)*100;
+    else
+        rate = 0;
+    i=max=0;
+       lcd.cls();
+    if (rate>50) {
+        for (j= 0; j<=3; j++) //clear printArray buffer to 0
+            printArray[j] = 0;
+        dataToarray(rate);
+        //lcd.printf("XFC: %6dHz\n",tempXFCTotal*2); //print XFC: 023000Hz,remeber I'm using one counter to trigger the count
+        lcd.printf("Working\n"); //Don't show XFC message.
+        lcd.printf("Sig: %6dHz\n",tempsignalTotal);//print Sig: 023000Hz
+        j = max; //max is from dataToarray, global variable to index the lenth of the array
+        //printf("The array to print is:");
+        for (int a=0; a<=4; a++)
+            display.clear();
+        
+        while (j<3) {
+            //printf("%d",0);
+            j++;
+            for (int tempIndex = j; tempIndex>=1; tempIndex--)
+                printArray[tempIndex]=printArray[tempIndex-1];
+            printArray[0] = 0;
+        }
+        for (j=0; (j<=3); j++) {
+            //printf("%d ",printArray[j]);
+            LEDArray[j]=printArray[j];
+        }
+        if (max>3) { //which means 100%
+            display.write(LEDArray[3],false);
+            display.write(LEDArray[2],true);
+            display.write(LEDArray[1],false);
+            display.write(LEDArray[0],false);
+        } else {
+            display.write(LEDArray[3],false);
+            display.write(LEDArray[2],false);
+            display.write(LEDArray[1],true);
+            display.write(LEDArray[0],false);
+        }
+    }
+    else{
+      lcd.printf("Warning!\n"); //show warning message message.
+      lcd.printf("No Sync signal\n");
+    }
+     signalTotal = sync_Pass=sync_Fail =0;
+    CalcRate.attach(&calcRate,1.0);
+    led1 = !led1;
+    
+#if pin_debug
+    pinFlip.attach_us(&pinflip,10); //the best speed is 100k/s
+#endif
+}
+#if pin_debug
+void pinflip() {
+    pin8 = !pin8;
+}
+#endif
+float dataToarray(float data) {
+    if (data>=0.1) {
+        i++; //counter increase
+        data = dataToarray(data/10)*10;
+    }
+
+    if (max<=i)
+        //make sure max is the biggest number pointer
+        max = i;
+    float tempData;
+    tempData = data*100;
+    printArray[max-i] = (int)tempData;
+    data = (tempData-printArray[max-i])/100;
+    i--;
+    return (data);
+
+
+}
+int main() {
+    int tempSignal,tempXFC;
+    //lcd.printf("XFC: 0Hz\n"); //initialize
+    lcd.printf("Working\n"); //initialize
+    lcd.printf("Sig: 0Hz\n");//The Sig: 0Hz
+    //clear the led first
+     for (int a=0; a<=4; a++)
+            display.clear();
+    signalTotal = sync_Pass=sync_Fail =0;
+    tempSignal = tempXFC = 0;
+    CalcRate.attach(&calcRate,1.0);
+#if pin_debug
+    pin8 = pin6 =0;
+    pinFlip.attach_us(&pinflip,10); //the best speed is 100k/s
+#endif
+   
+    while (1) {
+        if(tempSignal!=signal)
+        {
+            tempSignal = signal;
+            signalTotal++;
+        }
+        if(tempXFC!=XFC)
+        {
+            tempXFC = XFC;
+            wait_us(sync_delay);
+            if(tempSignal!=signal) //synchronized 
+            {
+                tempSignal = signal;
+                signalTotal++;  
+                sync_Pass++;
+            }
+            else //didn't find sync signal
+                sync_Fail++;
+        }
+        wait_us(1);
+        
+        
+
+
+    }
+
+}
diff -r 000000000000 -r 3d76f0b19b14 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Apr 17 07:40:02 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4c0c40fd0593