pic32mx230 spi число байтов

Этот ниже я делаю новый код для 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 ответ

Решение

Иногда лучший ответ самый простой:

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 */
}

Вам нужно будет выбрать правильный SPIXCON для шины SPI, к которой вы подключены в вашей схеме.

РЕДАКТИРОВАТЬ:

Я не буду делать проект для вас, вот мой пример с немного большим количеством примера кода.

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 */
}
Другие вопросы по тегам