Please note as of Wednesday, August 15th, 2018 this wiki has been set to read only. If you are a TI Employee and require Edit ability please contact x0211426 from the company directory.

Using a Digital Potentiometer via SPI with a MSP430

From Texas Instruments Wiki
Jump to: navigation, search

We will be configuring the TPL0501 Booster pack to both the MSP430FR5739 and the MSP430G2553. On the FR5739, Grace + Driverlib will used to quickly program the module, then the same functionality will be shown on the G2553 using register level code. This will help demonstrate the ease of use Grace offers in configuring peripherals as well as a chance to demonstrate DriverLib’s abstraction from register level code.

The TPL0501 booster pack is a digital potentiometer that has two modes of operation. The first is an adjustable voltage reference mode and the second is an adjustable variable gain mode. We will demonstrate the former.

Digital Potentiometers are frequently used for trimming and scaling analog signals by microcontrollers. These devices can be thought of a digitally controlled variable resistor.

Common devices uses include:

      • Adjustable Power Supplies
      • Adjustable Gain Amplifiers and Offset Trimming
      • Precision Calibration of Set Point Thresholds
      • Sensor Trimming and Calibration
      • Mechanical Potentiometer Replacement

The TPL0501 is a single channel, linear-taper digital potentiometer with 256 wiper positions. This device can be used as a three-terminal potentiometer or as a two-terminal rheostat. The TPL0501 has an end–to-end resistance of 100kΩ. The internal registers of the TPL0501 can be accessed using a SPI-compatible interface.

The device uses a 3-wire SPI compatible serial data interface. This write-only interface has three inputs: chip-select (CS), data clock (SCLK), and data input (DIN). Drive CS low to enable the serial interface and clock data synchronously into the shift register on each SCLK rising edge. After loading data into the shift register, drive CS high to latch the data into the appropriate potentiometer control register and disable the serial interface. Keep CS low during the entire serial data stream to avoid corruption of the data.

SPIWrite.png


Equipment

    • CCS5.3: http://processors.wiki.ti.com/index.php/Code_Composer_Studio_Beta_Downloads
    • Grace 2.0: http://www.ti.com/tool/grace
    • MSP430Ware 1.40: http://www.ti.com/tool/msp430ware
    • TPL0501 Booster pack: http://www.ti.com/tool/tpl0501evm
    • MSP430FR5739: http://www.ti.com/product/msp430fr5739
    • MSP430G2553: http://www.ti.com/product/msp430g2553
    • Multimeter and/or LED’s

Software

This project will make use of both MSP430Ware and Grace. MSP430ware is collection of code examples, datasheets and other design resources for ALL MSP430 devices delivered in a convenient package.

In addition to prodviding a complete collection of existing MSP430 design resources, MSP430Ware also includes high level API called MSP430 Driver Library which we will make use of. This new library makes it easy to control to MSP430 hardware from C programs.

Grace is a graphical peripheral configuration tool that is available as a stand along program within Code Composer Studio. Grace enables the configuration of ADCs, DACs, timers, clocks, serial communication interfaces and more, by interacting with buttons, drop-down menus, and text fields. Fully commented and easy-to-understand editable C code in the form of MSP430 Driver Library API’s is generated.

Procedure

We will need to examine the pin out of the headers to the Launchpad as the FR5739 is not a booster pack compliant device, this will require blue wiring to the correct pins. Arbitrarily choosing eUSCI module B on the FR5739 device we see we will need to connect SCLK and SIMO on the booster pack to P2.2 (UCB0CLK) and P1.6 (UCB0SIMO) on the FR5739 experimenter’s board. We will choose P1.0 as a GPIO to use as the CS and then connect VCC and ground appropriately.

Headers.png

And pinout

Pinout.png

We will now walk through starting a new Grace project. After opening CCS (5.3 will be used for this demonstration) click on File  CCS Project and look at the settings below.

Wiki1.png

To use Grace you must select a Grace project from the menu above. Click on the “next” tab to verify the version of MSP430ware you are using.

Wiki2.png

Now opening the main.cfg under the Project Explorer we can began to graphically configure the peripherals and generate C code that will initialize these peripherals

Wiki3.png

Click on the device overview to bring up the functional block diagram seem below.

Wiki4.png

The blue boxes indicate they are configurable, click on eUSCI_B0 SPI, I2C. In the next window then check the “Configure eUSCI_B0 Peripheral” and click on SPI Mode.

Wiki5.png

Match your project to the setting above. This window lets use choose the clock we are using to source the SPI module (SMCLK is chosen since it requires no external crystal). In this view we can choose 3 or 4 wire Master/Slave, the bitrate and what pins you would like to enable. If you recall from earlier, we will need the clock and SIMO to communicate with the device. We will be transmiting 8 bits at a time with the MSB being transmitting first.

Now going back to the overall block diagram we can configure the clock module. Which is seen below.

Wiki6.png

We will be using the SMCLK which is sourced by the DCO. All setting can be left as is. Next we will go back to the block diagram overview and select the GPIO module. In this module (similar to the others) you have the option to graphically configure or to select register bits. We will click the “Pinout 40-pin VQFN” and the screen below should appear.

Wiki7.png

