Hi everyone
I am using Olimex MSP430-449STK2 and ADE7878 development board. I am using IAR Embedded Workbench IDE.
I am trying to set-up SPI .
This is the code I have written below and I cannot find where I am making mistake .
Can someone help me with the code?
//SPI INITAILIZE*
void spiInit(void) {
U0CTL = 0x17; // SPI, 8 bit data, no loopback, SPI mode, master, reset
U0TCTL = 0xE3; // SPI Mode 3, CKPH=0, CKPL=0, SMCLK, 3 pin mode, Tx empty
U0BR0 = 4; // smclk divider Maximum serial clock frequency = 2.5Mhz.
U0BR1 = 0; // 8MHz/4=2Mhz.
U0MCTL = 0; // not used, but should be 0
//IE1 &= ~0x80; // disable interrupts
IE1 = 0xC0;
ME1 |= 0x40; // enable spi module
//U0CTL &= ~1; // release reset
//initialize
P3SEL = 0x00;
P3OUT = 0x00;
P3SEL |= 0xe; // P3 bits 1,2,3 used for spi
P3DIR |= 0xb; // P3 bits 0,1,3 used as output
P3DIR &= ~4; // P3 bit 2 input
P3OUT |= 0x01; // CS high
}
// Private module functions to manipulate SPI
void EnableSPI( void )
{ P3OUT &= ~0x01; // Set CS low
}
void DisableSPI( void )
{ P3OUT |= 0x01; // Set CS high
}
unsigned int ExecuteSPI( unsigned int nArg )
{ unsigned int data;
while ((IFG1 & UTXIFG0) == 0); // USART0 TX buffer ready?
IFG1 &= ~URXIFG0; // Clear Rx flag
// Exchange byte
TXBUF0 = nArg & 0xFF;
while ((IFG1 & UTXIFG0) == 0);
while ((IFG1 & URXIFG0) == 0);
data = RXBUF0;
return data;
}
unsigned int Read_Energy_Register(void)
{
unsigned int data;
EnableSPI();
ExecuteSPI(READ_ENERGY_REGISTER);
ExecuteSPI(0x00);
ExecuteSPI(0x00);
ExecuteSPI(0x01);
data = ExecuteSPI(0x00);
DisableSPI();
return data;
}
//SPI turn OFF**************
void spiOff(void)
{
ME1 &= ~0x40; // disable spi module
}