Hi all,
Because of the necessory of the other part of code, we need to use 16MHz SMCLK, the highest SPI clock is 5MHz.
When I tried with 1MHZ clock, it works correctly. the working code is as below:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <msp430.h>
#include "ADXL_Message.h"
unsigned char MST_Data[]={0xF2,0xF3,0xF4,0xF5,0xF6,0xF7};
char Byte[100];
volatile unsigned int j,k;
void WriteByte(unsigned char WriteAddress, unsigned char message);
void ReadByte(unsigned char ReadAddress);
int main(void)
{
volatile unsigned int i=0;
j=k=0;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT4 + BIT5 + BIT7; //
P1SEL = BIT6 + BIT7 + BIT5;
P1SEL2 = BIT6 + BIT7 + BIT5;
if (CALBC1_1MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // SMCLK
UCB0BR0 |= 0x01; // /1
UCB0BR1 = 0; //
//UCB0MCTL = 0; // No modulation
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
WriteByte(MESG_RST_Add, MESG_RST);
WriteByte(MESG_BaudRate_Add,MESG_BaudRate);
WriteByte(MESG_Power_Add,MESG_Power);
WriteByte(MESG_DataFormat_Add,MESG_DataFormat);
IE2 |= UCB0TXIE;
__bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
}
// Test for valid RX and TX character
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCIB0TX_ISR(void)
{
ReadByte(MESG_Z0_Add);
while (!(IFG2 & UCB0TXIFG));
Byte[k]=UCB0RXBUF;
k++;
ReadByte(MESG_Z1_Add);
while (!(IFG2 & UCB0TXIFG));
Byte[k]=UCB0RXBUF;
k++;
} // make sure slave can keep up
void WriteByte(unsigned char WriteAddress, unsigned char message)
{
__delay_cycles(1);
P1OUT |= BIT4;
__delay_cycles(1);
P1OUT &= ~BIT4;
__delay_cycles(1);
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = WriteAddress;
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = message;
}
void ReadByte(unsigned char ReadAddress)
{
__delay_cycles(1);
P1OUT |= BIT4;
__delay_cycles(1);
P1OUT &= ~BIT4;
__delay_cycles(1);
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = ReadAddress;
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0xFF;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
However, when I change the clock to 16MHZ, I still set the baudrate to 1MHZ, but it doesn't work any more. Is that possible for SPI works upon 16MHZ? Or is that possible for spi to work at 4MHZ baudrate?The doesn't work code is as below, Thank you very much!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <msp430.h>
#include "ADXL_Message.h"
unsigned char MST_Data[]={0xF2,0xF3,0xF4,0xF5,0xF6,0xF7};
char Byte[100];
volatile unsigned int j,k;
void WriteByte(unsigned char WriteAddress, unsigned char message);
void ReadByte(unsigned char ReadAddress);
int main(void)
{
volatile unsigned int i=0;
j=k=0;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT4 + BIT5 + BIT7; //
P1SEL = BIT6 + BIT7 + BIT5;
P1SEL2 = BIT6 + BIT7 + BIT5;
if (CALBC1_16MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_16MHZ; // Set DCO
DCOCTL = CALDCO_16MHZ;
UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // SMCLK
UCB0BR0 |= 0x10; // /1
UCB0BR1 = 0; //
//UCB0MCTL = 0; // No modulation
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
WriteByte(MESG_RST_Add, MESG_RST);
WriteByte(MESG_BaudRate_Add,MESG_BaudRate);
WriteByte(MESG_Power_Add,MESG_Power);
WriteByte(MESG_DataFormat_Add,MESG_DataFormat);
IE2 |= UCB0TXIE;
__bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
}
// Test for valid RX and TX character
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCIB0TX_ISR(void)
{
ReadByte(MESG_Z0_Add);
while (!(IFG2 & UCB0TXIFG));
Byte[k]=UCB0RXBUF;
k++;
ReadByte(MESG_Z1_Add);
while (!(IFG2 & UCB0TXIFG));
Byte[k]=UCB0RXBUF;
k++;
} // make sure slave can keep up
void WriteByte(unsigned char WriteAddress, unsigned char message)
{
__delay_cycles(16);
P1OUT |= BIT4;
__delay_cycles(16);
P1OUT &= ~BIT4;
__delay_cycles(16);
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = WriteAddress;
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = message;
}
void ReadByte(unsigned char ReadAddress)
{
__delay_cycles(16);
P1OUT |= BIT4;
__delay_cycles(16);
P1OUT &= ~BIT4;
__delay_cycles(16);
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = ReadAddress;
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = 0xFF;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////