Counter using TIM2

Dependencies:   mbed

Fork of Nucleo_Hello_counter by paolo della vedova

Revision:
5:e1783d6ee885
Parent:
4:26948bebef6c
--- a/main.cpp	Wed Sep 30 08:15:19 2015 +0000
+++ b/main.cpp	Fri Dec 04 10:06:23 2015 +0000
@@ -24,6 +24,16 @@
 TIM_Encoder_InitTypeDef encoder2;
 TIM_HandleTypeDef timer2;
 TIM_IC_InitTypeDef counterSet2;
+DigitalOut myled(LED1);
+DigitalIn userBtn(USER_BUTTON);
+Ticker myTimer;
+
+void ReadTIM2(void) {
+        int32_t count1;
+        count1=TIM2->CNT; 
+        printf("%d\r\n", count1);
+        myled = !myled;
+}
 
 int main()
 {
@@ -33,22 +43,20 @@
     //EncoderInit(encoder1, timer1, TIM1, 0xffff, TIM_ENCODERMODE_TI1);
     
     //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
-    EncoderInit(encoder2, timer2, counterSet2, TIM2, 0xffff, 0);
+    EncoderInit(encoder2, timer2, counterSet2, TIM2, 0xffffffff, 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=TIM2->CNT; 
+    myTimer.attach(&ReadTIM2, 1.0); //Pour lire la valeur du TIM2 toutes les 1 seconde
 
-        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
+ while (1) {        
+        if (userBtn == 0) TIM2->CNT = 0; //Remet à zéro la valeur du TIM2 si le User Button est pressé
+        
+        //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
  }
 }