Microchip
直播中

李玲

7年用户 1233经验值
私信 关注
[问答]

怎么将SPI2与MCC配合使用

嗨,我希望有人能帮助我。我使用来自MCC的SPI2设置有麻烦。这是针对DSPIC33EP256MC502的。基本设置看起来是正确的,因为当我独立调用时,我可以看到范围上的信号中的数据、clk和数据,它们与MCP3002应该进行的读数相对应。在ADC值中没有读取。使用不起作用。我已经尝试了不同的值MyoBuffiSe大小,包括0,1,2,8,16,它从不离开DO,而…这个来自SPI2.h文件,作为如何访问SPI端口的示例。有人能建议一下它有什么问题吗?我可以设置错误的中断设置吗?(如果有人需要查看我的代码的其余部分,我不知道在哪里上传这个项目?)我已经花了3天的时间,我准备把我的头发拔掉。

以上来自于百度翻译


      以下为原文

    Hi,
I hope someone can help me. I'm having hassles using the SPI2 setup that comes from MCC. It's for a DSPIC33EP256MC502.

The basic setup appears to be correct as when I independently call

readData = SPI2_Exchange16bit(writeData);

I can see the data out, clk and data in signals on a scope and they correspond with the readings that the MCP3002 is supposed to be making.

This is how I currently have it setup

void SPI2_Initialize (void)
{
    // MSTEN Master; DISSDO disabled; PPRE 4:1; SPRE 5:1; MODE16 enabled; SMP End; DISSCK disabled; CKP Idle:Low, Active:High; CKE Idle to Active; SSEN disabled;
    SPI2CON1 = 0x62E;
    // SPIFSD disabled; SPIBEN enabled; FRMPOL disabled; FRMDLY disabled; FRMEN disabled;
    SPI2CON2 = 0x1;
    // SISEL SPI_INT_SPIRBF; SPIROV disabled; SPIEN enabled; SPISIDL disabled;
    SPI2STAT = 0x800C;
}

with the following in code parameters

    int MY_BUFER_SIZE = 2;
    uint16_t myWriteBuffer[MY_BUFER_SIZE];
    uint16_t myReadBuffer[MY_BUFER_SIZE];
    uint16_t writeData = 0b0110100000000000;
    uint16_t readData;
    SPI2_STATUS status;
    unsigned int total;
    total = 0;



However it is not reading in the ADC values.

Using
            CS0_SetLow();
            do {
                total = SPI2_Exchange16bitBuffer(&myWriteBuffer[total], MY_BUFER_SIZE - total, &myReadBuffer[total]);
                Fault_SetLow();
                // Do something else...
                //total = 16;

            } while (total < MY_BUFER_SIZE);
            Fault_SetHigh();
            readData = SPI2_Exchange16bit(writeData);

            status = SPI2_StatusGet();
            CS0_SetHigh();

does not work. I've tried different values for MY_BUFER_SIZE including 0,1,2,8,16 and it never leaves the do while... loop.

This comes from the SPI2.h file as an example of how to access the SPI port.

Can someone advise what might be wrong with it?

Could I have the wrong interrupt set?


