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, ¶m);
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.
//
//******************************************************************************