/*
Freq5 bis 65 kHz
Systick 1s
*/
#include "debug.h"
volatile uint32_t interrupt_count = 0;
volatile uint32_t pulse_count;
void TIM1_ETRClockMode1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOD, ENABLE);
TIM_CounterModeConfig(TIM1, TIM_CounterMode_Up);
TIM_SetAutoreload(TIM1, 0xFFFF);
TIM_ETRClockMode1Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0x0);
/* GPIOD2 Input as TIM Clock Source */
TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0x00);
TIM_Cmd(TIM1, ENABLE);
}
int main(void)
{
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
TIM1_ETRClockMode1_Init();
NVIC_EnableIRQ(SysTick_IRQn);
SysTick->SR &= ~(1 << 0);
SysTick->CMP = SystemCoreClock-1; //Sekunde
SysTick->CNT = 0;
SysTick->CTLR = 0xF;
while(1);
}
void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void SysTick_Handler(void)
{
pulse_count = TIM1->CNT;
TIM1->CNT=0;
printf("%d\r\n", pulse_count);
SysTick->SR = 0;
}
/*
Freq6 bis 24 MHz
Systick 1 ms
*/
#include "debug.h"
volatile uint32_t interrupt_count = 0;
volatile uint32_t pulse_count = 0;
volatile uint32_t pulse_last = 0;
volatile uint32_t pulse_high = 0;
void TIM1_ETRClockMode1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOD, ENABLE);
TIM_CounterModeConfig(TIM1, TIM_CounterMode_Up);
TIM_SetAutoreload(TIM1, 0xFFFF);
TIM_ETRClockMode1Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0x0);
/* GPIOD2 Input as TIM Clock Source */
TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0x00);
TIM_Cmd(TIM1, ENABLE);
}
int main(void)
{
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
TIM1_ETRClockMode1_Init();
NVIC_EnableIRQ(SysTick_IRQn);
SysTick->SR &= ~(1 << 0);
SysTick->CMP = SystemCoreClock/1000-1; //Millisekunde
SysTick->CNT = 0;
SysTick->CTLR = 0xF;
while(1);
}
void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void SysTick_Handler(void)
{
pulse_count = TIM1->CNT;
interrupt_count++;
if (pulse_count < pulse_last) {pulse_high++;}
pulse_last = pulse_count;
if (interrupt_count==1000){
printf("%d\r\n", (pulse_high << 16) | pulse_count);
pulse_high = 0;
pulse_last = 0;
interrupt_count = 0;
TIM1->CNT=0;
SysTick->CNT = 0;
}
SysTick->SR = 0;
}