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 00003 Serial pc(USBTX, USBRX); // tx, rx 00004 Serial ss(p9, p10); //tx rx 00005 Serial ss_sne(p13, p14); 00006 DigitalOut myled(LED3); 00007 DigitalOut rst1(p11); 00008 DigitalOut slaveOut(p23); 00009 00010 00011 void Timer0_init(void); 00012 void Timer0_IRQHandler(void); 00013 void slavesyn(void); 00014 void backtime(void); 00015 long long assembletime(long long t); 00016 int runAtTime (void (*fptr)(void), struct timeval tv); 00017 void getTime(struct timeval *tv); 00018 void toggle(void); 00019 00020 long counter = 0; 00021 int divider = 4; 00022 long long t1; 00023 long long t2, t3, t4, t5, t6; 00024 long long delay; 00025 long long offdri; 00026 char test; 00027 long long receive(void); 00028 long long receive_sne(void); 00029 long long snesig; 00030 long long sne(void); 00031 int task_flag = 0; 00032 00033 typedef struct timeval { 00034 unsigned long tv_sec; 00035 unsigned long tv_usec; 00036 } timeval; 00037 00038 struct timeval t_run; 00039 00040 int main( ) 00041 { 00042 pc.printf("\nbegin\n"); 00043 Timer0_init( ); 00044 t_run.tv_sec = 1000; 00045 t_run.tv_usec = 0; 00046 00047 while (1) 00048 { 00049 myled = 0; 00050 00051 if (!task_flag){ 00052 task_flag = runAtTime (&toggle, t_run); 00053 } 00054 00055 if (ss_sne.readable()){ 00056 snesig = receive_sne(); 00057 t_run.tv_sec = snesig/1000000; 00058 t_run.tv_usec = snesig%1000000; 00059 pc.printf("\nSNE is %llu\n",snesig); 00060 } 00061 00062 if (ss.readable( )){ 00063 myled = 1; 00064 test = ss.getc(); 00065 pc.printf("%d\n", test-'S'); 00066 00067 if (test == 'S' ){ 00068 slavesyn(); 00069 00070 } 00071 else{ 00072 if (test == 'T'){ 00073 backtime(); 00074 } 00075 else{ 00076 pc.printf("\nSystem Error--1!!\n"); 00077 } 00078 } 00079 } 00080 } 00081 } 00082 00083 void Timer0_init(void) 00084 { 00085 LPC_SC->PCLKSEL0 = 0x0066; 00086 LPC_TIM0->MR0 = 2880000000; // Match count for 100uS 00087 LPC_TIM0->MCR = 3; // Interrupt and Reset on Match 00088 LPC_TIM0->TCR = 1; // Enable Timer0 00089 NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler); 00090 NVIC_EnableIRQ(TIMER0_IRQn); 00091 } 00092 00093 00094 00095 void Timer0_IRQHandler(void) 00096 { 00097 LPC_TIM0->IR = 1; 00098 LPC_TIM0->MR0 = 2880000000; // Match count for 100uS 00099 LPC_TIM0->TCR = 1; // Enable Timer0 00100 counter = counter + 1; 00101 } 00102 00103 void slavesyn(void) 00104 { 00105 pc.printf("in to fuc\n"); 00106 t2 = counter * 4294967296 + LPC_TIM0->TC; 00107 pc.printf("t2 = %llu\n",t2); 00108 00109 t1 = receive(); 00110 pc.printf("t1 = %llu\n",t1); 00111 00112 wait(2); 00113 t3 = counter * 4294967296 + LPC_TIM0->TC; 00114 ss.putc('S'); 00115 pc.printf("t3 = %llu\n", t3); 00116 00117 t4 = receive(); 00118 pc.printf("t4 = %llu\n",t4); 00119 00120 00121 t1 = assembletime(t1); 00122 t2 = assembletime(t2); 00123 t3 = assembletime(t3); 00124 t4 = assembletime(t4); 00125 00126 // pc.printf("t2-t1 = %ll\n t1-t2 = %ll \n", (lld)t2-t1,(lld)t1-t2); 00127 00128 00129 offdri = (t2/4294967296 - t1/4294967296) *30*1000000000 + (t2%4294967296 - t1%4294967296); 00130 pc.printf("offderi = %llu\n",offdri); 00131 00132 t_run.tv_sec = t_run.tv_sec + (t2 - t1)/1000000000; 00133 t_run.tv_usec = t_run.tv_usec + ((t2-t1)/1000)%1000000; 00134 00135 delay = (t4/4294967296 - t3/4294967296) *4294967296 + (t4%4294967296 - t3%4294967296); 00136 pc.printf("delay = %llu\n",delay); 00137 00138 //assume t2>t1 since slave reset first 00139 if(t3>t4){ 00140 if ((t2-t1)>(t3-t4)){ 00141 delay = ((t2-t1)-(t3-t4))/2; 00142 offdri = ((t2-t1)+(t3-t4))/2; 00143 } 00144 else{ 00145 delay = ((t3-t4)-(t2-t1))/2; 00146 offdri = ((t2-t1)+(t3-t4))/2; 00147 } 00148 } 00149 else{ //t4>t3 00150 if ((t2-t1)<(t4-t3)){ 00151 delay = ((t2-t1)+(t4-t3))/2; 00152 offdri = ((t4-t3)-(t2-t1))/2; 00153 } 00154 else{ 00155 delay = ((t2-t1)+(t4-t3))/2; 00156 offdri = ((t2-t1)-(t4-t3))/2; 00157 } 00158 } 00159 00160 pc.printf("Delay = %llu\n",delay); 00161 pc.printf("offderi = %llu\n",offdri); 00162 00163 00164 } 00165 00166 long long receive(void){ 00167 char x; 00168 long long y; 00169 00170 y=0; 00171 while(1){ 00172 if(ss.readable()){ 00173 x = ss.getc(); 00174 if(x == 'E'){ 00175 return y; 00176 } 00177 else{ 00178 y = y*10 + (int)(x-48); 00179 } 00180 } 00181 } 00182 } 00183 00184 long long assembletime(long long t){ 00185 long long temp; 00186 00187 temp = (t/4294967296)*30*1000000000 + (t%4294967296)*10; 00188 return temp; 00189 } 00190 00191 void backtime(){ 00192 long long t; 00193 long long temp; 00194 t = counter * 4294967296 + LPC_TIM0->TC; 00195 t = assembletime(t); 00196 t = t - offdri + delay; 00197 /* 00198 temp = t/30/1000000000; 00199 t = (t%30000000000)/10 + temp*4294967296; 00200 */ 00201 ss.printf("%llu",t); 00202 ss.putc('E'); 00203 } 00204 00205 void toggle(){ 00206 slaveOut = !slaveOut; 00207 } 00208 00209 long long receive_sne(void){ 00210 char x; 00211 long long y; 00212 00213 y=0; 00214 while(1){ 00215 if(ss_sne.readable()){ 00216 x = ss_sne.getc(); 00217 if(x == 'E'){ 00218 return y; 00219 } 00220 else{ 00221 y = y*10 + (int)(x-48); 00222 } 00223 } 00224 } 00225 } 00226 00227 void getTime(struct timeval *tv){ 00228 unsigned long ns, hs; 00229 ns = LPC_TIM0->TC; 00230 hs = counter; 00231 tv->tv_sec = hs*30 + ns/100000000; 00232 tv->tv_usec = (ns%100000000)/100; 00233 } 00234 00235 int runAtTime (void (*fptr)(void), struct timeval tv){ 00236 struct timeval current_t; 00237 getTime(¤t_t); 00238 if (tv.tv_sec <= current_t.tv_sec && tv.tv_usec <= current_t.tv_usec){ 00239 (*fptr) ( ); 00240 return 1; 00241 } 00242 else 00243 return 0; 00244 } 00245
Generated on Fri Aug 12 2022 11:05:28 by
1.7.2