c - pic32mx230 spi 字节数

标签 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;
    }
  }
  }

谢谢

名字

最佳答案

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

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

关于c - pic32mx230 spi 字节数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42067588/

相关文章:

c - 程序和共享库之间何时进行动态链接?

c - 有没有办法在编译时确定成员偏移量?

c - 是否有可能检测到 C 中保留标识符的冲突使用?

c - PIC32 取消引用指针可能是编译器错误

c - PIC32MZ 上的 Microchip Harmony 时序问题

c - PIC32 WDT 复位不适用于某些 PIC32

c - 使用 time.h,我可以根据 .ics 文件中的时间构造日期和时间字符串吗?

c - 如何使用 Pic32 打开连接到 PT6965 LED Controller 的 RGB LED?

c - 如果一个结构同时定义了它的 uint16_t 字和 uint8_t 字节,则数组的大小加倍

c - 有没有更短的方法来初始化结构数组?