发 帖  
张飞软硬开源基于STM32 BLDC直流无刷电机驱动器开发视频套件, 👉戳此立抢👈
[问答] 不能得到PIC32 MZ来接收和发送数据
2706 PIC32 CAN
分享
我已经包含了用于我的PIC32 MZ的CAN总线代码,运行在100MHz,同样适用于外围总线。我使用的是来自微芯片的CAN总线分析仪来测试它。但是我不能得到PIC32 MZ来接收或发送数据。硬件似乎工作良好,因为其他人以前使用过它。如果有人用这个代码指出这个问题,那就太棒了。谢谢,Omkar J.。

以上来自于百度翻译


      以下为原文

     
I have included the CAN bus code Im using for my PIC32mz, running at 100MHz, same for peripheral bus. I am using the CAN BUS analyzer from microchip trying to test it. But I cant get the the PIC32mz to either receive or transmit data. The hardware seems to be working fine as someone else has used it before. If someone could point out the issue with the code that would be awesome.

Thanks,
Omkar J.




void can2_init()
{
    //CAN2_RX
    TRISEbits.TRISE9 = 1;
    ANSELEbits.ANSE9 = 0;
    C2RXR = 0b1100;
    //CAN2_TX
    TRISEbits.TRISE8 = 0;
    ANSELEbits.ANSE8 = 0;
    RPE8R = 0b1111;
   
    CAN_BIT_CONFIG canBitConfig;
   
    CANEnableModule(CAN2, TRUE); //C2CONbits.ON = 1;
   
    CANSetOperatingMode(CAN2, CAN_CONFIGURATION); // C2CONbits.REQOP = 0b100;
    while (CANGetOperatingMode(CAN2) != CAN_CONFIGURATION); //while(C2CONbits.OPMOD != 0b100);
   
    canBitConfig.phaseSeg1Tq = CAN_BIT_3TQ;
    canBitConfig.phaseSeg2TimeSelect = TRUE;
    canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ;
    canBitConfig.propagationSegTq = CAN_BIT_1TQ;
    canBitConfig.sample3Time = TRUE;
    canBitConfig.syncJumpWidth = CAN_BIT_1TQ;

    CANSetSpeed(CAN2, &canBitConfig, 100000000L , 125000L);

    CANAssignMemoryBuffer(CAN2, CanFifoMessageBuffers, 2 * 2 * CAN_TX_RX_MESSAGE_SIZE_BYTES);

    //CANEnableFeature(CAN2, CAN_STOP_IN_IDLE | CAN_RX_TIMESTAMP | CAN_WAKEUP_BUS_FILTER, FALSE);

    CANConfigureChannelForTx(CAN2, CAN_CHANNEL1, 2, CAN_TX_RTR_DISABLED, CAN_HIGHEST_PRIORITY); //Channel_1 is TX buffer
   
    CANConfigureChannelForRx(CAN2, CAN_CHANNEL0, 2, CAN_RX_FULL_RECEIVE); //Channel_0 is RX buffer

    CANConfigureFilter(CAN2, CAN_FILTER0, 0, CAN_SID);

    CANConfigureFilterMask (CAN2, CAN_FILTER_MASK0, 0x0, CAN_EID, CAN_FILTER_MASK_IDE_TYPE); //accept all messages

    CANLinkFilterToChannel(CAN2, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL0);

    CANEnableFilter (CAN2, CAN_FILTER0, TRUE);
   
    CANEnableChannelEvent (CAN2, CAN_CHANNEL0, (CAN_RX_CHANNEL_NOT_EMPTY|CAN_RX_CHANNEL_FULL), TRUE);
    CANEnableModuleEvent (CAN2, CAN_RX_EVENT, TRUE);
   
    // set interrupts
    C2INTbits.RBIF = 0; // clear level 2 interrupt flag
    C2INTbits.RBIE = 1; // enable level 2 interrupt

    IFS4bits.CAN2IF = 0; // clear level 1 interrupt flag
    IEC4bits.CAN2IE = 1; // enable level 1 interrupt
   
    IPC38bits.CAN2IP = 0b111; // priority 7
    IPC38bits.CAN2IS = 0b00; // sub-priority 0

    C2FIFOINT0bits.RXNEMPTYIF = 0; // clear level 3 interrupt flag
    C2FIFOINT0bits.RXNEMPTYIE = 1; // enable level 3 interrupt

    CANSetOperatingMode(CAN2, CAN_NORMAL_OPERATION);
    while (CANGetOperatingMode(CAN2) != CAN_NORMAL_OPERATION);
   
    serial_transmit("CAN Setup Done\r\n");
}void send_can2()
{
    CANTxMessageBuffer* buffer;

    buffer = CANGetTxMessageBuffer(CAN2, CAN_CHANNEL1); ;
   
    if (buffer != NULL) // NULL indicates Channel Full
    {
        /* Clear the message buffer. */
        buffer->messageWord[0] = 0;
        buffer->messageWord[1] = 0;
        buffer->messageWord[2] = 0;
        buffer->messageWord[3] = 0;

        buffer->msgSID.SID = 0x100;/* Message SID */
        message->msgEID.IDE = 0;
        buffer->msgEID.DLC = 7;/* Data Length is 2 */
        message->msgEID.DLC = 2;
        message->msgEID.RTR = 0;
        message->msgEID.SRR = 0;
        message->data[0] = 0x13;
        message->data[1] = 0x00;
        message->data[2] = 0x15;
        message->data[3] = 0x01;
        message->data[4] = 0x01;
        message->data[5] = 0xF4;
        message->data[6] = 0x01;
        
        CANUpdateChannel(CAN2,CAN_CHANNEL1);
        CANFlushTxChannel(CAN2,CAN_CHANNEL1);
    }
      
}void __attribute__((vector(_CAN2_VECTOR), interrupt(IPL7AUTO))) CAN2InterruptHandler(void)
{
        serial_transmit("CAN_ISR");
        if((CANGetModuleEvent(CAN2) & CAN_RX_EVENT) != 0)
{

      if((CANGetPendingEventCode(CAN2) == CAN_CHANNEL0_EVENT)/* && (CANGetPendingEventCode(CAN1) ==                 CAN_CHANNEL2_EVENT)*/)
            {
                     CANEnableChannelEvent(CAN2, CAN_CHANNEL0, CAN_RX_CHANNEL_NOT_EMPTY, FALSE);
             }

}
    IFS4bits.CAN2IF = 0; // clear level 1 interrupt flag
}
0
2019-2-21 06:16:19   评论 分享淘帖 邀请回答
2个回答
1。确保IOLoCK(CFGCON & LT;13和GT)在您为CAN Tx和RX引脚配置PPS时不锁定。否则,你需要通过解锁序列。2。确保PBCK5被设置和启用。MZ有8个外围时钟,3个。如果项目从MX转换为PLIB支持,请确保更改MZ的CAN1/2基地址。它们应该是4。利用MZ的影子寄存器集(SRS)。启用所有它们,并相应地使用ISR 7级的所有5级。当你编写PIC32代码时,你不应该像这样清除任何中断标志,而是使用相应的CLR寄存器,例如,

