This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Counting rising edges using edge_count example.

Other Parts Discussed in Thread: CC1310

Hi all,

I wrote some software which would count rising edges of a signal coming in from an encoder, I had initially done this using the simple GPIO interrupts.

I wanted to step it up a bit, and measure the frequency of the signal by counting the rising edges of a signal coming from a function generator within a certain timespan, thus calculating the frequency.

I started it off by simply trying to count the edges in a similar fashion to what I did before (with the GPIO interrupts) but this time using the timer as a means of counting the rising edges.

After some snooping in the TivaWare directory, I found the edge_count example, and began basing my code off of that. However, when I try to run what I've eventually wrote, absolutely nothing happens. I'm positive it has something to do with either my initialisation of the timer or the calling of the interrupt routine.

I figured this out by not calling my function where I initialise the timer, but _keeping_ the calling of the interrupt routine (TimerValueGet). In this case, I managed to see the output in the UART ("Hello, world!") but it didn't go past the TimerValueGet statement.

In the case where I don't call the function where I intialise the timer and I also remove the line where I call the interrupt routine (TimerValueGet) I do manage to get past this and see data coming into the UART, but it's ofcourse 0 because it isn't counting something.

At first I thought it might be a hardware fault (using the LaunchPad so maybe I just misconfigured a pin) but then I'd at least thought I would get to the "Hello, world!" part of my software, which it doesn't.

Any help would be much appreciated, so can I start to figure out how to measure the frequency.

Regards,

James

EDIT:
Ignore all of the above, I had simply been mixing timers up, that's what you get when you attempt to do this when you're nackered.

I now have the problem that I'm simply not detecting any edges, as my edge counter is remaining at 0. I have attached my updated code this post.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_timer.h"
#include "inc/hw_ints.h"
#include "driverlib/gpio.h"
#include "drivers/pinout.h"
#include "driverlib/pin_map.h"
#include "driverlib/timer.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "utils/uartstdio.h"
#include "inc/tm4c1294ncpdt.h"
//*****************************************************************************
//
// System clock rate in Hz.
//
//*****************************************************************************
uint32_t g_ui32SysClock;
uint32_t count, count2;

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//*****************************************************************************
//
// Configure the UART and its pins.  This must be called before UARTprintf().
//
//*****************************************************************************
void
InitUART(void)
{
    //
    // Configure GPIO Pins for UART mode.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, g_ui32SysClock);
}

void
InitSys(void)
{
/*	Init stuff for UART.*/
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

/*	Init stuff for timer.*/
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

/*	Turn on interrupts.*/
	IntMasterEnable();

}

void
InitTimer2(void)
{
	GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_0);
	GPIOPinConfigure(GPIO_PD0_T0CCP0);

	TimerConfigure(TIMER0_BASE, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_COUNT));
	TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
    	TimerLoadSet(TIMER0_BASE, TIMER_A, 9);
   	TimerMatchSet(TIMER0_BASE, TIMER_A, 0);
    	
    	IntEnable(INT_TIMER0A);
	TimerIntEnable(TIMER0_BASE, TIMER_CAPA_MATCH);
	
	TimerEnable(TIMER0_BASE, TIMER_A);
}