(I don't see where to upload the project if someone needs to look at the rest of my code?)



I've spent 3 days at this and I'm ready to tear my hair out :(

回帖(4)

汪岑

2019-5-27 14:31:05
对不起,这应该是在16位william hill官网 。有人能搬动它吗?

以上来自于百度翻译


      以下为原文

    Sorry, this should be under the 16 bit forum. Any chance someone can move it? 
举报

王焕树

2019-5-27 14:40:02
我从未使用过MCC,所以这只是一个猜测。我怀疑这一行:.=SPI2_Exchange16bitBuffer(&myWriteBuffer[.]、MY_BUFER_SIZE-.、&myReadBuffer[.]);SPI2_Exchange16bitBuffer()的返回值到底是多少?如果传输的字数是单词的数量,则此代码是错误的,它应该是:.+=SPI2_Exchange16bitBuffer(&myWriteBuffer[.]、MY_BUFER_SIZE-.、&myReadBuffer[.]);

以上来自于百度翻译


      以下为原文

    I've never used MCC, so this is just a guess.
I am suspicious of this line:
total = SPI2_Exchange16bitBuffer(&myWriteBuffer[total], MY_BUFER_SIZE - total, &myReadBuffer[total]);
 
What exactly is the return value from SPI2_Exchange16bitBuffer() ?
If it is the number of words that were transferred, then this code is wrong, it should be:
total += SPI2_Exchange16bitBuffer(&myWriteBuffer[total], MY_BUFER_SIZE - total, &myReadBuffer[total]);
 
举报

汪岑

2019-5-27 14:56:17
嗨,它似乎返回1或零。如果我阅读了正确的描述,它应该返回2。我今晚可以尝试一下,看看它是否修复了。我已经包括了SPI2.h的代码。

以上来自于百度翻译


      以下为原文

    Hi, 
 
It seems to return 1 or zero.

If I am reading the right description, it is supposed to return 2. 

I can try that tonight and see if it fixes it. 

I've included the code for SPI2.h

/**
  SPI2 Generated Driver API Header File

  Company:
    Microchip Technology Inc.

  File Name:
    spi2.h

  @Summary
    This is the generated header file for the SPI2 driver using MPLAB(c) Code Configurator

  @Description
    This header file provides APIs for driver for SPI2.
    Generation Information :
        Product Revision : MPLAB(c) Code Configurator - pic24-dspic-pic32mm : v1.25
        Device : dsPIC33EP256MC502
        Driver Version : 0.5
    The generated drivers are tested against the following:
        Compiler : XC16 1.26
        MPLAB : MPLAB X 3.45
*/

/*
    (c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS.
*/

#ifndef _SPI2_H
#define _SPI2_H

/**
 Section: Included Files
*/

#include
#include
#include
#include

#ifdef __cplusplus // Provide C++ Compatibility

    extern "C" {

#endif

/**
 Section: Data Type Definitions
*/
        
/**
  SPI2_DUMMY_DATA

  @Summary
    Dummy data to be sent.

  @Description
    Dummy data to be sent, when no input buffer is specified in the buffer APIs.
 */
#define SPI2_DUMMY_DATA 0x0
  
/**
  SPI2_FIFO_FILL_LIMIT

  @Summary
    FIFO fill limit for data transmission.

  @Description
    The amount of data to be filled in the FIFO during transmission. The maximum limit allowed is 8.
 */
#define SPI2_FIFO_FILL_LIMIT 0x8

//Check to make sure that the FIFO limit does not exceed the maximum allowed limit of 8
#if (SPI2_FIFO_FILL_LIMIT > 8)

    #define SPI2_FIFO_FILL_LIMIT 8

#endif

/**
  SPI2 Status Enumeration

  @Summary
    Defines the status enumeration for SPI2.

  @Description
    This defines the status enumeration for SPI2.
 */
typedef enum {
    SPI2_SHIFT_REGISTER_EMPTY = 1 << 7,
    SPI2_RECEIVE_OVERFLOW = 1 << 6,
    SPI2_RECEIVE_FIFO_EMPTY = 1 << 5,
    SPI2_TRANSMIT_BUFFER_FULL = 1 << 1,
    SPI2_RECEIVE_BUFFER_FULL = 1 << 0
}SPI2_STATUS;

/**
  SPI2 Mode Enumeration

  @Summary
    Defines the mode of operation for SPI2.

  @Description
    This defines the mode of operation for SPI2.
 */
typedef enum {
    SPI2_DRIVER_TRANSFER_MODE_8BIT = 0,
    SPI2_DRIVER_TRANSFER_MODE_16BIT = 1,
    SPI2_DRIVER_TRANSFER_MODE_32BIT = 2,
} SPI2_TRANSFER_MODE;

/**
 Section: Interface Routines
*/

/**
  @Summary
    Initializes the SPI instance : 2

  @Description
    This routine initializes the spi2 driver instance for : 2
    index, making it ready for clients to open and use it.

    This routine must be called before any other SPI2 routine is called.
    This routine should only be called once during system initialization.
 
  @Preconditions
    None.

  @Returns
    None.

  @Param
    None.

  @Example
    
    uint16_t myWriteBuffer[MY_BUFFER_SIZE];
    uint16_t myReadBuffer[MY_BUFFER_SIZE];
    uint16_t writeData;
    uint16_t readData;
    SPI2_STATUS status;
    unsigned int total;
    SPI2_Initialize;
    total = 0;
    do
    {
        total = SPI2_Exchange16bitBuffer( &myWriteBuffer[total], MY_BUFFER_SIZE - total, &myWriteBuffer[total]);

        // Do something else...

    } while( total < MY_BUFFER_SIZE );

    readData = SPI2_Exchange16bit( writeData);

    status = SPI2_StatusGet();

    


*/

void SPI2_Initialize (void);

/**
  @Summary
    Exchanges one word of data from SPI2

  @Description
    This routine exchanges one word of data from SPI2.
    This is a blocking routine.

  @Preconditions
    The SPI2_Initialize routine must have been called for the specified
    SPI2 driver instance.
    The SPI transfer mode should be selected as 16bit mode in the initialization.
    Do not select 8 bit mode, only the lower byte of the data will sent or received if selected.

  @Returns
    Data read from SPI2

  @Param
    data - Data to be written onto SPI2.

  @Example
    Refer to SPI2_Initialize() for an example
 
*/
        
uint16_t SPI2_Exchange16bit( uint16_t data );

/**
  @Summary
    Exchanges data from a buffer of size one word from SPI2

  @Description
    This routine exchanges data from a buffer of size one word from the SPI2.
    This is a blocking routine.

  @Preconditions
    The SPI2_Initialize routine must have been called for the specified
    SPI2 driver instance.
    The SPI transfer mode should be selected as 16bit mode in the initialization.
    Do not select 8 bit mode, only the lower byte of the data will sent or received if selected.

  @Returns
    Number of words written/read.

  @Param
    dataTransmitted - Buffer of data to be written onto SPI2.
 
  @Param
    byteCount - Number of bytes to be exchanged.
 
  @Param
    dataTransmitted - Buffer of data to be read from SPI2.

  @Example
    Refer to SPI2_Initialize() for an example
 
*/

uint16_t SPI2_Exchange16bitBuffer(uint16_t *dataTransmitted, uint16_t byteCount, uint16_t *dataReceived);


/**
  @Summary
    Returns the value of the status register of SPI instance : 2

  @Description
    This routine returns the value of the status register of SPI2 driver instance : 2

  @Preconditions
    None.

  @Returns
    Returns the value of the status register.

  @Param
    None.

  @Example
    Refer to SPI2_Initialize() for an example
 
*/

SPI2_STATUS SPI2_StatusGet(void);

#ifdef __cplusplus // Provide C++ Compatibility

    }

#endif

#endif //_SPI2_H
    
/*******************************************************************************
 End of File
*/
举报

汪岑

2019-5-27 15:12:32
它仍然不起作用。我打算用微芯片开一张支持票,明天试用DMA设置。版主能把这个移动到MCC线程吗?

以上来自于百度翻译


      以下为原文

    It's still not working. I'm going to open a support ticket with Microchip and try out the DMA setup tomorrow. 

Can a Moderator please move this to the MCC thread? 
举报

更多回帖

发帖
×
20
完善资料,
赚取积分