以上来自于百度翻译


      以下为原文

   
1. Make sure IOLOCK (CFGCON<13>) is not locked when you configure PPS for the CAN Tx and RX pins. Otherwise you need to go through unlocking sequence.
2. Make sure PBCLK5 is set and enabled. MZ has 8 peripheral clocks.
3. If the project was converted from the MX with PLIB support, make sure you change the CAN1/2 base addresses for the MZ. They should be

#define _CAN1_BASE_ADDRESS 0xBF880000
#define _CAN2_BASE_ADDRESS 0xBF881000

4. Take advantage of the MZ's shadow register set (SRS). Enable all of them and use them accordingly for all 7 levels of ISRs.
5. When you write PIC32 code, you shouldn't clear any interrupt flag like this

IFS4bits.CAN2IF = 0; // clear level 1 interrupt flag

Instead, use the corresponding CLR register, e.g.,

IFS4CLR = _IFS4_CAN2IF_MASK;
2019-2-21 06:32:46 评论

举报

谢谢:我会做出建议的改变,让你知道它是否有效,我没有想到PPS的东西,但是时钟肯定是为所有外设启用的。代码已经被移植了,我想出了大部分。我以前没有用过,而且配置比O还难。我已经用过了。

以上来自于百度翻译


      以下为原文

    Thanks :) Ill make the suggested changes and let you know if it works, I did not think of the PPS thing, but the clocks is certainly enabled for all peripherals.
The code hasent been ported and I came up with most of it. 
I havent used CAN before and the configuration is pretty tough compared to the others I have used.
 
2019-2-21 06:47:57 评论

举报

只有小组成员才能发言,加入小组>>

33个成员聚集在这个小组

加入小组

创建小组步骤

关闭

站长推荐 上一条 /8 下一条

快速回复 返回顶部 返回列表