Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "7SegSRDriver.h" 00003 #include "TextLCD.h" //driver 1602 lcd screen 00004 00005 #define pin_debug 0 00006 #if pin_debug 00007 DigitalOut pin8(P2_7),pin6(P1_24); 00008 Ticker pinFlip; 00009 #endif 00010 00011 Timeout CalcRate; 00012 DigitalOut led1(P2_2); 00013 DigitalIn signal(P2_9),XFC(P2_8);; 00014 //InterruptIn flip(P1_21),flip1(P1_23); in mbed P1_21 is defined to be LED1, connect LCD to 3V3, can't generate interrupt 00015 //InterruptIn flip(P2_8),flip1(P2_9); //flip XFC flip1 signal 00016 TextLCD lcd(P0_24, P0_25, P0_26, P0_3, P0_2,P1_0,TextLCD::LCD16x2); // rs, e, d4-d7 00017 int signalTotal,XFCTotal,sync_Pass,sync_Fail; 00018 int i,max; //used for dataToArry function 00019 int printArray[4]; 00020 float dataToarray(float data); 00021 int sync_delay = 16;// sync delay micro seconds. 00022 void pinflip(); 00023 //7 segment class definition,Data,Clock,latch 00024 SSegSRDriver display(P2_1,P0_7,P0_9, SSegSRDriver_COMN_CATHODE); 00025 00026 00027 void calcRate() { 00028 float rate = 0; 00029 int tempsignalTotal,temp_pass,temp_fail; 00030 int j =0; 00031 int LEDArray[4]; 00032 #if pin_debug 00033 pinFlip.detach(); 00034 #endif 00035 //put to temp value to ensure these values not change after this. 00036 tempsignalTotal =signalTotal; 00037 temp_pass = sync_Pass; 00038 temp_fail = sync_Fail; 00039 if(sync_Pass>0) 00040 rate = (float)temp_pass/(float)(temp_pass+temp_fail)*100; 00041 else 00042 rate = 0; 00043 i=max=0; 00044 lcd.cls(); 00045 if (rate>50) { 00046 for (j= 0; j<=3; j++) //clear printArray buffer to 0 00047 printArray[j] = 0; 00048 dataToarray(rate); 00049 //lcd.printf("XFC: %6dHz\n",tempXFCTotal*2); //print XFC: 023000Hz,remeber I'm using one counter to trigger the count 00050 lcd.printf("Working\n"); //Don't show XFC message. 00051 lcd.printf("Sig: %6dHz\n",tempsignalTotal);//print Sig: 023000Hz 00052 j = max; //max is from dataToarray, global variable to index the lenth of the array 00053 //printf("The array to print is:"); 00054 for (int a=0; a<=4; a++) 00055 display.clear(); 00056 00057 while (j<3) { 00058 //printf("%d",0); 00059 j++; 00060 for (int tempIndex = j; tempIndex>=1; tempIndex--) 00061 printArray[tempIndex]=printArray[tempIndex-1]; 00062 printArray[0] = 0; 00063 } 00064 for (j=0; (j<=3); j++) { 00065 //printf("%d ",printArray[j]); 00066 LEDArray[j]=printArray[j]; 00067 } 00068 if (max>3) { //which means 100% 00069 display.write(LEDArray[3],false); 00070 display.write(LEDArray[2],true); 00071 display.write(LEDArray[1],false); 00072 display.write(LEDArray[0],false); 00073 } else { 00074 display.write(LEDArray[3],false); 00075 display.write(LEDArray[2],false); 00076 display.write(LEDArray[1],true); 00077 display.write(LEDArray[0],false); 00078 } 00079 } 00080 else{ 00081 lcd.printf("Warning!\n"); //show warning message message. 00082 lcd.printf("No Sync signal\n"); 00083 } 00084 signalTotal = sync_Pass=sync_Fail =0; 00085 CalcRate.attach(&calcRate,1.0); 00086 led1 = !led1; 00087 00088 #if pin_debug 00089 pinFlip.attach_us(&pinflip,10); //the best speed is 100k/s 00090 #endif 00091 } 00092 #if pin_debug 00093 void pinflip() { 00094 pin8 = !pin8; 00095 } 00096 #endif 00097 float dataToarray(float data) { 00098 if (data>=0.1) { 00099 i++; //counter increase 00100 data = dataToarray(data/10)*10; 00101 } 00102 00103 if (max<=i) 00104 //make sure max is the biggest number pointer 00105 max = i; 00106 float tempData; 00107 tempData = data*100; 00108 printArray[max-i] = (int)tempData; 00109 data = (tempData-printArray[max-i])/100; 00110 i--; 00111 return (data); 00112 00113 00114 } 00115 int main() { 00116 int tempSignal,tempXFC; 00117 //lcd.printf("XFC: 0Hz\n"); //initialize 00118 lcd.printf("Working\n"); //initialize 00119 lcd.printf("Sig: 0Hz\n");//The Sig: 0Hz 00120 //clear the led first 00121 for (int a=0; a<=4; a++) 00122 display.clear(); 00123 signalTotal = sync_Pass=sync_Fail =0; 00124 tempSignal = tempXFC = 0; 00125 CalcRate.attach(&calcRate,1.0); 00126 #if pin_debug 00127 pin8 = pin6 =0; 00128 pinFlip.attach_us(&pinflip,10); //the best speed is 100k/s 00129 #endif 00130 00131 while (1) { 00132 if(tempSignal!=signal) 00133 { 00134 tempSignal = signal; 00135 signalTotal++; 00136 } 00137 if(tempXFC!=XFC) 00138 { 00139 tempXFC = XFC; 00140 wait_us(sync_delay); 00141 if(tempSignal!=signal) //synchronized 00142 { 00143 tempSignal = signal; 00144 signalTotal++; 00145 sync_Pass++; 00146 } 00147 else //didn't find sync signal 00148 sync_Fail++; 00149 } 00150 wait_us(1); 00151 00152 00153 00154 00155 } 00156 00157 }
Generated on Fri Jul 15 2022 08:30:50 by
1.7.2