pic32mx230 spi字节数

时间:2017-02-06 12:15:59

标签: c microchip pic32

下面这个我为pic32mx230 SPI模块和声设计制作新代码我需要发送96个字节
寄存器值和每3个字节开始CS将从低到高可以任何人帮助
我们哪里错了?

 `APP_DATA appData;
  static uint8_t __attribute__ ((aligned (8))) app_spi_tx_buffer[128]  =0xD4,0x1E,
    0x00
    0xD4,0x1D,0x80,\
    0xD4,0x1C,0x09,\
    0xD4,0x1B,0x00,\
    0xD4,0x1A,0x00,\
    0xD4,0x19,0x64,\
    0xD4,0x18,0x18,\
    0xD4,0x17,0x00,\
    0xD4,0x16,0x80,\
    0xD4,0x15,0x00,\
    0xD4,0x14,0x00,\
    0xD4,0x13,0x00,\
    0xD4,0x12,0x00,\
    0xD4,0x11,0x00,\
    0xD4,0x10,0x00,\
    0xD4,0x0F,0x00,\
    0xD4,0x0E,0x80,\
    0xD4,0x0D,0xE8,\
    0xD4,0x0C,0x18,\
    0xD4,0x0B,0x00,\
    0xD4,0x0A,0x01,\
    0xD4,0x09,0xF0,\
    0xD4,0x08,0x00,\
    0xD4,0x07,0x00,\
    0xD4,0x06,0x53,\
    0xD4,0x05,0x00,\
    0xD4,0x04,0x01,\
    0xD4,0x03,0x04,\
    0xD4,0x02,0xAA,\
    0xD4,0x01,0xAA,\
    0xD4,0x00,0xAB,\
    0xD4,0x1B,0x00,\
    0xD4,0x1D,0x81};


  void Modulator_SPI_Enable ( void )
   {


  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN); //start clock from here 
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN); //start clock from here 
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  TIME_DelayMs(20);

  }


 /* state machine for the SPI */
    static void SPI_Task(void)
{
   unsigned char i;
  /* run the state machine here for SPI */
   switch (appData.spiStateMachine)
{
    default:
    case APP_SPI_STATE_START:
        /* set the state to 'wait' early so that the interrupt doesn't
            finish fast and write the state and then is overwritten */
        appData.spiStateMachine =  APP_SPI_STATE_WAIT;

        MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);    //start clock from here 
        for (i=0; i<3; i++)
        {
            app_spi_tx_buffer[i];
        MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
        }
        appData.drvSPIBufferHandle = DRV_SPI_BufferAddWrite(appData.handleSPI0,
            app_spi_tx_buffer, sizeof(app_spi_tx_buffer),
            0, 0);

        if (DRV_SPI_BUFFER_HANDLE_INVALID == appData.drvSPIBufferHandle)
        {

            /* try again if we get a bad handle */
            appData.spiStateMachine =  APP_SPI_STATE_START;
        }
    break;

    case APP_SPI_STATE_WAIT:
    {
 if ( DRV_SPI_BufferStatus(appData.drvSPIBufferHandle) &    DRV_SPI_BUFFER_EVENT_COMPLETE)
        {
            appData.spiStateMachine = APP_SPI_STATE_DONE;
        }
    }
    break;

    case APP_SPI_STATE_DONE:
    break;
   }
 }

 void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
appData.state = APP_STATE_INIT;
Modulator_SPI_Enable();
MOD_SPI_CS_DESELECT(SPI_SLAVE_2_CS_PORT_ID,SPI_SLAVE_2_CS_PORT_PIN);
TXDIS_DSELECT();
MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN); //  CS  will be low 

appData.handleSPI0 = DRV_HANDLE_INVALID;

/* TODO: Initialize your application's state machine and other
 * parameters.
 */
}


 void APP_Tasks ( void )
 {

/* Check the application's current state. */
switch ( appData.state )
  {
    /* Application's initial state. */
    case APP_STATE_INIT:
    {
        bool appInitialized = true;


        if (DRV_HANDLE_INVALID == appData.handleSPI0)
        {
            appData.handleSPI0 = DRV_SPI_Open(0, DRV_IO_INTENT_WRITE);
            appInitialized &= (DRV_HANDLE_INVALID != appData.handleSPI0);
        }

        if (appInitialized)
        {
            /* initialize the SPI state machine */
            appData.spiStateMachine = APP_SPI_STATE_START;

            appData.state = APP_STATE_SERVICE_TASKS;
        }
        break;
    }

    case APP_STATE_SERVICE_TASKS:
    {
        /* run the state machine for servicing the SPI */
        SPI_Task();

        break;
    }

    /* TODO: implement your application state machine.*/


    /* The default state should never be executed. */
    default:
    {
        /* TODO: Handle error in application's state machine. */
        break;
    }
  }
  }

由于

诺姆

1 个答案:

答案 0 :(得分:0)

有时最好的答案是最简单的:

unsigned char buff={0xD4,0x04,0x03,0x02,0x01,0x1E};
unsigned char i;
SPI1CON = 0x8020; // SPI on and SPI Master see 61106G.pdf PIC32 FRM
for(i = 0; i < 6; i++)
{
    /* CS = HIGH */
    SPI1BUF=buff[i];
    while(SPI1STATbits.SPIBUSY);
    // read the SDI line
    unsigned char x = SPI1BUF;
    /* CS = LOW */
}

您需要为原理图中连接的SPI总线选择正确的SPIXCON。

编辑:

我不会为你做项目,这是我的示例,其中包含更多示例代码。

unsigned char buff[128]={0xD4,0x04,0x03,0x02,0x01,0x1E.....};
unsigned char i;
SPI1CON = 0x8020; // SPI on and SPI Master see 61106G.pdf PIC32 FRM
unsigned char j = 0;
for(i = 0; i < 128; i++)
{
    /* CS = HIGH */
    if (j == 0)            
        MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
    SPI1BUF=buff[i];
    while(SPI1STATbits.SPIBUSY);
    // read the SDI line
    unsigned char x = SPI1BUF;
    if ( j == 2)
    {
       j = 0;
       MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
    }
    else
       j++;
    /* CS = LOW */
}