Hi I'm trying write spi code, but only get 0x0000. you can see my code and tell me what I'm doing wrong.
Best regards. Jhon.
#include <msp430.h>
#include <stdio.h>
#include <stdint.h>
// registro de spi msp430f2274
#define SPI_PxSEL P3SEL
#define SPI_PxDIR P3DIR
#define SPI_PxIN P3IN
#define SPI_PxOUT P3OUT
#define SPI_SIMO 0x02 //P3.1
#define SPI_SOMI 0x04 //P3.2
#define SPI_UCLK 0x08 //P3.3
// chip select
#define SPI_CS 0x80 //P3.7
#define CS_LOW() SPI_PxOUT &=~ SPI_CS
#define CS_HI() SPI_PxOUT |= SPI_CS
// registro de spi ADS8684
#define ADS8684_NO_OP 0x0000 //Continue operation in previous mode
#define ADS8684_STDBY 0x8200 //Device is placed into standby mode
#define ADS8684_PWR_DN 0x8300 //Device is powered down
#define ADS8684_RST 0x8500
#define ADS8684_AUTO_RST 0xA000
// Seleccion Manuel de los Canales
#define ADS8684_MAN_CH_0 0xC000
#define ADS8684_MAN_CH_1 0xC400
#define ADS8684_MAN_CH_2 0xC800
#define ADS8684_MAN_CH_3 0xCC00
// address register
#define ADS8684_CONFIG_REG 0x00
#define ADS8684_READ_BIT 0x80
#define ADS8684_WRITE_BIT 0x7F
// Program Register Map-Range 2.5v
#define ADS8684_CH0_RANGE 0x0B05
#define ADS8684_CH1_RANGE 0x0D05
#define ADS8684_CH2_RANGE 0x0F05
#define ADS8684_CH3_RANGE 0x1105
//*****************************************************************************************
// funtion
uint16_t ADS8684_SPI_WRITE_REG(uint16_t value);
//*****************************************************************************************
void SPI_SETUP(void)
{
SPI_PxOUT |=SPI_CS;
SPI_PxDIR |=SPI_CS; //Disable ADS8684, CS disable
UCB0CTL1 |= UCSWRST; // **Disable USCI state machine**
UCB0CTL0 = UCCKPL+UCMSB+UCMST+UCSYNC; //3-pin, 8-bit SPI master
UCB0CTL1 = UCSSEL_2; //SMCLK,RESET
UCB0BR0 = 0x02; //UCLK/2
UCB0BR1 = 0;
SPI_PxSEL|=SPI_SIMO|SPI_SOMI|SPI_UCLK;
SPI_PxDIR|=SPI_SIMO|SPI_SOMI|SPI_UCLK;
UCB0CTL1 &=~UCSWRST; // **Initialize USCI state machine**
}
uint16_t ADS8684_SPI_WRITE_REG(uint16_t value)
{
uint16_t x=0;
uint8_t aux;
CS_LOW(); // /CS enable
aux = value>>8; // Get msb data to send
while (!(IFG2 & UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = aux; // Send msb
aux = value & 0x00FF; // Get lsb data to send
while (!(IFG2&UCB0TXIFG)); // Wait for TXBUF ready
UCB0TXBUF = aux; // Send lsb data value
// while (UCB0STAT & UCBUSY); // Wait for TX complete
while (!(IFG2 & UCB0RXIFG)); // USCI_B0 RX buffer ready?
x = UCB0RXBUF; // Read
__delay_cycles(50);
CS_HI(); // /CS disable
return x; // return value
}
/*
* main.c
*/
void main(void)
{
// defino variables
uint16_t reg_write;
uint16_t reg_read;
uint16_t num;
volatile unsigned int i;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction
P1OUT &= ~0x01; // Toggle P1.0 using exclusive-OR
SPI_SETUP();
reg_write=ADS8684_RST;
reg_read=ADS8684_SPI_WRITE_REG(reg_write);
reg_write=ADS8684_CH0_RANGE;
reg_read=ADS8684_SPI_WRITE_REG(reg_write);
__no_operation();
// test write reg
// num=ADS8684_CH0_RANGE;
num=reg_read;
if (num==reg_write)
{
P1OUT |= 0x01; // // led on;
__delay_cycles(250000);
__no_operation();
}
else
{
P1OUT &= ~0x01; // led off;
__delay_cycles(250000);
__no_operation();
}
}