Wait function that uses 16 Bit Timer 1 instead of using 32 bit Timer 0 to be used in conjunction with 'TextLCD16x4_Wait' and 'PulseWidthCapture' to measure the +ve and -ve Pulsewdth of a signal
Dependents: PulseWidthCapture_Program
LCD_Wait.cpp@0:538efd9574f7, 2014-12-10 (annotated)
- Committer:
- Ellor1
- Date:
- Wed Dec 10 10:33:53 2014 +0000
- Revision:
- 0:538efd9574f7
- Child:
- 1:57aba608a20d
working Wait Function added;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ellor1 | 0:538efd9574f7 | 1 | #include "mbed.h" |
Ellor1 | 0:538efd9574f7 | 2 | #include "TextLCD_16x4.h" |
Ellor1 | 0:538efd9574f7 | 3 | #include "LCD_Wait.h" |
Ellor1 | 0:538efd9574f7 | 4 | |
Ellor1 | 0:538efd9574f7 | 5 | //DigitalOut led(P0_9); |
Ellor1 | 0:538efd9574f7 | 6 | //DigitalOut led2(P0_8); |
Ellor1 | 0:538efd9574f7 | 7 | //DigitalOut led3(P0_7); |
Ellor1 | 0:538efd9574f7 | 8 | //DigitalOut led4(P0_17); |
Ellor1 | 0:538efd9574f7 | 9 | //DigitalOut led5(P0_1); |
Ellor1 | 0:538efd9574f7 | 10 | |
Ellor1 | 0:538efd9574f7 | 11 | LCD_Wait *LCD_Wait::instance; |
Ellor1 | 0:538efd9574f7 | 12 | |
Ellor1 | 0:538efd9574f7 | 13 | LCD_Wait::LCD_Wait(void) |
Ellor1 | 0:538efd9574f7 | 14 | { |
Ellor1 | 0:538efd9574f7 | 15 | |
Ellor1 | 0:538efd9574f7 | 16 | LPC_SYSCON->SYSAHBCLKCTRL |= CT16B1_CLK_ENABLE; |
Ellor1 | 0:538efd9574f7 | 17 | |
Ellor1 | 0:538efd9574f7 | 18 | LPC_CT16B1->PR = 48000; |
Ellor1 | 0:538efd9574f7 | 19 | |
Ellor1 | 0:538efd9574f7 | 20 | //LPC_CT16B1->MCR = CT16B1_MR0I | CT16B1_MR0R; |
Ellor1 | 0:538efd9574f7 | 21 | |
Ellor1 | 0:538efd9574f7 | 22 | // LPC_CT16B1->IR = CT16B1_IR_MR0INT; |
Ellor1 | 0:538efd9574f7 | 23 | |
Ellor1 | 0:538efd9574f7 | 24 | |
Ellor1 | 0:538efd9574f7 | 25 | |
Ellor1 | 0:538efd9574f7 | 26 | //instance = this; |
Ellor1 | 0:538efd9574f7 | 27 | //NVIC_SetVector(TIMER_16_1_IRQn, (uint32_t)&_Wait_ISR); |
Ellor1 | 0:538efd9574f7 | 28 | |
Ellor1 | 0:538efd9574f7 | 29 | } |
Ellor1 | 0:538efd9574f7 | 30 | |
Ellor1 | 0:538efd9574f7 | 31 | |
Ellor1 | 0:538efd9574f7 | 32 | void LCD_Wait::Wait(float num_wait) { |
Ellor1 | 0:538efd9574f7 | 33 | |
Ellor1 | 0:538efd9574f7 | 34 | // wait_n = num_wait; |
Ellor1 | 0:538efd9574f7 | 35 | //led = !led; |
Ellor1 | 0:538efd9574f7 | 36 | |
Ellor1 | 0:538efd9574f7 | 37 | LPC_CT16B1->TC = 0; |
Ellor1 | 0:538efd9574f7 | 38 | |
Ellor1 | 0:538efd9574f7 | 39 | // Start the timer |
Ellor1 | 0:538efd9574f7 | 40 | LPC_CT16B1->TCR = CT16B1_TCR_CEN; // enable |
Ellor1 | 0:538efd9574f7 | 41 | |
Ellor1 | 0:538efd9574f7 | 42 | // blocking while loop that waits for the timer counter to exceed the set value |
Ellor1 | 0:538efd9574f7 | 43 | // If another interrupt fires during this while loop, this Wait function might take a little longer than expected |
Ellor1 | 0:538efd9574f7 | 44 | // while(LPC_CT16B1->TC < my_max_count_value); |
Ellor1 | 0:538efd9574f7 | 45 | while(LPC_CT16B1->TC < num_wait); |
Ellor1 | 0:538efd9574f7 | 46 | |
Ellor1 | 0:538efd9574f7 | 47 | } |
Ellor1 | 0:538efd9574f7 | 48 | |
Ellor1 | 0:538efd9574f7 | 49 | //Enable interrupt |
Ellor1 | 0:538efd9574f7 | 50 | // NVIC_EnableIRQ(TIMER_16_1_IRQn); |
Ellor1 | 0:538efd9574f7 | 51 | |
Ellor1 | 0:538efd9574f7 | 52 | // Start the timer |
Ellor1 | 0:538efd9574f7 | 53 | // LPC_CT16B1->TCR = CT16B1_TCR_CEN; // enable |
Ellor1 | 0:538efd9574f7 | 54 | |
Ellor1 | 0:538efd9574f7 | 55 | // LPC_CT16B1->MR0 = wait_n; |
Ellor1 | 0:538efd9574f7 | 56 | |
Ellor1 | 0:538efd9574f7 | 57 | // } |
Ellor1 | 0:538efd9574f7 | 58 | |
Ellor1 | 0:538efd9574f7 | 59 | |
Ellor1 | 0:538efd9574f7 | 60 | |
Ellor1 | 0:538efd9574f7 | 61 | /* |
Ellor1 | 0:538efd9574f7 | 62 | void LCD_Wait::Wait(int num_wait) { |
Ellor1 | 0:538efd9574f7 | 63 | |
Ellor1 | 0:538efd9574f7 | 64 | wait_n = num_wait; |
Ellor1 | 0:538efd9574f7 | 65 | led = !led; |
Ellor1 | 0:538efd9574f7 | 66 | |
Ellor1 | 0:538efd9574f7 | 67 | //Enable interrupt |
Ellor1 | 0:538efd9574f7 | 68 | NVIC_EnableIRQ(TIMER_16_1_IRQn); |
Ellor1 | 0:538efd9574f7 | 69 | |
Ellor1 | 0:538efd9574f7 | 70 | // Start the timer |
Ellor1 | 0:538efd9574f7 | 71 | LPC_CT16B1->TCR = CT16B1_TCR_CEN; // enable |
Ellor1 | 0:538efd9574f7 | 72 | |
Ellor1 | 0:538efd9574f7 | 73 | //for ( |
Ellor1 | 0:538efd9574f7 | 74 | |
Ellor1 | 0:538efd9574f7 | 75 | //if (LPC_CT16B1->TC == wait_n) { |
Ellor1 | 0:538efd9574f7 | 76 | |
Ellor1 | 0:538efd9574f7 | 77 | // NVIC_DisableIRQ(TIMER_16_1_IRQn); |
Ellor1 | 0:538efd9574f7 | 78 | |
Ellor1 | 0:538efd9574f7 | 79 | //return; |
Ellor1 | 0:538efd9574f7 | 80 | |
Ellor1 | 0:538efd9574f7 | 81 | // } |
Ellor1 | 0:538efd9574f7 | 82 | |
Ellor1 | 0:538efd9574f7 | 83 | |
Ellor1 | 0:538efd9574f7 | 84 | |
Ellor1 | 0:538efd9574f7 | 85 | // LPC_CT16B1->MR0 = wait_n; |
Ellor1 | 0:538efd9574f7 | 86 | |
Ellor1 | 0:538efd9574f7 | 87 | } |
Ellor1 | 0:538efd9574f7 | 88 | */ |
Ellor1 | 0:538efd9574f7 | 89 | |
Ellor1 | 0:538efd9574f7 | 90 | /* |
Ellor1 | 0:538efd9574f7 | 91 | void LCD_Wait::Wait(int num_wait) { |
Ellor1 | 0:538efd9574f7 | 92 | |
Ellor1 | 0:538efd9574f7 | 93 | wait_n = num_wait; |
Ellor1 | 0:538efd9574f7 | 94 | int x; |
Ellor1 | 0:538efd9574f7 | 95 | for (x = 0; x < wait_n; x++) { |
Ellor1 | 0:538efd9574f7 | 96 | |
Ellor1 | 0:538efd9574f7 | 97 | } |
Ellor1 | 0:538efd9574f7 | 98 | x = 0; |
Ellor1 | 0:538efd9574f7 | 99 | |
Ellor1 | 0:538efd9574f7 | 100 | } |
Ellor1 | 0:538efd9574f7 | 101 | */ |
Ellor1 | 0:538efd9574f7 | 102 | |
Ellor1 | 0:538efd9574f7 | 103 | /* |
Ellor1 | 0:538efd9574f7 | 104 | void LCD_Wait::_Wait_ISR(void) |
Ellor1 | 0:538efd9574f7 | 105 | { |
Ellor1 | 0:538efd9574f7 | 106 | instance->Wait_ISR(); |
Ellor1 | 0:538efd9574f7 | 107 | } |
Ellor1 | 0:538efd9574f7 | 108 | |
Ellor1 | 0:538efd9574f7 | 109 | void LCD_Wait::Wait_ISR(void) { |
Ellor1 | 0:538efd9574f7 | 110 | |
Ellor1 | 0:538efd9574f7 | 111 | led2 = !led2; |
Ellor1 | 0:538efd9574f7 | 112 | |
Ellor1 | 0:538efd9574f7 | 113 | // NVIC_DisableIRQ(TIMER_32_0_IRQn); |
Ellor1 | 0:538efd9574f7 | 114 | // NVIC_DisableIRQ(TIMER_32_1_IRQn); |
Ellor1 | 0:538efd9574f7 | 115 | |
Ellor1 | 0:538efd9574f7 | 116 | |
Ellor1 | 0:538efd9574f7 | 117 | NVIC_DisableIRQ(TIMER_16_1_IRQn); |
Ellor1 | 0:538efd9574f7 | 118 | |
Ellor1 | 0:538efd9574f7 | 119 | LPC_CT16B1->IR = CT16B1_IR_MR0INT; |
Ellor1 | 0:538efd9574f7 | 120 | |
Ellor1 | 0:538efd9574f7 | 121 | return; |
Ellor1 | 0:538efd9574f7 | 122 | |
Ellor1 | 0:538efd9574f7 | 123 | |
Ellor1 | 0:538efd9574f7 | 124 | } |
Ellor1 | 0:538efd9574f7 | 125 | */ |