
Counter using TIM2
Fork of Nucleo_Hello_counter by
Diff: main.cpp
- Revision:
- 4:26948bebef6c
- Parent:
- 3:5c895f9199d6
- Child:
- 5:e1783d6ee885
--- a/main.cpp Tue Sep 29 06:56:28 2015 +0000 +++ b/main.cpp Wed Sep 30 08:15:19 2015 +0000 @@ -19,25 +19,36 @@ */ #include "mbed.h" -#include "counter.h" +#include "Encoder.h" -TIM_IC_InitTypeDef counterSet1; -TIM_HandleTypeDef timer1; - +TIM_Encoder_InitTypeDef encoder2; +TIM_HandleTypeDef timer2; +TIM_IC_InitTypeDef counterSet2; + int main() { - - CounterInit(counterSet1, timer1, TIM1, 0xffff); - + //examples + + //counting on A-input only, 2 ticks per cycle, rolls over at 100 + //EncoderInit(encoder1, timer1, TIM1, 0xffff, TIM_ENCODERMODE_TI1); - printf("STM HAL encoder demo\n\r"); - - while(1) { + //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count + EncoderInit(encoder2, timer2, counterSet2, TIM2, 0xffff, 0); + TIM2->SMCR |= 0x0007; // Ext. clk mode 1 + TIM2->SMCR |= 0x0060; // TI2FP2 as ext. clock + TIM2->CR1 |= 0x0001; // enable counting + TIM2->CNT = 0; // reset counter + + while (1) { int16_t count1; - - count1=TIM1->CNT; //OK 401 411 TICKER 030 + count1=TIM2->CNT; printf("%d\r\n", count1); wait(1.0); - } + if (GPIOE->IDR & 0x01) TIM2->CNT = 0; // reset counter + if (1 * TIM_CR1_DIR) TIM2->CR1 |= 0; // 0 - count up; 1 - count down + + //if (GPIOE->IDR & 0x02) TIM2->CR1 |= 0x01; // enable counter + //if (GPIOE->IDR & 0x04) TIM2->CR1 &= ~0x01; // disable counter + } }