atmega 2560 high resolution timestamp capture

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi 

i am trying to capture timestamp of a trigger with using arduino mega's timer5 and trying to get the timestamp of falling edge trigger.

I am trying to get capture time and overflow and send to my pc via serial.

I expect that overflow increases but it seems to be i am getting wrong value.Because it give sometime smaller value than the previous.

Problem could be anywhere.Both serial or timer side.

Is there anyone who can help me?

Thanks in advance.

#include <avr/io.h>
#include <avr/interrupt.h>
#include<stdio.h>
#include <stdint.h>
#include <stdlib.h>


#define F_CPU 16000000
#define BUAD 9600
#define BUAD_RATE_CALC ((F_CPU/16/BUAD) - 1)


//Counts overflovs
volatile uint16_t T5Ovs1;
//Variables holding three timestamps
volatile uint16_t Capt1;

volatile bool isCaptured = false;
//Initialize timer
void InitTimer5(void)
{
  //Set Initial Timer value
  TCNT5 = 0;
  //First capture on faling edge
  TCCR5B &= !(1 << ICES5);
  //Enable input capture and overflow interrupts
  TIMSK5 |= (1 << ICIE5) | (1 << TOIE5);
}

void StartTimer5(void)
{
  //Start timer without prescaller
  TCCR5B |= (1 << CS10);
  //Enable global interrutps
  sei();
}

//capture ISR
ISR(TIMER5_CAPT_vect)
{
  Capt1 = ICR5;
  isCaptured = true;
}

//Overflow ISR
ISR(TIMER5_OVF_vect)
{
  //increment overflow counter
  T5Ovs1++;


}
//Function definitions
void getReady(void);
void Loop(void);
void serialSetup(void);
void serialSend(char* sendString);

//Global variables.
char s[50];

void serialSetup() {
  //Register settings
  //High and low bits
  UBRR0H = (BUAD_RATE_CALC >> 8);
  UBRR0L = BUAD_RATE_CALC;
  //transimit and recieve enable
  UCSR0B = (1 << TXEN0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << RXCIE0);
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  //8 bit data format
}

void serialSend(char* sendString) {
  for (int i = 0; i < strlen(sendString); i++) {
    while (( UCSR0A & (1 << UDRE0))  == 0) {};
    UDR0 = sendString[i];
  }
}

void getReady() {
  //Code to be run once (At start).

  serialSetup();

  InitTimer5();
  StartTimer5();
}

void Loop() {
  //Code to be run continiously.
  
  if (isCaptured)
  {
   
    sprintf(s, "%u\n", Capt1);
    serialSend(s);
    sprintf(s, "%u\n", T5Ovs1);
    serialSend(s);
    isCaptured = false;

  }

}

int main(void) {
  getReady();

  while (1) {

    Loop();
  }
  while (1) {} //If you ever return from Loop(); This while will catch you, so it won't continue doing randomness.
}

 

 

This topic has a solution.
Last Edited: Thu. Mar 1, 2018 - 09:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

isot wrote:
Problem could be anywhere.Both serial or timer side.
   Then iso-late your code to test only one thing at a time, I would begin with the serial output to the pc.

 

Make a simple program to send the letter "U" to your PC, get that working, then add code to send "simulated" timer values (0-65535) to the PC. (Hint: you will need to convert integer binary values to printable ascii text strings, i.e. printf())

Once that is working, now you can add your timer capture function.

 

Have fun, and Happy New Year!

 

Jim

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  TCCR5B &= !(1 << ICES5);

Nope, does not do what you think. But that is not the root of your problem.

 

The root is here:

  UCSR0B = (1 << TXEN0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << RXCIE0);

Enabled interrupts without an ISR.

After sending the results over the serial line, the controller starts over, therefore:

I expect that overflow increases but it seems to be i am getting wrong value.Because it give sometime smaller value than the previous.

 

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:

  TCCR5B &= !(1 << ICES5);

Nope, does not do what you think.

See: https://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...