Dependencies:   mbed

Committer:
sun831011
Date:
Fri Dec 03 22:20:14 2010 +0000
Revision:
0:53009704d289

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sun831011 0:53009704d289 1 #include "mbed.h"
sun831011 0:53009704d289 2
sun831011 0:53009704d289 3 Serial pc(USBTX, USBRX); // tx, rx
sun831011 0:53009704d289 4 Serial ss(p9, p10); //tx rx
sun831011 0:53009704d289 5 Serial ss_sne(p13, p14); //tx rx
sun831011 0:53009704d289 6
sun831011 0:53009704d289 7 DigitalOut myled1(LED1);
sun831011 0:53009704d289 8 DigitalOut myled2(LED2);
sun831011 0:53009704d289 9
sun831011 0:53009704d289 10 DigitalOut masterOut(p23);
sun831011 0:53009704d289 11 InterruptIn masterIn(p22);
sun831011 0:53009704d289 12 InterruptIn slaveIn(p21);
sun831011 0:53009704d289 13
sun831011 0:53009704d289 14 unsigned long counter = 0;
sun831011 0:53009704d289 15 int divider = 1;
sun831011 0:53009704d289 16 int pro_flag = 0;
sun831011 0:53009704d289 17 int task_flag = 0;
sun831011 0:53009704d289 18 unsigned long long t1, t2, t3, t4;
sun831011 0:53009704d289 19 typedef struct timeval {
sun831011 0:53009704d289 20 unsigned long tv_sec;
sun831011 0:53009704d289 21 unsigned long tv_usec;
sun831011 0:53009704d289 22 } timeval;
sun831011 0:53009704d289 23
sun831011 0:53009704d289 24 void Timer0_init(void);
sun831011 0:53009704d289 25 void Timer0_IRQHandler(void);
sun831011 0:53009704d289 26 void Sync_protocol(void);
sun831011 0:53009704d289 27 void getTime(struct timeval *tv);
sun831011 0:53009704d289 28 int runAtTime (void (*fptr)(void), struct timeval tv);
sun831011 0:53009704d289 29 void toggle(void);
sun831011 0:53009704d289 30 void runAtTrigger( );
sun831011 0:53009704d289 31 void reportToggle(struct timeval *tv);
sun831011 0:53009704d289 32 long long sne(void);
sun831011 0:53009704d289 33
sun831011 0:53009704d289 34 int main( )
sun831011 0:53009704d289 35 {
sun831011 0:53009704d289 36 struct timeval t_run;
sun831011 0:53009704d289 37 long long t_run_m;
sun831011 0:53009704d289 38 t_run.tv_sec = 1000;
sun831011 0:53009704d289 39 t_run.tv_usec = 0;
sun831011 0:53009704d289 40 wait(1);
sun831011 0:53009704d289 41 // masterIn.rise(&runAtTrigger);
sun831011 0:53009704d289 42 // masterIn.fall(&runAtTrigger);
sun831011 0:53009704d289 43 // slaveIn.rise(&runAtTrigger);
sun831011 0:53009704d289 44 // slaveIn.fall(&runAtTrigger);
sun831011 0:53009704d289 45 Timer0_init( );
sun831011 0:53009704d289 46 while (1)
sun831011 0:53009704d289 47 { /*
sun831011 0:53009704d289 48 if (pc.readable( )){
sun831011 0:53009704d289 49 t_run_m = sne( );
sun831011 0:53009704d289 50 t_run.tv_sec = t_run_m/1000000;
sun831011 0:53009704d289 51 t_run.tv_usec = t_run_m%1000000;
sun831011 0:53009704d289 52 pc.printf("%lu\n",t_run.tv_sec);
sun831011 0:53009704d289 53 pc.printf("%lu\n",t_run.tv_usec);
sun831011 0:53009704d289 54 } */
sun831011 0:53009704d289 55 // if (!task_flag)
sun831011 0:53009704d289 56 // task_flag = runAtTime (&toggle, t_run);
sun831011 0:53009704d289 57 if (pro_flag == 1){
sun831011 0:53009704d289 58 Sync_protocol( );
sun831011 0:53009704d289 59 pro_flag = 0;
sun831011 0:53009704d289 60 }
sun831011 0:53009704d289 61 }
sun831011 0:53009704d289 62 }
sun831011 0:53009704d289 63
sun831011 0:53009704d289 64 void Timer0_init(void)
sun831011 0:53009704d289 65 {
sun831011 0:53009704d289 66 LPC_SC->PCLKSEL0 = 0x0066;
sun831011 0:53009704d289 67 LPC_SC->PCONP |= 1<<1; // Timer0 Power On
sun831011 0:53009704d289 68 LPC_TIM0->MR0 = 2880000000; // Match count for 30S
sun831011 0:53009704d289 69 LPC_TIM0->MCR = 3; // Interrupt and Reset on Match
sun831011 0:53009704d289 70 LPC_TIM0->TCR = 1; // Enable Timer0
sun831011 0:53009704d289 71 NVIC_SetVector (TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler);
sun831011 0:53009704d289 72 NVIC_EnableIRQ(TIMER0_IRQn);
sun831011 0:53009704d289 73 }
sun831011 0:53009704d289 74
sun831011 0:53009704d289 75 void Timer0_IRQHandler(void)
sun831011 0:53009704d289 76 {
sun831011 0:53009704d289 77 LPC_TIM0->IR = 1;
sun831011 0:53009704d289 78 LPC_TIM0->MR0 = 2880000000; // Match count for 30S
sun831011 0:53009704d289 79 LPC_TIM0->TCR = 1; // Enable Timer0
sun831011 0:53009704d289 80 counter = counter + 1;
sun831011 0:53009704d289 81 if ( counter % divider ==0 )
sun831011 0:53009704d289 82 pro_flag = 1;
sun831011 0:53009704d289 83 }
sun831011 0:53009704d289 84
sun831011 0:53009704d289 85 void Sync_protocol(void)
sun831011 0:53009704d289 86 {
sun831011 0:53009704d289 87 char x;
sun831011 0:53009704d289 88 t1 = counter * 4294967296 + LPC_TIM0->TC;
sun831011 0:53009704d289 89 ss.putc('S');
sun831011 0:53009704d289 90 pc.printf("t1 = %llu\n", t1);
sun831011 0:53009704d289 91 ss.printf("%llu", t1);
sun831011 0:53009704d289 92 ss.putc('E');
sun831011 0:53009704d289 93 while (1){
sun831011 0:53009704d289 94 if (ss.readable( )){
sun831011 0:53009704d289 95 x = ss.getc( );
sun831011 0:53009704d289 96 t4 = counter * 4294967296 + LPC_TIM0->TC;
sun831011 0:53009704d289 97 ss.printf("%llu", t4);
sun831011 0:53009704d289 98 ss.putc('E');
sun831011 0:53009704d289 99 pc.printf("%c\n", x);
sun831011 0:53009704d289 100 pc.printf("t4 = %llu\n", t4);
sun831011 0:53009704d289 101 break;
sun831011 0:53009704d289 102 }
sun831011 0:53009704d289 103 }
sun831011 0:53009704d289 104 }
sun831011 0:53009704d289 105
sun831011 0:53009704d289 106 void getTime(struct timeval *tv){
sun831011 0:53009704d289 107 unsigned long ns, hs;
sun831011 0:53009704d289 108 ns = LPC_TIM0->TC;
sun831011 0:53009704d289 109 hs = counter;
sun831011 0:53009704d289 110 tv->tv_sec = hs*30 + ns/100000000;
sun831011 0:53009704d289 111 tv->tv_usec = (ns%100000000)/100;
sun831011 0:53009704d289 112 }
sun831011 0:53009704d289 113 /*
sun831011 0:53009704d289 114 int runAtTime (void (*fptr)(void), struct timeval tv){
sun831011 0:53009704d289 115 struct timeval current_t;
sun831011 0:53009704d289 116 getTime(&current_t);
sun831011 0:53009704d289 117 if (tv.tv_sec <= current_t.tv_sec && tv.tv_usec <= current_t.tv_usec){
sun831011 0:53009704d289 118 (*fptr) ( );
sun831011 0:53009704d289 119 return 1;
sun831011 0:53009704d289 120 }
sun831011 0:53009704d289 121 else
sun831011 0:53009704d289 122 return 0;
sun831011 0:53009704d289 123 }
sun831011 0:53009704d289 124
sun831011 0:53009704d289 125 void toggle(void){
sun831011 0:53009704d289 126 masterOut = !masterOut;
sun831011 0:53009704d289 127 }
sun831011 0:53009704d289 128
sun831011 0:53009704d289 129 void runAtTrigger(void){
sun831011 0:53009704d289 130 struct timeval response_t;
sun831011 0:53009704d289 131 reportToggle(&response_t);
sun831011 0:53009704d289 132 }
sun831011 0:53009704d289 133
sun831011 0:53009704d289 134 void reportToggle(struct timeval *tv)
sun831011 0:53009704d289 135 {
sun831011 0:53009704d289 136 getTime(tv);
sun831011 0:53009704d289 137 pc.printf("%u\n", tv->tv_sec);
sun831011 0:53009704d289 138 pc.printf("%u\n", tv->tv_usec);
sun831011 0:53009704d289 139 myled2 = !myled2;
sun831011 0:53009704d289 140 }
sun831011 0:53009704d289 141
sun831011 0:53009704d289 142 long long sne( ){
sun831011 0:53009704d289 143 char temp;
sun831011 0:53009704d289 144 char incode[100];
sun831011 0:53009704d289 145 int i=0;
sun831011 0:53009704d289 146 int j=0;
sun831011 0:53009704d289 147 long long sum = 0;
sun831011 0:53009704d289 148 while ((temp = pc.getc())!= 'E') { //getchar
sun831011 0:53009704d289 149 incode[i] = temp; //put the original char in incode
sun831011 0:53009704d289 150 i++;
sun831011 0:53009704d289 151 }
sun831011 0:53009704d289 152 for (j=1;j<i;j++){
sun831011 0:53009704d289 153 sum = (incode[j]-'0')+sum*10;
sun831011 0:53009704d289 154 }
sun831011 0:53009704d289 155 if (incode[0]=='S'){
sun831011 0:53009704d289 156 ss_sne.printf("%llu", sum);
sun831011 0:53009704d289 157 ss_sne.putc('E');
sun831011 0:53009704d289 158 return sum;
sun831011 0:53009704d289 159 }
sun831011 0:53009704d289 160 else
sun831011 0:53009704d289 161 return 0;
sun831011 0:53009704d289 162 }
sun831011 0:53009704d289 163 */