打印
[MCU]

MSP430F5529LP的SPI中断方式通讯

[复制链接]
682|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ddh531810421|  楼主 | 2020-11-9 17:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
ddh531810421|  楼主 | 2020-11-9 18:00 | 只看该作者
#include "driverlib.h"
#include "msp430.h"
// Initialize data values
static uint8_t transmitData = 0x01;
static uint8_t receiveData = 0;
int main( void )
{
  WDT_A_hold(WDT_A_BASE);
  
//Initialize Master,USCI_B1作主机
  USCI_B_SPI_initMasterParam param = {0};
  param.selectClockSource = USCI_B_SPI_CLOCKSOURCE_SMCLK;
  param.clockSourceFrequency = UCS_getSMCLK();
  param.desiredSpiClock = 115200;
  param.msbFirst = USCI_B_SPI_MSB_FIRST;
  param.clockPhase = USCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
  param.clockPolarity = USCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
  bool returnValue = USCI_B_SPI_initMaster(USCI_B1_BASE, &param);
  if (STATUS_FAIL == returnValue){
    return 0;
  }
  //Initialize Slave,USCI_B0作从机
  returnValue = USCI_B_SPI_initSlave(USCI_B0_BASE, USCI_B_SPI_MSB_FIRST,USCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,USCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH);
  if (STATUS_FAIL == returnValue){
    return 0;
  }
  //Enable USCI_B_SPI module
  USCI_B_SPI_enable(USCI_B0_BASE);
  USCI_B_SPI_enable(USCI_B1_BASE);
  //Enable Receive interrupt
  USCI_B_SPI_enableInterrupt(USCI_B0_BASE, UCRXIE);
  USCI_B_SPI_enableInterrupt(USCI_B1_BASE, UCTXIE);
  //Configure port pins to reset slave
  // Wait for slave to initialize
  __delay_cycles(100);

  // USCI_B1 TX buffer ready?
// while (!USCI_B_SPI_getInterruptStatus(USCI_B1_BASE, UCTXIFG));
  //Transmit Data to slave
  //USCI_B_SPI_transmitData(USCI_B1_BASE, transmitData);
  
  
  // CPU off, enable interrupts
  __bis_SR_register(LPM0_bits + GIE);
  //while(1);
  }
  //******************************************************************************
  //
  // This is the USCI_B0 interrupt vector service routine.
  //
  //******************************************************************************
  #pragma vector=USCI_B0_VECTOR
  __interrupt void USCI_B0_ISR(void)
  {
  switch(__even_in_range(UCB0IV,4))
  {
  // Vector 2 - RXIFG
  case 2:
    while (!USCI_B_SPI_getInterruptStatus(USCI_B0_BASE, UCRXIFG));
    receiveData = USCI_B_SPI_receiveData(USCI_B0_BASE);
  break;
  default: break;
  }
  }

  #pragma vector=USCI_B1_VECTOR
  __interrupt void USCI_B1_ISR(void)
  {
  switch(__even_in_range(UCB1IV,4))
  {
  // Vector 2 - RXIFG
  case 4:
  transmitData++;
  // Send next value
  while (!USCI_B_SPI_getInterruptStatus(USCI_B1_BASE, UCTXIFG));
  USCI_B_SPI_transmitData(USCI_B1_BASE, transmitData);
  //Delay between transmissions for slave to process information
  __delay_cycles(40);
  break;
  default: break;
  }
  }


其中使用的函数和宏都是driverlib中给的usci_b_spi.h和.c的,以及板子定义的资源

使用特权

评论回复
板凳
zhangmangui| | 2020-11-9 23:18 | 只看该作者
建议下载MSP430WARE看看有可以参考的例程不

使用特权

评论回复
地板
ddh531810421|  楼主 | 2020-11-10 15:27 | 只看该作者
zhangmangui 发表于 2020-11-9 23:18
建议下载MSP430WARE看看有可以参考的例程不

