mbed-os
Fork of mbed-os by
features/unsupported/tests/KL25Z/lptmr/main.cpp@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | #include "mbed.h" |
elessair | 0:f269e3021894 | 2 | |
elessair | 0:f269e3021894 | 3 | volatile unsigned int ticks = 0; |
elessair | 0:f269e3021894 | 4 | |
elessair | 0:f269e3021894 | 5 | DigitalOut led(LED_BLUE); |
elessair | 0:f269e3021894 | 6 | |
elessair | 0:f269e3021894 | 7 | extern "C" void lptmr_isr(void) { |
elessair | 0:f269e3021894 | 8 | // write 1 to TCF to clear the LPT timer compare flag |
elessair | 0:f269e3021894 | 9 | LPTMR0->CSR |= LPTMR_CSR_TCF_MASK; |
elessair | 0:f269e3021894 | 10 | |
elessair | 0:f269e3021894 | 11 | ticks++; |
elessair | 0:f269e3021894 | 12 | } |
elessair | 0:f269e3021894 | 13 | |
elessair | 0:f269e3021894 | 14 | int main() { |
elessair | 0:f269e3021894 | 15 | /* Clock the timer */ |
elessair | 0:f269e3021894 | 16 | SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK; |
elessair | 0:f269e3021894 | 17 | |
elessair | 0:f269e3021894 | 18 | /* Reset */ |
elessair | 0:f269e3021894 | 19 | LPTMR0->CSR = 0; |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | /* Compare value */ |
elessair | 0:f269e3021894 | 22 | LPTMR0->CMR = 1000; |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | /* Enable interrupt */ |
elessair | 0:f269e3021894 | 25 | LPTMR0->CSR |= LPTMR_CSR_TIE_MASK; |
elessair | 0:f269e3021894 | 26 | |
elessair | 0:f269e3021894 | 27 | /* Set interrupt handler */ |
elessair | 0:f269e3021894 | 28 | NVIC_SetVector(LPTimer_IRQn, (uint32_t)lptmr_isr); |
elessair | 0:f269e3021894 | 29 | NVIC_EnableIRQ(LPTimer_IRQn); |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | /* select LPO for RTC and LPTMR */ |
elessair | 0:f269e3021894 | 32 | LPTMR0->PSR = LPTMR_PSR_PCS(3); // ERCLK32K -> 8MHz |
elessair | 0:f269e3021894 | 33 | LPTMR0->PSR |= LPTMR_PSR_PRESCALE(2); // divide by 8 |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | /* Start the timer */ |
elessair | 0:f269e3021894 | 36 | LPTMR0->CSR |= LPTMR_CSR_TEN_MASK; |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | led = 0; |
elessair | 0:f269e3021894 | 39 | while (true) { |
elessair | 0:f269e3021894 | 40 | wait(1); |
elessair | 0:f269e3021894 | 41 | led = 1; |
elessair | 0:f269e3021894 | 42 | printf("%d\n", ticks); |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | wait(1); |
elessair | 0:f269e3021894 | 45 | led = 0; |
elessair | 0:f269e3021894 | 46 | printf("%d\n", ticks); |
elessair | 0:f269e3021894 | 47 | } |
elessair | 0:f269e3021894 | 48 | } |