#include "stm32f4xx.h" #include "stm32f4xx_rcc.c" #include "stm32f4xx_gpio.c" #include "stm32f4xx_tim.c" #include "LCD2x16.c" void GPIOAinit_TIM2_ETR(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2); // PA15:TIM2_Ch1/ETR GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void TIM2init_counter(void) { // counting from ETR TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 100000; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0x00); TIM_Cmd(TIM2, ENABLE); } int main () { GPIOAinit_TIM2_ETR(); // GPIO clock enable, digital pin definitions TIM2init_counter(); // Timer 2 as counter: ETR input LCD_init(); // Init LCD LCD_string("TIM2 counter ETR", 0); // write title // waste time - indefinite while (1) { LCD_uInt32(TIM2->CNT, 0x40, 0x01); // show counts for (int i = 0; i < 1000000; i++) {}; // waste time }; }