cancel
Showing results for 
Search instead for 
Did you mean: 

STM32C031C6 Timer Input Capture

MHank.1
Associate III

I'm attempting to do a simple Input Capture using Timer1. I thought as a start I would generate a single 200uS pulse using a Signal Generator and use Timer1 to detect it. I can't seem to get the capture to work no matter what I do.

I tried using HAL and examples from Web...No luck.

I tried examples from a book I have and from the Reference Manual...No luck.

 Here is the code I'm currently using;

// TIM1 clock enable
	RCC->APBENR2 |= (1 << 11);
 
	// Configure PA8 as alt func TIM1 CH1
	RCC->IOPENR = 1;
	GPIOA->MODER &= ~(3 << 16);
	GPIOA->MODER |= (2 << 16);
	GPIOA->AFR[1] &= ~(0x0f);
	GPIOA->AFR[1] |= 2;
 
	// TI1 as input
	TIM1->CCMR1 |= 0x41;
	TIM1->SMCR |= (5 << 4) | 4;
	// CC1E Enable output
	TIM1->CCER |= 0x0b;
 
	TIM1->CR1 |= 1;
 
	while (1)
	{
		while(!(TIM1->SR & TIM_SR_CC1IF)){}
 
		// Reset the Update Interrupt Flag
		TIM1->SR &= ~TIM_SR_UIF;
	}

PartsBin - An Electronic Parts Organizer for Windows
1 ACCEPTED SOLUTION

Accepted Solutions
MHank.1
Associate III

Turns out what I had worked...somewhat.

In the debugger I wasn't waiting long enough for result. Sometimes it detected pulse immediately and sometimes had to wait some time. When I unhooked the scope I had attached to the input it started to work correctly. I guess the added impedance was causing it to only occassionally detect the input pulse.

I found the problem by switching from a one shot to a repeating pulse.

PartsBin - An Electronic Parts Organizer for Windows

View solution in original post

2 REPLIES 2

Read out and check/post TIM and relevant GPIO registers' content.

Plain blinky works?

Make sure the pin has the connection you think it does, e.g. by setting it as output and toggling it, while observing the physical point where you made the connection; or just by leaving it set as it is and grounding it by hand while observing GPIOx_IDR in debugger.

You may want to consider using constants defined in the CMSIS-mandated device header systematically, it increases readability of the code for others.

JW

 PS.

> TIM1->SR &= ~TIM_SR_UIF;

Don't use RMW when clearing status flags.

MHank.1
Associate III

Turns out what I had worked...somewhat.

In the debugger I wasn't waiting long enough for result. Sometimes it detected pulse immediately and sometimes had to wait some time. When I unhooked the scope I had attached to the input it started to work correctly. I guess the added impedance was causing it to only occassionally detect the input pulse.

I found the problem by switching from a one shot to a repeating pulse.

PartsBin - An Electronic Parts Organizer for Windows