This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
//******************************************************************************
// MSP430G2xx3 Demo - USCI_A0, SPI 3-Wire Master Incremented Data
//
// Description: 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.
// ACLK = n/a, MCLK = SMCLK = DCO ~1.2MHz, BRCLK = SMCLK/2
//
// Use with SPI Slave Data Echo code example. If slave is in debug mode, P3.6
// 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.
//
// MSP430G2xx3
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.2|-> Data Out (UCA0SIMO)
// | |
// LED <-|P1.0 P1.1|<- Data In (UCA0SOMI)
// | |
// Slave reset <-|P1.5 P1.4|-> Serial Clock Out (UCA0CLK)
//
//
// D. Dang
// Texas Instruments Inc.
// February 2011
// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
//******************************************************************************
#include "msp430g2553.h"
#include "delay.h"
unsigned int i = 0 ;
unsigned char MST_Data, SLV_Data;
void main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1OUT = 0x00; // P1 setup for LED & reset output
P1DIR |= BIT0 + BIT5; //
P1SEL = BIT1 + BIT2 + BIT4;
P1SEL2 = BIT1 + BIT2 + BIT4;
UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 |= 0x02; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI0 RX interrupt
P1OUT &= ~BIT5; // Now with SPI signals initialized,
P1OUT |= BIT5; // reset slave
__delay_cycles(75); // Wait for slave to initialize
MST_Data = 0x01; // Initialize data values
SLV_Data = 0x00;
UCA0TXBUF = MST_Data; // Transmit first character
__bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
}
// Test for valid RX and TX character
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCIA0RX_ISR(void)
{
volatile unsigned int i;
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
if (UCA0RXBUF == SLV_Data) // Test for correct character RX'd
i++;
if(i>100){
i =0;
P1OUT ^= BIT0;
}
// P1OUT |= BIT0; // If correct, light LED
// else
// P1OUT &= ~BIT0; // If incorrect, clear LED
MST_Data++; // Increment master value
SLV_Data++; // Increment expected slave value
UCA0TXBUF = MST_Data; // Send next value
__delay_cycles(50); // Add time between transmissions to
} // make sure slave can keep up
这是master的程序。
本来因该是master发送数据给slave。slave将接受到的数据又传给master。
但现在master和slave之间没任何连接。但master竟然能进入接收中断。
并且
if (UCA0RXBUF == SLV_Data) // Test for correct character RX'd
还能这一条判断为真。。
不解。
1.程序分析:打开了接受中断,因为CLK是主机发出的,故SPI的接受端会在CLK的上升沿往UCA0RXBUF 中搬数据,在程序中 SLV_Data = 0x00;,如果悬空状态有可能SOMI脚读到的数据就是0.故能进中断而且会判断IF为真。
2.中断程序写简短一些,最好是读UCA0RXBUF将其赋值给一个变量,然后就返回,其余的操作放在主函数中。
IE2 |= UCA0RXIE; // Enable USCI0 RX interrupt
只使能了RX中断。TX中断应该不会发生吧。
刚才我示波器测了下 MI,和MO(因为这里是master) 两个脚。发现MI 脚一直是高电平。
MO 脚是有波形的。
然后断点在中断服务程序中。UCA0RXBUF 每次进中断值都是0xFF。
每次进中断 都会 SLV_Data++;
于是如果发生255次中断后 SLV_Data 就 = 255 了。
if (UCA0RXBUF == SLV_Data)
就能判断通过了。
是不是这样?
1.是的,所以在SPI通讯时要考虑一个简单的应答判断,如果MISO是拉高的,则可判断收到有些0xFF是从机断开的时候发生的
2.为了确认你的推断,可以在断点调试中看SVL_DATA的值变化,或者用toggle GPIO的方法用示波器观察