Pulse Functions

The mpy pulse functions are used to output pulse signals using the MSP430 timer counter circuits.

The MSP430 Microcontroller chip contains timer counter circuits which can be used to generate square wave signals on the output pins.   A square wave signal is a voltage that repeatedly switches from low to high and back to low again.

The square wave signal has two properties that can be adjusted

  • The period, this is how long it takes to repeat the square wave
  • The width, this is the high part of the square wave, and is often called the pulse width

Square wave signals or pulse  signals are useful for controlling output devices such as Servo Motors or Loudspeakers. They can be controlled by varying the pulse periiod and pulse width properties using the Pulse Functions.  Varying the pulse width is called Pulse Width Modulation or PWM (see details here for a description of how to use and control Servo Motors using the pulse functions).

mpy Pulse Functions

4 different Pulse functions are used to setup the pulse output and allow the period and the pulse width to be varied.

In the above commands the parameter timer refers to the number of the timer counter.  There are a maximum of three timer circuits numbered 0, 1, or 2.  Only the largest MSP430G2553 microcontroller has three timers. The other chips only have one.

The parameter clocks is the number of clock periods for the period or width. The MSP430 clock frequency is normally set   to 1MHz,  which means that each microcontroller clock period is 1us long.  Therefore the time for the period and width is specified in microseconds. (If a different clock frequency is used the period and width need to be scaled accordingly)

Before a timer counter can be used it needs to be enabled using the pulse_enable( timer ) function.

The timer counter signals can be connected to the output pins using the dirpulse( pinname ) function. However only some of the output pins can be used for as pulse outputs.


pulse_enable(0)                # Enables the Pulse 0 circuit
pindir(P1_2,PULSEOUT)          # Enables the Pulse 0 output onto pin P1_2
pulse_period(0, 20000)         # Output a pulse that repeats every 20,000 clocks
pulse_width(0, 1500)           # Output a pulse that has a width of 1500 clocks

This mpy  example shows how to set up Timer0 to output a pulse onto output pin P1_2. The pulse repeats every is 20000 clocks (20,000us or 20ms) and the pulse width is 1500 clocks (1500us or 1.5ms). The period and pulse width of 1.5ms  is suitable for controlling many types of servo motor,  it will typically position the servo near it’s center position. The output squarewave pulse signal looks like this:

If you have access to an oscilloscope, you can connect it to the output pin P1_2 and see the output square wave pulse directly. You can also connect it up to a servo motor and try changing the pulse width to see the motor change position. (see here for details on servo motors)

Pulse Timer Counter Block

The block diagram above shows how the different timer outputs are routed to the different output pins.

This is a very simplified diagram of the actual timer counter circuits,  The mpy pulse functions only configures the basic pulse mode.  In reality the timer counters are more complex and  have many modes of operation. Using the mpy register commands it is possible to use all the timer counter modes.

All the MSP430 microcontroller chips have general purpose  timer counter circuits. The are called the Timer_A block by TI, but in mpy they are called the Pulse Timers, this is because they are mainly used in mpy to produce squrewave pulse signals.  The Timer_A block is described fully in the TI datasheet.

If you find that you need to use more timer counter outputs than are available you can always use the regular dirout() and out() function to set and reset the output pins directly from software. This is called bit-banging.  Doing this however means that your program will spend most of its time continuously writing to the output pins. This will not leave many cpu cycles to do other things.