• AVR Freaks

AnsweredHot!How to read the 16bit timer from a capture event?

Author
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
2019/11/12 11:48:18 (permalink)
0

How to read the 16bit timer from a capture event?

From the MCC generated CCP1.c code, I see that a timer value is being put together in the CCP1_CaptureISR function, but how does one access that from their main.c file? I am trying to interface with an ultrasonic sensor HCSR04 making use of the MCC generated files. I am using the PIC16F18446 nano development board with MPLABX v 5.30.
 
 

Attached Image(s)

#1
ric
Super Member
  • Total Posts : 24638
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/12 12:27:31 (permalink)
0
Presumably whatever code is in "CCP1_CalBack()" is meant to store it somewhere you can access.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/12 12:46:04 (permalink)
0
This is where my lack of "C" experience will shine forth. The CCP1_CallBack() is a static void function, so I'm not sure how to reference it on the main.c without hitting compile errors. It is declared in the CCP1.c file as static void (*CCP1_CallBack)(uint16_t); but not defined...or maybe it is and I don't know it? I'm fuzzy on void pointers and how they work.
 
 
#3
Aussie Susan
Super Member
  • Total Posts : 3638
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/13 18:18:51 (permalink)
0
That is a pointer to a function so where is probably another function you can call that lets you pass the address of your function that will be called as the callback.
All your function needs to do is accept a uint16_t parameter and have void 'return'. You can call it whatever you want.
Susan
#4
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/15 15:42:02 (permalink)
0
Hello Susan,
Please be patient with me on this one. So, looking through the ccp1.h and ccp1.c files I can see a few places where they mention for me to "add your code here".

static void (*CCP1_CallBack)(uint16_t);

/**
  Section: Capture Module APIs:
*/

static void CCP1_DefaultCallBack(uint16_t capturedValue)
{
    // Add your code here
    
}

 
and then in the header file there is an example of how to put in a callback function, but I'm not connecting the dots I guess.
 
 
 //@Example
    //<code>
    void Capture_CallBack(uint16_t capturedValue)
    {
        // Custom callback routine
    }
    
    void main(void)
    {
        // initialize the device
        SYSTEM_Initialize();
        
        // set the custom callback
        CCP1_SetCallBack(Capture_CallBack);
        
        while(1)
        {
            //Add your application code
        }
    }
  

 
Where would I declare a variable and set it equal to the 16bit timer value( CCP1_CallBack(module.ccpr1_16Bit) ) supposedly captured in the CCP1_CaptureISR?
#5
Aussie Susan
Super Member
  • Total Posts : 3638
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/17 18:51:28 (permalink) ☄ Helpfulby cgaufin 2019/11/19 12:32:17
+2 (2)
Look at your last code example and it is all there for you.
The 'CCP1_SetCallBack' function is the one that tells the MCC generated code which function to call back. In that code it is being told to call the 'Capture_CallBack' function that is defined just above the 'main ' function.
What you need to do is to add your code to the 'Capture_CallBack' function where it says "// Custom callback routine". The value that has just been captured is the parameter 'capturedValue' and you can do what you need to with that.
If you are wanting to make sure that your callback function is being called, set a breakpoint in it and it will be triggered after a capture. 
Susan
#6
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/18 14:18:27 (permalink)
0
Susan,
Thank you for the explanation. I am now getting it to compile, however the readings are all over the place, so I want to dig in and see what went wrong. I am feeding it a pulsed signal that is high for 100ms and low for 100ms. I have the CCP1 module set to record every rising edge through the MCC interface. To help me visualize, I have an LED blinking at this rate and I am tapping into the anode leg of the LED and feeding this signal to the CCP1 input pin.  I would anticipate that the 'capturedValue' should be 200ms, but it is all over the place, which means I am still not comprehending the capture. When I remove the CCP1 input pin, the printed read out value remains at whatever the last value was. When I connect the pin again, it starts jumping all over the place.
post edited by cgaufin - 2019/11/18 14:22:55
#7
ric
Super Member
  • Total Posts : 24638
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/18 14:42:07 (permalink)
0
what does your code look like?

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/18 15:04:51 (permalink)
0
ric,
Right now it is fairly simple. I set up the CCP1 driver in MCC. My code is reading the input of the toggling LED and then trying to print the duration. I know that I have to use Timer 1 and I have read that it should be set up as FOSC/4, but I am unsure what the timer period should be set for. I have tried many values to no avail. I even had the LED pulse rate be much faster so that I could get the Timer 1 period greater than the anticipated LED period, but that didn't work either. I do have it set for 16bit output. Below is the cleaned up main.c I am trying to use if that helps. I can try and package the project file as well if needed.
 

