S32K144-EVB开发板CAN实例代码
时间: 2023-10-01 15:06:56 浏览: 157
以下是S32K144-EVB开发板CAN实例代码,供参考:
```c
#include "S32K144.h"
#define CAN0_TX_PIN 16U /* PTC16 */
#define CAN0_RX_PIN 17U /* PTC17 */
void CAN0_Init(void)
{
/* Enable the clock to the module */
PCC->PCCn[PCC_PORTC_INDEX] |= PCC_PCCn_CGC_MASK;
PCC->PCCn[PCC_FlexCAN0_INDEX] |= PCC_PCCn_CGC_MASK;
/* Configure the pins for CAN0 */
PORTC->PCR[CAN0_TX_PIN] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[CAN0_TX_PIN] |= PORT_PCR_MUX(2);
PORTC->PCR[CAN0_RX_PIN] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[CAN0_RX_PIN] |= PORT_PCR_MUX(2);
/* Reset the module */
FLEXCAN0->MCR |= FLEXCAN_MCR_SOFTRST_MASK;
while (FLEXCAN0->MCR & FLEXCAN_MCR_SOFTRST_MASK);
/* Configure the module */
FLEXCAN0->MCR |= FLEXCAN_MCR_FRZ_MASK;
FLEXCAN0->MCR |= FLEXCAN_MCR_MDIS_MASK;
FLEXCAN0->CTRL1 |= FLEXCAN_CTRL1_CLKSRC_MASK;
FLEXCAN0->CTRL1 |= FLEXCAN_CTRL1_PRESDIV(0x01);
FLEXCAN0->CTRL1 &= ~FLEXCAN_CTRL1_LBUF_MASK;
FLEXCAN0->MCR &= ~FLEXCAN_MCR_MDIS_MASK;
while (FLEXCAN0->MCR & FLEXCAN_MCR_LPMACK_MASK);
while (!(FLEXCAN0->MCR & FLEXCAN_MCR_FRZACK_MASK));
FLEXCAN0->MCR &= ~FLEXCAN_MCR_FRZ_MASK;
/* Configure the mailbox */
FLEXCAN0->MB[0].CS |= FLEXCAN_MB_CS_IDE_MASK;
FLEXCAN0->MB[0].ID |= FLEXCAN_MB_ID_IDSTD(0x123);
FLEXCAN0->MB[0].CS |= FLEXCAN_MB_CS_RTR_MASK;
FLEXCAN0->MB[0].CS |= FLEXCAN_MB_CS_DLC(8);
FLEXCAN0->MB[0].CS |= FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_INACTIVE);
}
void CAN0_Send(uint8_t *data)
{
uint32_t i;
/* Copy data to mailbox */
for (i = 0; i < 8; i++)
{
FLEXCAN0->MB[0].DATA[i] = data[i];
}
/* Send the message */
FLEXCAN0->MB[0].CS &= ~FLEXCAN_MB_CS_CODE_MASK;
FLEXCAN0->MB[0].CS |= FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_ONCE);
}
int main(void)
{
uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
CAN0_Init();
CAN0_Send(data);
return 0;
}
```
注意,以上代码仅供参考,具体实现需要根据实际情况进行修改。例如,需要根据实际需求设置CAN的波特率、ID等参数。
阅读全文