Other Parts Discussed in Thread: LMX2595
Hello everyone. I am working on MSP430F5529 from few months. My area of application is to make MSP430F5529 operate in master mode and transfer 24-bit data serially to the slave device (LMX2595). So, I had referred the sample code which was available in the resource explorer and modified according to my application. could any one please find the code below any do let me know any corrections required.
1) How can I check whether the data is transferred to slave device, I mean without slave device can I check the data by interfacing the controller with LCD display.
2) How to make use of interrupt LPMx combined with ISR mode
3) Once the data is been shifted and loaded into TX buffer how could I write into slave device. Is Write function required.
#include <msp430.h>
#include <stdint.h>
#include <stdbool.h>
void IOInitiate(void);
void SPI_master_Initiate(void);
//void spi_write(unsigned char * data, unsigned char count);
unsigned long registervalues;
unsigned int reg=79;
#define DUMMY 0xFF
#define STE 1
#define SIMO 2
#define SOMI 3
#define CLK 4
#define CS_REG P2OUT
#define CS_BIT BIT7
#define SLAVE_CS_DIR P2DIR
#define SLAVE_CS_PIN BIT0
#define MAX_BUFFER_SIZE 10
typedef enum SPI_ModeEnum{
IDLE_MODE,
TX_REG_ADDRESS_MODE,
RX_REG_ADDRESS_MODE,
TX_DATA_MODE,
RX_DATA_MODE,
TIMEOUT_MODE
} SPI_Mode;
SPI_Mode MasterMode = IDLE_MODE;
uint8_t TransmitRegAddr = 0;
uint8_t ReceiveBuffer[MAX_BUFFER_SIZE] = {0};
uint8_t RXByteCtr = 0;
uint8_t ReceiveIndex = 0;
uint8_t TransmitBuffer[MAX_BUFFER_SIZE] = {0};
uint8_t TXByteCtr = 0;
uint8_t TransmitIndex = 0;
unsigned char count;
void spi_write(unsigned char *data, unsigned char count)
{
static unsigned char cnt;
CS_REG &= ~CS_BIT;
for(cnt=0; cnt < count; count++)
{
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = data[cnt];
}
while(UCB0STAT & UCBUSY);
CS_REG |= CS_BIT;
}
void initClockTo16MHz()
{
UCSCTL3 |= SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation
UCSCTL2 = FLLD_0 + 487; // Set DCO Multiplier for 16MHz
// (N + 1) * FLLRef = Fdco
// (487 + 1) * 32768 = 16MHz
// Set FLL Div = fDCOCLK
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 16 MHz / 32,768 Hz = 500000 = MCLK cycles for DCO to settle
__delay_cycles(500000);//
// Loop until XT1,XT2 & DCO fault flag is cleared
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
}
void initGPIO()
{
//LEDs
P1OUT = 0x00; // P1 setup for LED & reset output
P1DIR |= BIT0 + BIT5;
P4DIR |= BIT7;
P4OUT &= ~(BIT7);
//SPI Pins
P3SEL |= BIT3 + BIT4; // P3.3,4 option select
P2SEL |= BIT7; // P2.7 option select
//Button to initiate transfer
P1DIR &= ~BIT1; // Set P1.1 to input direction
P1REN |= BIT1; // Enable P1.1 internal resistance
P1OUT |= BIT1; // Set P1.1 as pull-Up resistance
P1IES |= BIT1; // P1.1 High/Low edge
P1IFG &= ~BIT1; // P1.1 IFG cleared
P1IE |= BIT1; // P1.1 interrupt enabled
}
void initSPI()
{
//Clock Polarity: The inactive state is high
//MSB First, 8-bit, Master, 3-pin mode, Synchronous
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC;
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 |= 0x20; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation must be cleared for SPI
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI0 RX interrupt
SLAVE_CS_DIR |= SLAVE_CS_PIN;
//SLAVE_CS_OUT |= SLAVE_CS_PIN;
}
unsigned int cnt=0;
unsigned long register_values[79] = {0x00251C,0x010808,0x020500,0x030642,0x040A43,0x0500C8,0x06C802,0x0740B2,
0x082000,0x091604,0x0A10D8,0x0B0018,0x0C5001,0x0D4000,0x0E1E40,0x0F064F,
0x100080,0x11012C,0x120064,0x1327B7,0x14E048,0x150401,0x160001,0x17007C,
0x18071A,0x190C2B,0x1A0DB0,0x1B0003,0x1C0488,0x1D318C,0x1E318C,0x1F43EC,
0x200393,0x211E21,0x220000,0x230004,0x24002A,0x250404,0x26FFFF,0x27FFFA,
0x280000,0x290000,0x2A7FFF,0x2BFFFD,0x2C0AE3,0x2DD0DF,0x2E07FD,0x2F0300,
0x300300,0x314180,0x320000,0x330080,0x340820,0x350000,0x360000,0x370000,
0x380000,0x390020,0x3A8001,0x3B0001,0x3C0000,0x3D00A8,0x3E0322,0x3F0000,
0x401388,0x410000,0x4201F4,0x430000,0x4403E8,0x450000,0x46C350,0x470081,
0x480001,0x49003F,0x4A0000,0x4B0800,0x4C000C,0x4D0000,0x4E0003};
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog timer
P3SEL |= 0x18;
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC;
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 |= 0x20; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation must be cleared for SPI
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE;
//unsigned char count;
//unsigned int reg;
for(cnt=0;cnt < reg;cnt++)
{
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = register_values[cnt]>>16;
void spi_write(unsigned char *data, unsigned char count);
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = register_values[cnt]>>8;
void spi_write(unsigned char *data, unsigned char count);
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = register_values[cnt];
void spi_write(unsigned char *data, unsigned char count);
}
//unsigned long registervalues;
//registervalues=UCB0TXBUF;
}
No where I am finding enough data and I think this is the only forum which can help me. Please go through the code attached and do let me know for the correction of mistakes as I am running out of time.
Any help will be greatly greatly appreciated.