Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
sun831011
Date:
Fri Dec 03 22:22:06 2010 +0000
Commit message:

Changed in this revision

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 02918d5d9986 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Dec 03 22:22:06 2010 +0000
@@ -0,0 +1,201 @@
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX); // tx, rx
+Serial ss(p9, p10); //tx rx
+DigitalOut myled(LED3);
+DigitalOut rst1(p11);
+DigitalOut slaveOut(p23);
+
+
+void Timer0_init(void);
+void Timer0_IRQHandler(void);
+void slavesyn(void);
+void backtime(void);
+long long assembletime(long long t);
+
+long counter = 0;
+int divider = 4;  
+long long t1;
+long long  t2, t3, t4, t5, t6;
+long long delay;
+long long offdri;
+char test;
+long long receive(void);
+
+long long sne(void);
+
+int main( )
+{   
+    pc.printf("\nbegin\n");
+    Timer0_init( );
+     
+    while (1)
+    {
+        myled = 0;
+           
+        if (ss.readable( )){
+            myled = 1;
+            test = ss.getc();
+            pc.printf("%d\n", test-'S');
+
+            if (test == 'S' ){
+                slavesyn();
+            }
+            else{
+                if (test == 'T'){
+                    backtime();
+                }
+                else{
+                    pc.printf("\nSystem Error--1!!\n");
+                }            
+            }  
+        }
+    }
+}
+
+void Timer0_init(void)
+{
+    LPC_SC->PCLKSEL0 = 0x0066;
+    LPC_TIM0->MR0 = 2880000000;  // Match count for 100uS
+    LPC_TIM0->MCR = 3;     // Interrupt and Reset on Match
+    LPC_TIM0->TCR = 1;     // Enable Timer0  
+    NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler);
+    NVIC_EnableIRQ(TIMER0_IRQn);
+}
+
+
+
+void Timer0_IRQHandler(void)
+{
+    LPC_TIM0->IR = 1;
+    LPC_TIM0->MR0 = 2880000000;  // Match count for 100uS
+    LPC_TIM0->TCR = 1;     // Enable Timer0
+    counter = counter + 1;
+}
+
+void slavesyn(void)
+{
+    pc.printf("in to fuc\n");
+        t2 = counter * 4294967296 + LPC_TIM0->TC;
+        pc.printf("t2 = %llu\n",t2);
+       
+        t1 = receive();
+        pc.printf("t1 = %llu\n",t1);
+        
+        wait(2);
+        t3 = counter * 4294967296 + LPC_TIM0->TC;
+        ss.putc('S');        
+        pc.printf("t3 = %llu\n", t3);
+
+        t4 = receive();
+        pc.printf("t4 = %llu\n",t4);
+        
+        
+        t1 = assembletime(t1);
+        t2 = assembletime(t2);
+        t3 = assembletime(t3);
+        t4 = assembletime(t4);
+        
+      //  pc.printf("t2-t1 = %ll\n t1-t2 = %ll \n", (lld)t2-t1,(lld)t1-t2);
+        
+        /*
+        offdri = (t2/4294967296 - t1/4294967296) *30*1000000000 +  (t2%4294967296 - t1%4294967296);
+        pc.printf("offderi = %llu\n",offdri);
+        
+        delay = (t4/4294967296 - t3/4294967296) *4294967296 +  (t4%4294967296 - t3%4294967296);
+        pc.printf("delay = %llu\n",delay);
+*/
+        //assume t2>t1 since slave reset first
+        if(t3>t4){
+            if ((t2-t1)>(t3-t4)){
+                delay = ((t2-t1)-(t3-t4))/2;
+                offdri = ((t2-t1)+(t3-t4))/2;   
+            }
+            else{
+                delay = ((t3-t4)-(t2-t1))/2;
+                offdri = ((t2-t1)+(t3-t4))/2;
+            }
+        }
+        else{ //t4>t3
+            if ((t2-t1)<(t4-t3)){
+                delay = ((t2-t1)+(t4-t3))/2;
+                offdri = ((t4-t3)-(t2-t1))/2;
+            }
+            else{
+                delay = ((t2-t1)+(t4-t3))/2;
+                offdri = ((t2-t1)-(t4-t3))/2;
+            }
+        } 
+        
+        pc.printf("Delay = %llu\n",delay);
+        pc.printf("offderi = %llu\n",offdri);
+
+        
+}
+
+long long receive(void){
+    char x;
+    long long y;
+    
+    y=0;
+    while(1){
+            if(ss.readable()){
+                x = ss.getc();
+                if(x == 'E'){
+                    return y;
+                }
+                else{
+                    y = y*10 + (int)(x-48);
+                }
+            }
+    }
+}
+
+long long assembletime(long long t){
+    long long temp;
+
+    temp = (t/4294967296)*30*1000000000 + t%4294967296*10;
+    return temp;
+}
+
+void backtime(){
+    long long t;
+    long long temp;
+    t = counter * 4294967296 + LPC_TIM0->TC;
+    t = assembletime(t);
+    t = t - offdri + delay;
+    /*
+    temp = t/30/1000000000;
+    t = (t%30000000000)/10 + temp*4294967296;
+    */
+    ss.printf("%llu",t);
+    ss.putc('E');
+}
+
+void toggle(){
+    slaveOut = !slaveOut;
+}
+
+long long sne(){
+    char temp;
+    char incode[100];
+    int i=0;
+    int j=0;
+    long long sum = 0;
+    while ((temp = pc.getc())!= 'E') {    //getchar
+        pc.printf("%c", temp);
+        incode[i] = temp;    //put the original char in incode      
+        i++;
+    } 
+    pc.printf("E");
+    for (j=1;j<i;j++){
+        sum = (incode[j]-'0')+sum*10;
+    }
+    pc.printf("\n The time is %llu\n",sum);
+    if (incode[0]=='S')
+        return sum;
+    else
+        return 0;
+}
+
+ 
\ No newline at end of file
diff -r 000000000000 -r 02918d5d9986 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Dec 03 22:22:06 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e