Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Nucleo_Hello_counter by
Revision 5:e1783d6ee885, committed 2015-12-04
- Comitter:
- fadamo
- Date:
- Fri Dec 04 10:06:23 2015 +0000
- Parent:
- 4:26948bebef6c
- Commit message:
- Counter using TIM2
Changed in this revision
--- a/Encoder/EncoderMspInitF4.cpp Wed Sep 30 08:15:19 2015 +0000
+++ b/Encoder/EncoderMspInitF4.cpp Fri Dec 04 10:06:23 2015 +0000
@@ -7,14 +7,32 @@
* I have encoder common at 3V3, using GPIO_PULLDOWN on inputs.
* Encoder A&B outputs connected directly to GPIOs.
*
+ * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf
+ * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf
+ *
+ * TIM1_CH1: AF1 @ PA_8, PE_9
+ * TIM1_CH2: AF1 @ PA_9, PE_11
+ *
+ * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8* *F446 only
+ * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9* *F446 only
+ *
+ * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6
+ * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7
+ *
+ * TIM4_CH1: AF2 @ PB_6, PD_12
+ * TIM4_CH2: AF2 @ PB_7, PD_13
+ *
+ * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable
+ * TIM5_CH2: AF2 @ PA_1*
+ *
*/
-
+
#ifdef TARGET_STM32F4
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_InitStruct;
-
- if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8, poss PB0 PB1 usable too (complementary?)
+
+ if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8
__TIM1_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
@@ -24,7 +42,7 @@
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
- else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1, PA5 PB3 = D13 D3 poss too
+ else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1
__TIM2_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
@@ -34,7 +52,7 @@
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
- else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4, PA6 PA7 & PC6 PC7 also an option for Nucleo
+ else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4
__TIM3_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
@@ -54,15 +72,5 @@
GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
- else if (htim->Instance == TIM5) { // here for completeness, mbed sytem timer uses this
- __TIM5_CLK_ENABLE();
- __GPIOA_CLK_ENABLE();
- GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
}
#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder/EncoderMspInitL0.cpp Fri Dec 04 10:06:23 2015 +0000
@@ -0,0 +1,29 @@
+#include "mbed.h"
+/*
+ * HAL_TIM_Encoder_MspInit()
+ * Overrides the __weak function stub in stm32f4xx_hal_tim.h
+ *
+ * Edit the below for your preferred pin wiring & pullup/down
+ * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs.
+ * Encoder A&B outputs connected directly to GPIOs.
+ *
+ */
+
+#ifdef TARGET_STM32L0
+void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
+{
+ GPIO_InitTypeDef GPIO_InitStruct;
+
+ if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1
+ __TIM2_CLK_ENABLE();
+ __GPIOA_CLK_ENABLE();
+ GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+ GPIO_InitStruct.Pull = GPIO_PULLDOWN;
+ GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
+ GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+ }
+}
+#endif
+
\ No newline at end of file
--- 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
}
}
