Timer "Input Capture" ?

Post here first, or if you can't find a relevant section!
Cesco
Posts: 26
Joined: Mon Apr 18, 2016 3:34 pm

Timer "Input Capture" ?

Post by Cesco » Mon Apr 18, 2016 3:42 pm

Hi, i do use timer1 and timer4 for PWM output, works nicely.

Now i want to use pin PA0, timer2 channel1, for input capture. Are there any examples?
With Timer2.setMode(TIMER_CH1,mode) function, what mode must i use? What else must i set?

I have a hard time finding documentation or header files for arduino-stm32 environment. Where should i look?

User avatar
mrburnette
Posts: 2226
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Timer "Input Capture" ?

Post by mrburnette » Mon Apr 18, 2016 4:42 pm

Hello and welcome, Cesco:

The prototype functions for timerx are here

I scanned our site and could not find any hits, but on the old Leaflabs forum, I found 2 discussions that may assist you:

http://forums.leaflabs.com/topic.php?id=1291

http://forums.leaflabs.com/topic.php?id=737&page=2


Good luck. If you get it working, please consider posting an example ... the Snippets sub-forum is a good place.

Ray

Cesco
Posts: 26
Joined: Mon Apr 18, 2016 3:34 pm

Re: Timer "Input Capture" ?

Post by Cesco » Mon Apr 18, 2016 5:44 pm

Uluckily i found in timer.h :

/* TIMER_INPUT_CAPTURE, TODO: In this mode, the timer can measure the pulse lengths of input signals */

TODO means its not implemented :(

Cesco
Posts: 26
Joined: Mon Apr 18, 2016 3:34 pm

Re: Timer "Input Capture" ?

Post by Cesco » Tue Apr 19, 2016 10:27 am

Made it:

Code: Select all

#define RX1 PA0 //t2c1

void init_rx() 
{  
  pinMode(RX1, INPUT);
  
  Timer2.setPrescaleFactor(72); 

  TIMER2->regs.gen->CCMR1 = 0b010000001;
  TIMER2->regs.gen->CCMR2 = 0b0;
  TIMER2->regs.gen->CCER  = 0b0001;
  
  Timer2.attachCompare1Interrupt(ppm_input);
}

uint16_t last;
uint8_t chan = 0;
boolean newrc = false;

void ppm_input(void) 
{
    uint16_t diff,act;
    act = TIMER2->regs.gen->CCR1;

......


User avatar
mrburnette
Posts: 2226
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Timer "Input Capture" ?

Post by mrburnette » Tue Apr 19, 2016 1:13 pm

Congrats!

And thanks for posting the sample code. We also have a 'snippets' section that I generally recommend to members for such things, it is just another way to get visibility and socialize the solution.

Ray

User avatar
RogerClark
Posts: 7691
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Timer "Input Capture" ?

Post by RogerClark » Tue Apr 19, 2016 9:46 pm

@cesco

Do you think it would be possible to use your code in the "core" where it currently has TO DO... ?

It would be great if we could.

Cesco
Posts: 26
Joined: Mon Apr 18, 2016 3:34 pm

Re: Timer "Input Capture" ?

Post by Cesco » Thu Apr 21, 2016 11:02 am

> Do you think it would be possible to use your code in the "core" where it currently has TO DO... ?

Input capture is quite complex. There are parameters for filter and prescaler, capture units can be cross-linked, lots of complicated goodies.

Concentrating on the basic function, no prescaler, no filter, just parameter for rising or faling edge schould be doable. I did search an NOT find where timer.c is. The file structure is quite hard to understand.

User avatar
martinayotte
Posts: 1241
Joined: Mon Apr 27, 2015 1:45 pm

Re: Timer "Input Capture" ?

Post by martinayotte » Thu Apr 21, 2016 12:31 pm

Cesco wrote:I did search an NOT find where timer.c is. The file structure is quite hard to understand.
What do you mean ? file structure is pretty straight forward, for example about STM32F1, it is located there :

https://github.com/rogerclarkmelbourne/ ... le/timer.c

racemaniac
Posts: 699
Joined: Sat Nov 07, 2015 9:09 am

Re: Timer "Input Capture" ?

Post by racemaniac » Thu Apr 21, 2016 1:27 pm

martinayotte wrote:
Cesco wrote:I did search an NOT find where timer.c is. The file structure is quite hard to understand.
What do you mean ? file structure is pretty straight forward, for example about STM32F1, it is located there :

https://github.com/rogerclarkmelbourne/ ... le/timer.c
by now i find everything i need, but calling it straightforward... nah ,not really :p
How many libmaple directories are there? i don't even know why there is a core directory & a system directory which both contain libmaple stuff.
My way of finding the pieces of code i need is via a text search trough the stm32f1 folder, not because i have any clue in which of the libmaple/system/core/includes/... directories scattered trough that folder it could be (because i don't even see why we've got them all ^^).

User avatar
martinayotte
Posts: 1241
Joined: Mon Apr 27, 2015 1:45 pm

Re: Timer "Input Capture" ?

Post by martinayotte » Thu Apr 21, 2016 2:38 pm

All those directories structure is coming from the legacy code of LeafLab, they are not even consistent between STM32F1 and STM32F4, but we need to live with it.

Post Reply