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.

CCS/MSP430F5529: About the MSP430 SPI communication with a potentiometer

Part Number: MSP430F5529

Tool/software: Code Composer Studio

Hi, I am new to the msp430. The one I use is msp430f5529. I tried to communicate with a potentiometer which model is mcp41010.

I modified the SPI example code, but it didn't work. The led that connected in series with the potentiometer did not change its brightness when I change the value of the resistance through SPI.

I use the same circuit communicating with a Raspberry Pi, and it worked. I believe it is the problem with the code. Maybe I did not correctly set up the SPI on MSP430.

The following is my code.

/* --COPYRIGHT--,BSD
 * Copyright (c) 2017, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --/COPYRIGHT--*/
#include "driverlib.h"

//*****************************************************************************
//! This example shows how SPI master talks to SPI slave using 3-wire mode.
//! Incrementing data is sent by the master starting at 0x01. Received data is
//! expected to be same as the previous transmission.  USCI RX ISR is used to
//! handle communication with the CPU, normally in LPM0. If high, P1.0 indicates
//! valid data reception.  Because all execution after LPM0 is in ISRs,
//! initialization waits for DCO to stabilize against ACLK.
//! ACLK = ~32.768kHz, MCLK = SMCLK = DCO ~ 1048kHz.  BRCLK = SMCLK/2
//!
//! Use with SPI Slave Data Echo code example.  If slave is in debug mode, P1.1
//! slave reset signal conflicts with slave's JTAG; to work around, use IAR's
//! "Release JTAG on Go" on slave device.  If breakpoints are set in
//! slave RX ISR, master must stopped also to avoid overrunning slave
//! RXBUF.
//!
//!                  MSP430F5438A
//!                 -----------------
//!            /|\ |                 |
//!             |  |                 |
//!    Master---+->|RST              |
//!                |                 |
//!                |             P3.1|-> Data Out (UCB0SIMO)
//!                |                 |
//!                |             P3.2|<- Data In (UCB0SOMI)
//!                |                 |
//!                |             P3.3|-> Serial Clock Out (UCB0CLK)
//!
//!
//! This example uses the following peripherals and I/O signals.  You must
//! review these and change as needed for your own board:
//! - SPI peripheral
//! - GPIO Port peripheral (for SPI pins)
//! - UCB0SIMO
//! - UCB0SOMI
//! - UCB0CLK
//!
//! This example uses the following interrupt handlers.  To use this example
//! in your own application you must add these interrupt handlers to your
//! vector table.
//! - USCI_A0_VECTOR
//!
//*****************************************************************************

//*****************************************************************************
//
//Specify desired frequency of SPI communication
//
//*****************************************************************************
#define SPICLK                          500000

uint8_t CommandCode = 0x00;
uint8_t DataCode = 0x00;
uint8_t returnValue = 0x00;

void main (void)
{
    //Stop watchdog timer
    WDT_A_hold(WDT_A_BASE);

    //Set P1.1 for slave reset

    //Set P1.1 for slave reset
    //Set P1.0 to output direction
    GPIO_setAsOutputPin(
        GPIO_PORT_P1,
        GPIO_PIN2
        );

    //P3.5,4,0 option select
    GPIO_setAsPeripheralModuleFunctionOutputPin(
        GPIO_PORT_P3,
        GPIO_PIN0+GPIO_PIN2
        );

    //Initialize Master
    USCI_B_SPI_initMasterParam param = {0};
    param.selectClockSource = USCI_B_SPI_CLOCKSOURCE_SMCLK;
    param.clockSourceFrequency = UCS_getSMCLK();
    param.desiredSpiClock = SPICLK;
    param.msbFirst = USCI_B_SPI_MSB_FIRST;
    param.clockPhase = USCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
    param.clockPolarity = USCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
    returnValue =  USCI_B_SPI_initMaster(USCI_B0_BASE, &param);

    if (STATUS_FAIL == returnValue){
        return;
    }

    //Enable SPI module
    GPIO_setOutputHighOnPin(
            GPIO_PORT_P1,
            GPIO_PIN2
            );
    USCI_B_SPI_enable(USCI_B0_BASE);

    //Enable Receive interrupt

    //Wait for slave to initialize
    __delay_cycles(100);
    while (1)
    {
        CommandCode = 0x11;
            DataCode=0x0A;
            //USCI_A0 TX buffer ready?

            //Transmit Data to slave
            GPIO_setOutputLowOnPin(
                        GPIO_PORT_P1,
                        GPIO_PIN2
                        );
            USCI_B_SPI_transmitData(USCI_B0_BASE, CommandCode);
            USCI_B_SPI_transmitData(USCI_B0_BASE, DataCode);
            GPIO_setOutputHighOnPin(
                        GPIO_PORT_P1,
                        GPIO_PIN2
                        );
            __delay_cycles(1000);
            CommandCode = 0x20;
            GPIO_setOutputLowOnPin(
                                    GPIO_PORT_P1,
                                    GPIO_PIN2
                                    );
            USCI_B_SPI_transmitData(USCI_B0_BASE, CommandCode);
            GPIO_setOutputHighOnPin(
                                    GPIO_PORT_P1,
                                    GPIO_PIN2
                                    );
            __delay_cycles(1000);
    }
    //Initialize data values

    //CPU off, enable interrupts
    __bis_SR_register(LPM0_bits + GIE);
}

