Stellaris Timer Example

In this Stellaris timer example two timers are used to code a basic frequency counter.

The code process is quite simple to implement, and as mentioned previously with other stellaris examples I would recommend using the StellarisWare Peripheral Driver Library PDF as a reference, which can be found here.

The first timer is configured to count the leading edge of the incoming square wave, the second timer is used to count for a fixed time and then generate an interrupt. Then a simple calculation is performed using the second fixed timer value, and the accumulated leading edges counted. The positive going edge count is then stored, and also reset every time the second timers interrupt is generated.  The image below outlines the basic operation of the example that will be shown.

Stellaris Timer Example Code frequency counter LM3S6965


As can be seen from the image timer 2 was chosen to generate an interrupt every 100mS, this was deemed as an adequate time to refresh the count, and can easily be increased or decreased pending on the application.  The stellaris LM3S clock frequency is running at 8MHz. Each clock pulse period can be calculated by taking the reciprocal of 8MHz (1/8MHz) which is 125nS, then dividing 100mS by 125nS we reach a value of 800,000.  The 800,000 is not used directly in this code, as the clock is simply divided by a constant, but this illustrates how the value was reached.

A signal generator was used to feed a square wave into the stellaris, this was then varied and the results of the stellaris code displayed on the on-board OLED.  A video of the frequency counter in action can be seen below.

So now lets dive into the code.  As mentioned before the system clock was running at 8MHz, using the on-board crystal, the code snippet below is used to configure the clock and also some OLED initialisation.

Then both of the timer peripherals need to be enabled.

In the code example Timer0 is used to generate an interrupt every 100mS, and Timer1 is used to measure the leading edge pulses from the signal generator.  Therefore a GPIO input needs to be setup and assigned to function with Timer1, the next section of code performs this.

An interrupt needs to be enabled and set-up for Timer0A.

Then the 2 timers need to be configured.  The first 2 lines of code set-up Timer0 as a full width periodic timer, and Timer1 as a half-width edge count capture.  Then Timer0 (TimerA) is loaded with the 100mS count period, and Timer1 is loaded with a value of 10000.  Timer1 is loaded with a value as it counts down for every positive going edge.  This timer example could effectively count upto 2MHz (a quarter of the 8MHz clock frequency), it was tested successfully with higher frequencies, to do so the value preloaded into Timer1 needs to be increased.  The final line in this snippet, sets Timer1 (TimerA) to trigger on positive going edges.

Finally the timers need to be enabled.

Now that the timers are configured and enabled the code for the interrupt handler for Timer0 can be configured.  This basically clears the interrupt, stores the value counted by Timer1 and then resets Timer1.

This is not necessarily the best way to do this, but it performed the task required of the test at the time.  As has been mentioned the code displays the frequency value on the OLED, to do this an itoa function is used to change an integer into a char and then print to the display, all this takes quite a few processor cycles, and in this way has been removed from delaying the interrupt handler.

The final code block shows the function used to calculate the timer frequency.  It could be improved upon using a modulus to display the frequency in kHz, and printing a decimal point in the appropriate place, this would allow displaying a greater range of frequencies with more ease.

The following image shows a screen capture from the YouTube video, which demonstrates the program works within a reasonable accuracy.

Stellaris Timer Example Code frequency counter LM3S6965

Example Code

The link below contains the zip file with the complete C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

Frequency Counter

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.