|
本帖最后由 Mcuzone_TKN 于 2020-5-15 09:13 编辑
关键词:Microchip Atmel SAM4E SAM4E-EK SAM4E16E 芯片CAN 控制器局域网络
概述:实现板载两路之间的收发
开发板有两路CAN,每个CAN有8个信箱。CAN总线上的逻辑数值是用显性电平和隐性电平表示,显性是指在同时传输显性电平和隐性电平时,总线上呈现的是显性电平。显性电平表示逻辑0,隐性电平表示逻辑1。
打开产品光盘SAM4E16E-EK/SAM4E16E-EK中文资料/softpack软件包/Atmel Studio 7,打开03_CAN_EXAMPLE例子。
int main(void)
{
uint32_t ul_sysclk;
uint8_t uc_char;
sysclk_init();
board_init();
configure_console();
//输出示例信息
puts(STRING_HEADER);
//初始化CAN0收发信机
sn65hvd234_set_rs(&can0_transceiver, PIN_CAN0_TR_RS_IDX);
sn65hvd234_set_en(&can0_transceiver, PIN_CAN0_TR_EN_IDX);
//启用CAN0收发信机
sn65hvd234_disable_low_power(&can0_transceiver);
sn65hvd234_enable(&can0_transceiver);
/初始化CAN1收发信机/
sn65hvd234_set_rs(&can1_transceiver, PIN_CAN1_TR_RS_IDX);
sn65hvd234_set_en(&can1_transceiver, PIN_CAN1_TR_EN_IDX);
//使能CAN1收发信机
sn65hvd234_disable_low_power(&can1_transceiver);
sn65hvd234_enable(&can1_transceiver);
//使能CAN0和CAN1时钟
pmc_enable_periph_clk(ID_CAN0);
pmc_enable_periph_clk(ID_CAN1);
ul_sysclk = sysclk_get_cpu_hz();
if (can_init(CAN0, ul_sysclk, CAN_BPS_1000K) &&
can_init(CAN1, ul_sysclk, CAN_BPS_1000K)) {
puts("CAN initialization is completed." STRING_EOL);
//关闭CAN0和CAN1的中断
can_disable_interrupt(CAN0, CAN_DISABLE_ALL_INTERRUPT_MASK);
can_disable_interrupt(CAN1, CAN_DISABLE_ALL_INTERRUPT_MASK);
//配置并启用CAN1的中断
NVIC_EnableIRQ(CAN1_IRQn);
//运行测试
puts("Press any key to start test" STRING_EOL);
while (uart_read(CONSOLE_UART, &uc_char)) {
}
test_1();
g_ul_recv_status = 0;
puts("Press any key to continue..." STRING_EOL);
while (uart_read(CONSOLE_UART, &uc_char)) {
}
test_2();
g_ul_recv_status = 0;
puts("Press any key to continue..." STRING_EOL);
while (uart_read(CONSOLE_UART, &uc_char)) {
}
test_3();
g_ul_recv_status = 0;
puts("Press any key to continue..." STRING_EOL);
while (uart_read(CONSOLE_UART, &uc_char)) {
}
test_4();
g_ul_recv_status = 0;
//关闭CAN0
can_disable(CAN0);
//关闭CAN0收发信机
sn65hvd234_enable_low_power(&can0_transceiver);
sn65hvd234_disable(&can0_transceiver);
//关闭CAN1
can_disable(CAN1);
//关闭CAN1收发器
sn65hvd234_enable_low_power(&can1_transceiver);
sn65hvd234_disable(&can1_transceiver);
puts("End of all tests" STRING_EOL);
} else {
puts("CAN initialization (sync) ERROR" STRING_EOL);
}
while (1) {
}
}
|
-
图1.png
(55.8 KB, 下载次数: 9873)
原理图
|