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@0:ae7a710b93e5, 2010-12-04 (annotated)
- Committer:
- guruimage
- Date:
- Sat Dec 04 06:53:45 2010 +0000
- Revision:
- 0:ae7a710b93e5
HW3B getTime with Synchronization in Slave
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| guruimage | 0:ae7a710b93e5 | 1 | #include "mbed.h" |
| guruimage | 0:ae7a710b93e5 | 2 | |
| guruimage | 0:ae7a710b93e5 | 3 | |
| guruimage | 0:ae7a710b93e5 | 4 | struct timeval |
| guruimage | 0:ae7a710b93e5 | 5 | { |
| guruimage | 0:ae7a710b93e5 | 6 | unsigned long long int tv_sec; /* Seconds. */ |
| guruimage | 0:ae7a710b93e5 | 7 | unsigned long long int tv_usec; /* Microseconds. */ |
| guruimage | 0:ae7a710b93e5 | 8 | }; |
| guruimage | 0:ae7a710b93e5 | 9 | void sync(void); |
| guruimage | 0:ae7a710b93e5 | 10 | void getValue(void); |
| guruimage | 0:ae7a710b93e5 | 11 | void readTime(void); |
| guruimage | 0:ae7a710b93e5 | 12 | void SetupTimer1(void); |
| guruimage | 0:ae7a710b93e5 | 13 | void SetupTimer0(void); |
| guruimage | 0:ae7a710b93e5 | 14 | void Timer1_IRQHandler(void); |
| guruimage | 0:ae7a710b93e5 | 15 | void Timer0_IRQHandler(void); |
| guruimage | 0:ae7a710b93e5 | 16 | unsigned long long int atoi1(char *str); |
| guruimage | 0:ae7a710b93e5 | 17 | void getTime(struct timeval *tv); |
| guruimage | 0:ae7a710b93e5 | 18 | Serial device(p9, p10); |
| guruimage | 0:ae7a710b93e5 | 19 | Serial pc(USBTX, USBRX); |
| guruimage | 0:ae7a710b93e5 | 20 | DigitalOut myled1(LED1); |
| guruimage | 0:ae7a710b93e5 | 21 | DigitalOut myled2(LED2); |
| guruimage | 0:ae7a710b93e5 | 22 | DigitalOut myled3(LED3); |
| guruimage | 0:ae7a710b93e5 | 23 | char* store=new char; |
| guruimage | 0:ae7a710b93e5 | 24 | int i=0; |
| guruimage | 0:ae7a710b93e5 | 25 | int k=0; |
| guruimage | 0:ae7a710b93e5 | 26 | int timeNum=0; |
| guruimage | 0:ae7a710b93e5 | 27 | unsigned long long int t1=0; |
| guruimage | 0:ae7a710b93e5 | 28 | unsigned long long int t2=0; |
| guruimage | 0:ae7a710b93e5 | 29 | unsigned long long int t3=0; |
| guruimage | 0:ae7a710b93e5 | 30 | unsigned long long int t4=0; |
| guruimage | 0:ae7a710b93e5 | 31 | long long int t11=0; |
| guruimage | 0:ae7a710b93e5 | 32 | long long int t22=0; |
| guruimage | 0:ae7a710b93e5 | 33 | long long int t33=0; |
| guruimage | 0:ae7a710b93e5 | 34 | long long int t44=0; |
| guruimage | 0:ae7a710b93e5 | 35 | long long int offset; |
| guruimage | 0:ae7a710b93e5 | 36 | char buffRx[100]; |
| guruimage | 0:ae7a710b93e5 | 37 | int handleTrue = 0; |
| guruimage | 0:ae7a710b93e5 | 38 | |
| guruimage | 0:ae7a710b93e5 | 39 | |
| guruimage | 0:ae7a710b93e5 | 40 | int main() { |
| guruimage | 0:ae7a710b93e5 | 41 | wait(0.001); |
| guruimage | 0:ae7a710b93e5 | 42 | for(int i =0;i<100;i++){ |
| guruimage | 0:ae7a710b93e5 | 43 | buffRx[i]=NULL; |
| guruimage | 0:ae7a710b93e5 | 44 | } |
| guruimage | 0:ae7a710b93e5 | 45 | timeval *currentTime = new timeval; |
| guruimage | 0:ae7a710b93e5 | 46 | |
| guruimage | 0:ae7a710b93e5 | 47 | SetupTimer1(); |
| guruimage | 0:ae7a710b93e5 | 48 | |
| guruimage | 0:ae7a710b93e5 | 49 | pc.printf("out of the loop!"); |
| guruimage | 0:ae7a710b93e5 | 50 | while(1){ |
| guruimage | 0:ae7a710b93e5 | 51 | wait(1); |
| guruimage | 0:ae7a710b93e5 | 52 | sync(); |
| guruimage | 0:ae7a710b93e5 | 53 | } |
| guruimage | 0:ae7a710b93e5 | 54 | } |
| guruimage | 0:ae7a710b93e5 | 55 | |
| guruimage | 0:ae7a710b93e5 | 56 | void sync(void){ |
| guruimage | 0:ae7a710b93e5 | 57 | char temp1[40]; |
| guruimage | 0:ae7a710b93e5 | 58 | char temp2[40]; |
| guruimage | 0:ae7a710b93e5 | 59 | int numcount=0; |
| guruimage | 0:ae7a710b93e5 | 60 | |
| guruimage | 0:ae7a710b93e5 | 61 | t1=(unsigned long long int)(LPC_TIM1->TC+4294967296*timeNum); |
| guruimage | 0:ae7a710b93e5 | 62 | device.putc('s');// send s to initialize synchronization. |
| guruimage | 0:ae7a710b93e5 | 63 | device.attach(&getValue); |
| guruimage | 0:ae7a710b93e5 | 64 | if(handleTrue == 1){ |
| guruimage | 0:ae7a710b93e5 | 65 | myled1=!myled1; |
| guruimage | 0:ae7a710b93e5 | 66 | handleTrue = 0; |
| guruimage | 0:ae7a710b93e5 | 67 | k=0; |
| guruimage | 0:ae7a710b93e5 | 68 | if(buffRx[0] == 'd'){ |
| guruimage | 0:ae7a710b93e5 | 69 | t4=(unsigned long long int)(LPC_TIM1->TC+4294967296*timeNum); |
| guruimage | 0:ae7a710b93e5 | 70 | myled2=!myled2; |
| guruimage | 0:ae7a710b93e5 | 71 | |
| guruimage | 0:ae7a710b93e5 | 72 | } |
| guruimage | 0:ae7a710b93e5 | 73 | for(int i=0; i<40;i++){ |
| guruimage | 0:ae7a710b93e5 | 74 | temp1[i]=NULL; |
| guruimage | 0:ae7a710b93e5 | 75 | temp2[i]=NULL; |
| guruimage | 0:ae7a710b93e5 | 76 | } |
| guruimage | 0:ae7a710b93e5 | 77 | if(buffRx[1] == 'm'){ |
| guruimage | 0:ae7a710b93e5 | 78 | for(int i=2;buffRx[i]!='f';i++){ |
| guruimage | 0:ae7a710b93e5 | 79 | temp1[i-2]=buffRx[i]; |
| guruimage | 0:ae7a710b93e5 | 80 | numcount = i; |
| guruimage | 0:ae7a710b93e5 | 81 | } |
| guruimage | 0:ae7a710b93e5 | 82 | t2=atoi1(temp1); |
| guruimage | 0:ae7a710b93e5 | 83 | for(int i=0; i<40;i++){ |
| guruimage | 0:ae7a710b93e5 | 84 | temp1[i]=NULL; |
| guruimage | 0:ae7a710b93e5 | 85 | } |
| guruimage | 0:ae7a710b93e5 | 86 | |
| guruimage | 0:ae7a710b93e5 | 87 | for(int i=numcount+2;buffRx[i]!='n';i++){ |
| guruimage | 0:ae7a710b93e5 | 88 | temp2[i-numcount-2]=buffRx[i]; |
| guruimage | 0:ae7a710b93e5 | 89 | } |
| guruimage | 0:ae7a710b93e5 | 90 | numcount=0; |
| guruimage | 0:ae7a710b93e5 | 91 | t3=atoi1(temp2); |
| guruimage | 0:ae7a710b93e5 | 92 | |
| guruimage | 0:ae7a710b93e5 | 93 | for(int i=0; i<40;i++){ |
| guruimage | 0:ae7a710b93e5 | 94 | temp2[i]=NULL; |
| guruimage | 0:ae7a710b93e5 | 95 | } |
| guruimage | 0:ae7a710b93e5 | 96 | |
| guruimage | 0:ae7a710b93e5 | 97 | t11=(long long int)t1; |
| guruimage | 0:ae7a710b93e5 | 98 | t22=(long long int)t2; |
| guruimage | 0:ae7a710b93e5 | 99 | t33=(long long int)t3; |
| guruimage | 0:ae7a710b93e5 | 100 | t44=(long long int)t4; |
| guruimage | 0:ae7a710b93e5 | 101 | offset = (long long int)(((t22-t11)-(t44-t33))/2); |
| guruimage | 0:ae7a710b93e5 | 102 | //LPC_TIM1->TC=LPC_TIM1->TC-offset; |
| guruimage | 0:ae7a710b93e5 | 103 | for(int i=0;buffRx[i]!=NULL;i++){ |
| guruimage | 0:ae7a710b93e5 | 104 | buffRx[i]=NULL; |
| guruimage | 0:ae7a710b93e5 | 105 | } |
| guruimage | 0:ae7a710b93e5 | 106 | //pc.printf("\r\nOffset is %llu!\r\n",offset); |
| guruimage | 0:ae7a710b93e5 | 107 | } |
| guruimage | 0:ae7a710b93e5 | 108 | } |
| guruimage | 0:ae7a710b93e5 | 109 | pc.printf("\r\nT1: %llu, T2: %llu, T3: %llu T4: %llu timeNum: %d\r\nOffset is %lld!\r\n",t1,t2,t3,t4, timeNum,offset); |
| guruimage | 0:ae7a710b93e5 | 110 | |
| guruimage | 0:ae7a710b93e5 | 111 | } |
| guruimage | 0:ae7a710b93e5 | 112 | |
| guruimage | 0:ae7a710b93e5 | 113 | void getValue(void){ |
| guruimage | 0:ae7a710b93e5 | 114 | buffRx[k]=device.getc(); |
| guruimage | 0:ae7a710b93e5 | 115 | //pc.printf("%c",buffRx[k]); |
| guruimage | 0:ae7a710b93e5 | 116 | if(buffRx[k]=='n'){ |
| guruimage | 0:ae7a710b93e5 | 117 | handleTrue = 1; |
| guruimage | 0:ae7a710b93e5 | 118 | } |
| guruimage | 0:ae7a710b93e5 | 119 | k++; |
| guruimage | 0:ae7a710b93e5 | 120 | } |
| guruimage | 0:ae7a710b93e5 | 121 | |
| guruimage | 0:ae7a710b93e5 | 122 | void SetupTimer1(void) |
| guruimage | 0:ae7a710b93e5 | 123 | { |
| guruimage | 0:ae7a710b93e5 | 124 | LPC_SC->PCONP |= 1 << 2; // Power on Timer` |
| guruimage | 0:ae7a710b93e5 | 125 | LPC_SC->PCLKSEL0 |= 1 << 4; |
| guruimage | 0:ae7a710b93e5 | 126 | LPC_TIM1->TCR = 0x2; // Reset and set to timer mode |
| guruimage | 0:ae7a710b93e5 | 127 | LPC_TIM1->CTCR = 0x0; |
| guruimage | 0:ae7a710b93e5 | 128 | LPC_TIM1->PR = 0; // No prescale |
| guruimage | 0:ae7a710b93e5 | 129 | LPC_TIM1->MR0 = 0xFFFFFFFF; // Match count for 100mS |
| guruimage | 0:ae7a710b93e5 | 130 | LPC_TIM1->MCR = 3; // Interrupt and Reset on Match |
| guruimage | 0:ae7a710b93e5 | 131 | LPC_TIM1->TCR = 1; // Enable Timer |
| guruimage | 0:ae7a710b93e5 | 132 | |
| guruimage | 0:ae7a710b93e5 | 133 | // Enable the ISR vector |
| guruimage | 0:ae7a710b93e5 | 134 | NVIC_SetVector (TIMER1_IRQn, (uint32_t)&Timer1_IRQHandler); |
| guruimage | 0:ae7a710b93e5 | 135 | NVIC_EnableIRQ(TIMER1_IRQn); |
| guruimage | 0:ae7a710b93e5 | 136 | } |
| guruimage | 0:ae7a710b93e5 | 137 | void Timer1_IRQHandler(void) |
| guruimage | 0:ae7a710b93e5 | 138 | { |
| guruimage | 0:ae7a710b93e5 | 139 | timeNum++; |
| guruimage | 0:ae7a710b93e5 | 140 | LPC_TIM1->IR = 1; |
| guruimage | 0:ae7a710b93e5 | 141 | } |
| guruimage | 0:ae7a710b93e5 | 142 | |
| guruimage | 0:ae7a710b93e5 | 143 | void getTime(struct timeval *tv) { |
| guruimage | 0:ae7a710b93e5 | 144 | tv->tv_sec=(unsigned long long int)((LPC_TIM1->TC+4294967296*timeNum-offset)/96000000); |
| guruimage | 0:ae7a710b93e5 | 145 | tv->tv_usec=(unsigned long long int)((LPC_TIM1->TC+4294967296*timeNum-offset)/96000); |
| guruimage | 0:ae7a710b93e5 | 146 | } |
| guruimage | 0:ae7a710b93e5 | 147 | |
| guruimage | 0:ae7a710b93e5 | 148 | |
| guruimage | 0:ae7a710b93e5 | 149 | unsigned long long int atoi1(char *str){ |
| guruimage | 0:ae7a710b93e5 | 150 | unsigned long long int result=0; |
| guruimage | 0:ae7a710b93e5 | 151 | for(int i =0;str[i]!=NULL;i++){ |
| guruimage | 0:ae7a710b93e5 | 152 | int temp = str[i]-'0'; |
| guruimage | 0:ae7a710b93e5 | 153 | result=result*10+temp; |
| guruimage | 0:ae7a710b93e5 | 154 | } |
| guruimage | 0:ae7a710b93e5 | 155 | return result; |
| guruimage | 0:ae7a710b93e5 | 156 | } |