#include "mcc_generated_files/mcc.h"

uint16_t duration;

void Capture_CallBack(uint16_t capturedValue);

void Capture_CallBack(uint16_t capturedValue) {
    // Custom callback routine
    duration = capturedValue;

}

/*
                         Main application
 */
void main(void) {
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();

    while (1) {
        // Add your application code
 // set the custom callback
        CCP1_SetCallBack(Capture_CallBack);
        printf("duration: %d \n\r", duration);

        __delay_ms(250);
    }
}
/**
 End of File
 */

#9
Mysil
Super Member
  • Total Posts : 3488
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/18 18:58:45 (permalink) ☼ Best Answerby cgaufin 2019/11/19 12:34:11
+2 (2)
Hi,
 
When using Timer 1 together with a CCP module for Input Capture,
it doesn't really need a period time, since it should be running all the time with interrupt Not enabled.
So a reload value will not be used.
You may set the period time as high as it will go, if it make MCC happy.
 
When doing Input Capture, interrupts for the CCP module should be Enabled,
while interrupts for the Timer should be Not enabled.
There is a tool in MCC  'Interrupt Manager' to check or uncheck those enables.
 
I think the setup of the callback pointer is in the wrong place,
you may move it to just before the main loop.
    CCP1_SetCallBack(Capture_CallBack);
 
    while (1)  {   

Definition of the 'duration' variable should have a 'volatile' specifier:
volatile uint16_t duration; 

 
If the print of the 'duration' variable vary wildly,
then you may do like this in the Capture_CallBack(...) function:
void Capture_CallBack(uint16_t capturedValue) {
    // Custom callback routine
static  uint16_t  previousValue;

    duration = capturedValue - previousValue;
    previousValue = capturedValue;
}   

 
Regards,
    Mysil
 
 
 
 
#10
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/19 08:53:11 (permalink)
0
Mysil,
Thank you again. That helped. My values are consistent, but not not what I am expecting. I was expecting it to read in the units selected for my timer 1, but that doesn't seem to be the case. For instance, I ran the following states.
LED toggles every 1ms --> duration is 8000
LED toggles every 2ms --> duration is 16000
LED toggles every 3ms --> duration is 24000
LED toggles every 4ms --> duration is 32000
LED toggles every 5ms --> duration is -25536
 
This led me to look into the system clock and it was configured to HFINTOSC  16MHz. Now, I'm wondering if this duration is really a count of clock cycles? If so, I need to find an equation for this, correct? This would be in this case of triggering every rising edge 2x(duration/clock)=toggle time. Also, if this is the case and the pulse duration is too large and causes a roll over there would need to be special adjustments taken, correct?
 
This has really been an interesting learning opportunity for the CCP peripheral.
#11
ric
Super Member
  • Total Posts : 24638
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/19 12:15:08 (permalink) ☄ Helpfulby cgaufin 2019/11/19 12:20:55
+1 (1)
I would assume that your timer is configured to be clocked by Fosc/4.
That means it is clocking at 4MHz.
If your LED is toggled every 1ms, that means it turns on every 2ms.
4MHz for 2ms is 8000 counts, exactly what you observed.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#12
cgaufin
New Users
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2018/03/23 06:42:51
  • Location: 0
  • Status: offline
Re: How to read the 16bit timer from a capture event? 2019/11/19 12:22:08 (permalink)
0
ric,
Yes, yes. Your explanation is better than what I had put previously. The timer 1 clock is set to FOSC/4.
#13
Jump to:
© 2019 APG vNext Commercial Version 4.5