We will click on P1.0 and set it to GPIO Output as shown above. Notice, that P2.2 and P1.6 are already configured in this screen since it recognized the changes made in the SPI module. For this reason it may be helpful for GPIO to be one of the last modules to configure in Grace since setting the other modules dynamically configures this view. Now we are done with our initialization and can build our project. Looking at the main.c you can see the call to the Grace generated code. If you press F3 over the call to “Grace_init” it will take you to the different peripheral initialization functions where you can see the generated code.

Code

//***************************************************************************************
* ======== Standard MSP430 includes ========
 */
#include "inc/hw_memmap.h"
#include "driverlib/5xx_6xx/espi.h"
#include "driverlib/5xx_6xx/frgpio.h"
/*
 * ======== Grace related includes ========
 */
#include <ti/mcu/msp430/Grace.h>
 
unsigned char DPOT = 255;
unsigned char commandByte = 0;
/*
 *  ======== main ========
 */
int main(void)
{
    Grace_init();                   // Activate Grace-generated configuration
 
    while(1){
 
   	  // Enable Serial Interface
          FRGPIO_setOutputLowOnPin(__MSP430_BASEADDRESS_PORT1_R__,FRGPIO_PORT_P1, FRGPIO_PIN0);
 
          // Waits for slave to initialize
          __delay_cycles(1000);
 
          //Transmit data to slave
   	  eSPI_transmitData(__MSP430_BASEADDRESS_EUSCI_B0__, commandByte);
 
   	  // Waits for shift register and buffer to be empty
   	  while(UCB0STATW & UCBUSY);
 
   	  // Latches data into potentiometer control register
   	  FRGPIO_setOutputHighOnPin(__MSP430_BASEADDRESS_PORT1_R__,FRGPIO_PORT_P1, FRGPIO_PIN0);
 
   	  __delay_cycles(1000);
 
   	  // Enable Serial Interface
   	  FRGPIO_setOutputLowOnPin(__MSP430_BASEADDRESS_PORT1_R__,FRGPIO_PORT_P1, FRGPIO_PIN0);
 
   	  //Send DPOT value
   	  eSPI_transmitData(__MSP430_BASEADDRESS_EUSCI_B0__, DPOT);
 
   	  // Waits for shift register and buffer to be empty
   	  while(UCB0STATW & UCBUSY);
 
   	  // Latches data into potentiometer control register
   	  FRGPIO_setOutputHighOnPin(__MSP430_BASEADDRESS_PORT1_R__,FRGPIO_PORT_P1, FRGPIO_PIN0);
 
   	  __delay_cycles(1000);
    }
 
    return (0);
}

Now that we have seen this using Grace and DriverLib, the example code provided below demonstrates the same functionality using register level code on the G2553 that the booster pack ships with. <syntaxhighlightlang='c'>#include <msp430.h>

/* BIT0 = CS

* BIT5 = SCLK
* BIT6 = SIMO
*
* Drive CS low to enable serial interface and clock data synchronously into the shift register on each SCLK rising edge
* After loading data into shift register drive CS high to latch data into the appropriate potentiometer control register
* Keep CS low during entire serial data stream.
*/

unsigned char DPOT = 255; unsigned char commandByte = 0;

int main(void) {


           WDTCTL = WDTPW + WDTHOLD;				// Disable watchdog timer

/* Ports */

           P1OUT = BIT0;                                       // CS High
           P1DIR |= BIT0 + BIT5;				// Set as outputs
           P1SEL = BIT5 + BIT6 + BIT7;                         // Configures SPI
           P1SEL2 = BIT5 + BIT6+ BIT7;
           /* USCI */
           UCB0CTL1 = UCSWRST;                                 // Put state machine in reset
           UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC;        // 3 pin 8-bit SPI Master
           UCB0CTL1 |= UCSSEL_2;				// USCI Clock Source: SMCLK
           UCB0BR0 |= 0x02;
           UCB0BR1 = 0;
           UCB0CTL1 &= ~UCSWRST;				// Release from reset and initialize USCI state machine
           /* Transfer */
           while(1) {
               P1OUT &= ~BIT0;					// Enables serial interface
               __delay_cycles(1000);				// Waits for slave to initialize
               UCB0TXBUF = commandByte;     			// Send Command Byte
               while(UCB0STAT & UCBUSY);                       // Waits for completion for shift register and buffer to be empty
               P1OUT |= BIT0;                                  // Latches data into potentiometer control register
               __delay_cycles(1000);
               P1OUT &= ~BIT0;
               UCB0TXBUF = DPOT;				// Enter value of 0-255 to transmit
               __delay_cycles(100);
             /*  DPOT--;
               __delay_cycles(1000);				// Decrements voltage to 0
               if (DPOT == 0)
               	DPOT = 255;
              	  */
               while(UCB0STAT & UCBUSY);			// Waits for completion for shift register and buffer to be empty
               P1OUT |= BIT0;					// Latches data into potentiometer control register
               __delay_cycles(1000);
           			}

} </syntaxhighlight> An additional commented out snippet of code was added to decrement the voltage to zero. You could attach an LED and observe it becoming dimmer as the voltage decrements.

References

  1. MSP430FR57xx Family User Guide
  2. MSP430G2x31 Datasheet
  3. TPL0501Datasheet