【ACM32G103RCT6开发板测评】+ CAN通信测试
[复制链接]
测试下CAN收发通信。
一、硬件部分
1.1、CAN通信测试需要外接CAN收发器芯片,使用CAN1接口的PB8和PB9引脚
1.2、引脚映射到CAN1接口
1.3、硬件连接图
连接电位器,将采集的模拟电压,通过CAN通信输出
二、程序部分
2.1、can.c
#include "main.h"
CAN_HandleTypeDef can_handle;
CanTxRxMsg CAN_RxMessage;
CanTxRxMsg CAN_TxMessage;
volatile uint8_t can_recflag = 0;
void CAN_ReceiveIT_Callback(CAN_HandleTypeDef *hcan)
{
can_recflag = 1;
}
void can_send(uint8_t *txdat)
{
uint8_t i=0;
CAN_TxMessage.StdId = 0x666; //11bit, not used in extended mode
CAN_TxMessage.ExtId = 0x18FE0000; //29bit
CAN_TxMessage.IDE = CAN_Id_Extended; //extended mode
CAN_TxMessage.RTR = CAN_RTR_Data; //frame type: data
CAN_TxMessage.DLC = 8; //data length
for (i = 0; i < CAN_TxMessage.DLC; i++)
{
CAN_TxMessage.Data[i] = txdat[i];
}
if(HAL_CAN_Transmit(&can_handle, &CAN_TxMessage, 10) != HAL_OK)
{
CAN_ErrCodeInfo_t ErrCode;
HAL_CAN_GetErrorCode(&can_handle, &ErrCode);
printfS("CAN send failed!\r\n");
printfS("ErrCode.ALC = %d\r\n", ErrCode.ALC);
printfS("ErrCode.ErrCode = %d\r\n", ErrCode.ErrCode);
printfS("ErrCode.ErrDirection = %d\r\n", ErrCode.ErrDirection);
printfS("ErrCode.SegCode = %d\r\n", ErrCode.SegCode);
}
}
void can_rec(uint8_t *rxdat)
{
uint8_t i=0;
HAL_CAN_Receive_IT(&can_handle, &CAN_RxMessage);
if(can_recflag == 1)
{
can_recflag = 0;
//printf("recdat\r\n");
if(CAN_RxMessage.IDE==CAN_Id_Standard)
{
//printfS("CAN IDE type: Standard\n");
//printfS("CAN ID=0x%.8x\n",CAN_RxMessage.StdId);
}
else
{
//printfS("CAN IDE type: Extended\n");
//printfS("CAN ID=0x%.8x\n",CAN_RxMessage.ExtId);
}
if(CAN_RxMessage.RTR==CAN_RTR_Data)
{
printf("rxdat: ");
//printfS("data length=%d\n",CAN_RxMessage.DLC);
for(i=0;i<CAN_RxMessage.DLC;i++)
{
//rxdat[i]=CAN_RxMessage.Data[i];
printf("%.2x ",CAN_RxMessage.Data[i]);
}
printf("\r\n");
}
else
{
//printfS("CAN RTR type: Remote frame\n");
}
//CAN_PrintfReceive();
//CAN_TxMessage = CAN_RxMessage;
//HAL_CAN_Transmit(&can_handle, &CAN_TxMessage, 10);/* echo back */
}
}
void init_can(void)
{
uint8_t i=0;
CAN_FilterInitTypeDef CAN_FilterInitStruct;
//模式配置
can_handle.Instance = CAN1;
can_handle.Init.CAN_Mode = CAN_OperatingMode_Normal;
can_handle.Init.CAN_SJW = CAN_SJW_1tq;
can_handle.Init.CAN_BRP = 5;
can_handle.Init.CAN_TSEG1 = CAN_TSEG1_5tq;
can_handle.Init.CAN_TSEG2 = CAN_TSEG2_4tq;
can_handle.Init.CAN_BOR = CAN_BOR_ENABLE;
can_handle.CAN_ReceiveIT_Callback=CAN_ReceiveIT_Callback;
HAL_CAN_Init(&can_handle);
//滤波配置
CAN_FilterInitStruct.FilterMode = CAN_FilterMode_Dual;
CAN_FilterInitStruct.FilterBank = 0;
CAN_FilterInitStruct.FilterId1 = 0x18FE0000;
CAN_FilterInitStruct.FilterId2 = 0x12340000;
/* if want receive all data,please set the CAN_FilterMaskId = 0xFFFFFFFF*/
CAN_FilterInitStruct.FilterMaskId1 = 0x0000FFFF; /* only receive CAN id=0x18FExxxx */
CAN_FilterInitStruct.FilterMaskId2 = 0x0000FFFF; /* only receive CAN id=0x1234xxxx */
CAN_FilterInitStruct.FilterActivation = CAN_FILTER_ENABLE;
printf("Filter Mode: Dual\r\n");
printf("Filter Id1 = 0x%08X, MaskId1 = 0x%08X\r\n", CAN_FilterInitStruct.FilterId1, CAN_FilterInitStruct.FilterMaskId1);
printf("Filter Id2 = 0x%08X, MaskId2 = 0x%08X\r\n\r\n", CAN_FilterInitStruct.FilterId2, CAN_FilterInitStruct.FilterMaskId2);
HAL_CAN_ConfigFilter(&can_handle, &CAN_FilterInitStruct);
}
2.2、main.c
#include "main.h"
int main(void)
{
uint8_t js=0;
uint8_t can_txbuf[10];
uint8_t can_rxbuf[10];
HAL_Init();
SystemClock_Config();
usart_init(115200);
printf("\r\n\r\n====== ACM32G103 MCU is runing ======\r\nSysCoreClk: %dHz, HCLK:%dHz\r\nPCLK1:%dHz, PCLK2:%dHz\r\n\r\n",g_SystemCoreClock, HAL_RCC_GetHCLKFreq(),HAL_RCC_GetPCLK1Freq(),HAL_RCC_GetPCLK2Freq());
init_led();
init_tim2();
init_key();
I2C_Init();
//adc_test();
//eeprom_test();
//oled_test();
init_rtc();
init_can();
//OLED_Init();
//OLED_Fill(0x00);
while(1)
{
//rtc_read();
//can_txbuf[0]=js++;
can_txbuf[0]=read_pa1_vol();
can_send(can_txbuf);
HAL_Delay(100);
can_rec(can_rxbuf);
//led1_tog();
//printf("https://bbs.eeworld.com.cn/\r\n");
}
}
三、测试结果
3.1、电位器采集的电压值通过CAN发出
3.2、CAN接收的数据
|