![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Testprogramm für Hardwaretimer TIM2 und TIM3 mit Nucleo-F401RE
main.cpp@3:b0716d14c3bf, 2021-10-14 (annotated)
- Committer:
- jack1930
- Date:
- Thu Oct 14 10:10:33 2021 +0000
- Revision:
- 3:b0716d14c3bf
- Parent:
- 2:a5ee88ab46b2
F401
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JoergSturm | 0:45170c77adc7 | 1 | #include "mbed.h" |
JoergSturm | 1:17722afc5909 | 2 | |
JoergSturm | 0:45170c77adc7 | 3 | |
JoergSturm | 0:45170c77adc7 | 4 | DigitalOut myled(PC_0); |
jack1930 | 2:a5ee88ab46b2 | 5 | DigitalOut myled2(PC_1); |
jack1930 | 2:a5ee88ab46b2 | 6 | DigitalOut PC2(PC_2); |
JoergSturm | 0:45170c77adc7 | 7 | |
JoergSturm | 1:17722afc5909 | 8 | void EX1IRQhandler(void) |
JoergSturm | 1:17722afc5909 | 9 | { |
jack1930 | 2:a5ee88ab46b2 | 10 | PC2=!PC2; |
JoergSturm | 1:17722afc5909 | 11 | } |
JoergSturm | 0:45170c77adc7 | 12 | |
jack1930 | 3:b0716d14c3bf | 13 | void TIM2_IRQHandler(void) |
JoergSturm | 0:45170c77adc7 | 14 | { |
JoergSturm | 0:45170c77adc7 | 15 | myled=!myled; |
jack1930 | 3:b0716d14c3bf | 16 | TIM2->SR=0; |
jack1930 | 3:b0716d14c3bf | 17 | HAL_NVIC_ClearPendingIRQ(TIM2_IRQn); |
JoergSturm | 0:45170c77adc7 | 18 | } |
jack1930 | 3:b0716d14c3bf | 19 | void TIM3_IRQHandler(void) |
jack1930 | 2:a5ee88ab46b2 | 20 | { |
jack1930 | 2:a5ee88ab46b2 | 21 | myled2=!myled2; |
jack1930 | 3:b0716d14c3bf | 22 | TIM3->SR=0; |
jack1930 | 3:b0716d14c3bf | 23 | HAL_NVIC_ClearPendingIRQ(TIM3_IRQn); |
jack1930 | 2:a5ee88ab46b2 | 24 | } |
jack1930 | 2:a5ee88ab46b2 | 25 | InterruptIn PA1(PA_1); |
jack1930 | 3:b0716d14c3bf | 26 | void EX1init(void); |
jack1930 | 3:b0716d14c3bf | 27 | |
jack1930 | 3:b0716d14c3bf | 28 | |
jack1930 | 3:b0716d14c3bf | 29 | void NVIC_Init(void) |
jack1930 | 3:b0716d14c3bf | 30 | { |
jack1930 | 3:b0716d14c3bf | 31 | /* TIM2_IRQn interrupt configuration */ |
jack1930 | 3:b0716d14c3bf | 32 | NVIC_SetVector(TIM2_IRQn, (uint32_t)&TIM2_IRQHandler); |
jack1930 | 3:b0716d14c3bf | 33 | HAL_NVIC_EnableIRQ(TIM2_IRQn); |
jack1930 | 3:b0716d14c3bf | 34 | /* TIM3_IRQn interrupt configuration */ |
jack1930 | 3:b0716d14c3bf | 35 | NVIC_SetVector(TIM3_IRQn, (uint32_t)&TIM3_IRQHandler); |
jack1930 | 3:b0716d14c3bf | 36 | HAL_NVIC_EnableIRQ(TIM3_IRQn); |
jack1930 | 3:b0716d14c3bf | 37 | } |
jack1930 | 3:b0716d14c3bf | 38 | |
jack1930 | 3:b0716d14c3bf | 39 | /* TIM2 init function */ |
jack1930 | 3:b0716d14c3bf | 40 | void TIM2_Init(void) |
jack1930 | 3:b0716d14c3bf | 41 | { |
jack1930 | 3:b0716d14c3bf | 42 | RCC->APB1ENR|=0b00001; //Clock Enable |
jack1930 | 3:b0716d14c3bf | 43 | TIM2->PSC=3199; //Prescaler 100µs |
jack1930 | 3:b0716d14c3bf | 44 | TIM2->ARR=5000; //Autoreload 5000*100µs = 0,5s |
jack1930 | 3:b0716d14c3bf | 45 | TIM2->DIER=1; //UIE = 1 (Update Interrupt Enable) |
jack1930 | 3:b0716d14c3bf | 46 | TIM2->SR=0; //UIF =0 (Update Interrupt Flag) |
jack1930 | 3:b0716d14c3bf | 47 | TIM2->CR1=1; //CEN=1 (Counter Enable) |
jack1930 | 3:b0716d14c3bf | 48 | } |
jack1930 | 3:b0716d14c3bf | 49 | void TIM3_Init(void); |
jack1930 | 3:b0716d14c3bf | 50 | |
jack1930 | 3:b0716d14c3bf | 51 | |
jack1930 | 3:b0716d14c3bf | 52 | |
jack1930 | 3:b0716d14c3bf | 53 | int main() |
jack1930 | 3:b0716d14c3bf | 54 | { |
jack1930 | 3:b0716d14c3bf | 55 | TIM2_Init(); |
jack1930 | 3:b0716d14c3bf | 56 | TIM3_Init(); |
jack1930 | 3:b0716d14c3bf | 57 | EX1init(); |
jack1930 | 3:b0716d14c3bf | 58 | NVIC_Init(); |
jack1930 | 3:b0716d14c3bf | 59 | while(1) { |
jack1930 | 3:b0716d14c3bf | 60 | } |
jack1930 | 3:b0716d14c3bf | 61 | |
jack1930 | 3:b0716d14c3bf | 62 | } |
jack1930 | 3:b0716d14c3bf | 63 | |
JoergSturm | 1:17722afc5909 | 64 | void EX1init(void) |
JoergSturm | 1:17722afc5909 | 65 | { |
jack1930 | 2:a5ee88ab46b2 | 66 | PA1.rise(&EX1IRQhandler); |
jack1930 | 2:a5ee88ab46b2 | 67 | PA1.mode(PullDown); |
JoergSturm | 1:17722afc5909 | 68 | } |
JoergSturm | 1:17722afc5909 | 69 | |
jack1930 | 3:b0716d14c3bf | 70 | void TIM3_Init(void) |
JoergSturm | 0:45170c77adc7 | 71 | { |
jack1930 | 3:b0716d14c3bf | 72 | RCC->APB1ENR|=0b000010; //Clock Enable |
jack1930 | 3:b0716d14c3bf | 73 | TIM3->PSC=3199; //Prescaler 100µs |
jack1930 | 3:b0716d14c3bf | 74 | TIM3->ARR=15000; //Autoreload 15000*100µs = 1,5s |
jack1930 | 3:b0716d14c3bf | 75 | TIM3->DIER=1; //UIE = 1 (Update Interrupt Enable) |
jack1930 | 3:b0716d14c3bf | 76 | TIM3->SR=0; //UIF =0 (Update Interrupt Flag) |
jack1930 | 3:b0716d14c3bf | 77 | TIM3->CR1=1; //CEN=1 (Counter Enable) |
JoergSturm | 0:45170c77adc7 | 78 | } |