//******************************************************************************
//
//This is the USCI_B0 interrupt vector service routine.
//
//******************************************************************************


  • 1) It appears you're switching from some resistance to shutdown, waiting 1ms in between. Your eye won't be able to see a 1ms transition in brightness (though the LED might appear to dim slightly). Try extending from 1000 cycles to e.g. 100000 cycles (0.1 second).

    2) Data sheet (DS11195C) Section 5.3 says you still need to send the byte after the Shutdown command, even though it is ignored.

    3) For each transaction, you need to wait until the last byte is sent before de-asserting /CS. Just spin until USCI_B_SPI_isBusy() returns 0.

    [Edit: Fixed typo.]

  • Hi,

    You seem to follow our code example to write the code. Can you use a digital scope to catch the wave? It will easy for me to find the reason.

    Eason

  • Hi, thanks for your advisement. I just followed your suggest and did some change on my code. However, unfortunately, it still cant work.

    I am thinking if it is the problem with my SPI set up. I connected Pin 3.2 as CLK, and 3.0 as MOSI. 

    I set the Pin 3.0 and 3.2 as ModuleFunctionOutputPin using the function "GPIO_setAsPeripheralModuleFunctionOutputPin". But the sample code set these all as input pin. 

    I still cant solve the problem. But I will do a digital scope tomorrow see if I can find something. 

    Oh the following is my code currently.

    #include "driverlib.h"
    
    //*****************************************************************************
    //! This example shows how SPI master talks to SPI slave using 3-wire mode.
    //! Incrementing data is sent by the master starting at 0x01. Received data is
    //! expected to be same as the previous transmission.  USCI RX ISR is used to
    //! handle communication with the CPU, normally in LPM0. If high, P1.0 indicates
    //! valid data reception.  Because all execution after LPM0 is in ISRs,
    //! initialization waits for DCO to stabilize against ACLK.
    //! ACLK = ~32.768kHz, MCLK = SMCLK = DCO ~ 1048kHz.  BRCLK = SMCLK/2
    //!
    //! Use with SPI Slave Data Echo code example.  If slave is in debug mode, P1.1
    //! slave reset signal conflicts with slave's JTAG; to work around, use IAR's
    //! "Release JTAG on Go" on slave device.  If breakpoints are set in
    //! slave RX ISR, master must stopped also to avoid overrunning slave
    //! RXBUF.
    //!
    //!                  MSP430F5438A
    //!                 -----------------
    //!            /|\ |                 |
    //!             |  |                 |
    //!    Master---+->|RST              |
    //!                |                 |
    //!                |             P3.1|-> Data Out (UCB0SIMO)
    //!                |                 |
    //!                |             P3.2|<- Data In (UCB0SOMI)
    //!                |                 |
    //!                |             P3.3|-> Serial Clock Out (UCB0CLK)
    //!
    //!
    //! This example uses the following peripherals and I/O signals.  You must
    //! review these and change as needed for your own board:
    //! - SPI peripheral
    //! - GPIO Port peripheral (for SPI pins)
    //! - UCB0SIMO
    //! - UCB0SOMI
    //! - UCB0CLK
    //!
    //! This example uses the following interrupt handlers.  To use this example
    //! in your own application you must add these interrupt handlers to your
    //! vector table.
    //! - USCI_A0_VECTOR
    //!
    //*****************************************************************************
    
    //*****************************************************************************
    //
    //Specify desired frequency of SPI communication
    //
    //*****************************************************************************
    #define SPICLK                          500000
    
    uint8_t CommandCode = 0x00;
    uint8_t DataCode = 0x00;
    uint8_t returnValue = 0x00;
    
    void main (void)
    {
        //Stop watchdog timer
        WDT_A_hold(WDT_A_BASE);
    
        //Set P1.1 for slave reset
    
        //Set P1.1 for slave reset
        //Set P1.0 to output direction
        GPIO_setAsOutputPin(
            GPIO_PORT_P1,
            GPIO_PIN2
            );
    
        //P3.5,4,0 option select
        GPIO_setAsPeripheralModuleFunctionOutputPin(
            GPIO_PORT_P3,
            GPIO_PIN0+GPIO_PIN2
            );
    
        //Initialize Master
        USCI_B_SPI_initMasterParam param = {0};
        param.selectClockSource = USCI_B_SPI_CLOCKSOURCE_SMCLK;
        param.clockSourceFrequency = UCS_getSMCLK();
        param.desiredSpiClock = SPICLK;
        param.msbFirst = USCI_B_SPI_MSB_FIRST;
        param.clockPhase = USCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
        param.clockPolarity = USCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
        returnValue =  USCI_B_SPI_initMaster(USCI_B0_BASE, &param);
    
        if (STATUS_FAIL == returnValue){
            return;
        }
    
        //Enable SPI module
        GPIO_setOutputHighOnPin(
                GPIO_PORT_P1,
                GPIO_PIN2
                );
        USCI_B_SPI_enable(USCI_B0_BASE);
    
        //Enable Receive interrupt
    
        //Wait for slave to initialize
        __delay_cycles(100);
        DataCode=0x0A;
        CommandCode = 0x11;
        while (1)
        {
    
                //Transmit Data to slave
                GPIO_setOutputLowOnPin(
                            GPIO_PORT_P1,
                            GPIO_PIN2
                            );
                USCI_B_SPI_transmitData(USCI_B0_BASE, CommandCode);
                while(USCI_B_SPI_isBusy(USCI_B0_BASE));
                USCI_B_SPI_transmitData(USCI_B0_BASE, DataCode);
                while(USCI_B_SPI_isBusy(USCI_B0_BASE));
                GPIO_setOutputHighOnPin(
                            GPIO_PORT_P1,
                            GPIO_PIN2
                            );
                __delay_cycles(100000);
                if(DataCode!=0xFE)
                {
                    DataCode=DataCode+0x01;
                }
                else{
                    DataCode=0x0A;
                }
        }
        //Initialize data values
    }
    

  • Have you find the problem?

    Eason

**Attention** This is a public forum