通过回环模式进行测试
#include "stm32f4xx.h"
#include "can.h"
void CAN_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能SPI1时钟
//MOSI,SCK,MISO
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; //上拉模式
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_High_Speed; //引脚响应速度
GPIO_Init(GPIOA ,&GPIO_InitStructure);
//PB3、4、5引脚复用
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
CAN_Self_Init();
can_Filter_Init();
}
//中断服务函数,已配置未使用
//static void CAN_SELF_NVIC_Init(void)
//{
// NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX0_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x1;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x1;
// NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
// NVIC_Init(&NVIC_InitStructure);
//}
//配置CAN单元
static void CAN_Self_Init(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_InitStructure.CAN_Prescaler = 6-1 ; //42MHZ / 6 = 6mhz
CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
//1MBPS = 42MHZ/PSC+1 /7 7(7是1+BS1+BS2)
CAN_InitStructure.CAN_SJW = CAN_SJW_2tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = ENABLE;
CAN_InitStructure.CAN_AWUM = ENABLE;
CAN_InitStructure.CAN_NART = ENABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_Init(CAN1 ,&CAN_InitStructure);
}
//配置过滤器
void can_Filter_Init(void)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_FilterInitStructure.CAN_FilterNumber = 0; /* 筛选器编号,范围0-27*/
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; /* 筛选器模式*/
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; /* 设置筛选器的尺度*/
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; /* 设置经过筛选后数据存储到哪个接收FIFO*/
//id:0X0000000
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; /*CAN_FxR1 寄存器的高16 位*/
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; /*CAN_FxR1 寄存器的低16 位*/
//掩码
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; /*CAN_FxR2 寄存器的高16 位*/
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; /*CAN_FxR2 寄存器的低16 位*/
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; /* 是否使能本筛选器*/
CAN_FilterInit(&CAN_FilterInitStructure);
}
//发送报文
uint8_t Can_Send_byte(uint8_t *msg)
{
uint8_t i, info;
uint32_t time = 0;
CanTxMsg TxMssage;
TxMssage.StdId = 0x11; /* 存储报文的标准标识符11 位,0-0x7FF. */
TxMssage.ExtId = 0x11; /* 存储报文的扩展标识符29 位,0-0x1FFFFFFF. */
TxMssage.IDE = CAN_Id_Standard; /* 存储IDE 扩展标志*/
TxMssage.RTR = CAN_RTR_Data; /* 存储RTR 远程帧标志*/
TxMssage.DLC = 8; /* 存储报文数据段的长度,0-8 */
for(i=0; i<8; i++)
{
TxMssage.Data[i] = msg[i];/* 存储报文数据段的内容*/
}
info = CAN_Transmit(CAN1, &TxMssage);
while((CAN_TransmitStatus(CAN1,info) == CAN_TxStatus_Failed) && (time < 0xfff))
{
time++;
}
if(time>=0xfff)
{
return 1;
}
return 0;
}
/**
* 接收结构体
*/
uint8_t CAN_Rx_byte(uint8_t *rcv)
{
uint32_t i;
CanRxMsg RxMsgage;
if(CAN_MessagePending(CAN1, CAN_FIFO0) == 0)
{
return 0;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMsgage);
for(i=0; i<RxMsgage.DLC; i++)
{
rcv[i] = RxMsgage.Data[i]; /* 存储了报文数据段的内容*/
}
return RxMsgage.DLC;
}
int main(void)
{
uint8_t rcv;
uint8_t i=0;
uint8_t cnt=0;
uint8_t canbuf[8];
uint8_t res;
SysTick_Init();
//串口通信
usart1_init(115200);
//CAN初始化
CAN_GPIO_Init();
while(1)
{
for(i=0;i<8;i++)
{
canbuf[i]=cnt+i;//填充发送缓冲区
}
res = Can_Send_byte(canbuf);//发送8个字节
if(res)
{
printf("发送失败\r\n");
}
else
{
printf("发送成功\r\n");
}
rcv=CAN_Rx_byte(canbuf);
if(rcv)//接收到有数据
{
printf("接收到数据为"); //显示接收到的数据
for(int i = 0; i < rcv; i++)
{
printf(" %x ",canbuf[i]); //显示数据
}
printf("\r\n");
}
Delay_ms(10);
cnt++;
printf("第 %d 次成功回环: \r\n",cnt); //显示第一次发送
Delay_ms(2000);
printf("\r\n");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.