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:7d55df32c988, 2010-12-04 (annotated)
- Committer:
- linxinda
- Date:
- Sat Dec 04 07:12:45 2010 +0000
- Revision:
- 0:7d55df32c988
HW3B Part1 runAtTrigger
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| linxinda | 0:7d55df32c988 | 1 | #include "mbed.h" |
| linxinda | 0:7d55df32c988 | 2 | #include "queue.h" |
| linxinda | 0:7d55df32c988 | 3 | |
| linxinda | 0:7d55df32c988 | 4 | DigitalOut myled1(LED1); |
| linxinda | 0:7d55df32c988 | 5 | DigitalOut myled2(LED2); |
| linxinda | 0:7d55df32c988 | 6 | struct timeval |
| linxinda | 0:7d55df32c988 | 7 | { |
| linxinda | 0:7d55df32c988 | 8 | unsigned long long int tv_sec; /* Seconds. */ |
| linxinda | 0:7d55df32c988 | 9 | unsigned long long int tv_usec; /* Microseconds. */ |
| linxinda | 0:7d55df32c988 | 10 | }; |
| linxinda | 0:7d55df32c988 | 11 | void runAtTime (void (*schedFunc)(), struct timeval *tv); |
| linxinda | 0:7d55df32c988 | 12 | void schedFunc(void); |
| linxinda | 0:7d55df32c988 | 13 | void SetupTimer1(struct timeval *tv); |
| linxinda | 0:7d55df32c988 | 14 | void Timer1_IRQHandler(void); |
| linxinda | 0:7d55df32c988 | 15 | void setTimeval(struct timeval *tv); |
| linxinda | 0:7d55df32c988 | 16 | timeval currentTime; |
| linxinda | 0:7d55df32c988 | 17 | |
| linxinda | 0:7d55df32c988 | 18 | PriorityQueue myQueue; |
| linxinda | 0:7d55df32c988 | 19 | _task runTime; |
| linxinda | 0:7d55df32c988 | 20 | unsigned long long int triggerTime; |
| linxinda | 0:7d55df32c988 | 21 | |
| linxinda | 0:7d55df32c988 | 22 | |
| linxinda | 0:7d55df32c988 | 23 | int main() { |
| linxinda | 0:7d55df32c988 | 24 | _task t1,t2,t3,t4,t5,t6,t7,t8,t9,t10; |
| linxinda | 0:7d55df32c988 | 25 | t1.usec=1000; |
| linxinda | 0:7d55df32c988 | 26 | triggerTime=t1.usec; |
| linxinda | 0:7d55df32c988 | 27 | t2.usec=2000; |
| linxinda | 0:7d55df32c988 | 28 | t3.usec=8000; |
| linxinda | 0:7d55df32c988 | 29 | t4.usec=4000; |
| linxinda | 0:7d55df32c988 | 30 | t5.usec=5000; |
| linxinda | 0:7d55df32c988 | 31 | t6.usec=3000; |
| linxinda | 0:7d55df32c988 | 32 | t7.usec=9000; |
| linxinda | 0:7d55df32c988 | 33 | t8.usec=5000; |
| linxinda | 0:7d55df32c988 | 34 | t9.usec=10000; |
| linxinda | 0:7d55df32c988 | 35 | //myQueue.Insert(&t1); |
| linxinda | 0:7d55df32c988 | 36 | myQueue.Insert(&t2); |
| linxinda | 0:7d55df32c988 | 37 | myQueue.Insert(&t3); |
| linxinda | 0:7d55df32c988 | 38 | myQueue.Insert(&t4); |
| linxinda | 0:7d55df32c988 | 39 | myQueue.Insert(&t5); |
| linxinda | 0:7d55df32c988 | 40 | myQueue.Insert(&t6); |
| linxinda | 0:7d55df32c988 | 41 | myQueue.Insert(&t7); |
| linxinda | 0:7d55df32c988 | 42 | myQueue.Insert(&t8); |
| linxinda | 0:7d55df32c988 | 43 | myQueue.Insert(&t9); |
| linxinda | 0:7d55df32c988 | 44 | setTimeval(¤tTime); |
| linxinda | 0:7d55df32c988 | 45 | SetupTimer1(¤tTime); |
| linxinda | 0:7d55df32c988 | 46 | //SetupTimer1(); |
| linxinda | 0:7d55df32c988 | 47 | while(1){ |
| linxinda | 0:7d55df32c988 | 48 | pc.printf("%llu\r\n",LPC_TIM1->TC); |
| linxinda | 0:7d55df32c988 | 49 | pc.printf("%llu\r\n",LPC_TIM1->IR); |
| linxinda | 0:7d55df32c988 | 50 | } |
| linxinda | 0:7d55df32c988 | 51 | } |
| linxinda | 0:7d55df32c988 | 52 | |
| linxinda | 0:7d55df32c988 | 53 | void SetupTimer1(struct timeval *tv){ |
| linxinda | 0:7d55df32c988 | 54 | LPC_SC->PCONP |= 1 << 2; // Power on Timer` |
| linxinda | 0:7d55df32c988 | 55 | LPC_SC->PCLKSEL0 |= 1 << 4; |
| linxinda | 0:7d55df32c988 | 56 | LPC_TIM1->TCR = 0x2; // Reset and set to timer mode |
| linxinda | 0:7d55df32c988 | 57 | LPC_TIM1->CTCR = 0x0; |
| linxinda | 0:7d55df32c988 | 58 | LPC_TIM1->PR = 0; // No prescale |
| linxinda | 0:7d55df32c988 | 59 | //unsigned long long int Tc=(unsigned long long int )tv->tv_sec*96000000; |
| linxinda | 0:7d55df32c988 | 60 | LPC_TIM1->MR0=tv->tv_usec*96000; |
| linxinda | 0:7d55df32c988 | 61 | //LPC_TIM1->MR0=0x20000000; |
| linxinda | 0:7d55df32c988 | 62 | LPC_TIM1->MR1=0xFFFFFFFF; |
| linxinda | 0:7d55df32c988 | 63 | LPC_TIM1->MCR = 0x00000001; // Interrupt and Reset on Match |
| linxinda | 0:7d55df32c988 | 64 | LPC_TIM1->TCR = 1; // Enable Timer |
| linxinda | 0:7d55df32c988 | 65 | //enable interrupt |
| linxinda | 0:7d55df32c988 | 66 | NVIC_SetVector (TIMER1_IRQn, (uint32_t)&Timer1_IRQHandler); |
| linxinda | 0:7d55df32c988 | 67 | NVIC_EnableIRQ (TIMER1_IRQn); |
| linxinda | 0:7d55df32c988 | 68 | } |
| linxinda | 0:7d55df32c988 | 69 | |
| linxinda | 0:7d55df32c988 | 70 | /*void TriggerHandler(void){ |
| linxinda | 0:7d55df32c988 | 71 | runAtTime (schedFunc,¤tTime); |
| linxinda | 0:7d55df32c988 | 72 | LPC_TIM1->IR=63; |
| linxinda | 0:7d55df32c988 | 73 | }*/ |
| linxinda | 0:7d55df32c988 | 74 | |
| linxinda | 0:7d55df32c988 | 75 | void Timer1_IRQHandler(void) |
| linxinda | 0:7d55df32c988 | 76 | { |
| linxinda | 0:7d55df32c988 | 77 | if((LPC_TIM1->IR|0xFFFFFFFE)==0xFFFFFFFF){//handle timer overflow |
| linxinda | 0:7d55df32c988 | 78 | //myled1=!myled1; |
| linxinda | 0:7d55df32c988 | 79 | runAtTime (schedFunc,¤tTime); |
| linxinda | 0:7d55df32c988 | 80 | LPC_TIM1->IR = 1<<0; |
| linxinda | 0:7d55df32c988 | 81 | } |
| linxinda | 0:7d55df32c988 | 82 | |
| linxinda | 0:7d55df32c988 | 83 | if((LPC_TIM1->IR|0xFFFFFFFD)==0xFFFFFFFF){//trigger function at specific time |
| linxinda | 0:7d55df32c988 | 84 | myled2=!myled2; |
| linxinda | 0:7d55df32c988 | 85 | |
| linxinda | 0:7d55df32c988 | 86 | LPC_TIM1->IR = 1<<1; |
| linxinda | 0:7d55df32c988 | 87 | } |
| linxinda | 0:7d55df32c988 | 88 | |
| linxinda | 0:7d55df32c988 | 89 | } |
| linxinda | 0:7d55df32c988 | 90 | |
| linxinda | 0:7d55df32c988 | 91 | void schedFunc(void){ |
| linxinda | 0:7d55df32c988 | 92 | |
| linxinda | 0:7d55df32c988 | 93 | runTime=*myQueue.Delete(); |
| linxinda | 0:7d55df32c988 | 94 | triggerTime=runTime.usec; |
| linxinda | 0:7d55df32c988 | 95 | setTimeval(¤tTime); |
| linxinda | 0:7d55df32c988 | 96 | myled1=!myled1; |
| linxinda | 0:7d55df32c988 | 97 | } |
| linxinda | 0:7d55df32c988 | 98 | void runAtTime(void (*scheFunc)(),struct timeval *tv){ |
| linxinda | 0:7d55df32c988 | 99 | (*scheFunc)(); |
| linxinda | 0:7d55df32c988 | 100 | LPC_TIM1->MR0=tv->tv_usec*96000; |
| linxinda | 0:7d55df32c988 | 101 | } |
| linxinda | 0:7d55df32c988 | 102 | void setTimeval(struct timeval *tv){ |
| linxinda | 0:7d55df32c988 | 103 | |
| linxinda | 0:7d55df32c988 | 104 | tv->tv_usec=triggerTime; |
| linxinda | 0:7d55df32c988 | 105 | } |
| linxinda | 0:7d55df32c988 | 106 |