void
Timer0IntHandler(void)
{
	TimerIntClear(TIMER0_BASE, TIMER_CAPA_MATCH);

	count++;

	TimerEnable(TIMER0_BASE, TIMER_A);
}
//*****************************************************************************
//
// Print "Hello World!" to the UART on the Intelligent UART Module.
//
//*****************************************************************************
int
main(void)
{
    //
    // Run from the PLL at 120 MHz.
    //
	g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
                SYSCTL_CFG_VCO_480), 120000000);

    //
    // Configure the device pins.
    //
    PinoutSet(false, false);

    InitSys();
    InitUART();
    InitTimer2();


    //
    // Hello!
    //
    UARTprintf("Hello, world!\n");

    //
    // We are finished.  Hang around flashing D1.
    //
    while(1)
    {
    	count2 = TimerValueGet(TIMER0_BASE, TIMER_A);
    	UARTprintf("%i; %i\n", count, count2);
    }
}

  • NOTE: I would like to postpone anyone from answering here, as I might have made a simple mistake with the incorrect interrupt handlers when switching timers. I will post here again what the issue was, if I have solved it.

    EDIT: Solved the issue with the timers, see main post.

    Moderators could you remove this message? When I try to delete it, I get a notifcation saying it's deleted, but it's still here!

  • Hello James,

    Would be worth the effort to add more such directed examples in TivaWare. What do you think?

    Regards
    Amit
  • Hi Amit,
    While (not James) may I suggest that a more descriptive/detailed explanation of, "When to use the timer as a counter - as opposed to its (normal) use as a timer" may deserve consideration?
    And surely - w/in reason - fundamental examples showing "when/where" the many different timer/counter modes are employed would prove useful to many. (surely to moi)
  • Hello cb1,

    Yes, I do remember the post where we discussed the timer v/s counter approach.

    EDIT: That is a part of the TO-DO on application notes and example references.

    Regards
    Amit

  • Hey Amit and cb1-,

    I definitely think that would be a good idea. I get an impression on this forum that a lof users (myself included) feel a little overwhelmed with the rich choice of peripherals on the TM4C (which isn't a bad thing). This often gets you a little bogged down because you find something which works, but halfway through you might find another peripheral which does it better.

    Kind of like what cb1- said, a little guide on what peripheral works best for certain concepts might be a good idea. Maybe another workbook which goes more in-depth?

    To go back on-topic though, anybody have an idea as to why my counter isn't detecting any positive edges?
  • Feel your pain.     Are you sure that you've forced your timer into, "Edge Count Mode?"    (pardon - I've not used that mode in years and recall "several reads" of the manual before my firm, "Got that right.")

    Now interrupts are of great value - yet too often - get in the way!   (i.e. their "care/handling" diverts you from the central task - which may demand total focus)   So - may I suggest the following:

    a) Try your best to force the timer (say a "half-timer" for simplicity) into edge count mode

    b) connect a free GPIO pin - configured as a "push-pull" output - to that timer's external MCU pin.   (some miss this - or are unclear where the timer pin is located)

    c) scope proves ideal here - "Led monitor less so" - yet you should be able to monitor a, "S   L   O   W" (1 pulse per 2-3 seconds (i.e. 1/3 Hz rate) GPIO pulse rate

    d) monitor the timer/counter after inputting 5 or so such pulses.   Has it captured this input?   (you must discern the correct timer register to view - and must insure that the IDE's "spying" upon that register does not impact its operation!)   (i.e. "some" registers are "write-only" or may "clear when read" - although I don't believe that's the case w/your timer)

    Purpose of the above is to comply with much heralded KISS.   If you cannot detect the basic input square wave - all further (interrupt-based) efforts are doomed.   Recall astronaut Armstrong's wording (bit modified), "That's one small step for reading the timer/counter - one giant leap for frequency measure..."   Doing too much - too soon - vastly complicates the process - and defies quick/easy diagnosis...

    Allez!  (et reportez - s'il vous plait...)

  • Your answers do always give me a good chuckle. I shall start from scratch and follow your approach, and see where it leads me. I will report back my findings here for future strugglers.

    I'm doing a few projects simultaneously so it's probably better if I keep this project seperated, both in mind and in code!
  • James Kent69 said:
    ...doing a few projects simultaneously

    Good that - thank God it's Friday!   Such "multi-tasking" always over-challenges this reporter.   (who on a "good day" - only (sometimes) recalls the proper freeway exit.   (to the delight of crack staff - who (secretly) bet on my, "time of arrival.")

    Be certain to keep those projects (really) separate.    More times than I can recall clients attempt similar - and "code bits/pieces" - are discovered which have, "zero bearing" on the task/project at hand!   Care to "guess" their origin?   (word to the wise...)

  • TGI Friday indeed! I'll be happy when this is done, take a short break, and figure out the next challenge to take on!

    Oh I can imagine, I've got loads of snippets of code everywhere, and I keep thinking it would be a great idea to copy paste it all together. I'd say about 70% of the problems I get is due to haphazardly copy-pasting everything without actually grasping what I'm putting together. (And I'd guess half of the code's origin is still from the Tiva examples.)

    Maybe I should take a KISS approach to my copy-pasting...

  • James Kent69 said:
    Maybe I should take a KISS approach to my copy-pasting...

    Indeed mon ami - indeed.    And you've now much justified, "That's one small step for mankind!"   (or at least "code-kind" - 46 years past, earlier this week)

  • Hi,

          I want to find the frequency of the signal , which is to be given as  an input to micro controller on CC1310 Launch pad.  I configured the Timer to its Counter mode, which counts the number of rising/falling edges in the signal. I am unclear about the statement  (b)  given above, i.e., how to configure and connect a GPIO pin to the timer's external MCU pin.I am also unaware where the Timer's pin is located on the CC1310 Launch pad. Could you please guide me with these steps , so that i can measure the frequency of an external signal. I am writing the code in Code Composer Studio.

    Thank you.

  • I'm neither fan nor user of, "xxxCC1310" so will respond w/general guidelines.

    The above device's data manual should reveal, "if and where" a Timer input pin appears. (usually the manual will be divided into peripheral functions - timer is one such peripheral)

    My idea is to "feed" (i.e. connect a GPIO set as low frequency output (push-pull) to the Timer's input pin) Thus - 2 MCU pins have been interconnected - one an output pin - the other a Timer input pin. This enables the Timer to "count" the edge transitions which arrive from the GPIO Output pin - which is fully under your control.

    As your question centers upon (a non TM4C) device - might (another) forum prove more appropriate for "nitty/gritty" detail?