在研究432的SPI时,先用的官网3线的收发例程。用的432A1做主机,B1做从机,连了个时钟,MISO和MOSI3根线,程序如下:#include "msp.h"
#include "driverlib/driverlib.h"static uint8_t TXData = 0;
static volatile uint8_t RXData = 0;
static volatile uint8_t transmitData = 0x87, receiveData = 0x00;const eUSCI_SPI_MasterConfig spiMasterConfig =
{
EUSCI_A_SPI_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
3000000, // SMCLK = DCO = 3MHZ
3000000, // SPICLK = 500khz
EUSCI_A_SPI_MSB_FIRST, // MSB First
EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,
EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH,
EUSCI_SPI_3PIN
};const eUSCI_SPI_SlaveConfig spiSlaveConfig =
{
EUSCI_B_SPI_MSB_FIRST, // MSB First
EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT, // Phase
EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // Normal Polarity
EUSCI_SPI_3PIN
};void main(void)
{
uint8_t aa=0,aaa=0;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
//主机设置
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2,
GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
//从机设置
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6,
GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4 | GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);
SPI_initMaster(EUSCI_A1_BASE, &spiMasterConfig);
SPI_initSlave(EUSCI_B1_BASE, &spiSlaveConfig);
SPI_enableModule(EUSCI_A1_BASE);
SPI_enableModule(EUSCI_B1_BASE);
SPI_enableInterrupt(EUSCI_B1_BASE, EUSCI_B_SPI_RECEIVE_INTERRUPT);
Interrupt_enableInterrupt(INT_EUSCIB1);
Interrupt_enableMaster();TXData = 0x53;
while (!(SPI_getInterruptStatus(EUSCI_A1_BASE,EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
SPI_transmitData(EUSCI_A1_BASE, TXData++);while (!(SPI_getInterruptStatus(EUSCI_A1_BASE,EUSCI_A_SPI_RECEIVE_INTERRUPT)));
aa=SPI_receiveData(EUSCI_A1_BASE);while (!(SPI_getInterruptStatus(EUSCI_A1_BASE,EUSCI_A_SPI_TRANSMIT_INTERRUPT)));
SPI_transmitData(EUSCI_A1_BASE, TXData);while (!(SPI_getInterruptStatus(EUSCI_A1_BASE,EUSCI_A_SPI_RECEIVE_INTERRUPT)));
aaa=SPI_receiveData(EUSCI_A1_BASE);while(1);
}void EUSCIB1_IRQHandler(void)
{
uint32_t status;status = SPI_getEnabledInterruptStatus(EUSCI_B1_BASE);
SPI_clearInterruptFlag(EUSCI_B1_BASE, status);if(status & EUSCI_B_SPI_RECEIVE_INTERRUPT)
{
/* USCI_B1 TX buffer ready? */
while (!(SPI_getInterruptStatus(EUSCI_B1_BASE, EUSCI_B_SPI_TRANSMIT_INTERRUPT)));/* Transmit data to master */
SPI_transmitData(EUSCI_B1_BASE, transmitData++);//Receive data from master
receiveData = SPI_receiveData(EUSCI_B1_BASE);
}
}中间绿 {MOD}那段是我测试的收发段,我的意图是主机发送一个字节,然后从机收到,进入从机接收中断;然后从机再给主机发送一个字节,主机收到。然后再循环一次。但是问题出现在第一次从中断返回的时候,进从机中断的时候可以收到主机发来的数据,但是在中断里由从机给主机发数据的时候,从机TXbuffer显示有发送,但是主机RXbuffer没有数据,然后aa赋值就为0(图1)。但是第二次进中断的时候,主机接收有了从机第一次发送的值(图2)(这个是延时吗?但是我等一好久,主机接收始终没有收到从机发送的第二个值,第二次的值应该是0x88)。
图1
图2
这我就不太明白了,明明我也加了接收寄存器判断的啊。不太懂,不知道有没有高人指点下,谢谢!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
不复杂。。就是很简单的想试试SPI的功能,后来知道哪有问题了。
http://www.deyisupport.com/quest ... h=Search-CN-Support这个帖子里有说
一周热门 更多>