APP Team
/
app3
led example with 2 timers
main.cpp@0:11dd239703cc, 2014-02-07 (annotated)
- Committer:
- trixrabbit
- Date:
- Fri Feb 07 19:55:57 2014 +0000
- Revision:
- 0:11dd239703cc
- Child:
- 1:6e31c704f4d6
led example with 2 timers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
trixrabbit | 0:11dd239703cc | 1 | #include "mbed.h" |
trixrabbit | 0:11dd239703cc | 2 | |
trixrabbit | 0:11dd239703cc | 3 | InterruptIn signal(p15); |
trixrabbit | 0:11dd239703cc | 4 | Serial pc(USBTX,USBRX); |
trixrabbit | 0:11dd239703cc | 5 | |
trixrabbit | 0:11dd239703cc | 6 | DigitalOut myled(LED1); |
trixrabbit | 0:11dd239703cc | 7 | DigitalOut myled2(LED2); |
trixrabbit | 0:11dd239703cc | 8 | volatile unsigned short timer_count, timer1_count; |
trixrabbit | 0:11dd239703cc | 9 | |
trixrabbit | 0:11dd239703cc | 10 | extern "C" void TIMER0_IRQHandler (void) |
trixrabbit | 0:11dd239703cc | 11 | { |
trixrabbit | 0:11dd239703cc | 12 | if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed |
trixrabbit | 0:11dd239703cc | 13 | { |
trixrabbit | 0:11dd239703cc | 14 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
trixrabbit | 0:11dd239703cc | 15 | timer_count++; //increment timer_count |
trixrabbit | 0:11dd239703cc | 16 | |
trixrabbit | 0:11dd239703cc | 17 | // pc.printf("timer_count %d \n\r", timer_count); |
trixrabbit | 0:11dd239703cc | 18 | if (timer_count >= 80) //Set timer_count for about 8 seconds |
trixrabbit | 0:11dd239703cc | 19 | { |
trixrabbit | 0:11dd239703cc | 20 | if (myled == 1) //If LED off, turn it on & |
trixrabbit | 0:11dd239703cc | 21 | { |
trixrabbit | 0:11dd239703cc | 22 | myled = 0; |
trixrabbit | 0:11dd239703cc | 23 | timer_count = 0; //reset count |
trixrabbit | 0:11dd239703cc | 24 | } |
trixrabbit | 0:11dd239703cc | 25 | else |
trixrabbit | 0:11dd239703cc | 26 | { |
trixrabbit | 0:11dd239703cc | 27 | myled = 1; //If LED on, turn it off & |
trixrabbit | 0:11dd239703cc | 28 | timer_count = 0; //reset count |
trixrabbit | 0:11dd239703cc | 29 | } |
trixrabbit | 0:11dd239703cc | 30 | } |
trixrabbit | 0:11dd239703cc | 31 | } |
trixrabbit | 0:11dd239703cc | 32 | } |
trixrabbit | 0:11dd239703cc | 33 | |
trixrabbit | 0:11dd239703cc | 34 | extern "C" void TIMER1_IRQHandler (void) |
trixrabbit | 0:11dd239703cc | 35 | { |
trixrabbit | 0:11dd239703cc | 36 | if((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed |
trixrabbit | 0:11dd239703cc | 37 | { |
trixrabbit | 0:11dd239703cc | 38 | LPC_TIM1->IR |= 1 << 0; // Clear MR0 interrupt flag |
trixrabbit | 0:11dd239703cc | 39 | timer1_count++; //increment timer_count |
trixrabbit | 0:11dd239703cc | 40 | |
trixrabbit | 0:11dd239703cc | 41 | // pc.printf("timer1_count %d \n\r", timer1_count); |
trixrabbit | 0:11dd239703cc | 42 | if (timer1_count >= 20) //Set timer_count for about 8 seconds |
trixrabbit | 0:11dd239703cc | 43 | { |
trixrabbit | 0:11dd239703cc | 44 | if (myled2 == 1) //If LED off, turn it on & |
trixrabbit | 0:11dd239703cc | 45 | { |
trixrabbit | 0:11dd239703cc | 46 | myled2 = 0; |
trixrabbit | 0:11dd239703cc | 47 | timer1_count = 0; //reset count |
trixrabbit | 0:11dd239703cc | 48 | } |
trixrabbit | 0:11dd239703cc | 49 | else |
trixrabbit | 0:11dd239703cc | 50 | { |
trixrabbit | 0:11dd239703cc | 51 | myled2 = 1; //If LED on, turn it off & |
trixrabbit | 0:11dd239703cc | 52 | timer1_count = 0; //reset count |
trixrabbit | 0:11dd239703cc | 53 | } |
trixrabbit | 0:11dd239703cc | 54 | } |
trixrabbit | 0:11dd239703cc | 55 | } |
trixrabbit | 0:11dd239703cc | 56 | } |
trixrabbit | 0:11dd239703cc | 57 | |
trixrabbit | 0:11dd239703cc | 58 | void timer0_init(void) |
trixrabbit | 0:11dd239703cc | 59 | { |
trixrabbit | 0:11dd239703cc | 60 | LPC_SC->PCONP |=1<1; //timer0 power on |
trixrabbit | 0:11dd239703cc | 61 | LPC_TIM0->MR0 = 2398000; //100 msec |
trixrabbit | 0:11dd239703cc | 62 | LPC_TIM0->MCR = 3; //interrupt and reset control |
trixrabbit | 0:11dd239703cc | 63 | //3 = Interrupt & reset timer0 on match |
trixrabbit | 0:11dd239703cc | 64 | //1 = Interrupt only, no reset of timer0 |
trixrabbit | 0:11dd239703cc | 65 | NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt |
trixrabbit | 0:11dd239703cc | 66 | LPC_TIM0->TCR = 1; //enable Timer0 |
trixrabbit | 0:11dd239703cc | 67 | pc.printf("Done timer0_init\n\r"); |
trixrabbit | 0:11dd239703cc | 68 | } |
trixrabbit | 0:11dd239703cc | 69 | |
trixrabbit | 0:11dd239703cc | 70 | void timer1_init(void) |
trixrabbit | 0:11dd239703cc | 71 | { |
trixrabbit | 0:11dd239703cc | 72 | LPC_SC->PCONP |=1<<2; //timer1 power on |
trixrabbit | 0:11dd239703cc | 73 | LPC_TIM1->MR0 = 2398000; //100 msec |
trixrabbit | 0:11dd239703cc | 74 | LPC_TIM1->MCR = 3; //interrupt and reset control |
trixrabbit | 0:11dd239703cc | 75 | //3 = Interrupt & reset timer1 on match |
trixrabbit | 0:11dd239703cc | 76 | //1 = Interrupt only, no reset of timer0 |
trixrabbit | 0:11dd239703cc | 77 | NVIC_EnableIRQ(TIMER1_IRQn); //enable timer1 interrupt |
trixrabbit | 0:11dd239703cc | 78 | LPC_TIM1->TCR = 1; //enable Timer1 |
trixrabbit | 0:11dd239703cc | 79 | pc.printf("Done timer1_init\n\r"); |
trixrabbit | 0:11dd239703cc | 80 | } |
trixrabbit | 0:11dd239703cc | 81 | |
trixrabbit | 0:11dd239703cc | 82 | |
trixrabbit | 0:11dd239703cc | 83 | int main (void) |
trixrabbit | 0:11dd239703cc | 84 | { |
trixrabbit | 0:11dd239703cc | 85 | myled = 0; |
trixrabbit | 0:11dd239703cc | 86 | myled2 = 0; |
trixrabbit | 0:11dd239703cc | 87 | timer0_init(); |
trixrabbit | 0:11dd239703cc | 88 | timer1_init(); |
trixrabbit | 0:11dd239703cc | 89 | |
trixrabbit | 0:11dd239703cc | 90 | while(1) |
trixrabbit | 0:11dd239703cc | 91 | { |
trixrabbit | 0:11dd239703cc | 92 | |
trixrabbit | 0:11dd239703cc | 93 | } |
trixrabbit | 0:11dd239703cc | 94 | } |