Stellaris LM3S6965 PWM Tutorial

In this tutorial the Stellaris LM3S6965 Evaluation board will be used to demonstrate a simple PWM (Pulse Width Modulated) example.  This is part of a larger project I am working on, which is not covered in this tutorial but will be posted at a later date.

When carrying out any programming on the Stellaris LM3S series, I would highly recommend downloading the Peripheral Driver Library PDF, this contains details of all the functions used to control the on chip peripherals.  The current release at the time of writing is spmu019p.pdf and can be downloaded from here

You will also need to install StellarisWare which is the Texas Instruments library files for the Stellaris and also contains code examples.  Viewing the various libraries while in code composer can be very useful to fully understand what each function does.  The 2 files for the PWM are pwm.h and pwm.c, which can be found in the StellarisWare installation folder, in my case this C:/StellarisWare/driverlib/

The first thing that needs to be setup with the Stellaris is the system clock, the following code below uses the external crystal oscillating at 8MHz.

Next the PWM peripheral needs to be enabled, there are 3 PWM modules on the LM3S6965 evaluation board.  For this example PWM0 will be used, which is located on GPIO port F.

The next step is assign a pin type to the GPIO, there is a specific function to configure a pin or pins for use by the PWM peripheral.

PWM0 and GPIO pin 0 on port F is assigned, this combination was used for the PWM as it allows the onboard status LED to be controlled by varying the brightness, and will also allow a meter to be used to measure the frequency from the PWM.  The image below and left shows the I/O Break Pads, the image on the right the on board peripheral signals, both are extracts from the LM3S6965 evaluation board user guide.

Stellaris LM3S6965 PWM TutorialStellaris LM3S6965 PWM Tutorial








After the GPIO is setup the PWM can now be configured.  As stated before there are 3 PWM modules on this particular microprocessor, only 1 is being used in this example PWM0.  Each PWM generator module has a 16 bit counter, 2 PWM comparators, a PWM signal generator, a dead-band generator and an interrupt/ADC-trigger selector.  This program example will just produce a simple output using the up down counter.

PWMGenConfigure() is used to set the mode of operation for the PWM generator.  The counting mode, syncronization mode, and debug behaviour are all configured.  Once configured the generator is in a disabled state.  A code snippet of this function is shown below.

PWMGenPeriodSet() is used to set the period of the PWM generator.  Note the comment regarding the placement of this function with regards to PWMGenConfigure().

PWMPulseWidthSet() is used to set the width of the pulse for the specified PWM ouput.    Note the comment regarding the placement of this function with regards to PWMGenConfigure().

PWMGenEnable() is used to enable the timer/counter of the PWM generator.

PWMOutputState() enables or disables the PWM outputs.

In this example a frequency of approximately 24kHz was generated, with approximately a 50% duty cycle.  The image below shows a basic multimeter with a frequency counter, displaying a value which coincided with the example program, and 2 further images showing Oscilloscope traces with the PWM duty cycle at 50% and 15%.

Stellaris LM3S6965 PWM Tutorial


Image with PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 166);

Stellaris LM3S6965 PWM Tutorial

Image with PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 50);

Stellaris LM3S6965 PWM Tutorial

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.

Stellaris LM3S6965 PWM C code

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.