# How to setup STM32F103C8 encoder triggered output signal

I have a project where I need to read the encoder phase A&B to output the pulse so I can drive a motor, I did some research and saw this picture where it is exactly what I wanted to do: output a pulse signal to a gpio pin triggered by the rising and falling edge of both A & B. my setting: using STM32CubeMX: encoder mode TIM2.

Now, can someone point out to me where and how I can output the pulses into a gpio pin? I only want to output the pulses when the encoder is running in a clockwise direction. I now can read the correct encoder value. I tried outputting the signal like this:

    Data1=(TIM2->CNT);
Data1=1;
delay_us(1);
Data2=(TIM2->CNT);
Data2=2;
if(Data1!=Data2){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
delay_us(5);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);



however, the output signal isn't consistent.