我下来了它的参考例程,修改以后能跑,例程的意思是先发送,然后就能进入接受中断(这里不是很明白,为什么发送数据后能进自己的接收中断,我从机根本没发数据,甚至线都没连),然后在中断里继续发送,(主机是MSP430F5529LP,从机是一块调试好的NXP板子)但是我发的数据是0,1,2这样递增,收到的却是128,0,129,1,130,2这样的,有点没想明白

使用特权

评论回复
5
ddh531810421|  楼主 | 2020-11-10 15:31 | 只看该作者
ddh531810421 发表于 2020-11-10 15:27
我下来了它的参考例程,修改以后能跑,例程的意思是先发送,然后就能进入接受中断(这里不是很明白,为什 ...

#define SPICLK                          500000

uint8_t transmitData = 0x00, receiveData = 0x00;
uint8_t returnValue = 0x00;

void main (void)
{
    //Stop watchdog timer
    WDT_A_hold(WDT_A_BASE);

    //Set P2.6 for cs
    GPIO_setAsOutputPin(

        GPIO_PORT_P2,
        GPIO_PIN6
        );
    GPIO_setOutputHighOnPin(
        GPIO_PORT_P2,
        GPIO_PIN6
        );

    //Set P1.0 to output direction
    GPIO_setAsOutputPin(
        GPIO_PORT_P1,
        GPIO_PIN0
        );

    //P3.5,4,0 option select
    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN0 + GPIO_PIN1 + GPIO_PIN2);
    //GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2,GPIO_PIN7);
    //Initialize Master
    USCI_B_SPI_initMasterParam param = {0};
    param.selectClockSource = USCI_B_SPI_CLOCKSOURCE_SMCLK;
    param.clockSourceFrequency = UCS_getSMCLK();
    param.desiredSpiClock = SPICLK;
    param.msbFirst = USCI_B_SPI_MSB_FIRST;
    param.clockPhase = USCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
    param.clockPolarity = USCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
    returnValue =  USCI_B_SPI_initMaster(USCI_B0_BASE, &param);

    if (STATUS_FAIL == returnValue){
        return;
    }

    //Enable SPI module
    USCI_B_SPI_enable(USCI_B0_BASE);

    //Enable Receive interrupt
        USCI_B_SPI_clearInterrupt(USCI_B0_BASE, USCI_B_SPI_RECEIVE_INTERRUPT);
    USCI_B_SPI_enableInterrupt(USCI_B0_BASE, USCI_B_SPI_RECEIVE_INTERRUPT);

    //LED On
    GPIO_setOutputHighOnPin(
        GPIO_PORT_P1,
        GPIO_PIN0
        );
//片选
    GPIO_setOutputLowOnPin(
        GPIO_PORT_P2,
        GPIO_PIN6
        );


    //Wait for slave to initialize
    __delay_cycles(100);

    //Initialize data values
    transmitData = 0x00;

    //USCI_A0 TX buffer ready?
    while (!USCI_B_SPI_getInterruptStatus(USCI_B0_BASE,
               USCI_B_SPI_TRANSMIT_INTERRUPT)) ;

    //Transmit Data to slave
    USCI_B_SPI_transmitData(USCI_B0_BASE, transmitData);
    //CPU off, enable interrupts
    __bis_SR_register(LPM0_bits + GIE);
}

//******************************************************************************
//
//This is the USCI_B0 interrupt vector service routine.
//
//******************************************************************************
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_B0_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(USCI_B0_VECTOR)))
#endif
void USCI_B0_ISR (void)
{
    switch (__even_in_range(UCB0IV,4)){
        //Vector 2 - RXIFG
        case 2:
            //USCI_A0 TX buffer ready?
            while (!USCI_B_SPI_getInterruptStatus(USCI_B0_BASE,
                       USCI_B_SPI_TRANSMIT_INTERRUPT)) ;

            receiveData = USCI_B_SPI_receiveData(USCI_B0_BASE);

            //Increment data
            transmitData++;
            //Send next value
            USCI_B_SPI_transmitData(USCI_B0_BASE,
            transmitData
            );
     
            //Delay between transmissions for slave to process information
            __delay_cycles(200);

            break;
        default: break;
    }
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